| 1 | use v5.40; | ||
| 2 | no warnings qw[experimental::class recursion]; | ||
| 3 | use feature 'class'; | ||
| 4 | class Acme::Parataxis::Driver::Mojo v0.1.6 : isa(Acme::Parataxis::Driver) { | ||
| 5 | use Carp qw[croak]; | ||
| 6 | use builtin qw[blessed]; | ||
| 7 | |||
| 8 | # Drives readiness through a Mojo::IOLoop. The loop and Parataxis cooperate: Mojo never runs Parataxis callbacks | ||
| 9 | # and Parataxis never runs Mojo callbacks; the reactor simply collects fd events and the tokencallback tells the | ||
| 10 | # scheduler which fibers to resume. The passed $loop may itself be a Mojo::Reactor (anything with the reactor | ||
| 11 | # API (io/watch/remove/timer/one_tick)) so a worker's own mini-loop can be run by Parataxis too. | ||
| 12 | field $loop : param; | ||
| 13 | field $reactor; | ||
| 14 | field %read_cb; # fileno => coderef | ||
| 15 | field %write_cb; # fileno => coderef | ||
| 16 | ADJUST { | ||
| 17 | croak 'Acme::Parataxis::Driver::Mojo requires a Mojo::IOLoop (or a Mojo::Reactor)' unless blessed($loop); | ||
| 18 | $reactor = $loop->can('reactor') ? $loop->reactor : $loop; | ||
| 19 | my $has_tick = 0; | ||
| 20 | if ( defined $reactor ) { | ||
| 21 | try { $has_tick = $reactor->can('one_tick') } catch ($e) { | ||
| 22 | } # can() itself may throw | ||
| 23 | } | ||
| 24 | croak 'Acme::Parataxis::Driver::Mojo requires a reactor with a one_tick() method' unless $has_tick; | ||
| 25 | } | ||
| 26 | method watch_read ( $fh, $cb ) { $self->_watch( $fh, 'r', $cb ) } | ||
| 27 | method watch_write ( $fh, $cb ) { $self->_watch( $fh, 'w', $cb ) } | ||
| 28 | |||
| 29 | # Mojo's reactor allows exactly one io() watcher per filehandle; directional interest is expressed through | ||
| 30 | # watch($fh, $read, $write). So we install the io() callback once per filehandle (if not already there) and flip | ||
| 31 | # the desired mask on every registration. | ||
| 32 | method _watch ( $fh, $dir, $cb ) { | ||
| 33 | my $fd = fileno($fh); | ||
| 34 | croak 'Mojo driver cannot watch a closed filehandle' unless defined $fd; | ||
| 35 | |||
| 36 | # has_watch() compares handles, not descriptor numbers, so a false here means this handle has no slot of its | ||
| 37 | # own... it is new to us, or the number it was just given belonged to a closed handle we were still holding. | ||
| 38 | # Either way the callbacks keyed to this number are not this handle's, and a stale direction left behind is | ||
| 39 | # live: a handle that only ever asked to read would have POLLOUT set from the previous owner and its dead | ||
| 40 | # write callback fired. Clear the slot before claiming it. | ||
| 41 | my $fresh = !$self->has_watch($fh); | ||
| 42 | if ($fresh) { | ||
| 43 | delete $read_cb{$fd}; | ||
| 44 | delete $write_cb{$fd}; | ||
| 45 | } | ||
| 46 | if ( $dir eq 'r' ) { $read_cb{$fd} = $cb } | ||
| 47 | else { $write_cb{$fd} = $cb } | ||
| 48 | if ($fresh) { | ||
| 49 | try { $fh->blocking(0) } catch ($e) { | ||
| 50 | } # a handle that refuses is still worth watching | ||
| 51 | $reactor->io( | ||
| 52 | $fh, | ||
| 53 | sub ( $reactor, $writable ) { | ||
| 54 | if ($writable) { $write_cb{$fd}->() if $write_cb{$fd} } | ||
| 55 | else { $read_cb{$fd}->() if $read_cb{$fd} } | ||
| 56 | } | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | $self->_track_watch($fh); | ||
| 60 | $reactor->watch( $fh, $read_cb{$fd} ? 1 : 0, $write_cb{$fd} ? 1 : 0 ); | ||
| 61 | return $fh; | ||
| 62 | } | ||
| 63 | |||
| 64 | method unwatch ($fh) { | ||
| 65 | my $fd = fileno($fh); | ||
| 66 | return 0 unless defined $fd && $self->has_watch($fh); | ||
| 67 | delete $read_cb{$fd}; | ||
| 68 | delete $write_cb{$fd}; | ||
| 69 | $self->_untrack_watch($fh); | ||
| 70 | try { $reactor->remove($fh) } catch ($e) { | ||
| 71 | } # already gone from the reactor's point of view | ||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | method timer ( $ms, $cb ) { | ||
| 76 | croak 'Mojo driver timer requires a positive delay' unless defined $ms && $ms > 0; | ||
| 77 | my $id; | ||
| 78 | $id = $reactor->timer( | ||
| 79 | $ms / 1000, | ||
| 80 | sub { | ||
| 81 | $self->_untrack_timer($id); | ||
| 82 | $cb->(); | ||
| 83 | } | ||
| 84 | ); | ||
| 85 | $self->_track_timer($id); | ||
| 86 | return $id; | ||
| 87 | } | ||
| 88 | |||
| 89 | method cancel_timer ($id) { | ||
| 90 | return 0 unless $self->_untrack_timer($id); | ||
| 91 | $reactor->remove($id); | ||
| 92 | return 1; | ||
| 93 | } | ||
| 94 | method drive () { $reactor->one_tick; return 1 } | ||
| 95 | method poll_ready () { $reactor->one_tick; return 1 } | ||
| 96 | }; | ||
| 97 | # | ||
| 98 | 1; |