| 1 | use v5.40; | ||
| 2 | no warnings qw[experimental::class recursion]; | ||
| 3 | use feature 'class'; | ||
| 4 | class Acme::Parataxis::Sync::RwLock v0.1.6 : isa(Acme::Parataxis::Sync) { | ||
| 5 | use Acme::Parataxis; | ||
| 6 | use Carp qw[croak]; | ||
| 7 | field $writer : reader; # fiber id holding the exclusive write lock; undef when no writer holds it | ||
| 8 | field $readers : reader = 0; # outstanding read holds (a fiber may hold more than one) | ||
| 9 | field %read_holders; # fiber id => how many read holds that fiber owns | ||
| 10 | field @read_waiters : reader; # fiber ids parked for a shared read lock, FIFO | ||
| 11 | field @write_waiters : reader; # fiber ids parked for the exclusive write lock, FIFO | ||
| 12 | |||
| 13 | # | ||
| 14 | method owner {$writer} # Sign of a poor API... | ||
| 15 | method read_holders { scalar keys %read_holders } | ||
| 16 | method waiters { scalar(@read_waiters) + scalar(@write_waiters) } | ||
| 17 | |||
| 18 | # Take (or deepen) a shared read hold, parking only when a writer holds the lock or is queued behind one. | ||
| 19 | method read_lock { | ||
| 20 | my $fid = $self->_fid('RwLock read_lock must occur inside a scheduled fiber'); | ||
| 21 | |||
| 22 | # Re-entering while this fiber already holds a read lock deepens the hold in place instead of parking. | ||
| 23 | # Parking here would deadlock: a queued writer waits for this hold to drain, and this read would wait for | ||
| 24 | # that writer. | ||
| 25 | if ( $read_holders{$fid} ) { | ||
| 26 | $read_holders{$fid}++; | ||
| 27 | $readers++; | ||
| 28 | return 1; | ||
| 29 | } | ||
| 30 | |||
| 31 | # This fiber already holds the write lock, so it has exclusive access already. Take the read hold in | ||
| 32 | # place -- parking would be waiting for the writer (this very fiber) to release. | ||
| 33 | if ( defined $writer && $writer == $fid ) { | ||
| 34 | $self->_grant_read($fid); | ||
| 35 | return 1; | ||
| 36 | } | ||
| 37 | |||
| 38 | # Writer-preferring: once a writer holds the lock or is queued for it, no *new* reader may jump ahead, | ||
| 39 | # otherwise a steady read stream would starve writers forever. | ||
| 40 | if ( !defined $writer && !@write_waiters ) { | ||
| 41 | $self->_grant_read($fid); | ||
| 42 | return 1; | ||
| 43 | } | ||
| 44 | push @read_waiters, $fid; | ||
| 45 | $self->_park( | ||
| 46 | 'RwLock read lock', | ||
| 47 | sub { | ||
| 48 | # A writer's release may have granted us a read hold an instant before an interrupt fired. The | ||
| 49 | # interrupt throws before read_lock() returns, so pass the hold on rather than stranding a read | ||
| 50 | # on a fiber id that is about to die (and later be reused). | ||
| 51 | $self->_drop_reads($fid) if $read_holders{$fid}; | ||
| 52 | $self->remove_read_waiter($fid); | ||
| 53 | } | ||
| 54 | ); | ||
| 55 | return 1; # _pump granted the hold before waking us | ||
| 56 | } | ||
| 57 | |||
| 58 | # Release one read hold. When the last hold goes the lock may finally be handed on. Only a fiber that actually | ||
| 59 | # holds one may call it. | ||
| 60 | method read_unlock { | ||
| 61 | my $fid = $self->_fid('RwLock read_unlock must occur inside a scheduled fiber'); | ||
| 62 | croak 'RwLock read_unlock: this fiber holds no read lock' unless $read_holders{$fid}; | ||
| 63 | |||
| 64 | # Release exactly one hold per call, so a nested read_guard's DESTROY only frees its own hold and cannot | ||
| 65 | # strand the outer guard on a lock it no longer owns. | ||
| 66 | $read_holders{$fid}--; | ||
| 67 | delete $read_holders{$fid} unless $read_holders{$fid}; | ||
| 68 | $readers--; | ||
| 69 | $self->_pump if $readers == 0; | ||
| 70 | return 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | # Non-blocking shared acquisition: false instead of parking when a writer holds the lock or is queued. | ||
| 74 | method try_read_lock { | ||
| 75 | my $fid = $self->_fid('RwLock try_read_lock must occur inside a scheduled fiber'); | ||
| 76 | if ( $read_holders{$fid} ) { | ||
| 77 | $read_holders{$fid}++; | ||
| 78 | $readers++; | ||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | if ( defined $writer && $writer == $fid ) { $self->_grant_read($fid); return 1 } # already exclusive | ||
| 82 | return 0 if defined $writer || @write_waiters; # never steal ahead of a waiting writer | ||
| 83 | $self->_grant_read($fid); | ||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | # Take the exclusive write lock, parking until every reader has drained and no writer is queued ahead. | ||
| 88 | method write_lock { | ||
| 89 | my $fid = $self->_fid('RwLock write_lock must occur inside a scheduled fiber'); | ||
| 90 | croak 'RwLock is not reentrant: this fiber already holds the write lock' if defined $writer && $writer == $fid; | ||
| 91 | croak 'RwLock write_lock: this fiber holds a read lock; upgrading read to write is not supported' if $read_holders{$fid}; | ||
| 92 | if ( !defined $writer && $readers == 0 && !@write_waiters ) { | ||
| 93 | $writer = $fid; | ||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | push @write_waiters, $fid; | ||
| 97 | $self->_park( | ||
| 98 | 'RwLock write lock', | ||
| 99 | sub { | ||
| 100 | # A release may have handed us the write lock just as an interrupt fired. That hand-off is never | ||
| 101 | # collected -- the interrupt throws before write_lock() returns -- so clear it and let _pump pass | ||
| 102 | # ownership on instead of leaving it stranded on a dying fiber id. | ||
| 103 | $writer = undef if defined $writer && $writer == $fid; | ||
| 104 | $self->remove_write_waiter($fid); | ||
| 105 | $self->_pump; # we may have been the writer every reader was waiting behind | ||
| 106 | } | ||
| 107 | ); | ||
| 108 | return 1; # _pump handed us the lock before waking us | ||
| 109 | } | ||
| 110 | |||
| 111 | # Release the exclusive write lock to the next queued writer (or to the waiting readers). Only the current | ||
| 112 | # writer may call it. | ||
| 113 | method write_unlock { | ||
| 114 | my $fid = $self->_fid('RwLock write_unlock must occur inside a scheduled fiber'); | ||
| 115 | croak 'RwLock write_unlock: the lock is not write-held' unless defined $writer; | ||
| 116 | croak 'RwLock write_unlock from a fiber that is not the writer' unless $writer == $fid; | ||
| 117 | $writer = undef; | ||
| 118 | $self->_pump; | ||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | |||
| 122 | # Non-blocking exclusive acquisition: false instead of parking. Never cuts ahead of a queued writer. | ||
| 123 | method try_write_lock { | ||
| 124 | my $fid = $self->_fid('RwLock try_write_lock must occur inside a scheduled fiber'); | ||
| 125 | croak 'RwLock is not reentrant: this fiber already holds the write lock' if defined $writer && $writer == $fid; | ||
| 126 | croak 'RwLock try_write_lock: this fiber holds a read lock; upgrading read to write is not supported' if $read_holders{$fid}; | ||
| 127 | return 0 if defined $writer || $readers > 0 || @write_waiters; | ||
| 128 | $writer = $fid; | ||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | method read_guard { | ||
| 133 | $self->read_lock; | ||
| 134 | Acme::Parataxis::Sync::RwLock::Guard->new( lock => $self, mode => 'read' ); | ||
| 135 | } | ||
| 136 | |||
| 137 | method write_guard { | ||
| 138 | $self->write_lock; | ||
| 139 | Acme::Parataxis::Sync::RwLock::Guard->new( lock => $self, mode => 'write' ); | ||
| 140 | } | ||
| 141 | |||
| 142 | method remove_read_waiter ($fid) { | ||
| 143 | my $before = @read_waiters; | ||
| 144 | @read_waiters = grep { $_ != $fid } @read_waiters; | ||
| 145 | return $before - @read_waiters; | ||
| 146 | } | ||
| 147 | |||
| 148 | method remove_write_waiter ($fid) { | ||
| 149 | my $before = @write_waiters; | ||
| 150 | @write_waiters = grep { $_ != $fid } @write_waiters; | ||
| 151 | return $before - @write_waiters; | ||
| 152 | } | ||
| 153 | |||
| 154 | # Record a read hold for $fid, dropping it out of the reader queue if it was parked there. | ||
| 155 | method _grant_read ($fid) { | ||
| 156 | $read_holders{$fid}++; | ||
| 157 | $readers++; | ||
| 158 | $self->remove_read_waiter($fid); | ||
| 159 | return 1; | ||
| 160 | } | ||
| 161 | |||
| 162 | # Give back every read hold $fid owns; when the last one goes, the lock may finally be handed on. Only the | ||
| 163 | # interruption path uses this - the fiber is unwinding, so any hold left on it would be stranded. | ||
| 164 | method _drop_reads ($fid) { | ||
| 165 | my $n = delete $read_holders{$fid}; | ||
| 166 | return 0 unless defined $n; | ||
| 167 | $readers -= $n; | ||
| 168 | $self->_pump if $readers == 0; | ||
| 169 | return 1; | ||
| 170 | } | ||
| 171 | |||
| 172 | # The lock may have become available: hand it to the next living writer, else to every waiting reader. | ||
| 173 | # Writers go first (writer preference) so a queued writer is served as soon as the readers drain. | ||
| 174 | method _pump { | ||
| 175 | return 0 if defined $writer || $readers > 0; # still busy; whoever frees it last calls us again | ||
| 176 | if (@write_waiters) { | ||
| 177 | my $next; | ||
| 178 | while (@write_waiters) { | ||
| 179 | my $waiter = shift @write_waiters; | ||
| 180 | next unless defined Acme::Parataxis->by_id($waiter); | ||
| 181 | $next = $waiter; | ||
| 182 | last; | ||
| 183 | } | ||
| 184 | if ( defined $next ) { | ||
| 185 | |||
| 186 | # Hand-off is atomic: the lock is recorded before the waiter is woken, so nobody can take it | ||
| 187 | # in between and the waiter only ever has to collect what is already its own. | ||
| 188 | $writer = $next; | ||
| 189 | Acme::Parataxis::_scheduler_enqueue_by_id($next); | ||
| 190 | return 1; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | my @grant = grep { defined Acme::Parataxis->by_id($_) } @read_waiters; | ||
| 194 | @read_waiters = (); | ||
| 195 | return 0 unless @grant; | ||
| 196 | |||
| 197 | # Readers share, so every parked reader is granted in the same pass rather than one per release. | ||
| 198 | $self->_grant_read($_) for @grant; | ||
| 199 | Acme::Parataxis::_scheduler_enqueue_by_id($_) for @grant; | ||
| 200 | return 1; | ||
| 201 | } | ||
| 202 | }; | ||
| 203 | |||
| 204 | # Util. A classic package, not feature::class: a perlclass 'method DESTROY' is one trigger of the perl_clone access | ||
| 205 | # violation behind threads->create on some 5.42.x builds (Acme::Parataxis::Blocking's spawn_blocking), so guard classes | ||
| 206 | # are kept as classic blessed packages. Note: the Sync::* namespaces themselves still hit a separate, name-dependent | ||
| 207 | # clone crash on affected perls regardless of this - see Acme::Parataxis::Blocking's pod warning. | ||
| 208 | package Acme::Parataxis::Sync::RwLock::Guard v0.1.6 { | ||
| 209 | |||
| 210 | sub new ( $class, %args ) { | ||
| 211 | return bless { lock => $args{lock}, mode => $args{mode} }, $class; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub DESTROY { | ||
| 215 | my $self = shift; | ||
| 216 | return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; | ||
| 217 | $self->{mode} eq 'write' ? $self->{lock}->write_unlock : $self->{lock}->read_unlock; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | # | ||
| 221 | 1; |