| 1 | use v5.40; | ||
| 2 | no warnings qw[experimental::class recursion]; | ||
| 3 | use feature 'class'; | ||
| 4 | class Acme::Parataxis::Semaphore v0.1.6 { | ||
| 5 | use Acme::Parataxis; | ||
| 6 | use Carp qw[croak]; | ||
| 7 | field $count : reader : param //= 1; | ||
| 8 | field @waiters : reader; # fiber ids in FIFO order and re-enqueued via the scheduler when woken | ||
| 9 | |||
| 10 | method _block_until_available ( $reason = 'Semaphore down', $level = 2 ) | ||
| 11 | { # Park the current fiber until a permit is available then recheck the count | ||
| 12 | while ( $count <= 0 ) { | ||
| 13 | my $fid = Acme::Parataxis->current_fid; | ||
| 14 | croak 'Semaphore waits must occur inside a scheduled fiber' if $fid < 0; | ||
| 15 | push @waiters, $fid; | ||
| 16 | Acme::Parataxis::_park( $reason, $level, sub { $self->remove_waiter($fid) } ); | ||
| 17 | } | ||
| 18 | 1; | ||
| 19 | } | ||
| 20 | |||
| 21 | method down ( $reason = 'Semaphore down', $level = 2 ) { | ||
| 22 | $self->_block_until_available( $reason, $level ); | ||
| 23 | $count--; | ||
| 24 | 1; | ||
| 25 | } | ||
| 26 | |||
| 27 | method try () { | ||
| 28 | return 0 if $count <= 0; | ||
| 29 | $count--; | ||
| 30 | 1; | ||
| 31 | } | ||
| 32 | |||
| 33 | method _wake_waiters ($budget) { # Wakes up to $budget waiters, skipping stale (destroyed) fiber ids | ||
| 34 | my $woken = 0; | ||
| 35 | while ( @waiters && $woken < $budget ) { | ||
| 36 | my $waiter = shift @waiters; | ||
| 37 | next unless defined Acme::Parataxis->by_id($waiter); | ||
| 38 | Acme::Parataxis::_scheduler_enqueue_by_id($waiter); | ||
| 39 | $woken++; | ||
| 40 | } | ||
| 41 | $woken; | ||
| 42 | } | ||
| 43 | |||
| 44 | method up () { | ||
| 45 | $count++; | ||
| 46 | $self->_wake_waiters(1) if $count > 0; | ||
| 47 | 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | method adjust ($diff) { | ||
| 51 | $count += $diff; | ||
| 52 | my $n = $count; | ||
| 53 | my $waiting = scalar @waiters; | ||
| 54 | $n = $waiting if $waiting < $n; | ||
| 55 | $self->_wake_waiters($n); | ||
| 56 | 1; | ||
| 57 | } | ||
| 58 | method wait ( $reason = 'Semaphore down', $level = 2 ) { $self->_block_until_available( $reason, $level ) } | ||
| 59 | |||
| 60 | method guard () { | ||
| 61 | $self->down( 'Semaphore down', 3 ); | ||
| 62 | Acme::Parataxis::Semaphore::Guard->new( semaphore => $self ); | ||
| 63 | } | ||
| 64 | |||
| 65 | method remove_waiter ($fid) { # Unregisters a parked fiber id so a later up()/adjust() will not wake it. | ||
| 66 | my $before = @waiters; | ||
| 67 | @waiters = grep { $_ != $fid } @waiters; | ||
| 68 | return $before - @waiters; | ||
| 69 | } | ||
| 70 | }; | ||
| 71 | |||
| 72 | # Util. A classic package, not feature::class: a perlclass 'method DESTROY' on a second class | ||
| 73 | # block in the same file crashes perl_clone on some perls (threads->create => access violation | ||
| 74 | # in Perl_newPADNAMEouter), and keep()/sync primitives must remain loadable pre-thread-create. | ||
| 75 | package Acme::Parataxis::Semaphore::Guard v0.1.6 { | ||
| 76 | |||
| 77 | sub new ( $class, %args ) { | ||
| 78 | return bless { semaphore => $args{semaphore} }, $class; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub DESTROY { | ||
| 82 | my $self = shift; | ||
| 83 | return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; | ||
| 84 | $self->{semaphore}->up; | ||
| 85 | } | ||
| 86 | }; | ||
| 87 | # | ||
| 88 | 1; |