| 1 | use v5.40; | ||
| 2 | no warnings qw[experimental::class recursion]; | ||
| 3 | use feature 'class'; | ||
| 4 | class Acme::Parataxis::Driver v0.1.6 { | ||
| 5 | use Carp qw[croak]; | ||
| 6 | use builtin qw[blessed]; | ||
| 7 | |||
| 8 | # Base class for the event-loop drivers behind Acme::Parataxis->attach_loop(). A driver wraps an existing CPAN | ||
| 9 | # event loop (Mojo::IOLoop, IO::Async::Loop, ...) so that await_read / await_write / await_sleep are serviced by | ||
| 10 | # epoll/kqueue/select readiness instead of burning an OS-thread pool job per filehandle. The scheduler keeps its | ||
| 11 | # own park/wake machinery; the driver only *tells* the loop which filehandles to watch and hands control to the | ||
| 12 | # loop when every fiber is parked. | ||
| 13 | # | ||
| 14 | # Subclasses implement the loop-specific bit (watch_read/watch_write/unwatch/timer/cancel_timer/drive/poll_ready); | ||
| 15 | # this base owns the bookkeeping -- which watches and timers are still live -- that the scheduler uses to decide | ||
| 16 | # whether the loop still needs control (pending) and to tear the whole thing down between runs (reset). | ||
| 17 | field %watches; # fileno => the filehandle being watched (one *multi-direction* slot per filehandle) | ||
| 18 | field %timers; # opaque timer id => 1, ids handed back by the subclass constructor | ||
| 19 | |||
| 20 | # True while this driver still has registered watches or timers. The scheduler's idle branch hands control to the | ||
| 21 | # loop only when this is true; with nothing pending a run would otherwise block forever in the loop instead of | ||
| 22 | # deadlock-detecting. | ||
| 23 | method pending { return scalar( keys %watches ) + scalar( keys %timers ) } | ||
| 24 | |||
| 25 | # Unwind every watch and timer this driver registered. Called by run() on teardown and by detach_loop(), so a | ||
| 26 | # stale listener socket or deadline timer from an exited run can never leave the next run blocked in the loop. | ||
| 27 | method reset { | ||
| 28 | my @fhs = values %watches; | ||
| 29 | $self->unwatch($_) for @fhs; | ||
| 30 | |||
| 31 | # unwatch() matches on identity, so it cannot find the slot of a handle that was closed while it was being | ||
| 32 | # watched - fileno() is undef by then, and there is no number left to look up. Such a slot would survive | ||
| 33 | # every reset, leaving pending() permanently true so the next run keeps handing control to a loop with | ||
| 34 | # nothing to do. Unwinding everything is the whole contract of this method, so empty the tables outright | ||
| 35 | # rather than trusting the walk above to have reached every entry. | ||
| 36 | %watches = (); | ||
| 37 | my @ids = keys %timers; | ||
| 38 | $self->cancel_timer($_) for @ids; | ||
| 39 | %timers = (); | ||
| 40 | return 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | # -- shared bookkeeping helpers. Subclasses call these when they (de)register with the underlying loop. -- | ||
| 44 | # The key is the descriptor number, but a number is not an identity: the OS recycles it the moment a handle is | ||
| 45 | # closed, so a slot can outlive the handle it was made for and be handed on to an unrelated one. The slot's | ||
| 46 | # value is that handle, held here, so refaddr() is a stable identity for as long as the slot exists, and every | ||
| 47 | # lookup compares the handle it was given against the handle actually stored. Believing the number alone is how | ||
| 48 | # a fiber gets parked on a registration that will never fire: the new handle sees has_watch() true for a slot it | ||
| 49 | # never made, the subclass skips registering it with the loop, and the slot's callback is never reached. | ||
| 50 | method _track_watch ($fh) { | ||
| 51 | my $fd = fileno($fh); | ||
| 52 | $watches{$fd} = $fh; # a slot keyed to a recycled number is replaced, releasing the stale handle | ||
| 53 | return $fd; | ||
| 54 | } | ||
| 55 | |||
| 56 | method _untrack_watch ($fh) { | ||
| 57 | my $fd = fileno($fh); | ||
| 58 | return 0 unless defined $fd; | ||
| 59 | my $held = $watches{$fd}; | ||
| 60 | return 0 unless defined $held; | ||
| 61 | return 0 unless builtin::refaddr($held) == builtin::refaddr($fh); # somebody else's slot | ||
| 62 | delete $watches{$fd}; | ||
| 63 | return 1; | ||
| 64 | } | ||
| 65 | |||
| 66 | method _track_timer ($id) { | ||
| 67 | $timers{$id} = 1; | ||
| 68 | return $id; | ||
| 69 | } | ||
| 70 | |||
| 71 | method _untrack_timer ($id) { | ||
| 72 | return 0 unless exists $timers{$id}; | ||
| 73 | delete $timers{$id}; | ||
| 74 | return 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | # True when $fh already has a live watch slot in this driver (both directions share one slot per filehandle). | ||
| 78 | method has_watch ($fh) { | ||
| 79 | my $fd = fileno($fh); | ||
| 80 | return 0 unless defined $fd; | ||
| 81 | my $held = $watches{$fd}; | ||
| 82 | return 0 unless defined $held; | ||
| 83 | return builtin::refaddr($held) == builtin::refaddr($fh) ? 1 : 0; | ||
| 84 | } | ||
| 85 | method watch_count () { return scalar( keys %watches ) } | ||
| 86 | method timer_count () { return scalar( keys %timers ) } | ||
| 87 | |||
| 88 | # subclass interface | ||
| 89 | method watch_read( $fh, $cb ) {...} # call $cb->() when $fh becomes readable | ||
| 90 | method watch_write( $fh, $cb ) {...} # call $cb->() when $fh becomes writable | ||
| 91 | method unwatch($fh) {...} # stop watching $fh (both directions), 0 if nothing was watched | ||
| 92 | method timer( $ms, $cb ) {...} # call $cb->() after $ms millseconds; returns an opaque id | ||
| 93 | method cancel_timer($id) {...} # cancel a pending timer, 0 if unknown/already fired | ||
| 94 | method drive() {...} # run the loop until at least one event fires (may block) | ||
| 95 | |||
| 96 | # One readiness pass, and the scheduler expects it back promptly -- it only reaches this on the pass where pool jobs | ||
| 97 | # are still in flight and nothing is runnable, so a version that waits turns a long driver timer (a with_timeout | ||
| 98 | # deadline, an await_read timeout) into a stall on that loop. Whether a loop can honour that is up to the loop: | ||
| 99 | # IO::Async's loop_once(0) returns at once and still fires a due timer, while Mojo's one_tick takes no timeout at | ||
| 100 | # all and always waits for the earliest registered timer, so Driver::Mojo's poll_ready is the same call as its | ||
| 101 | # drive(). Subclass this only if your loop can do a genuinely non-blocking pass. | ||
| 102 | method poll_ready() {...} # run the loop's readiness pass; expected not to block | ||
| 103 | |||
| 104 | # Wrap (or pass through) an event loop object as a Driver. attach_loop() routes everything through here, so callers | ||
| 105 | # may hand in a Mojo::IOLoop (or Mojo::Reactor) or an IO::Async::Loop and get the right reference driver for free. | ||
| 106 | # A plain package sub declared outside the class block, since it returns a driver rather than dispatching on one. | ||
| 107 | # Called as Acme::Parataxis::Driver::wrap($loop). | ||
| 108 | sub wrap ($loop) { | ||
| 109 | Carp::croak 'wrap() requires an event-loop object' unless builtin::blessed($loop); | ||
| 110 | return $loop if $loop->isa('Acme::Parataxis::Driver'); | ||
| 111 | my $pkg = ref $loop; | ||
| 112 | if ( $pkg =~ /^Mojo::/ || $loop->isa('Mojo::IOLoop') || $loop->isa('Mojo::Reactor') ) { | ||
| 113 | require Acme::Parataxis::Driver::Mojo; | ||
| 114 | return Acme::Parataxis::Driver::Mojo->new( loop => $loop ); | ||
| 115 | } | ||
| 116 | if ( $pkg =~ /^IO::Async::/ ) { | ||
| 117 | require Acme::Parataxis::Driver::IOAsync; | ||
| 118 | return Acme::Parataxis::Driver::IOAsync->new( loop => $loop ); | ||
| 119 | } | ||
| 120 | Carp::croak "wrap() does not know how to drive a $pkg event loop"; | ||
| 121 | } | ||
| 122 | }; | ||
| 123 | # | ||
| 124 | 1; |