blib/lib/Acme/Parataxis/Local.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 49 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.40;
2 no warnings 'experimental::class';
3 use feature 'class';
4 #
5 # A named, per-fiber value slot. Each Local object is one logical variable that reads
6 # differently depending on which fiber asks; fibers never see each other's values, and a
7 # completed fiber's values vanish when its fiber object is destroyed (DESTROY prunes
8 # %FIBER_LOCALS, the same place fids are released).
9 #
10 # Keys are a monotonic process-wide id, never a refaddr: a recycled memory address must not
11 # alias a newer Local's slot.
12 class Acme::Parataxis::Local v0.1.6 {
13 use Acme::Parataxis;
14 use Carp qw[croak];
15 my $NEXT_ID = 0;
16 field $id = $NEXT_ID++; # process-wide unique slot key
17 field $default : param = undef;
18 field $inherit : param = 0; # opt-in: spawn copies this slot's value from parent fiber to child (trace propagation)
19 ADJUST {
20 # register with spawn's trace-propagation hook: a fiber that spawns a child with a value in this slot
21 # seeds it into the child's stash before the child body runs (see Acme::Parataxis::spawn)
22 push @Acme::Parataxis::INHERIT_LOCAL_IDS, $id if $inherit;
23 }
24  
25 method get () {
26 my $fid = Acme::Parataxis->current_fid;
27 croak 'Local->get must be called from inside a scheduled fiber' if $fid < 0;
28 my $stash = Acme::Parataxis::_fiber_locals( Acme::Parataxis->by_id($fid) );
29 return $default unless exists $stash->{$id};
30 return $stash->{$id};
31 }
32  
33 method set ($value) {
34 my $fid = Acme::Parataxis->current_fid;
35 croak 'Local->set must be called from inside a scheduled fiber' if $fid < 0;
36 Acme::Parataxis::_fiber_locals( Acme::Parataxis->by_id($fid) )->{$id} = $value;
37 return $value;
38 }
39  
40 method DESTROY {
41 return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
42 @Acme::Parataxis::INHERIT_LOCAL_IDS = grep { $_ != $id } @Acme::Parataxis::INHERIT_LOCAL_IDS;
43 }
44 };
45 #
46 1;