| 1 | use v5.40; | ||
| 2 | no warnings 'experimental::class', 'recursion'; | ||
| 3 | use feature 'class'; | ||
| 4 | use Acme::Parataxis; | ||
| 5 | |||
| 6 | # Stackful lazy iterator backed by a private coroutine. | ||
| 7 | class Acme::Parataxis::Generator v0.1.6 { | ||
| 8 | use Carp qw[croak]; | ||
| 9 | our $RESERVED; | ||
| 10 | our $DRAIN = \do { my $x = 1 }; # an opaque scalar ref, so no user yield/die value can collide with the marker | ||
| 11 | field $code : param; # the user's producer; kept so construction can validate and seed the driver | ||
| 12 | field $fiber; # the private fiber the body runs on | ||
| 13 | field $err_ref; # ref to the fiber-local error the driver parks the body's die in | ||
| 14 | |||
| 15 | # The body's yield: suspends the private fiber and returns the next value to ->next. When DESTROY resumes | ||
| 16 | # the fiber to drain it, coro_yield returns our drain marker and the closure dies with it so the body stops | ||
| 17 | # on the next suspension boundary. The fiber never enters the scheduler run queue; each ->next resumes it via | ||
| 18 | # coro_call, i.e. it parks in the same way a coroutine parks, and exhaustion/error finish the fiber normally. | ||
| 19 | # Windows longjmp across the very first fiber allocated in a process (fid 0) is unreliable (the body's die | ||
| 20 | # escapes every anchored JMPENV -> "uncaught die" or 0xC0000005), so a generator must never land on fid 0; see | ||
| 21 | # _ensure_reserved_fiber below. A die raised by the body on a resumed trip is caught here (the scheduler's own | ||
| 22 | # G_EVAL can miss it on resume, see the fid-0 note) and the error string is parked in $err_ref, which ->next | ||
| 23 | # dereferences and rethrows on the caller's stack, so no die ever crosses a transfer. | ||
| 24 | ADJUST { | ||
| 25 | croak 'Generator->new() requires a CODE ref' unless ref $code eq 'CODE'; | ||
| 26 | _ensure_reserved_fiber(); | ||
| 27 | my $yield = sub ($value) { | ||
| 28 | my $got = Acme::Parataxis::coro_yield( [$value] ); | ||
| 29 | die $got->[0] if ref $got eq 'ARRAY' && @$got == 1 && ref( $got->[0] ) eq 'SCALAR' && $got->[0] == $DRAIN; | ||
| 30 | return; | ||
| 31 | }; | ||
| 32 | my $err; | ||
| 33 | my $driver = sub { | ||
| 34 | try { $code->($yield) } catch ($caught) { | ||
| 35 | $err = $caught | ||
| 36 | } | ||
| 37 | return; | ||
| 38 | }; | ||
| 39 | $fiber = Acme::Parataxis->new( code => $driver ); | ||
| 40 | $err_ref = \$err; | ||
| 41 | } | ||
| 42 | |||
| 43 | method next () { | ||
| 44 | return undef if $fiber->is_done; | ||
| 45 | my $rv = Acme::Parataxis::coro_call( $fiber->fid, [] ); | ||
| 46 | return ( ref $rv eq 'ARRAY' ) ? $rv->[0] : undef unless $fiber->is_done; | ||
| 47 | my $err = ${$err_ref}; | ||
| 48 | die $err if defined $err && !( ref($err) eq 'SCALAR' && $err == $DRAIN ); | ||
| 49 | return undef; | ||
| 50 | } | ||
| 51 | method is_done () { $fiber->is_done } | ||
| 52 | method fiber () {$fiber} # the private fiber (fid inspection, tests) | ||
| 53 | |||
| 54 | # An unexhausted (suspended) fiber is drained to its natural exit instead of being torn down in mid-shot: | ||
| 55 | # resuming it with the drain marker makes the yield closure die, the fiber-local try/catch absorbs it, and the | ||
| 56 | # fiber finishes normally (perl unwinds its own scopes), after which coro_call reaps it. (destroy_coro | ||
| 57 | # mid-eval became safe with the C-level fix that made destroying a parked fiber safe, but draining is retained | ||
| 58 | # so the body can run its own finalization.) | ||
| 59 | method DESTROY () { | ||
| 60 | return if !defined $fiber; | ||
| 61 | my $fid = $fiber->fid; | ||
| 62 | return if !defined $fid || $fid < 0; | ||
| 63 | if ( !$fiber->is_done ) { | ||
| 64 | |||
| 65 | # try/catch neither reads nor writes $@, so the local $@ this used to need (to keep a | ||
| 66 | # failed drain from clobbering the caller's error) is gone along with the eval. | ||
| 67 | try { Acme::Parataxis::coro_call( $fid, [$DRAIN] ) } catch ($caught) { | ||
| 68 | } | ||
| 69 | } | ||
| 70 | return if $fiber->is_done; | ||
| 71 | Acme::Parataxis::destroy_coro($fid); | ||
| 72 | $fiber->[Acme::Parataxis::F_FID] = -1; | ||
| 73 | $fiber->[Acme::Parataxis::F_IS_DONE] = 1; | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | # The reserved fiber is a permanent gift of fid 0, so every generator lands on fid >= 1. On the mainline | ||
| 78 | # nothing else has allocated a fiber yet, so one permanently parked fiber takes fid 0. Inside a run the run's | ||
| 79 | # own main fiber already holds fid 0 before the body executes, so a generator built there is on fid >= 1 | ||
| 80 | # regardless and reserving again buys nothing: all it adds is a fiber parked for the lifetime of the process, | ||
| 81 | # which the scheduler's deadlock detector counts as run work and trips over when that run ends. | ||
| 82 | # (Acme::Parataxis snapshots %PRESET_FIBERS when a run starts, so a fiber born *during* the run is not | ||
| 83 | # excluded from that count the way a fiber from an earlier run is.) | ||
| 84 | sub _ensure_reserved_fiber { | ||
| 85 | return if $RESERVED; | ||
| 86 | return if Acme::Parataxis->current_fid >= 0; # a run already owns fid 0; do not add a permanent fiber | ||
| 87 | my $fiber = Acme::Parataxis->new( | ||
| 88 | code => sub { | ||
| 89 | Acme::Parataxis::coro_yield( [] ); | ||
| 90 | return; | ||
| 91 | } | ||
| 92 | ); | ||
| 93 | Acme::Parataxis::coro_call( $fiber->fid, [] ); | ||
| 94 | $RESERVED = $fiber; | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | }; | ||
| 98 | # | ||
| 99 | 1; |