blib/lib/Zuzu/Runtime/Async/Scheduler.pm

filter: Statementstmt 86.2% 188 / 218 Branchbran 59.6% 74 / 124 Conditioncond 39.0% 41 / 105 MC/DCmcdc 9.6% 6 / 62 Subroutinesub 93.9% 31 / 33 Podpod 0.0% 0 / 17 totaltotal 60.8% 340 / 559 CC 103 SCAR 30.7
CC
103
Cov
68%
run_pending_task
132.0
trace_task
36.2
schedule
18.4
CRAP 21.4
« blib/lib/Zuzu/Util/NativeHelpers.pm ↑ Summary blib/lib/Zuzu/Doc/CLI.pm »
1 package Zuzu::Runtime::Async::Scheduler;
2  
3 69 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 69 our $VERSION = '0.007002';
6  
7 69 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 69 ▶use Coro qw( async cede );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 69 ▶use Scalar::Util qw( refaddr weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 69 ▶use Time::HiRes qw( sleep );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 69 ▶use Zuzu::Runtime::Async::TaskGroup;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 69 has 'runtime' => ( is => 'rw' );
15 547 ▶has 'tasks' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
16 has 'root_group' => (
17 is => 'rw',
18 547 ▶ default => sub { Zuzu::Runtime::Async::TaskGroup->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 69 );
20 69 has 'current_task' => ( is => 'rw' );
21 69 has 'current_group' => ( is => 'rw' );
22 547 ▶has 'next_task_id' => ( is => 'rw', default => sub { 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23 547 ▶has 'trace_events' => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 547 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.1
Pod undocumented
26 547 my ( $self ) = @_;
27  
28 547 ▶ weaken( $self->{runtime} ) if defined $self->{runtime};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"runtime"} 547 0 547
29 547 $self->current_group( $self->root_group );
30 }
31  
32 15 ▶sub new_group {
Subroutine new_group: called CC=3 SCAR=14.2
Pod undocumented
33 15 my ( $self, $parent ) = @_;
34  
35 15 ▶ return Zuzu::Runtime::Async::TaskGroup->new(
Condition33%2/6
($parent // $self->current_group) // $self->root_group
l!l&&r!l&&!r
1500
$parent // $self->current_group
l!l&&r!l&&!r
1500
MC/DC0%0/3
($parent // $self->current_group) // $self->root_group
$parent $self->current_group $self->root_group
Truth table25%1/4
A // B // C
A= $parentB= $self->current_groupC= $self->root_group
ABCresult
1XX1
01X1
0011
0000
36 parent => $parent // $self->current_group // $self->root_group,
37 );
38 }
39  
40 837 ▶sub debug_enabled {
Subroutine debug_enabled: called CC=2 SCAR=6.9
Pod undocumented
41 69 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 837 ▶ return $Zuzu::Runtime::DEBUG_LEVEL > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Zuzu::Runtime::DEBUG_LEVEL > 0 ? : 26 811 837
44 }
45  
46 837 ▶sub trace_task {
Subroutine trace_task: called CC=16 SCAR=35.9
Pod undocumented
47 837 my ( $self, $event, $task, $extra ) = @_;
48  
49 837 ▶ return if !$self->debug_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug_enabled 811 26 837
50 26 ▶ my $record = {
Branch: 6 branches
BranchTrueFalseTotal
defined $task && $task->can('id') ? : 26 0 26
defined $task && $task->can('parent_id') ? : 26 0 26
defined $task && $task->can('name') ? : 26 0 26
defined $task && $task->can('status') ? : 26 0 26
defined $task && $task->can('file') ? : 26 0 26
defined $task && $task->can('line') ? : 26 0 26
Condition33%6/18
defined $task and $task->can("id")
!ll&&!rl&&r
0026
defined $task and $task->can("parent_id")
!ll&&!rl&&r
0026
defined $task and $task->can("name")
!ll&&!rl&&r
0026
defined $task and $task->can("status")
!ll&&!rl&&r
0026
defined $task and $task->can("file")
!ll&&!rl&&r
0026
defined $task and $task->can("line")
!ll&&!rl&&r
0026
MC/DC0%0/2
defined $task and $task->can("id")
defined $task $task->can("id")
MC/DC0%0/2
defined $task and $task->can("parent_id")
defined $task $task->can("parent_id")
MC/DC0%0/2
defined $task and $task->can("name")
defined $task $task->can("name")
MC/DC0%0/2
defined $task and $task->can("status")
defined $task $task->can("status")
MC/DC0%0/2
defined $task and $task->can("file")
defined $task $task->can("file")
MC/DC0%0/2
defined $task and $task->can("line")
defined $task $task->can("line")
Truth table33%1/3
A and B
A= defined $taskB= $task->can("id")
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $taskB= $task->can("parent_id")
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $taskB= $task->can("name")
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $taskB= $task->can("status")
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $taskB= $task->can("file")
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $taskB= $task->can("line")
ABresult
0X0
100
111
51 event => $event,
52 task_id => defined $task && $task->can('id') ? $task->id : undef,
53 parent_task_id => defined $task && $task->can('parent_id')
54 ? $task->parent_id
55 : undef,
56 name => defined $task && $task->can('name') ? $task->name : undef,
57 status => defined $task && $task->can('status') ? $task->status : undef,
58 file => defined $task && $task->can('file') ? $task->file : undef,
59 line => defined $task && $task->can('line') ? $task->line : undef,
60 };
61 26 ▶ if ( defined $extra ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extra) 4 22 26
62 4 $record->{$_} = $extra->{$_} for keys %$extra;
63 }
64 26 push @{ $self->trace_events }, $record;
65  
66 26 return $record;
67 }
68  
69 0 ▶sub clear_trace {
Subroutine clear_trace: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my ( $self ) = @_;
71  
72 0 $self->trace_events( [] );
73  
74 0 return;
75 }
76  
77 159 ▶sub schedule {
Subroutine schedule: called CC=13 SCAR=29.1
Pod undocumented
78 159 my ( $self, $task, $group ) = @_;
79  
80 159 ▶ return $task if !defined $task;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $task 0 159 159
81 159 ▶ $task->scheduler($self) if $task->can('scheduler');
Branch: 1 branch
BranchTrueFalseTotal
if $task->can("scheduler") 159 0 159
82 159 ▶ $task->use_coro(1) if $task->can('use_coro');
Branch: 1 branch
BranchTrueFalseTotal
if $task->can("use_coro") 159 0 159
83 159 ▶ if ( $task->can('id') and !defined $task->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->can("id") and not defined $task->id) 159 0 159
Condition33%1/3
$task->can("id") and not defined $task->id
!ll&&!rl&&r
00159
MC/DC0%0/2
$task->can("id") and not defined $task->id
$task->can("id") not defined $task->id
Truth table33%1/3
A and not(B)
A= $task->can("id")B= not defined $task->id
ABresult
0X0
100
111
84 159 $task->id( $self->next_task_id );
85 159 $self->next_task_id( $self->next_task_id + 1 );
86 }
87 159 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($task->can("parent_id") and not defined $task->parent_id and defined $self->current_task and $self->current_task->can("id")) 129 30 159
Condition55%5/9
$task->can("parent_id") and not defined $task->parent_id and defined $self->current_task and $self->current_task->can("id")
!ll&&!rl&&r
300129
$task->can("parent_id") and not defined $task->parent_id and defined $self->current_task
!ll&&!rl&&r
030129
$task->can("parent_id") and not defined $task->parent_id
!ll&&!rl&&r
00159
MC/DC25%1/4
$task->can("parent_id") and not defined $task->parent_id and defined $self->current_task and $self->current_task->can("id")
$task->can("parent_id") not defined $task->parent_id defined $self->current_task $self->current_task->can("id")
Truth table40%2/5
A and not(B) and C and D
A= $task->can("parent_id")B= not defined $task->parent_idC= defined $self->current_taskD= $self->current_task->can("id")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
88 $task->can('parent_id')
89 and !defined $task->parent_id
90 and defined $self->current_task
91 and $self->current_task->can('id')
92 ) {
93 129 $task->parent_id( $self->current_task->id );
94 }
95 159 ▶ $group //= $self->current_group // $self->root_group;
Condition50%3/6
$group //= $self->current_group // $self->root_group
l!l&&r!l&&!r
113460
$self->current_group // $self->root_group
l!l&&r!l&&!r
4600
MC/DC0%0/3
$group //= $self->current_group // $self->root_group
$group $self->current_group $self->root_group
Truth table0%0/4
A //= B // C
A= $groupB= $self->current_groupC= $self->root_group
ABCresult
1XX1
01X1
0011
0000
96 159 ▶ $group->add($task) if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 159 0 159
97 159 $self->tasks->{ refaddr($task) } = $task;
98 159 $self->trace_task( schedule => $task );
99  
100 159 return $task;
101 }
102  
103 117 ▶sub ensure_scheduled {
Subroutine ensure_scheduled: called CC=6 SCAR=19.3
Pod undocumented
104 117 my ( $self, $task ) = @_;
105  
106 117 ▶ return $task if !defined $task;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $task 0 117 117
107 return $self->schedule($task)
108 117 ▶ if !exists $self->tasks->{ refaddr($task) };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->tasks->{builtin::refaddr($task)} 46 71 117
109 71 ▶ $task->scheduler($self) if $task->can('scheduler') and !$task->scheduler;
Branch: 1 branch
BranchTrueFalseTotal
if $task->can("scheduler") and not $task->scheduler 0 71 71
Condition33%1/3
$task->can("scheduler") and not $task->scheduler
!ll&&!rl&&r
0710
MC/DC0%0/2
$task->can("scheduler") and not $task->scheduler
$task->can("scheduler") not $task->scheduler
Truth table33%1/3
A and not(B)
A= $task->can("scheduler")B= not $task->scheduler
ABresult
0X0
100
111
110 71 ▶ $task->use_coro(1) if $task->can('use_coro');
Branch: 1 branch
BranchTrueFalseTotal
if $task->can("use_coro") 71 0 71
111  
112 71 return $task;
113 }
114  
115 32387 ▶sub _task_done {
Subroutine _task_done: called CC=4 SCAR=13.9
116 32387 my ( $task ) = @_;
117  
118 32387 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $task->status eq "fulfilled" or $task->status eq "rejected" or $task->status eq "cancelled" 696 31691 32387
Condition100%6/6
$task->status eq "fulfilled" or $task->status eq "rejected" or $task->status eq "cancelled"
l!l&&r!l&&!r
6692731691
$task->status eq "fulfilled" or $task->status eq "rejected"
l!l&&r!l&&!r
6393031718
MC/DC100%3/3
$task->status eq "fulfilled" or $task->status eq "rejected" or $task->status eq "cancelled"
$task->status eq "fulfilled" $task->status eq "rejected" $task->status eq "cancelled"
Truth table100%4/4
A or B or C
A= $task->status eq "fulfilled"B= $task->status eq "rejected"C= $task->status eq "cancelled"
ABCresult
1XX1
01X1
0011
0000
119 if $task->status eq 'fulfilled'
120 or $task->status eq 'rejected'
121 or $task->status eq 'cancelled';
122 31691 return 0;
123 }
124  
125 15687 ▶sub cleanup_task {
Subroutine cleanup_task: called CC=5 SCAR=16.7
Pod undocumented
126 15687 my ( $self, $task ) = @_;
127  
128 15687 ▶ return if !defined $task;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $task 0 15687 15687
129 15687 ▶ return if !_task_done($task);
Branch: 1 branch
BranchTrueFalseTotal
unless _task_done($task) 15233 454 15687
130 454 delete $self->tasks->{ refaddr($task) };
131 454 ▶ if ( $task->can('group') and defined $task->group ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->can("group") and defined $task->group) 454 0 454
Condition33%1/3
$task->can("group") and defined $task->group
!ll&&!rl&&r
00454
MC/DC0%0/2
$task->can("group") and defined $task->group
$task->can("group") defined $task->group
Truth table33%1/3
A and B
A= $task->can("group")B= defined $task->group
ABresult
0X0
100
111
132 454 $task->group->remove($task);
133 }
134 454 $self->trace_task( cleanup => $task );
135  
136 454 return;
137 }
138  
139 4 ▶sub cleanup_done {
Subroutine cleanup_done: called CC=2 SCAR=6.9
Pod undocumented
140 4 my ( $self ) = @_;
141  
142 4 for my $task ( values %{ $self->tasks } ) {
143 1 $self->cleanup_task($task);
144 }
145  
146 4 return;
147 }
148  
149 3 ▶sub active_count {
Subroutine active_count: called CC=1 SCAR=0.0
Pod undocumented
150 3 my ( $self ) = @_;
151  
152 3 $self->cleanup_done;
153 3 return scalar keys %{ $self->tasks };
154 }
155  
156 1 ▶sub shutdown {
Subroutine shutdown: called CC=5 SCAR=16.7
Pod undocumented
157 1 my ( $self, $reason ) = @_;
158  
159 1 for my $task ( values %{ $self->tasks } ) {
160 1 ▶ next if !defined $task;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $task 0 1 1
161 1 ▶ $task->cancel($reason)
Branch: 1 branch
BranchTrueFalseTotal
if $task->can("cancel") and not _task_done($task) 1 0 1
Condition33%1/3
$task->can("cancel") and not _task_done($task)
!ll&&!rl&&r
001
MC/DC0%0/2
$task->can("cancel") and not _task_done($task)
$task->can("cancel") not _task_done($task)
Truth table33%1/3
A and not(B)
A= $task->can("cancel")B= not _task_done($task)
ABresult
0X0
100
111
162 if $task->can('cancel') and !_task_done($task);
163 }
164 1 $self->cleanup_done;
165 1 $self->current_task(undef);
166 1 $self->current_group( $self->root_group );
167 1 $self->trace_task( shutdown => undef );
168  
169 1 return;
170 }
171  
172 25 ▶sub run_until {
Subroutine run_until: called CC=5 SCAR=16.1
Pod undocumented
173 25 my ( $self, $target ) = @_;
174  
175 25 ▶ $self->ensure_scheduled($target) if $target->can('scheduler');
Branch: 1 branch
BranchTrueFalseTotal
if $target->can("scheduler") 25 0 25
176 25 ▶ while ( !_task_done($target) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_task_done($target)) 3642 25 3667
177 3642 my $progress = $self->progress_once($target);
178 3642 cede;
179 3642 ▶ sleep(0.001) if !$progress and !_task_done($target);
Branch: 1 branch
BranchTrueFalseTotal
unless $progress or _task_done($target) 3519 123 3642
Condition100%3/3
$progress or _task_done($target)
l!l&&r!l&&!r
111123519
MC/DC100%2/2
$progress or _task_done($target)
$progress _task_done($target)
Truth table100%3/3
A or B
A= $progressB= _task_done($target)
ABresult
1X1
011
000
180 }
181 25 $self->cleanup_task($target);
182  
183 25 return $target;
184 }
185  
186 3642 ▶sub progress_once {
Subroutine progress_once: called CC=10 SCAR=24.2
Pod undocumented
187 3642 my ( $self, $target ) = @_;
188  
189 3642 my @tasks = values %{ $self->tasks };
190 push @tasks, $target
191 if defined $target
192 3642 ▶ and !exists $self->tasks->{ refaddr($target) };
Branch: 1 branch
BranchTrueFalseTotal
if defined $target and not exists $self->tasks->{builtin::refaddr($target)} 0 3642 3642
Condition33%1/3
defined $target and not exists $self->tasks->{builtin::refaddr($target)}
!ll&&!rl&&r
036420
MC/DC0%0/2
defined $target and not exists $self->tasks->{builtin::refaddr($target)}
defined $target not exists $self->tasks->{builtin::refaddr($target)}
Truth table33%1/3
A and not(B)
A= defined $targetB= not exists $self->tasks->{builtin::refaddr($target)}
ABresult
0X0
100
111
193  
194 3642 my $progress = 0;
195 3642 for my $task ( @tasks ) {
196 15270 ▶ next if !defined $task;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $task 0 15270 15270
197 15270 my $before = $task->status;
198 15270 ▶ if ( $task->status eq 'pending' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->status eq 'pending') { } 100 15170 15270
199 100 ▶ if ( $task->can('use_coro') and $task->use_coro ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->can('use_coro') and $task->use_coro) { } 100 0 100
Condition33%1/3
$task->can("use_coro") and $task->use_coro
!ll&&!rl&&r
00100
MC/DC0%0/2
$task->can("use_coro") and $task->use_coro
$task->can("use_coro") $task->use_coro
Truth table33%1/3
A and B
A= $task->can("use_coro")B= $task->use_coro
ABresult
0X0
100
111
200 100 $self->start_coro_task($task);
201 }
202 else {
203 0 $self->run_pending_task($task);
204 }
205 100 $progress = 1;
206 }
207 else {
208 15170 ▶ $progress = 1 if $task->poll;
Branch: 1 branch
BranchTrueFalseTotal
if $task->poll 38 15132 15170
209 }
210 15270 ▶ $progress = 1 if $task->status ne $before;
Branch: 1 branch
BranchTrueFalseTotal
if $task->status ne $before 38 15232 15270
211 15270 $self->cleanup_task($task);
212 }
213  
214 3642 return $progress;
215 }
216  
217 10332 ▶sub _save_task_runtime_stack {
Subroutine _save_task_runtime_stack: called CC=5 SCAR=17.8
218 10332 my ( $self, $task ) = @_;
219  
220 10332 my $runtime = $self->runtime;
221 10332 ▶ return if !defined $runtime or !defined $task;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $runtime and defined $task 0 10332 10332
Condition33%1/3
defined $runtime and defined $task
!ll&&!rl&&r
0010332
MC/DC0%0/2
defined $runtime and defined $task
defined $runtime defined $task
Truth table33%1/3
A and B
A= defined $runtimeB= defined $task
ABresult
0X0
100
111
222 10332 ▶ $task->runtime_stack( [ @{ $runtime->{_stack} // [] } ] )
Branch: 1 branch
BranchTrueFalseTotal
if $task->can("runtime_stack") 10332 0 10332
Condition50%1/2
$runtime->{"_stack"} // []
l!l
103320
MC/DC0%0/1
$runtime->{"_stack"} // []
$runtime->{"_stack"}
Truth table50%1/2
A
A= $runtime->{"_stack"}
Aresult
11
00
223 if $task->can('runtime_stack');
224  
225 10332 return;
226 }
227  
228 10328 ▶sub _restore_task_runtime_stack {
Subroutine _restore_task_runtime_stack: called CC=5 SCAR=18.1
229 10328 my ( $self, $task ) = @_;
230  
231 10328 my $runtime = $self->runtime;
232 10328 ▶ return if !defined $runtime or !defined $task;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $runtime and defined $task 0 10328 10328
Condition33%1/3
defined $runtime and defined $task
!ll&&!rl&&r
0010328
MC/DC0%0/2
defined $runtime and defined $task
defined $runtime defined $task
Truth table33%1/3
A and B
A= defined $runtimeB= defined $task
ABresult
0X0
100
111
233 10328 ▶ return if !$task->can('runtime_stack') or !defined $task->runtime_stack;
Branch: 1 branch
BranchTrueFalseTotal
unless $task->can("runtime_stack") and defined $task->runtime_stack 0 10328 10328
Condition33%1/3
$task->can("runtime_stack") and defined $task->runtime_stack
!ll&&!rl&&r
0010328
MC/DC0%0/2
$task->can("runtime_stack") and defined $task->runtime_stack
$task->can("runtime_stack") defined $task->runtime_stack
Truth table33%1/3
A and B
A= $task->can("runtime_stack")B= defined $task->runtime_stack
ABresult
0X0
100
111
234 10328 $runtime->{_stack} = [ @{ $task->runtime_stack } ];
235  
236 10328 return;
237 }
238  
239 10228 ▶sub _throw_if_task_cancelled {
Subroutine _throw_if_task_cancelled: called CC=4 SCAR=17.1
240 10228 my ( $self, $task ) = @_;
241  
242 10228 ▶ return if !defined $task;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $task 0 10228 10228
243 10228 ▶ return if !$task->can('status') or $task->status ne 'cancelled';
Branch: 1 branch
BranchTrueFalseTotal
if not $task->can("status") or $task->status ne "cancelled" 10228 0 10228
Condition33%1/3
not $task->can("status") or $task->status ne "cancelled"
l!l&&r!l&&!r
0102280
MC/DC0%0/2
not $task->can("status") or $task->status ne "cancelled"
not $task->can("status") $task->status ne "cancelled"
Truth table33%1/3
not(A) or B
A= not $task->can("status")B= $task->status ne "cancelled"
ABresult
1X1
011
000
244 0 die $task->error;
245 }
246  
247 0 ▶sub run_pending_task {
Subroutine run_pending_task: not called CC=11 SCAR=48.8
Pod undocumented
248 0 my ( $self, $task ) = @_;
249  
250 0 my $prior_task = $self->current_task;
251 0 my $prior_group = $self->current_group;
252 0 my $runtime = $self->runtime;
253 my $prior_stack = defined $runtime
254 0 ▶ ? [ @{ $runtime->{_stack} // [] } ]
Branch: 1 branch
BranchTrueFalseTotal
defined $runtime ? : 0 0 0
Condition0%0/2
$runtime->{"_stack"} // []
l!l
00
MC/DC0%0/1
$runtime->{"_stack"} // []
$runtime->{"_stack"}
Truth table0%0/2
A
A= $runtime->{"_stack"}
Aresult
11
00
255 : undef;
256 0 $self->current_task($task);
257 0 ▶ $self->current_group( $task->group // $prior_group // $self->root_group );
Condition0%0/6
($task->group // $prior_group) // $self->root_group
l!l&&r!l&&!r
000
$task->group // $prior_group
l!l&&r!l&&!r
000
MC/DC0%0/3
($task->group // $prior_group) // $self->root_group
$task->group $prior_group $self->root_group
Truth table0%0/4
A // B // C
A= $task->groupB= $prior_groupC= $self->root_group
ABCresult
1XX1
01X1
0011
0000
258 0 $self->_restore_task_runtime_stack($task);
259 0 $self->trace_task( start => $task );
260 0 my $ok = eval {
261 0 $task->_run_pending;
262 0 1;
263 };
264 0 ▶ my $err = $@ if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
265 0 $self->_save_task_runtime_stack($task);
266 0 ▶ $runtime->{_stack} = $prior_stack if defined $runtime and defined $prior_stack;
Branch: 1 branch
BranchTrueFalseTotal
if defined $runtime and defined $prior_stack 0 0 0
Condition0%0/3
defined $runtime and defined $prior_stack
!ll&&!rl&&r
000
MC/DC0%0/2
defined $runtime and defined $prior_stack
defined $runtime defined $prior_stack
Truth table0%0/3
A and B
A= defined $runtimeB= defined $prior_stack
ABresult
0X0
100
111
267 0 $self->current_task($prior_task);
268 0 $self->current_group($prior_group);
269 0 ▶ die $err if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
270 0 ▶ $self->trace_task( $task->status eq 'fulfilled' ? 'fulfill' : 'reject', $task )
Branch: 2 branches
BranchTrueFalseTotal
if _task_done($task) 0 0 0
$task->status eq 'fulfilled' ? : 0 0 0
271 if _task_done($task);
272 0 $self->cleanup_task($task);
273  
274 0 return $task;
275 }
276  
277 172 ▶sub start_coro_task {
Subroutine start_coro_task: called CC=4 SCAR=14.6
Pod undocumented
278 172 my ( $self, $task ) = @_;
279  
280 172 ▶ return $task if !defined $task;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $task 0 172 172
281 172 ▶ return $task if $task->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $task->is_done 0 172 172
282 172 ▶ return $task if defined $task->coro;
Branch: 1 branch
BranchTrueFalseTotal
if defined $task->coro 72 100 172
283 100 ▶ my $coro = async {
Subroutine __ANON__: called CC=12 SCAR=27.5
284 100 my $prior_task = $self->current_task;
285 100 my $prior_group = $self->current_group;
286 100 my $runtime = $self->runtime;
287 my $prior_stack = defined $runtime
288 100 ▶ ? [ @{ $runtime->{_stack} // [] } ]
Branch: 1 branch
BranchTrueFalseTotal
defined $runtime ? : 100 0 100
Condition50%1/2
$runtime->{"_stack"} // []
l!l
1000
MC/DC0%0/1
$runtime->{"_stack"} // []
$runtime->{"_stack"}
Truth table50%1/2
A
A= $runtime->{"_stack"}
Aresult
11
00
289 : undef;
290 100 $self->current_task($task);
291 100 ▶ $self->current_group(
Condition33%2/6
($task->group // $prior_group) // $self->root_group
l!l&&r!l&&!r
10000
$task->group // $prior_group
l!l&&r!l&&!r
10000
MC/DC0%0/3
($task->group // $prior_group) // $self->root_group
$task->group $prior_group $self->root_group
Truth table25%1/4
A // B // C
A= $task->groupB= $prior_groupC= $self->root_group
ABCresult
1XX1
01X1
0011
0000
292 $task->group // $prior_group // $self->root_group
293 );
294 100 $self->_restore_task_runtime_stack($task);
295 100 ▶ $task->status('running') if $task->status eq 'pending';
Branch: 1 branch
BranchTrueFalseTotal
if $task->status eq "pending" 99 1 100
296 100 $self->trace_task( start => $task );
297 100 my $ok = eval {
298 100 $task->_run_body;
299 100 1;
300 };
301 100 ▶ my $err = $@ if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 100 100
302 100 $self->_save_task_runtime_stack($task);
303 100 ▶ $runtime->{_stack} = $prior_stack
Branch: 1 branch
BranchTrueFalseTotal
if defined $runtime and defined $prior_stack 100 0 100
Condition33%1/3
defined $runtime and defined $prior_stack
!ll&&!rl&&r
00100
MC/DC0%0/2
defined $runtime and defined $prior_stack
defined $runtime defined $prior_stack
Truth table33%1/3
A and B
A= defined $runtimeB= defined $prior_stack
ABresult
0X0
100
111
304 if defined $runtime and defined $prior_stack;
305 100 $self->current_task($prior_task);
306 100 $self->current_group($prior_group);
307 100 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 100 100
308 0 $task->error($err);
309 0 $task->status('rejected');
310 }
311 $self->trace_task(
312 100 ▶ $task->status eq 'fulfilled' ? 'fulfill' : 'reject',
Branch: 2 branches
BranchTrueFalseTotal
if _task_done($task) 100 0 100
$task->status eq 'fulfilled' ? : 92 8 100
313 $task,
314 ) if _task_done($task);
315 100 $self->cleanup_task($task);
316 100 };
317 100 $task->coro($coro);
318  
319 100 return $task;
320 }
321  
322 10232 ▶sub yield_current_task {
Subroutine yield_current_task: called CC=1 SCAR=0.0
Pod undocumented
323 10232 my ( $self ) = @_;
324  
325 10232 my $current = $self->current_task;
326 10232 my $group = $self->current_group;
327 10232 $self->_save_task_runtime_stack($current);
328 10232 cede;
329 10228 $self->current_task($current);
330 10228 $self->current_group($group);
331 10228 $self->_restore_task_runtime_stack($current);
332 10228 $self->_throw_if_task_cancelled($current);
333  
334 10228 return;
335 }
336  
337 92 ▶sub await_from_current_task {
Subroutine await_from_current_task: called CC=7 SCAR=19.9
Pod undocumented
338 92 my ( $self, $target ) = @_;
339  
340 92 ▶ $self->ensure_scheduled($target) if $target->can('scheduler');
Branch: 1 branch
BranchTrueFalseTotal
if $target->can("scheduler") 92 0 92
341 92 my $current = $self->current_task;
342 92 ▶ while ( !_task_done($target) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_task_done($target)) 4656 89 4745
343 4656 ▶ if ( $target->status eq 'pending' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->status eq 'pending') { } 46 4610 4656
344 46 ▶ if ( $target->can('use_coro') and $target->use_coro ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->can('use_coro') and $target->use_coro) { } 46 0 46
Condition33%1/3
$target->can("use_coro") and $target->use_coro
!ll&&!rl&&r
0046
MC/DC0%0/2
$target->can("use_coro") and $target->use_coro
$target->can("use_coro") $target->use_coro
Truth table33%1/3
A and B
A= $target->can("use_coro")B= $target->use_coro
ABresult
0X0
100
111
345 46 $self->start_coro_task($target);
346 }
347 else {
348 0 $self->run_pending_task($target);
349 }
350 }
351 else {
352 4610 $target->poll;
353 }
354 4656 ▶ if ( !_task_done($target) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_task_done($target)) 4640 16 4656
355 4640 $self->yield_current_task;
356 }
357 }
358 89 $self->cleanup_task($target);
359  
360 89 return $target;
361 }
362  
363 69 1;
364  
365 =pod
366  
367 =head1 COPYRIGHT AND LICENCE
368  
369 B<< Zuzu::Runtime::Async::Scheduler >> is copyright Toby Inkster.
370  
371 It is free software; you may redistribute it and/or modify it under
372 the terms of either the Artistic License 1.0 or the GNU General Public
373 License version 2.
374  
375 =cut
« blib/lib/Zuzu/Util/NativeHelpers.pm ↑ Summary blib/lib/Zuzu/Doc/CLI.pm »