| 1 | use v5.40; | ||
| 2 | no warnings 'experimental::class', 'recursion'; | ||
| 3 | use feature 'class'; | ||
| 4 | |||
| 5 | # A monitor is pure observation, like an Erlang monitor: it resolves exactly once, when the | ||
| 6 | # target fiber exits, to undef for a clean end or to the fiber's death error for a crash. It | ||
| 7 | # rides the target's existing on_ready/on_death slot, so it needs no scheduler change and | ||
| 8 | # never owns the target (no stop, no join-without-error semantics). Watching an already-dead | ||
| 9 | # target fires immediately, because the underlying Future resolves inline during construction | ||
| 10 | # (the target parameter's on_ready hook fires right there). | ||
| 11 | # | ||
| 12 | # The target is held strongly so a monitor never loses its subject; reapability is about the | ||
| 13 | # C coroutine slot (driven by the fiber's own completion), which the monitor cannot delay. | ||
| 14 | # The reverse edge is cut: the completion callback captures the monitor weakly, so dropping | ||
| 15 | # a monitor lets it (and its strong target ref) dissolve instead of lingering in the target's | ||
| 16 | # callback list. | ||
| 17 | class Acme::Parataxis::Monitor v0.1.6 { | ||
| 18 | use Acme::Parataxis; | ||
| 19 | use Acme::Parataxis::Future; | ||
| 20 | use Carp qw[croak]; | ||
| 21 | use builtin qw[blessed weaken]; | ||
| 22 | field $target : param = undef; # the watched fiber/future/actor object; a fiber id is resolved to its object first | ||
| 23 | field $fid : param = undef; # optionally supplied watch target by id instead of by object | ||
| 24 | field $future; # the single-resolution future that backs ->await/->result/->is_ready | ||
| 25 | ADJUST { | ||
| 26 | croak 'Monitor->new() takes a target or a fid, not both' if defined $target && defined $fid; | ||
| 27 | if ( defined $fid ) { | ||
| 28 | croak "Monitor->new() expects a numeric fiber id (got '$fid')" unless $fid =~ /^-?\d+$/; | ||
| 29 | $target = Acme::Parataxis->by_id( 0 + $fid ); | ||
| 30 | croak "Monitor->new() couldn't find a fiber with id $fid" unless defined $target; | ||
| 31 | } | ||
| 32 | croak 'Monitor->new() requires a target to watch' unless defined $target; | ||
| 33 | croak 'Monitor->new() requires a blessed target (fibers, futures, actors)' unless blessed $target; | ||
| 34 | if ( !$target->can('on_ready') && !$target->can('on_death') ) { | ||
| 35 | croak 'Monitor->new() cannot watch this target: it has neither on_ready() (fibers, futures) nor on_death() (actors)'; | ||
| 36 | } | ||
| 37 | $fid //= $target->can('fid') ? $target->fid : undef; | ||
| 38 | $future = Acme::Parataxis::Future->new; | ||
| 39 | |||
| 40 | # The reverse edge is cut: the target's callback list holds only this weak copy, so a | ||
| 41 | # dropped monitor (and its strong target ref) dissolves instead of lingering there. | ||
| 42 | my $weak = $self; | ||
| 43 | weaken $weak; | ||
| 44 | if ( $target->can('on_ready') ) { | ||
| 45 | $target->on_ready( sub { $weak->_resolve( $weak->target->error ) if $weak } ); | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | $target->on_death( sub { $weak->_resolve( $_[1] ) if $weak } ); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | # Resolve exactly once. $err is the target's death error, or undef for a clean end; it becomes | ||
| 53 | # the monitor's *result* (never set_error), so ->await/->result return it instead of throwing. | ||
| 54 | method _resolve ($err) { | ||
| 55 | return $self if $future->is_ready; | ||
| 56 | $future->set_result($err); | ||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | method await () { $future->await } # parks until the target exits | ||
| 60 | method result () { $future->result } # the resolution value; croaks until ready | ||
| 61 | method error () { $future->is_ready ? $future->result : undef } # death error (or undef), ready or not | ||
| 62 | method is_ready () { $future->is_ready } | ||
| 63 | method is_done () { $future->is_ready } # resolved == done, mirroring the fiber-side naming | ||
| 64 | method target () {$target} # the watched fiber/future/actor object | ||
| 65 | method fid () {$fid} # the target's fiber id at watch time (undef for non-fiber targets) | ||
| 66 | |||
| 67 | method on_ready ($cb) { | ||
| 68 | croak 'on_ready() requires a CODE ref' unless ref $cb eq 'CODE'; | ||
| 69 | my $weak = $self; | ||
| 70 | weaken $weak; | ||
| 71 | $future->on_ready( sub { $cb->($weak) if $weak } ); # hand the caller the monitor, not the inner future | ||
| 72 | return $self; | ||
| 73 | } | ||
| 74 | }; | ||
| 75 | # | ||
| 76 | 1; |