| 1 | use v5.40; | ||
| 2 | no warnings 'experimental::class', 'recursion'; | ||
| 3 | use feature 'class'; | ||
| 4 | class Acme::Parataxis::Sync::Mutex v0.1.6 : isa(Acme::Parataxis::Sync) { | ||
| 5 | use Acme::Parataxis; | ||
| 6 | use Carp qw[croak]; | ||
| 7 | field $owner : reader; # fiber id holding the lock; undef when free | ||
| 8 | field @waiters; # fiber ids waiting to lock, FIFO | ||
| 9 | |||
| 10 | # Acquire the lock, parking until it is handed over. The lock is not reentrant: a fiber that already holds it croaks. | ||
| 11 | method lock { | ||
| 12 | my $fid = $self->_fid('Mutex locks must occur inside a scheduled fiber'); | ||
| 13 | if ( !defined $owner ) { $owner = $fid; return 1 } | ||
| 14 | croak 'Mutex is not reentrant: this fiber already holds the lock' if $owner == $fid; | ||
| 15 | push @waiters, $fid; | ||
| 16 | $self->_park( | ||
| 17 | 'Mutex lock', | ||
| 18 | sub { | ||
| 19 | # An unlock() may have handed the lock to us just as an interrupt (deadline/cancel) fired. That hand-off | ||
| 20 | # is never collected -- the interrupt throws before lock() returns -- so pass it on so ownership is not | ||
| 21 | # left stranded on a fid that is about to die (and later be reused). | ||
| 22 | if ( defined $owner && $owner == $fid ) { | ||
| 23 | $owner = undef; | ||
| 24 | $self->_serve; | ||
| 25 | } | ||
| 26 | $self->remove_waiter($fid); | ||
| 27 | } | ||
| 28 | ); | ||
| 29 | return 1; # unlock() handed the lock to us before waking us | ||
| 30 | } | ||
| 31 | |||
| 32 | # Non-blocking acquisition; returns false instead of parking when the lock is held. | ||
| 33 | method try_lock { | ||
| 34 | my $fid = $self->_fid('Mutex locks must occur inside a scheduled fiber'); | ||
| 35 | return 0 if defined $owner && $owner != $fid; | ||
| 36 | croak 'Mutex is not reentrant: this fiber already holds the lock' if defined $owner; | ||
| 37 | $owner = $fid; | ||
| 38 | return 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | # Release the lock to the next waiter (or free it). Only the owning fiber may release. | ||
| 42 | method unlock { | ||
| 43 | my $fid = $self->_fid('Mutex unlocks must occur inside a scheduled fiber'); | ||
| 44 | croak 'Mutex is not locked' unless defined $owner; | ||
| 45 | croak 'Mutex release from a fiber that is not the owner' unless $owner == $fid; | ||
| 46 | $self->_serve; | ||
| 47 | return 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | # Hand the lock to the next living waiter, or free it. owner is set before the waiter is woken so nobody else can | ||
| 51 | # steal the lock between the hand-off and the waiter running to collect it. | ||
| 52 | method _serve { | ||
| 53 | my $next; | ||
| 54 | while (@waiters) { | ||
| 55 | my $waiter = shift @waiters; | ||
| 56 | next unless defined Acme::Parataxis->by_id($waiter); | ||
| 57 | $next = $waiter; | ||
| 58 | last; | ||
| 59 | } | ||
| 60 | $owner = $next; # hand-off is atomic: nobody else can steal it before the waiter runs | ||
| 61 | Acme::Parataxis::_scheduler_enqueue_by_id($next) if defined $next; | ||
| 62 | return 1; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Return a Scope::Guard-style object that unlocks on destruction. | ||
| 66 | method guard { | ||
| 67 | $self->lock; | ||
| 68 | Acme::Parataxis::Sync::Mutex::Guard->new( mutex => $self ); | ||
| 69 | } | ||
| 70 | method waiters { scalar @waiters } | ||
| 71 | |||
| 72 | method remove_waiter ($fid) { # unregister a parked waiter (used by the lock park's interruption path) | ||
| 73 | my $before = @waiters; | ||
| 74 | @waiters = grep { $_ != $fid } @waiters; | ||
| 75 | return $before - @waiters; | ||
| 76 | } | ||
| 77 | }; | ||
| 78 | |||
| 79 | # Util. A classic package, not feature::class: a perlclass 'method DESTROY' is one trigger of the perl_clone access | ||
| 80 | # violation behind threads->create on some 5.42.x builds (Acme::Parataxis::Blocking's spawn_blocking), so guard classes | ||
| 81 | # are kept as classic blessed packages. Note: the Sync::* namespaces themselves still hit a separate, name-dependent | ||
| 82 | # clone crash on affected perls regardless of this - see Acme::Parataxis::Blocking's pod warning. | ||
| 83 | package Acme::Parataxis::Sync::Mutex::Guard v0.1.6 { | ||
| 84 | |||
| 85 | sub new ( $class, %args ) { | ||
| 86 | return bless { mutex => $args{mutex} }, $class; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub DESTROY { | ||
| 90 | my $self = shift; | ||
| 91 | return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; | ||
| 92 | $self->{mutex}->unlock; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | # | ||
| 96 | 1; |