Acme-Parataxis

 view release on metacpan or  search on metacpan

lib/Acme/Parataxis/Channel.pm  view on Meta::CPAN

use v5.40;
use feature 'class';
no warnings 'experimental::class', 'experimental::try', 'recursion';
#
class Acme::Parataxis::Channel v0.1.3 {
    use Acme::Parataxis qw[fiber await_sleep];
    use Acme::Parataxis::CancellationToken;
    use Acme::Parataxis::Semaphore;
    use Carp qw[croak];
    #
    field $capacity : reader : param //= 2_000_000_000;
    field $timeout  : reader : param //= 0;               # per-channel default wait bound in ms; 0 (and undef) means none
    field $sem_get = Acme::Parataxis::Semaphore->new( count => 0 );
    field $sem_put = Acme::Parataxis::Semaphore->new( count => $capacity );
    field @data : reader;
    field @select_waiters;                                # [fid, op] pairs parked here by select(); op is 'get' or 'put'
    ADJUST {
        $capacity >= 1                    or die "Channel capacity must be >= 1 (got $capacity)\n";
        defined $timeout && $timeout >= 0 or die "Channel timeout must be a non-negative number of milliseconds (got $timeout)\n";
    }

    # Runs $op (a get/put body that parks through the channel semaphores) under the channel's default
    # wait bound. The deadline is armed only when the op may actually park ($may_park): a ready channel
    # never pays for a timer. Exactly like Channel::select's deadline - one token registered before the
    # park (register-then-arm closes the lost-wakeup window) and one timer, re-used across the
    # semaphore's internal re-parks; Error::Timeout on expiry, the waiter unregistered from the
    # semaphore when interrupted. Teardown unregisters the caller and cancels the token on every exit,
    # which recalls the timer's armed sleep job instead of leaving a worker sleeping out the bound.
    method _with_deadline ( $may_park, $op ) {
        return $op->() if !defined $timeout || $timeout <= 0 || !$may_park;
        my $deadline = Acme::Parataxis::CancellationToken->new( kind => 'timeout' );
        $deadline->register;
        my $ms = $timeout;
        fiber {
            $deadline->register;
            try { await_sleep($ms); $deadline->cancel } catch ($e) {
            }    # swallows its own interrupt
        };
        my ( $ok, $err, @rv );
        $ok = 1;
        if (wantarray) {
            try { @rv = $op->() } catch ($e) {
                $ok  = 0;
                $err = $e
            }
        }
        else {
            try { $rv[0] = $op->() } catch ($e) {
                $ok  = 0;
                $err = $e
            }
        }
        $deadline->unregister;
        $deadline->cancel;
        die $err if !$ok;
        return wantarray ? @rv : $rv[0];
    }

    method put ($value) {
        return $self->_with_deadline(
            @data >= $capacity,
            sub {
                $sem_put->down( 'Channel put', 5 );
                push @data, $value;
                $sem_get->up;
                $self->_wake_select_waiters('get');
                1;
            }
        );
    }

    method put_priority ($value) {
        push @data, $value;
        $sem_get->up;
        $self->_wake_select_waiters('get');
        1;
    }

    method try_put ($value) {
        return 0 unless $sem_put->try;
        push @data, $value;
        $sem_get->up;
        $self->_wake_select_waiters('get');
        return 1;
    }

lib/Acme/Parataxis/Channel.pm  view on Meta::CPAN

        # never stack timers. "Register, then arm" (token.register before the sleep job) closes the lost-wakeup
        # window an await_sleep-only timeout would leave between the probe step and registration.
        my $deadline;
        my $timer_armed;
        my ( $out_ch, $out_val, $out_die ) = ( undef, undef, undef );
    OUTER: while (1) {

            # Phase 1 -- probe, no yield: shuffle so no ordering starves a case, commit the first match, and let
            # default run only when nothing is ready (it never parks).
            my @order = 0 .. $#cases;
            for ( my $i = $#order; $i > 0; $i-- ) {
                my $j = int rand( $i + 1 );
                @order[ $i, $j ] = @order[ $j, $i ];
            }
            for my $i (@order) {
                my ( $ch, $op, $v ) = @{ $cases[$i] };
                if ( $op eq 'get' ) {
                    my ( $ok, $got ) = $ch->try_get;
                    if ($ok) { ( $out_ch, $out_val ) = ( $ch, $got ); last OUTER; }
                }
                else {
                    if ( $ch->try_put($v) ) { ( $out_ch, $out_val ) = ( $ch, $v ); last OUTER; }
                }
            }
            if ( defined $default ) { ( $out_ch, $out_val ) = ( undef, $default->() ); last OUTER; }

            # Phase 2 -- register on every involved channel, arm the shared deadline, then park. On wake the
            # park's deregistrations were already dropped (_resume_hooks) or the per-channel wake skipped the
            # fiber, so re-probe exactly once and fall back to re-registering; the one-shot re-poll per wake is
            # natural here because registering happens before the next park.
            for my $case (@cases) { $case->[0]->_register_select_waiter( $fid, $case->[1] ); }
            if ( defined $timeout && $timeout > 0 ) {
                $deadline //= Acme::Parataxis::CancellationToken->new( kind => 'timeout' );
                $deadline->register;
                unless ($timer_armed) {
                    my $ms = $timeout;

                    # The timer registers on the shared deadline and swallows its own interrupt, so teardown can
                    # recall it when a case commits first instead of leaving a worker parked for the full $ms.
                    Acme::Parataxis::fiber {
                        $deadline->register;
                        try { Acme::Parataxis::await_sleep($ms); $deadline->cancel } catch ($e) {
                        }
                    };
                    $timer_armed = 1;
                }
            }
            my $dereg = sub {
                for my $case (@cases) { $case->[0]->_unregister_select_waiter($fid) }
                $deadline->unregister if defined $deadline;
            };
            my $ok = 1;
            my $err;
            try { Acme::Parataxis::_park( 'Channel select', 1, $dereg ) }
            catch ($e) { $ok = 0; $err = $e }
            finally { $dereg->() }    # idempotent: on interrupt _park already ran it; on a natural wake this is the cleanup
            if ( !$ok ) {

                # Our own deadline fired -> the API is (undef, undef) on timeout. A timeout sent by an *enclosing*
                # with_timeout/nursery did not cancel our token, so it propagates instead of being swallowed.
                if ( defined $deadline && $deadline->cancelled && ref($err) && $err->isa('Acme::Parataxis::Error::Timeout') ) {
                    ( $out_ch, $out_val ) = ( undef, undef );
                    last OUTER;
                }
                $out_die = $err;
                last OUTER;
            }
        }

        # Every exit funnels through here so the armed timeout helper is never orphaned: drop the select fiber off
        # the token first (a running fiber must not receive its own interrupt), then cancel to recall the timer's
        # sleep job. A deadline that already fired makes both steps no-ops.
        $deadline->unregister if defined $deadline;
        $deadline->cancel     if defined $deadline;
        die $out_die          if defined $out_die;
        return ( $out_ch, $out_val );
    }
};
#
1;



( run in 1.038 second using v1.01-cache-2.11-cpan-234fa220139 )