Future.xs

filter: Statementstmt 98.8% 778 / 787 Branchbran 74.7% 493 / 660 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.8% 1271 / 1447 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/EV/Future.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "EVAPI.h"
7  
8 #define IS_PVCV(sv) (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV)
9  
10 #define EVF_KIND_PARALLEL 0
11 #define EVF_KIND_PLIMIT 1
12 #define EVF_KIND_SERIES 2
13 #define EVF_KIND_RACE 3
14  
15 /* Refcounted cell between a context and its Perl-side handle object. The
16 context is freed the moment the operation completes, but the caller may hold
17 the handle long after that, so neither side may own the other. Completion
18 NULLs `ctx`, which makes every later method call a safe no-op. */
19 typedef struct {
20 void *ctx; /* NULL once the operation is over */
21 int kind; /* which primitive, so we can pick the right cleanup */
22 int refcnt; /* held by the Perl object and, while live, by the context */
23 } evf_handle;
24  
25 55 static evf_handle *evf_handle_new(pTHX_ void *ctx, int kind) {
26 evf_handle *h;
27 55 Newx(h, 1, evf_handle);
28 55 h->ctx = ctx;
29 55 h->kind = kind;
30 55 h->refcnt = 1; /* the Perl object we are about to hand back */
31 55 return h;
32 }
33  
34 151 static void evf_handle_dec(pTHX_ evf_handle *h) {
35 151 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 50 101 151
36 101 ▶ if (--h->refcnt <= 0) Safefree(h);
Branch: 1 branch
BranchTrueFalseTotal
if (--h->refcnt <= 0) Safefree(h); 54 47 101
37 }
38  
39 47 static void evf_handle_attach(pTHX_ evf_handle *h, void *ctx) {
40 47 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 47 47
41 47 h->ctx = ctx;
42 47 h->refcnt++; /* the context's reference */
43 }
44  
45 51 static int evf_handle_mg_free(pTHX_ SV *sv, MAGIC *mg) {
46 PERL_UNUSED_ARG(sv);
47 51 ▶ if (mg->mg_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_ptr) { 0 51 51
48 0 evf_handle *h = (evf_handle *)mg->mg_ptr;
49 0 mg->mg_ptr = NULL;
50 0 evf_handle_dec(aTHX_ h);
51 }
52 51 return 0;
53 }
54  
55 static MGVTBL evf_handle_vtbl = {
56 NULL, /* get */
57 NULL, /* set */
58 NULL, /* len */
59 NULL, /* clear */
60 evf_handle_mg_free, /* free */
61 NULL, /* copy */
62 NULL, /* dup */
63 #ifdef MGf_LOCAL
64 NULL, /* local */
65 #endif
66 };
67  
68 51 static SV *evf_handle_wrap(pTHX_ evf_handle *h) {
69 51 SV *sv = newSViv(0);
70 51 SV *rv = newRV_noinc(sv);
71 51 sv_bless(rv, gv_stashpv("EV::Future::Handle", GV_ADD));
72 51 sv_magicext(sv, NULL, PERL_MAGIC_ext, &evf_handle_vtbl, (const char *)h, 0);
73 51 SvREADONLY_on(sv);
74 51 return rv; /* does not increment: evf_handle_new already counted this */
75 }
76  
77 /* The one place a method turns its invocant back into a cell. Uses
78 mg_findext to look up our PERL_MAGIC_ext attached with evf_handle_vtbl.
79 A forged, unblessed, or foreign invocant carries no such magic and returns
80 NULL, which every caller safely tolerates. */
81 74 static evf_handle *evf_handle_from_sv(pTHX_ SV *self) {
82 74 ▶ if (!self || !SvROK(self)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!self || !SvROK(self)) return NULL; 74 0 74
if (!self || !SvROK(self)) return NULL; 0 74 74
83 74 MAGIC *mg = mg_findext(SvRV(self), PERL_MAGIC_ext, &evf_handle_vtbl);
84 74 ▶ if (!mg) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) return NULL; 10 64 74
85 64 return (evf_handle *)mg->mg_ptr;
86 }
87  
88 typedef struct {
89 AV *tasks;
90 SV *final_cb;
91 SV *worker; /* NULL for the task form; the worker CV for the map form */
92 I32 remaining;
93 CV **cvs;
94 I32 num_cvs;
95 CV *shared_cv; /* for unsafe mode */
96 int abandoned;
97 int *is_freed_ptr;
98 evf_handle *h; /* NULL unless the caller asked for a handle */
99 } parallel_ctx;
100  
101 /* Guard registered with SAVEDESTRUCTOR_X by every frame that dispatches tasks,
102 so the frame unwinds cleanly even when an unsafe-mode task croaks straight
103 through it. is_freed is the flag the frame pointed ctx->is_freed_ptr at; it
104 lives in the guard, so it stays valid for exactly as long as the field
105 pointing at it. If it is set, cleanup already ran and the context is gone:
106 nothing may be dereferenced, we only propagate the flag to the enclosing
107 frame. Otherwise the context outlived the frame, so put back what the frame
108 borrowed - and if the frame never reached its normal exit (completed == 0)
109 the unwind was an exception, so abandon the operation rather than let a
110 later completion run against half-updated state. */
111 typedef struct {
112 int **target_field; /* &ctx->is_freed_ptr */
113 int *restore_val; /* what to put back there; NULL at XSUB level */
114 int *running_field; /* &ctx->running, or NULL if the primitive has none */
115 int *abandon_field; /* &ctx->abandoned */
116 int is_freed;
117 int completed;
118 evf_handle *pending_h; /* handle reference not yet handed to Perl */
119 } ifp_guard;
120  
121 1367 static void ifp_guard_destroy(pTHX_ void *p) {
122 1367 ifp_guard *g = (ifp_guard *)p;
123 1367 ▶ if (g->is_freed) {
Branch: 1 branch
BranchTrueFalseTotal
if (g->is_freed) { 1204 163 1367
124 1204 ▶ if (g->restore_val) *(g->restore_val) = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (g->restore_val) *(g->restore_val) = 1; 70 1134 1204
125 } else {
126 163 *(g->target_field) = g->restore_val;
127 163 ▶ if (g->running_field) *(g->running_field) = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (g->running_field) *(g->running_field) = 0; 86 77 163
128 163 ▶ if (!g->completed) *(g->abandon_field) = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!g->completed) *(g->abandon_field) = 1; 14 149 163
129 }
130 /* The handle cell is created before dispatch but only becomes a Perl
131 object once the frame returns normally. An unwind (a task that croaked)
132 never gets there, so release that reference here instead of orphaning
133 the cell. The context, if it still lives, holds its own. */
134 1367 ▶ if (!g->completed) evf_handle_dec(aTHX_ g->pending_h);
Branch: 1 branch
BranchTrueFalseTotal
if (!g->completed) evf_handle_dec(aTHX_ g->pending_h); 54 1313 1367
135 1367 Safefree(g);
136 1367 }
137  
138 /* Call inside an ENTER/LEAVE pair. Takes over ctx->is_freed_ptr for the life of
139 the frame and hands the guard back so the caller can read the flag and mark
140 its normal exit. */
141 1367 static ifp_guard *ifp_guard_push(pTHX_ int **target_field, int *restore_val,
142 int *running_field, int *abandon_field) {
143 ifp_guard *g;
144 1367 Newx(g, 1, ifp_guard);
145 1367 g->target_field = target_field;
146 1367 g->restore_val = restore_val;
147 1367 g->running_field = running_field;
148 1367 g->abandon_field = abandon_field;
149 1367 g->is_freed = 0;
150 1367 g->completed = 0;
151 1367 g->pending_h = NULL;
152 1367 *target_field = &g->is_freed;
153 1367 SAVEDESTRUCTOR_X(ifp_guard_destroy, g);
154 1367 return g;
155 }
156  
157 typedef struct {
158 AV *tasks;
159 SV *final_cb;
160 SV *worker; /* NULL for the task form; the worker CV for the map form */
161 I32 current_idx;
162 int running;
163 int delayed;
164 I32 total_tasks;
165 int unsafe;
166 int abandoned;
167 CV *current_cv;
168 int *is_freed_ptr;
169 evf_handle *h; /* NULL unless the caller asked for a handle */
170 } series_ctx;
171  
172 typedef struct {
173 AV *tasks;
174 SV *final_cb;
175 SV *worker; /* NULL for the task form; the worker CV for the map form */
176 I32 remaining;
177 I32 current_idx;
178 I32 total_tasks;
179 I32 limit;
180 I32 active;
181 int unsafe;
182 int running;
183 int delayed;
184 int abandoned;
185 CV **cvs;
186 I32 num_cvs;
187 CV *shared_cv;
188 int *is_freed_ptr;
189 evf_handle *h; /* NULL unless the caller asked for a handle */
190 } plimit_ctx;
191  
192 typedef struct {
193 AV *tasks;
194 SV *final_cb;
195 int settled;
196 I32 total_tasks;
197 int abandoned;
198 CV **cvs;
199 I32 num_cvs;
200 CV *shared_cv;
201 int *is_freed_ptr;
202 evf_handle *h; /* NULL unless the caller asked for a handle */
203 } race_ctx;
204  
205 static evf_handle *parallel_start(pTHX_ AV *list, SV *worker, SV *final_cb, int unsafe, int want_handle);
206 static evf_handle *series_start(pTHX_ AV *list, SV *worker, SV *final_cb, int unsafe, int want_handle);
207 static evf_handle *plimit_start(pTHX_ AV *list, SV *worker, SV *limit_sv, SV *final_cb, int unsafe, int want_handle);
208  
209 52 static void parallel_cleanup(pTHX_ parallel_ctx **ctx_ptr) {
210 52 ▶ if (!ctx_ptr || !*ctx_ptr) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx_ptr || !*ctx_ptr) return; 52 0 52
if (!ctx_ptr || !*ctx_ptr) return; 0 52 52
211 52 parallel_ctx *ctx = *ctx_ptr;
212 52 *ctx_ptr = NULL;
213  
214 52 ▶ if (ctx->is_freed_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->is_freed_ptr) { 33 19 52
215 33 *(ctx->is_freed_ptr) = 1;
216 }
217  
218 /* Detach first: freeing tasks/worker/final_cb below can run user DESTROY,
219 and a cancel from there must not re-enter this cleanup. */
220 52 ▶ if (ctx->h) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->h) { 18 34 52
221 18 ctx->h->ctx = NULL;
222 18 evf_handle_dec(aTHX_ ctx->h);
223 18 ctx->h = NULL;
224 }
225  
226 52 ▶ if (ctx->shared_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->shared_cv) { 4 48 52
227 4 CvXSUBANY(ctx->shared_cv).any_ptr = NULL;
228 4 SvREFCNT_dec((SV*)ctx->shared_cv);
229 }
230 52 ▶ if (ctx->cvs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cvs) { 48 4 52
231 I32 i;
232 6128 ▶ for (i = 0; i < ctx->num_cvs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->num_cvs; i++) { 6080 48 6128
233 6080 ▶ if (ctx->cvs[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cvs[i]) { 6069 11 6080
234 6069 CvXSUBANY(ctx->cvs[i]).any_ptr = NULL;
235 6069 SvREFCNT_dec((SV*)ctx->cvs[i]);
236 }
237 }
238 48 Safefree(ctx->cvs);
239 /* Belt and braces: nothing may reach this context again now that the
240 handle is detached, but a dangling cvs with a non-zero count is what
241 turned an earlier re-entrancy into a segfault rather than a no-op. */
242 48 ctx->cvs = NULL;
243 48 ctx->num_cvs = 0;
244 }
245 52 ▶ if (ctx->tasks) SvREFCNT_dec((SV*)ctx->tasks);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->tasks) SvREFCNT_dec((SV*)ctx->tasks); 52 0 52
246 52 ▶ if (ctx->final_cb) SvREFCNT_dec(ctx->final_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->final_cb) SvREFCNT_dec(ctx->final_cb); 52 0 52
247 52 ▶ if (ctx->worker) SvREFCNT_dec(ctx->worker);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->worker) SvREFCNT_dec(ctx->worker); 10 42 52
248 52 Safefree(ctx);
249 }
250  
251 6174 static void parallel_task_done(pTHX_ CV *cv) {
252 6174 dXSARGS;
253 6174 ▶ SvREFCNT_inc_simple_void(cv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(cv); 6174 0 6174
254 6174 sv_2mortal((SV*)cv);
255 6174 parallel_ctx *ctx = (parallel_ctx *)CvXSUBANY(cv).any_ptr;
256 6174 ▶ if (!ctx || ctx->abandoned) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx || ctx->abandoned) { 6170 4 6174
if (!ctx || ctx->abandoned) { 3 6167 6170
257 7 XSRETURN_EMPTY;
258 }
259  
260 6167 ▶ if (ctx->cvs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cvs) { 6058 109 6167
261 6058 CvXSUBANY(cv).any_ptr = NULL;
262 }
263  
264 6167 ▶ if (--ctx->remaining <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--ctx->remaining <= 0) { 41 6126 6167
265 41 SV *cb = ctx->final_cb;
266 80 ▶ if (IS_PVCV(cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(cb)) { 41 0 41
if (IS_PVCV(cb)) { 40 1 41
if (IS_PVCV(cb)) { 40 0 40
267 40 dSP;
268 40 ENTER;
269 40 SAVETMPS;
270 40 SvREFCNT_inc(cb);
271 40 sv_2mortal(cb);
272 40 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 40 40
273 40 PUTBACK;
274  
275 40 parallel_cleanup(aTHX_ &ctx);
276  
277 40 call_sv(cb, G_DISCARD | G_VOID);
278  
279 39 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 39 0 39
280 39 LEAVE;
281 } else {
282 1 parallel_cleanup(aTHX_ &ctx);
283 }
284 }
285 6166 XSRETURN_EMPTY;
286 }
287  
288 51 static void series_cleanup(pTHX_ series_ctx **ctx_ptr) {
289 51 ▶ if (!ctx_ptr || !*ctx_ptr) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx_ptr || !*ctx_ptr) return; 51 0 51
if (!ctx_ptr || !*ctx_ptr) return; 0 51 51
290 51 series_ctx *ctx = *ctx_ptr;
291 51 *ctx_ptr = NULL;
292  
293 51 ▶ if (ctx->is_freed_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->is_freed_ptr) { 50 1 51
294 50 *(ctx->is_freed_ptr) = 1;
295 }
296  
297 /* Detach first: freeing tasks/worker/final_cb below can run user DESTROY,
298 and a cancel from there must not re-enter this cleanup. */
299 51 ▶ if (ctx->h) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->h) { 10 41 51
300 10 ctx->h->ctx = NULL;
301 10 evf_handle_dec(aTHX_ ctx->h);
302 10 ctx->h = NULL;
303 }
304  
305 51 ▶ if (ctx->current_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->current_cv) { 12 39 51
306 12 CvXSUBANY(ctx->current_cv).any_ptr = NULL;
307 12 CV *cv = ctx->current_cv;
308 12 ctx->current_cv = NULL;
309 12 SvREFCNT_dec((SV*)cv);
310 }
311 51 ▶ if (ctx->tasks) SvREFCNT_dec((SV*)ctx->tasks);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->tasks) SvREFCNT_dec((SV*)ctx->tasks); 51 0 51
312 51 ▶ if (ctx->final_cb) SvREFCNT_dec(ctx->final_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->final_cb) SvREFCNT_dec(ctx->final_cb); 51 0 51
313 51 ▶ if (ctx->worker) SvREFCNT_dec(ctx->worker);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->worker) SvREFCNT_dec(ctx->worker); 8 43 51
314 51 Safefree(ctx);
315 }
316  
317 static void _series_next(pTHX_ series_ctx **ctx_ptr);
318  
319 5076 static void series_next_cb(pTHX_ CV *cv) {
320 5076 dXSARGS;
321 5076 ▶ SvREFCNT_inc_simple_void(cv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(cv); 5076 0 5076
322 5076 sv_2mortal((SV*)cv);
323 5076 series_ctx *ctx = (series_ctx *)CvXSUBANY(cv).any_ptr;
324 5076 ▶ if (!ctx || ctx->abandoned) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx || ctx->abandoned) { 5072 4 5076
if (!ctx || ctx->abandoned) { 2 5070 5072
325 6 XSRETURN_EMPTY;
326 }
327  
328 5070 ▶ if (!ctx->unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->unsafe) { 5061 9 5070
329 5061 CvXSUBANY(cv).any_ptr = NULL;
330 5061 ▶ if (ctx->current_cv == cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->current_cv == cv) { 5061 0 5061
331 5061 ctx->current_cv = NULL;
332 5061 SvREFCNT_dec((SV*)cv);
333 }
334 }
335  
336 5070 ▶ if (items > 0 && SvTRUE(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 0 && SvTRUE(ST(0))) { 6 5064 5070
if (items > 0 && SvTRUE(ST(0))) { 6 0 6
337 6 ctx->current_idx = ctx->total_tasks;
338 }
339 5070 _series_next(aTHX_ &ctx);
340 5069 XSRETURN_EMPTY;
341 }
342  
343 5124 static void _series_next(pTHX_ series_ctx **ctx_ptr) {
344 5124 ▶ if (!ctx_ptr || !*ctx_ptr) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx_ptr || !*ctx_ptr) return; 5124 0 5124
if (!ctx_ptr || !*ctx_ptr) return; 0 5124 5124
345 5124 series_ctx *ctx = *ctx_ptr;
346  
347 5124 ▶ if (ctx->running) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->running) { 5047 77 5124
348 5047 ctx->delayed = 1;
349 5047 return;
350 }
351  
352 77 ENTER;
353  
354 77 int *old_is_freed_ptr = ctx->is_freed_ptr;
355 77 ifp_guard *guard = ifp_guard_push(aTHX_ &ctx->is_freed_ptr, old_is_freed_ptr,
356 &ctx->running, &ctx->abandoned);
357 77 int *is_freed = &guard->is_freed;
358  
359 77 SV *worker = ctx->worker;
360 77 const int have_worker = (worker != NULL);
361  
362 77 ctx->running = 1;
363 77 ctx->delayed = 1;
364  
365 5161 ▶ while (ctx->delayed) {
Branch: 1 branch
BranchTrueFalseTotal
while (ctx->delayed) { 5136 25 5161
366 5136 ctx->delayed = 0;
367 5136 ▶ SV **task_ary = (AvREAL(ctx->tasks) && !SvMAGICAL(ctx->tasks)) ? AvARRAY(ctx->tasks) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **task_ary = (AvREAL(ctx->tasks) && !SvMAGICAL(ctx->tasks)) ? AvARRAY(ctx->tasks) : NULL; 5136 0 5136
SV **task_ary = (AvREAL(ctx->tasks) && !SvMAGICAL(ctx->tasks)) ? AvARRAY(ctx->tasks) : NULL; 5128 8 5136
368 5136 ▶ if (ctx->current_idx >= ctx->total_tasks) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->current_idx >= ctx->total_tasks) { 43 5093 5136
369 43 SV *cb = ctx->final_cb;
370 83 ▶ if (IS_PVCV(cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(cb)) { 43 0 43
if (IS_PVCV(cb)) { 42 1 43
if (IS_PVCV(cb)) { 42 0 42
371 42 dSP;
372 42 ENTER;
373 42 SAVETMPS;
374 42 SvREFCNT_inc(cb);
375 42 sv_2mortal(cb);
376 42 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 42 42
377 42 PUTBACK;
378 42 series_cleanup(aTHX_ ctx_ptr);
379 42 call_sv(cb, G_DISCARD | G_VOID);
380 40 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 40 0 40
381 40 LEAVE;
382 } else {
383 1 series_cleanup(aTHX_ ctx_ptr);
384 }
385 41 goto done;
386 }
387  
388 5087 ▶ SV **fetch_ptr = (task_ary && ctx->current_idx <= AvFILL(ctx->tasks))
Branch: 1 branch
BranchTrueFalseTotal
SV **fetch_ptr = (task_ary && ctx->current_idx <= AvFILL(ctx->tasks)) 5087 0 5087
389 5087 ? &task_ary[ctx->current_idx]
390 10180 ▶ : av_fetch(ctx->tasks, ctx->current_idx, 0);
Branch: 1 branch
BranchTrueFalseTotal
: av_fetch(ctx->tasks, ctx->current_idx, 0); 5087 6 5093
391 5093 ▶ SV *task_sv = fetch_ptr ? *fetch_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *task_sv = fetch_ptr ? *fetch_ptr : NULL; 5093 0 5093
392  
393 5093 ctx->current_idx++;
394  
395 10165 ▶ if (have_worker || IS_PVCV(task_sv)) {
Branch: 4 branches
BranchTrueFalseTotal
if (have_worker || IS_PVCV(task_sv)) { 5075 18 5093
if (have_worker || IS_PVCV(task_sv)) { 5073 2 5075
if (have_worker || IS_PVCV(task_sv)) { 5064 9 5073
if (have_worker || IS_PVCV(task_sv)) { 5063 1 5064
396 5081 ▶ if (!ctx->unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->unsafe) { 5070 11 5081
397 5070 ▶ if (ctx->current_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->current_cv) { 0 5070 5070
398 0 CvXSUBANY(ctx->current_cv).any_ptr = NULL;
399 0 SvREFCNT_dec((SV*)ctx->current_cv);
400 }
401 5070 CV *cv = newXS(NULL, series_next_cb, __FILE__);
402 5070 CvXSUBANY(cv).any_ptr = ctx;
403 5070 ctx->current_cv = (CV*)SvREFCNT_inc((SV*)cv);
404 11 ▶ } else if (!ctx->current_cv) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!ctx->current_cv) { 6 5 11
405 6 ctx->current_cv = newXS(NULL, series_next_cb, __FILE__);
406 6 CvXSUBANY(ctx->current_cv).any_ptr = ctx;
407 }
408  
409 5081 SV *next_rv = NULL;
410 5081 dSP;
411 5081 ENTER;
412 5081 SAVETMPS;
413 5081 ▶ if (!ctx->unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->unsafe) { 5070 11 5081
414 5070 next_rv = sv_2mortal(newRV_noinc((SV*)ctx->current_cv));
415 } else {
416 11 next_rv = sv_2mortal(newRV_inc((SV*)ctx->current_cv));
417 }
418 5081 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5081 5081
419 5081 ▶ if (have_worker) {
Branch: 1 branch
BranchTrueFalseTotal
if (have_worker) { 18 5063 5081
420 18 ▶ XPUSHs(task_sv ? sv_2mortal(SvREFCNT_inc(task_sv)) : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(task_sv ? sv_2mortal(SvREFCNT_inc(task_sv)) : &PL_sv_undef); 0 18 18
XPUSHs(task_sv ? sv_2mortal(SvREFCNT_inc(task_sv)) : &PL_sv_undef); 18 0 18
421 }
422 5081 ▶ XPUSHs(next_rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(next_rv); 0 5081 5081
423 5081 PUTBACK;
424  
425 5081 int task_unsafe = ctx->unsafe;
426 /* G_VOID: see parallel_start. */
427 5081 ▶ U32 flags = G_DISCARD | G_VOID | (task_unsafe ? 0 : G_EVAL);
Branch: 1 branch
BranchTrueFalseTotal
U32 flags = G_DISCARD | G_VOID | (task_unsafe ? 0 : G_EVAL); 11 5070 5081
428 5081 ▶ call_sv(have_worker ? worker : task_sv, flags);
Branch: 1 branch
BranchTrueFalseTotal
call_sv(have_worker ? worker : task_sv, flags); 18 5063 5081
429  
430 5079 ▶ if (!task_unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!task_unsafe) { 5070 9 5079
431 5070 SPAGAIN;
432 5070 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 5070 0 5070
if (SvTRUE(ERRSV)) { 6 5064 5070
433 6 ▶ SV *err = sv_mortalcopy(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_mortalcopy(ERRSV); 6 0 6
434 6 ▶ if (!*is_freed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!*is_freed) { 6 0 6
435 6 series_cleanup(aTHX_ ctx_ptr);
436 }
437 6 croak_sv(err);
438 }
439 }
440  
441 5073 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 5073 0 5073
442 5073 LEAVE;
443 5073 ▶ if (*is_freed) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (*is_freed) goto done; 1 5072 5073
444 } else {
445 12 ctx->delayed = 1;
446 }
447 }
448  
449 /* The guard restores is_freed_ptr and clears running; on an unwind that
450 skipped this point it abandons the series instead. */
451 25 done:
452 67 guard->completed = 1;
453 67 LEAVE;
454 }
455  
456 53 static void plimit_cleanup(pTHX_ plimit_ctx **ctx_ptr) {
457 53 ▶ if (!ctx_ptr || !*ctx_ptr) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx_ptr || !*ctx_ptr) return; 53 0 53
if (!ctx_ptr || !*ctx_ptr) return; 0 53 53
458 53 plimit_ctx *ctx = *ctx_ptr;
459 53 *ctx_ptr = NULL;
460  
461 53 ▶ if (ctx->is_freed_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->is_freed_ptr) { 35 18 53
462 35 *(ctx->is_freed_ptr) = 1;
463 }
464  
465 /* Detach first: freeing tasks/worker/final_cb below can run user DESTROY,
466 and a cancel from there must not re-enter this cleanup. */
467 53 ▶ if (ctx->h) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->h) { 11 42 53
468 11 ctx->h->ctx = NULL;
469 11 evf_handle_dec(aTHX_ ctx->h);
470 11 ctx->h = NULL;
471 }
472  
473 53 ▶ if (ctx->shared_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->shared_cv) { 6 47 53
474 6 CvXSUBANY(ctx->shared_cv).any_ptr = NULL;
475 6 SvREFCNT_dec((SV*)ctx->shared_cv);
476 }
477 53 ▶ if (ctx->cvs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cvs) { 47 6 53
478 I32 i;
479 1158 ▶ for (i = 0; i < ctx->num_cvs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->num_cvs; i++) { 1111 47 1158
480 1111 ▶ if (ctx->cvs[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cvs[i]) { 1089 22 1111
481 1089 CvXSUBANY(ctx->cvs[i]).any_ptr = NULL;
482 1089 SvREFCNT_dec((SV*)ctx->cvs[i]);
483 }
484 }
485 47 Safefree(ctx->cvs);
486 /* See parallel_cleanup. */
487 47 ctx->cvs = NULL;
488 47 ctx->num_cvs = 0;
489 }
490 53 ▶ if (ctx->tasks) SvREFCNT_dec((SV*)ctx->tasks);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->tasks) SvREFCNT_dec((SV*)ctx->tasks); 53 0 53
491 53 ▶ if (ctx->final_cb) SvREFCNT_dec(ctx->final_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->final_cb) SvREFCNT_dec(ctx->final_cb); 53 0 53
492 53 ▶ if (ctx->worker) SvREFCNT_dec(ctx->worker);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->worker) SvREFCNT_dec(ctx->worker); 8 45 53
493 53 Safefree(ctx);
494 }
495  
496 static void _plimit_dispatch(pTHX_ plimit_ctx **ctx_ptr);
497  
498 1206 static void plimit_task_done(pTHX_ CV *cv) {
499 1206 dXSARGS;
500 1206 ▶ SvREFCNT_inc_simple_void(cv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(cv); 1206 0 1206
501 1206 sv_2mortal((SV*)cv);
502 1206 plimit_ctx *ctx = (plimit_ctx *)CvXSUBANY(cv).any_ptr;
503 1206 ▶ if (!ctx || ctx->abandoned) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx || ctx->abandoned) { 1198 8 1206
if (!ctx || ctx->abandoned) { 4 1194 1198
504 12 XSRETURN_EMPTY;
505 }
506  
507 1194 ▶ if (ctx->cvs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cvs) { 1077 117 1194
508 1077 CvXSUBANY(cv).any_ptr = NULL;
509 }
510  
511 1194 ctx->active--;
512  
513 1194 ▶ if (--ctx->remaining <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--ctx->remaining <= 0) { 38 1156 1194
514 38 SV *cb = ctx->final_cb;
515 74 ▶ if (IS_PVCV(cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(cb)) { 38 0 38
if (IS_PVCV(cb)) { 37 1 38
if (IS_PVCV(cb)) { 37 0 37
516 37 dSP;
517 37 ENTER;
518 37 SAVETMPS;
519 37 SvREFCNT_inc(cb);
520 37 sv_2mortal(cb);
521 37 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 37 37
522 37 PUTBACK;
523  
524 37 plimit_cleanup(aTHX_ &ctx);
525  
526 37 call_sv(cb, G_DISCARD | G_VOID);
527  
528 36 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 36 0 36
529 36 LEAVE;
530 } else {
531 1 plimit_cleanup(aTHX_ &ctx);
532 }
533 } else {
534 1156 _plimit_dispatch(aTHX_ &ctx);
535 }
536 1189 XSRETURN_EMPTY;
537 }
538  
539 1213 static void _plimit_dispatch(pTHX_ plimit_ctx **ctx_ptr) {
540 1213 ▶ if (!ctx_ptr || !*ctx_ptr) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx_ptr || !*ctx_ptr) return; 1213 0 1213
if (!ctx_ptr || !*ctx_ptr) return; 0 1213 1213
541 1213 plimit_ctx *ctx = *ctx_ptr;
542  
543 1213 ▶ if (ctx->running) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->running) { 1119 94 1213
544 1119 ctx->delayed = 1;
545 1119 return;
546 }
547  
548 94 ENTER;
549  
550 94 int *old_is_freed_ptr = ctx->is_freed_ptr;
551 94 ifp_guard *guard = ifp_guard_push(aTHX_ &ctx->is_freed_ptr, old_is_freed_ptr,
552 &ctx->running, &ctx->abandoned);
553 94 int *is_freed = &guard->is_freed;
554  
555 94 SV *worker = ctx->worker;
556 94 const int have_worker = (worker != NULL);
557  
558 94 ctx->running = 1;
559 94 ctx->delayed = 1;
560  
561 150 ▶ while (ctx->delayed) {
Branch: 1 branch
BranchTrueFalseTotal
while (ctx->delayed) { 96 54 150
562 96 ctx->delayed = 0;
563  
564 1284 ▶ while (!*is_freed && ctx->active < ctx->limit && ctx->current_idx < ctx->total_tasks) {
Branch: 3 branches
BranchTrueFalseTotal
while (!*is_freed && ctx->active < ctx->limit && ctx->current_idx < ctx->total_tasks) { 1284 0 1284
while (!*is_freed && ctx->active < ctx->limit && ctx->current_idx < ctx->total_tasks) { 1245 39 1284
while (!*is_freed && ctx->active < ctx->limit && ctx->current_idx < ctx->total_tasks) { 1228 17 1245
565 1228 ▶ SV **task_ary = (AvREAL(ctx->tasks) && !SvMAGICAL(ctx->tasks)) ? AvARRAY(ctx->tasks) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **task_ary = (AvREAL(ctx->tasks) && !SvMAGICAL(ctx->tasks)) ? AvARRAY(ctx->tasks) : NULL; 1228 0 1228
SV **task_ary = (AvREAL(ctx->tasks) && !SvMAGICAL(ctx->tasks)) ? AvARRAY(ctx->tasks) : NULL; 1225 3 1228
566 1225 ▶ SV **fetch_ptr = (task_ary && ctx->current_idx <= AvFILL(ctx->tasks))
Branch: 1 branch
BranchTrueFalseTotal
SV **fetch_ptr = (task_ary && ctx->current_idx <= AvFILL(ctx->tasks)) 1225 0 1225
567 1225 ? &task_ary[ctx->current_idx]
568 2453 ▶ : av_fetch(ctx->tasks, ctx->current_idx, 0);
Branch: 1 branch
BranchTrueFalseTotal
: av_fetch(ctx->tasks, ctx->current_idx, 0); 1225 3 1228
569 1228 ▶ SV *task_sv = fetch_ptr ? *fetch_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *task_sv = fetch_ptr ? *fetch_ptr : NULL; 1228 0 1228
570  
571 1228 ctx->current_idx++;
572  
573 2407 ▶ if (have_worker || IS_PVCV(task_sv)) {
Branch: 4 branches
BranchTrueFalseTotal
if (have_worker || IS_PVCV(task_sv)) { 1207 21 1228
if (have_worker || IS_PVCV(task_sv)) { 1205 2 1207
if (have_worker || IS_PVCV(task_sv)) { 1195 10 1205
if (have_worker || IS_PVCV(task_sv)) { 1194 1 1195
574 1215 SV *done_rv = NULL;
575 1215 CV *cv = NULL;
576 1215 dSP;
577  
578 1215 ▶ if (!ctx->unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->unsafe) { 1089 126 1215
579 1089 cv = newXS(NULL, plimit_task_done, __FILE__);
580 1089 CvXSUBANY(cv).any_ptr = ctx;
581 1089 ctx->cvs[ctx->current_idx - 1] = (CV*)SvREFCNT_inc((SV*)cv);
582 }
583  
584 1215 ctx->active++;
585 1215 int task_unsafe = ctx->unsafe;
586  
587 1215 ENTER;
588 1215 SAVETMPS;
589 1215 ▶ if (!ctx->unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->unsafe) { 1089 126 1215
590 1089 done_rv = sv_2mortal(newRV_noinc((SV*)cv));
591 } else {
592 126 done_rv = sv_2mortal(newRV_inc((SV*)ctx->shared_cv));
593 }
594 1215 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1215 1215
595 1215 ▶ if (have_worker) {
Branch: 1 branch
BranchTrueFalseTotal
if (have_worker) { 21 1194 1215
596 21 ▶ XPUSHs(task_sv ? sv_2mortal(SvREFCNT_inc(task_sv)) : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(task_sv ? sv_2mortal(SvREFCNT_inc(task_sv)) : &PL_sv_undef); 0 21 21
XPUSHs(task_sv ? sv_2mortal(SvREFCNT_inc(task_sv)) : &PL_sv_undef); 21 0 21
597 }
598 1215 ▶ XPUSHs(done_rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(done_rv); 0 1215 1215
599 1215 PUTBACK;
600  
601 /* G_VOID: see parallel_start. */
602 1215 ▶ U32 flags = G_DISCARD | G_VOID | (task_unsafe ? 0 : G_EVAL);
Branch: 1 branch
BranchTrueFalseTotal
U32 flags = G_DISCARD | G_VOID | (task_unsafe ? 0 : G_EVAL); 126 1089 1215
603 1215 ▶ call_sv(have_worker ? worker : task_sv, flags);
Branch: 1 branch
BranchTrueFalseTotal
call_sv(have_worker ? worker : task_sv, flags); 21 1194 1215
604  
605 1210 ▶ if (!task_unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!task_unsafe) { 1089 121 1210
606 1089 SPAGAIN;
607 1089 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 1089 0 1089
if (SvTRUE(ERRSV)) { 6 1083 1089
608 6 ▶ SV *err = sv_mortalcopy(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_mortalcopy(ERRSV); 6 0 6
609 6 ▶ if (!*is_freed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!*is_freed) { 5 1 6
610 5 plimit_cleanup(aTHX_ ctx_ptr);
611 }
612 6 croak_sv(err);
613 }
614 }
615  
616 1204 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1204 0 1204
617 1204 LEAVE;
618 1204 ▶ if (*is_freed) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (*is_freed) goto done; 25 1179 1204
619 } else {
620 13 ▶ if (--ctx->remaining <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--ctx->remaining <= 0) { 4 9 13
621 4 SV *cb = ctx->final_cb;
622 7 ▶ if (IS_PVCV(cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(cb)) { 4 0 4
if (IS_PVCV(cb)) { 4 0 4
if (IS_PVCV(cb)) { 4 0 4
623 4 dSP;
624 4 ENTER;
625 4 SAVETMPS;
626 4 SvREFCNT_inc(cb);
627 4 sv_2mortal(cb);
628 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
629 4 PUTBACK;
630 4 plimit_cleanup(aTHX_ ctx_ptr);
631 4 call_sv(cb, G_DISCARD | G_VOID);
632 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3 0 3
633 3 LEAVE;
634 } else {
635 0 plimit_cleanup(aTHX_ ctx_ptr);
636 }
637 3 goto done;
638 }
639 9 ctx->delayed = 1;
640 }
641 }
642 }
643  
644 /* The guard restores is_freed_ptr and clears running, or propagates the
645 flag outwards when cleanup already freed the context. On an unwind that
646 skipped this point it abandons the remaining tasks instead. */
647 54 done:
648 82 guard->completed = 1;
649 82 LEAVE;
650 }
651  
652 1027 static void race_cleanup(pTHX_ race_ctx **ctx_ptr) {
653 1027 ▶ if (!ctx_ptr || !*ctx_ptr) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx_ptr || !*ctx_ptr) return; 1027 0 1027
if (!ctx_ptr || !*ctx_ptr) return; 0 1027 1027
654 1027 race_ctx *ctx = *ctx_ptr;
655 1027 *ctx_ptr = NULL;
656  
657 1027 ▶ if (ctx->is_freed_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->is_freed_ptr) { 1016 11 1027
658 1016 *(ctx->is_freed_ptr) = 1;
659 }
660  
661 /* Detach first: freeing tasks/final_cb below can run user DESTROY, and a
662 cancel from there must not re-enter this cleanup. */
663 1027 ▶ if (ctx->h) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->h) { 7 1020 1027
664 7 ctx->h->ctx = NULL;
665 7 evf_handle_dec(aTHX_ ctx->h);
666 7 ctx->h = NULL;
667 }
668  
669 1027 ▶ if (ctx->shared_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->shared_cv) { 3 1024 1027
670 3 CvXSUBANY(ctx->shared_cv).any_ptr = NULL;
671 3 SvREFCNT_dec((SV*)ctx->shared_cv);
672 }
673 1027 ▶ if (ctx->cvs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cvs) { 1024 3 1027
674 I32 i;
675 3066 ▶ for (i = 0; i < ctx->num_cvs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->num_cvs; i++) { 2042 1024 3066
676 2042 ▶ if (ctx->cvs[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cvs[i]) { 1033 1009 2042
677 1033 CvXSUBANY(ctx->cvs[i]).any_ptr = NULL;
678 1033 SvREFCNT_dec((SV*)ctx->cvs[i]);
679 }
680 }
681 1024 Safefree(ctx->cvs);
682 /* See parallel_cleanup. */
683 1024 ctx->cvs = NULL;
684 1024 ctx->num_cvs = 0;
685 }
686 1027 ▶ if (ctx->tasks) SvREFCNT_dec((SV*)ctx->tasks);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->tasks) SvREFCNT_dec((SV*)ctx->tasks); 1027 0 1027
687 1027 ▶ if (ctx->final_cb) SvREFCNT_dec(ctx->final_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->final_cb) SvREFCNT_dec(ctx->final_cb); 1027 0 1027
688 1027 Safefree(ctx);
689 }
690  
691 1029 static void race_task_done(pTHX_ CV *cv) {
692 1029 dXSARGS;
693 1029 ▶ SvREFCNT_inc_simple_void(cv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(cv); 1029 0 1029
694 1029 sv_2mortal((SV*)cv);
695 1029 race_ctx *ctx = (race_ctx *)CvXSUBANY(cv).any_ptr;
696 1029 ▶ if (!ctx || ctx->settled || ctx->abandoned) {
Branch: 3 branches
BranchTrueFalseTotal
if (!ctx || ctx->settled || ctx->abandoned) { 1022 7 1029
if (!ctx || ctx->settled || ctx->abandoned) { 1022 0 1022
if (!ctx || ctx->settled || ctx->abandoned) { 2 1020 1022
697 9 XSRETURN_EMPTY;
698 }
699 1020 ctx->settled = 1;
700  
701 1020 ▶ if (ctx->cvs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cvs) { 1017 3 1020
702 1017 CvXSUBANY(cv).any_ptr = NULL;
703 }
704  
705 1020 SV *cb = ctx->final_cb;
706 2038 ▶ if (IS_PVCV(cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(cb)) { 1020 0 1020
if (IS_PVCV(cb)) { 1019 1 1020
if (IS_PVCV(cb)) { 1019 0 1019
707 1019 dSP;
708 1019 ENTER;
709 1019 SAVETMPS;
710 1019 SvREFCNT_inc(cb);
711 1019 sv_2mortal(cb);
712  
713 /* race_cleanup frees the losers, whose DESTROY may free an arg */
714 2034 ▶ for (I32 i = 0; i < items; i++) SAVEFREESV(SvREFCNT_inc_simple_NN(ST(i)));
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i < items; i++) SAVEFREESV(SvREFCNT_inc_simple_NN(ST(i))); 1015 1019 2034
715  
716 1019 race_cleanup(aTHX_ &ctx);
717  
718 1019 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1019 1019
719 2034 ▶ for (I32 i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i < items; i++) { 1015 1019 2034
720 1015 ▶ XPUSHs(sv_mortalcopy(ST(i)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_mortalcopy(ST(i))); 0 1015 1015
721 }
722 1019 PUTBACK;
723  
724 1019 call_sv(cb, G_DISCARD | G_VOID);
725  
726 1018 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1018 0 1018
727 1018 LEAVE;
728 } else {
729 1 race_cleanup(aTHX_ &ctx);
730 }
731 1019 XSRETURN_EMPTY;
732 }
733  
734 61 static evf_handle *parallel_start(pTHX_ AV *list, SV *worker, SV *final_cb, int unsafe, int want_handle) {
735 61 I32 len = av_len(list) + 1;
736 61 ▶ if (len <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 0) { 5 56 61
737 5 ▶ if (IS_PVCV(final_cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(final_cb)) { 5 0 5
if (IS_PVCV(final_cb)) { 5 0 5
if (IS_PVCV(final_cb)) { 5 0 5
738 5 dSP;
739 5 ENTER;
740 5 SAVETMPS;
741 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
742 5 PUTBACK;
743 5 call_sv(final_cb, G_DISCARD | G_VOID);
744 5 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 5 5
745 5 LEAVE;
746 }
747 /* Already over: hand back a dead handle so callers get a uniform
748 object rather than undef. */
749 5 ▶ return want_handle ? evf_handle_new(aTHX_ NULL, EVF_KIND_PARALLEL) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return want_handle ? evf_handle_new(aTHX_ NULL, EVF_KIND_PARALLEL) : NULL; 3 2 5
750 }
751  
752 56 ENTER;
753  
754 parallel_ctx *ctx;
755 56 Newx(ctx, 1, parallel_ctx);
756 56 ctx->is_freed_ptr = NULL;
757 56 ctx->h = NULL;
758 56 ctx->tasks = (AV*)SvREFCNT_inc((SV*)list);
759 56 ctx->final_cb = SvREFCNT_inc(final_cb);
760 56 ▶ ctx->worker = worker ? SvREFCNT_inc(worker) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ctx->worker = worker ? SvREFCNT_inc(worker) : NULL; 11 45 56
761 56 ctx->remaining = len;
762 56 ctx->cvs = NULL;
763 56 ctx->num_cvs = 0;
764 56 ctx->shared_cv = NULL;
765 56 ctx->abandoned = 0;
766  
767 56 ifp_guard *guard = ifp_guard_push(aTHX_ &ctx->is_freed_ptr, NULL,
768 56 NULL, &ctx->abandoned);
769 56 int *is_freed = &guard->is_freed;
770  
771 /* Counted before anything is dispatched: a task that completes the whole
772 operation synchronously runs cleanup, which drops the context's
773 reference, and only this one keeps the cell alive until we return it. */
774 56 evf_handle *h = NULL;
775 56 ▶ if (want_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_handle) { 18 38 56
776 18 h = evf_handle_new(aTHX_ NULL, EVF_KIND_PARALLEL);
777 18 evf_handle_attach(aTHX_ h, ctx);
778 18 ctx->h = h;
779 18 guard->pending_h = h;
780 }
781  
782 56 SV *done_rv = NULL;
783 56 ▶ if (unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (unsafe) { 7 49 56
784 7 ctx->shared_cv = newXS(NULL, parallel_task_done, __FILE__);
785 7 CvXSUBANY(ctx->shared_cv).any_ptr = ctx;
786 7 done_rv = sv_2mortal(newRV_inc((SV*)ctx->shared_cv));
787 } else {
788 49 ctx->num_cvs = len;
789 49 Newxz(ctx->cvs, len, CV*);
790 }
791  
792 56 dSP;
793  
794 I32 i;
795 /* G_VOID as well as G_DISCARD: the value is discarded either way, but
796 without the want bit the task's tail expression runs in scalar context,
797 so a nested primitive there would allocate a handle nobody asked for.
798 The final_cb calls in this file already pass G_VOID for the same reason. */
799 56 ▶ U32 flags = G_DISCARD | G_VOID | (unsafe ? 0 : G_EVAL);
Branch: 1 branch
BranchTrueFalseTotal
U32 flags = G_DISCARD | G_VOID | (unsafe ? 0 : G_EVAL); 7 49 56
800 56 const int have_worker = (worker != NULL);
801  
802 6238 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 6194 44 6238
803 6194 ▶ if (*is_freed) break;
Branch: 1 branch
BranchTrueFalseTotal
if (*is_freed) break; 0 6194 6194
804  
805 6194 ▶ SV **task_ary = (AvREAL(list) && !SvMAGICAL(list)) ? AvARRAY(list) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **task_ary = (AvREAL(list) && !SvMAGICAL(list)) ? AvARRAY(list) : NULL; 6194 0 6194
SV **task_ary = (AvREAL(list) && !SvMAGICAL(list)) ? AvARRAY(list) : NULL; 6191 3 6194
806 6191 ▶ SV **fetch_ptr = (task_ary && i <= AvFILL(list))
Branch: 1 branch
BranchTrueFalseTotal
SV **fetch_ptr = (task_ary && i <= AvFILL(list)) 6190 1 6191
807 6190 ? &task_ary[i]
808 12385 ▶ : av_fetch(list, i, 0);
Branch: 1 branch
BranchTrueFalseTotal
: av_fetch(list, i, 0); 6191 3 6194
809 6194 ▶ SV *task_sv = fetch_ptr ? *fetch_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *task_sv = fetch_ptr ? *fetch_ptr : NULL; 6193 1 6194
810  
811 12368 ▶ if (have_worker || IS_PVCV(task_sv)) {
Branch: 4 branches
BranchTrueFalseTotal
if (have_worker || IS_PVCV(task_sv)) { 6171 23 6194
if (have_worker || IS_PVCV(task_sv)) { 6168 3 6171
if (have_worker || IS_PVCV(task_sv)) { 6160 8 6168
if (have_worker || IS_PVCV(task_sv)) { 6159 1 6160
812 6182 CV *cv = NULL;
813 6182 ▶ if (!unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unsafe) { 6070 112 6182
814 6070 cv = newXS(NULL, parallel_task_done, __FILE__);
815 6070 CvXSUBANY(cv).any_ptr = ctx;
816 6070 ctx->cvs[i] = (CV*)SvREFCNT_inc((SV*)cv);
817 }
818  
819 6182 ENTER;
820 6182 SAVETMPS;
821 6182 ▶ if (!unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unsafe) { 6070 112 6182
822 6070 done_rv = sv_2mortal(newRV_noinc((SV*)cv));
823 }
824 6182 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6182 6182
825 6182 ▶ if (have_worker) {
Branch: 1 branch
BranchTrueFalseTotal
if (have_worker) { 23 6159 6182
826 23 ▶ XPUSHs(task_sv ? sv_2mortal(SvREFCNT_inc(task_sv)) : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(task_sv ? sv_2mortal(SvREFCNT_inc(task_sv)) : &PL_sv_undef); 0 23 23
XPUSHs(task_sv ? sv_2mortal(SvREFCNT_inc(task_sv)) : &PL_sv_undef); 21 2 23
827 }
828 6182 ▶ XPUSHs(done_rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(done_rv); 0 6182 6182
829 6182 PUTBACK;
830  
831 6182 ▶ call_sv(have_worker ? worker : task_sv, flags);
Branch: 1 branch
BranchTrueFalseTotal
call_sv(have_worker ? worker : task_sv, flags); 23 6159 6182
832 6179 SPAGAIN;
833 6179 ▶ if (!unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unsafe) { 6070 109 6179
834 6070 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 6070 0 6070
if (SvTRUE(ERRSV)) { 5 6065 6070
835 5 ▶ SV *err = sv_mortalcopy(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_mortalcopy(ERRSV); 5 0 5
836 5 ▶ if (!*is_freed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!*is_freed) { 4 1 5
837 4 parallel_cleanup(aTHX_ &ctx);
838 }
839 5 croak_sv(err);
840 }
841 }
842  
843 6174 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6068 106 6174
844 6174 LEAVE;
845 } else {
846 12 ▶ if (--ctx->remaining <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--ctx->remaining <= 0) { 4 8 12
847 4 SV *cb = ctx->final_cb;
848 4 ▶ if (IS_PVCV(cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(cb)) { 4 0 4
if (IS_PVCV(cb)) { 4 0 4
if (IS_PVCV(cb)) { 4 0 4
849 4 ENTER;
850 4 SAVETMPS;
851 4 SvREFCNT_inc(cb);
852 4 sv_2mortal(cb);
853 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
854 4 PUTBACK;
855 4 parallel_cleanup(aTHX_ &ctx);
856 4 call_sv(cb, G_DISCARD | G_VOID);
857 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3 0 3
858 3 LEAVE;
859 } else {
860 0 parallel_cleanup(aTHX_ &ctx);
861 }
862 3 break;
863 }
864 }
865 }
866 47 guard->completed = 1;
867 47 LEAVE;
868 47 return h;
869 }
870  
871 57 static evf_handle *series_start(pTHX_ AV *list, SV *worker, SV *final_cb, int unsafe, int want_handle) {
872 57 I32 len = av_len(list) + 1;
873 57 ▶ if (len <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 0) { 3 54 57
874 3 ▶ if (IS_PVCV(final_cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(final_cb)) { 3 0 3
if (IS_PVCV(final_cb)) { 3 0 3
if (IS_PVCV(final_cb)) { 3 0 3
875 3 dSP;
876 3 ENTER;
877 3 SAVETMPS;
878 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
879 3 PUTBACK;
880 3 call_sv(final_cb, G_DISCARD | G_VOID);
881 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 3 3
882 3 LEAVE;
883 }
884 /* Already over: hand back a dead handle so callers get a uniform
885 object rather than undef. */
886 3 ▶ return want_handle ? evf_handle_new(aTHX_ NULL, EVF_KIND_SERIES) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return want_handle ? evf_handle_new(aTHX_ NULL, EVF_KIND_SERIES) : NULL; 2 1 3
887 }
888  
889 54 ENTER;
890  
891 series_ctx *ctx;
892 54 Newx(ctx, 1, series_ctx);
893 54 ctx->is_freed_ptr = NULL;
894 54 ctx->h = NULL;
895 54 ctx->tasks = (AV*)SvREFCNT_inc((SV*)list);
896 54 ctx->final_cb = SvREFCNT_inc(final_cb);
897 54 ▶ ctx->worker = worker ? SvREFCNT_inc(worker) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ctx->worker = worker ? SvREFCNT_inc(worker) : NULL; 8 46 54
898 54 ctx->current_idx = 0;
899 54 ctx->running = 0;
900 54 ctx->delayed = 0;
901 54 ctx->total_tasks = len;
902 54 ctx->unsafe = unsafe;
903 54 ctx->abandoned = 0;
904 54 ctx->current_cv = NULL;
905  
906 54 ifp_guard *guard = ifp_guard_push(aTHX_ &ctx->is_freed_ptr, NULL,
907 54 NULL, &ctx->abandoned);
908  
909 /* Counted before anything is dispatched; see parallel_start. */
910 54 evf_handle *h = NULL;
911 54 ▶ if (want_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_handle) { 10 44 54
912 10 h = evf_handle_new(aTHX_ NULL, EVF_KIND_SERIES);
913 10 evf_handle_attach(aTHX_ h, ctx);
914 10 ctx->h = h;
915 10 guard->pending_h = h;
916 }
917  
918 54 _series_next(aTHX_ &ctx);
919  
920 45 guard->completed = 1;
921 45 LEAVE;
922 45 return h;
923 }
924  
925 60 static evf_handle *plimit_start(pTHX_ AV *list, SV *worker, SV *limit_sv, SV *final_cb, int unsafe, int want_handle) {
926 60 I32 len = av_len(list) + 1;
927 60 ▶ if (len <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 0) { 3 57 60
928 3 ▶ if (IS_PVCV(final_cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(final_cb)) { 3 0 3
if (IS_PVCV(final_cb)) { 3 0 3
if (IS_PVCV(final_cb)) { 3 0 3
929 3 dSP;
930 3 ENTER;
931 3 SAVETMPS;
932 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
933 3 PUTBACK;
934 3 call_sv(final_cb, G_DISCARD | G_VOID);
935 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 3 3
936 3 LEAVE;
937 }
938 /* Already over: hand back a dead handle so callers get a uniform
939 object rather than undef. */
940 3 ▶ return want_handle ? evf_handle_new(aTHX_ NULL, EVF_KIND_PLIMIT) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return want_handle ? evf_handle_new(aTHX_ NULL, EVF_KIND_PLIMIT) : NULL; 2 1 3
941 }
942  
943 IV limit;
944 57 NV limit_nv = SvNV(limit_sv);
945 57 ▶ if (!(limit_nv >= 1.0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(limit_nv >= 1.0)) { 4 53 57
946 4 limit = 1;
947 53 ▶ } else if (limit_nv >= (NV)len) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (limit_nv >= (NV)len) { 28 25 53
948 28 limit = (IV)len;
949 } else {
950 25 limit = (IV)limit_nv;
951 }
952  
953 57 ENTER;
954  
955 plimit_ctx *ctx;
956 57 Newx(ctx, 1, plimit_ctx);
957 57 ctx->is_freed_ptr = NULL;
958 57 ctx->h = NULL;
959 57 ctx->tasks = (AV*)SvREFCNT_inc((SV*)list);
960 57 ctx->final_cb = SvREFCNT_inc(final_cb);
961 57 ▶ ctx->worker = worker ? SvREFCNT_inc(worker) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ctx->worker = worker ? SvREFCNT_inc(worker) : NULL; 8 49 57
962 57 ctx->remaining = len;
963 57 ctx->current_idx = 0;
964 57 ctx->total_tasks = len;
965 57 ctx->limit = limit;
966 57 ctx->active = 0;
967 57 ctx->unsafe = unsafe;
968 57 ctx->running = 0;
969 57 ctx->delayed = 0;
970 57 ctx->abandoned = 0;
971 57 ctx->cvs = NULL;
972 57 ctx->num_cvs = 0;
973 57 ctx->shared_cv = NULL;
974  
975 57 ifp_guard *guard = ifp_guard_push(aTHX_ &ctx->is_freed_ptr, NULL,
976 57 NULL, &ctx->abandoned);
977  
978 /* Counted before anything is dispatched; see parallel_start. */
979 57 evf_handle *h = NULL;
980 57 ▶ if (want_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_handle) { 12 45 57
981 12 h = evf_handle_new(aTHX_ NULL, EVF_KIND_PLIMIT);
982 12 evf_handle_attach(aTHX_ h, ctx);
983 12 ctx->h = h;
984 12 guard->pending_h = h;
985 }
986  
987 57 ▶ if (unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (unsafe) { 10 47 57
988 10 ctx->shared_cv = newXS(NULL, plimit_task_done, __FILE__);
989 10 CvXSUBANY(ctx->shared_cv).any_ptr = ctx;
990 } else {
991 47 ctx->num_cvs = len;
992 47 Newxz(ctx->cvs, len, CV*);
993 }
994  
995 57 _plimit_dispatch(aTHX_ &ctx);
996  
997 49 guard->completed = 1;
998 49 LEAVE;
999 49 return h;
1000 }
1001  
1002 /* Cancel can only stop future dispatch; a task already in flight is user code
1003 sitting in an EV callback and cannot be interrupted. Running the primitive's
1004 normal cleanup NULLs any_ptr on every outstanding done CV, so those tasks
1005 calling done later are ignored. Cleanup also sets *(ctx->is_freed_ptr), which
1006 is how a cancel issued from inside a task makes the dispatch loop bail on its
1007 next turn. */
1008 29 static void evf_handle_cancel(pTHX_ evf_handle *h, int fire) {
1009 29 SV *cb = NULL;
1010  
1011 29 ▶ if (!h || !h->ctx) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || !h->ctx) return; 26 3 29
if (!h || !h->ctx) return; 13 13 26
1012  
1013 13 switch (h->kind) {
1014 3 case EVF_KIND_PARALLEL: {
1015 3 parallel_ctx *ctx = (parallel_ctx *)h->ctx;
1016 3 ▶ if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb));
Branch: 4 branches
BranchTrueFalseTotal
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 3 3
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 0 0
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 0 0
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 0 0
1017 3 parallel_cleanup(aTHX_ &ctx);
1018 3 break;
1019 }
1020 6 case EVF_KIND_PLIMIT: {
1021 6 plimit_ctx *ctx = (plimit_ctx *)h->ctx;
1022 6 ▶ if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb));
Branch: 4 branches
BranchTrueFalseTotal
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 2 4 6
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 2 0 2
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 2 0 2
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 2 0 2
1023 6 plimit_cleanup(aTHX_ &ctx);
1024 6 break;
1025 }
1026 2 case EVF_KIND_SERIES: {
1027 2 series_ctx *ctx = (series_ctx *)h->ctx;
1028 2 ▶ if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb));
Branch: 4 branches
BranchTrueFalseTotal
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 2 2
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 0 0
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 0 0
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 0 0
1029 2 series_cleanup(aTHX_ &ctx);
1030 2 break;
1031 }
1032 2 case EVF_KIND_RACE: {
1033 2 race_ctx *ctx = (race_ctx *)h->ctx;
1034 2 ▶ if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb));
Branch: 4 branches
BranchTrueFalseTotal
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 2 2
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 0 0
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 0 0
if (fire && IS_PVCV(ctx->final_cb)) cb = sv_2mortal(SvREFCNT_inc(ctx->final_cb)); 0 0 0
1035 2 race_cleanup(aTHX_ &ctx);
1036 2 break;
1037 }
1038 }
1039  
1040 13 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 2 11 13
1041 2 dSP;
1042 2 ENTER;
1043 2 SAVETMPS;
1044 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
1045 2 PUTBACK;
1046 2 call_sv(cb, G_DISCARD | G_VOID);
1047 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 2 2
1048 2 LEAVE;
1049 }
1050 }
1051  
1052 /* pending: tasks not yet completed, counting both in flight and not yet
1053 dispatched. active: tasks currently in flight. parallel and race collapse the
1054 two because they dispatch everything up front and the handle is not returned
1055 until the XSUB returns. Returns 0 once the operation is over, so
1056 pending == 0 exactly when it has finished or been cancelled. */
1057 45 static IV evf_handle_count(pTHX_ evf_handle *h, int want_active) {
1058 45 ▶ if (!h || !h->ctx) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || !h->ctx) return 0; 36 9 45
if (!h || !h->ctx) return 0; 15 21 36
1059  
1060 21 switch (h->kind) {
1061 5 case EVF_KIND_PARALLEL: {
1062 5 parallel_ctx *ctx = (parallel_ctx *)h->ctx;
1063 5 return (IV)ctx->remaining;
1064 }
1065 9 case EVF_KIND_PLIMIT: {
1066 9 plimit_ctx *ctx = (plimit_ctx *)h->ctx;
1067 IV act;
1068 9 ▶ if (!want_active) return (IV)ctx->remaining;
Branch: 1 branch
BranchTrueFalseTotal
if (!want_active) return (IV)ctx->remaining; 4 5 9
1069 /* Reachable, unlike the SERIES clamp below: an unsafe-mode task
1070 that calls its done more than once while the dispatch loop is
1071 still running decrements active per call with no re-dispatch to
1072 put it back, and a read from inside that task sees the deficit.
1073 Floor it rather than hand Perl a negative in-flight count. */
1074 5 act = (IV)ctx->active;
1075 5 return act < 0 ? 0 : act;
1076 }
1077 5 case EVF_KIND_SERIES: {
1078 5 series_ctx *ctx = (series_ctx *)h->ctx;
1079 IV left;
1080 5 ▶ if (want_active) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (want_active) return 1; 2 3 5
1081 3 left = (IV)ctx->total_tasks - (IV)ctx->current_idx + 1;
1082 /* Unreachable today - current_idx never exceeds total_tasks - but
1083 this floors a value crossing into Perl, so it stays cheap
1084 insurance against a future current_idx writer. */
1085 3 return left < 0 ? 0 : left;
1086 }
1087 2 case EVF_KIND_RACE: {
1088 2 race_ctx *ctx = (race_ctx *)h->ctx;
1089 2 return (IV)ctx->total_tasks;
1090 }
1091 }
1092 0 return 0;
1093 }
1094  
1095 MODULE = EV::Future PACKAGE = EV::Future
1096  
1097 PROTOTYPES: DISABLE
1098  
1099 BOOT:
1100 /* No libev entry point is called from here; the primitives are pure
1101 callback-flow control over CVs the caller supplies. This pins the EV
1102 ABI the module was built against, which is why EV stays a prereq. */
1103 12 ▶ I_EV_API ("EV::Future");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API ("EV::Future"); 0 12 12
I_EV_API ("EV::Future"); 12 0 12
I_EV_API ("EV::Future"); 0 12 12
1104  
1105 void
1106 parallel(list, final_cb, ...)
1107 AV *list
1108 SV *final_cb
1109 CODE:
1110 48 ▶ int unsafe = (items > 2 && SvTRUE(ST(2)));
Branch: 2 branches
BranchTrueFalseTotal
int unsafe = (items > 2 && SvTRUE(ST(2))); 5 43 48
int unsafe = (items > 2 && SvTRUE(ST(2))); 5 0 5
1111 48 evf_handle *h = parallel_start(aTHX_ list, NULL, final_cb, unsafe,
1112 48 GIMME_V != G_VOID);
1113 40 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 18 22 40
1114 18 ST(0) = sv_2mortal(evf_handle_wrap(aTHX_ h));
1115 18 XSRETURN(1);
1116 }
1117  
1118 void
1119 parallel_map(list, worker, final_cb, ...)
1120 AV *list
1121 SV *worker
1122 SV *final_cb
1123 CODE:
1124 14 ▶ int unsafe = (items > 3 && SvTRUE(ST(3)));
Branch: 2 branches
BranchTrueFalseTotal
int unsafe = (items > 3 && SvTRUE(ST(3))); 2 12 14
int unsafe = (items > 3 && SvTRUE(ST(3))); 2 0 2
1125 14 ▶ if (!IS_PVCV(worker))
Branch: 3 branches
BranchTrueFalseTotal
if (!IS_PVCV(worker)) 14 0 14
if (!IS_PVCV(worker)) 13 1 14
if (!IS_PVCV(worker)) 0 13 13
1126 1 croak("EV::Future::parallel_map: worker must be a code reference");
1127 13 evf_handle *h = parallel_start(aTHX_ list, worker, final_cb, unsafe,
1128 13 GIMME_V != G_VOID);
1129 12 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 2 10 12
1130 2 ST(0) = sv_2mortal(evf_handle_wrap(aTHX_ h));
1131 2 XSRETURN(1);
1132 }
1133  
1134 void
1135 series(list, final_cb, ...)
1136 AV *list
1137 SV *final_cb
1138 CODE:
1139 48 ▶ int unsafe = (items > 2 && SvTRUE(ST(2)));
Branch: 2 branches
BranchTrueFalseTotal
int unsafe = (items > 2 && SvTRUE(ST(2))); 5 43 48
int unsafe = (items > 2 && SvTRUE(ST(2))); 5 0 5
1140 48 evf_handle *h = series_start(aTHX_ list, NULL, final_cb, unsafe,
1141 48 GIMME_V != G_VOID);
1142 39 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 9 30 39
1143 9 ST(0) = sv_2mortal(evf_handle_wrap(aTHX_ h));
1144 9 XSRETURN(1);
1145 }
1146  
1147 void
1148 series_map(list, worker, final_cb, ...)
1149 AV *list
1150 SV *worker
1151 SV *final_cb
1152 CODE:
1153 10 ▶ int unsafe = (items > 3 && SvTRUE(ST(3)));
Branch: 2 branches
BranchTrueFalseTotal
int unsafe = (items > 3 && SvTRUE(ST(3))); 1 9 10
int unsafe = (items > 3 && SvTRUE(ST(3))); 1 0 1
1154 10 ▶ if (!IS_PVCV(worker))
Branch: 3 branches
BranchTrueFalseTotal
if (!IS_PVCV(worker)) 10 0 10
if (!IS_PVCV(worker)) 10 0 10
if (!IS_PVCV(worker)) 1 9 10
1155 1 croak("EV::Future::series_map: worker must be a code reference");
1156 9 evf_handle *h = series_start(aTHX_ list, worker, final_cb, unsafe,
1157 9 GIMME_V != G_VOID);
1158 9 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 2 7 9
1159 2 ST(0) = sv_2mortal(evf_handle_wrap(aTHX_ h));
1160 2 XSRETURN(1);
1161 }
1162  
1163 void
1164 parallel_limit(list, limit_sv, final_cb, ...)
1165 AV *list
1166 SV *limit_sv
1167 SV *final_cb
1168 CODE:
1169 51 ▶ int unsafe = (items > 3 && SvTRUE(ST(3)));
Branch: 2 branches
BranchTrueFalseTotal
int unsafe = (items > 3 && SvTRUE(ST(3))); 9 42 51
int unsafe = (items > 3 && SvTRUE(ST(3))); 9 0 9
1170 51 evf_handle *h = plimit_start(aTHX_ list, NULL, limit_sv, final_cb, unsafe,
1171 51 GIMME_V != G_VOID);
1172 43 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 9 34 43
1173 9 ST(0) = sv_2mortal(evf_handle_wrap(aTHX_ h));
1174 9 XSRETURN(1);
1175 }
1176  
1177 void
1178 parallel_map_limit(list, worker, limit_sv, final_cb, ...)
1179 AV *list
1180 SV *worker
1181 SV *limit_sv
1182 SV *final_cb
1183 CODE:
1184 10 ▶ int unsafe = (items > 4 && SvTRUE(ST(4)));
Branch: 2 branches
BranchTrueFalseTotal
int unsafe = (items > 4 && SvTRUE(ST(4))); 1 9 10
int unsafe = (items > 4 && SvTRUE(ST(4))); 1 0 1
1185 10 ▶ if (!IS_PVCV(worker))
Branch: 3 branches
BranchTrueFalseTotal
if (!IS_PVCV(worker)) 10 0 10
if (!IS_PVCV(worker)) 9 1 10
if (!IS_PVCV(worker)) 0 9 9
1186 1 croak("EV::Future::parallel_map_limit: worker must be a code reference");
1187 9 evf_handle *h = plimit_start(aTHX_ list, worker, limit_sv, final_cb, unsafe,
1188 9 GIMME_V != G_VOID);
1189 9 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 4 5 9
1190 4 ST(0) = sv_2mortal(evf_handle_wrap(aTHX_ h));
1191 4 XSRETURN(1);
1192 }
1193  
1194 void
1195 race(tasks, final_cb, ...)
1196 AV *tasks
1197 SV *final_cb
1198 CODE:
1199 1032 int unsafe = 0;
1200 1032 ▶ if (items > 2 && SvTRUE(ST(2))) unsafe = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && SvTRUE(ST(2))) unsafe = 1; 5 1027 1032
if (items > 2 && SvTRUE(ST(2))) unsafe = 1; 5 0 5
1201  
1202 1032 I32 len = av_len(tasks) + 1;
1203 1032 ▶ if (len <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 0) { 3 1029 1032
1204 3 ▶ if (IS_PVCV(final_cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(final_cb)) { 3 0 3
if (IS_PVCV(final_cb)) { 2 1 3
if (IS_PVCV(final_cb)) { 2 0 2
1205 2 dSP;
1206 2 ENTER;
1207 2 SAVETMPS;
1208 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
1209 2 PUTBACK;
1210 2 call_sv(final_cb, G_DISCARD | G_VOID);
1211 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 2 2
1212 2 LEAVE;
1213 }
1214 /* Already over: hand back a dead handle so callers get a uniform
1215 object rather than undef. */
1216 3 ▶ if (GIMME_V != G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V != G_VOID) { 1 2 3
1217 1 ST(0) = sv_2mortal(evf_handle_wrap(aTHX_
1218 evf_handle_new(aTHX_ NULL, EVF_KIND_RACE)));
1219 9 XSRETURN(1);
1220 }
1221 2 XSRETURN_EMPTY;
1222 }
1223  
1224 1029 ENTER;
1225  
1226 race_ctx *ctx;
1227 1029 Newx(ctx, 1, race_ctx);
1228 1029 ctx->is_freed_ptr = NULL;
1229 1029 ctx->h = NULL;
1230 1029 ctx->tasks = (AV*)SvREFCNT_inc((SV*)tasks);
1231 1029 ctx->final_cb = SvREFCNT_inc(final_cb);
1232 1029 ctx->settled = 0;
1233 1029 ctx->total_tasks = len;
1234 1029 ctx->abandoned = 0;
1235 1029 ctx->cvs = NULL;
1236 1029 ctx->num_cvs = 0;
1237 1029 ctx->shared_cv = NULL;
1238  
1239 1029 ifp_guard *guard = ifp_guard_push(aTHX_ &ctx->is_freed_ptr, NULL,
1240 1029 NULL, &ctx->abandoned);
1241 1029 int *is_freed = &guard->is_freed;
1242  
1243 /* Counted before anything is dispatched; see parallel_start. */
1244 1029 evf_handle *h = NULL;
1245 1029 ▶ if (GIMME_V != G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V != G_VOID) { 7 1022 1029
1246 7 h = evf_handle_new(aTHX_ NULL, EVF_KIND_RACE);
1247 7 evf_handle_attach(aTHX_ h, ctx);
1248 7 ctx->h = h;
1249 7 guard->pending_h = h;
1250 }
1251  
1252 1029 SV *done_rv = NULL;
1253 1029 ▶ if (unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (unsafe) { 5 1024 1029
1254 5 ctx->shared_cv = newXS(NULL, race_task_done, __FILE__);
1255 5 CvXSUBANY(ctx->shared_cv).any_ptr = ctx;
1256 5 done_rv = sv_2mortal(newRV_inc((SV*)ctx->shared_cv));
1257 } else {
1258 1024 ctx->num_cvs = len;
1259 1024 Newxz(ctx->cvs, len, CV*);
1260 }
1261  
1262 1029 dSP;
1263  
1264 I32 i;
1265 /* G_VOID: see parallel_start. */
1266 1029 ▶ U32 flags = G_DISCARD | G_VOID | (unsafe ? 0 : G_EVAL);
Branch: 1 branch
BranchTrueFalseTotal
U32 flags = G_DISCARD | G_VOID | (unsafe ? 0 : G_EVAL); 5 1024 1029
1267  
1268 2064 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 2047 17 2064
1269 2047 ▶ if (*is_freed || ctx->settled) break;
Branch: 2 branches
BranchTrueFalseTotal
if (*is_freed || ctx->settled) break; 1043 1004 2047
if (*is_freed || ctx->settled) break; 1043 0 1043
1270  
1271 1043 ▶ SV **task_ary = (AvREAL(tasks) && !SvMAGICAL(tasks)) ? AvARRAY(tasks) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **task_ary = (AvREAL(tasks) && !SvMAGICAL(tasks)) ? AvARRAY(tasks) : NULL; 1043 0 1043
SV **task_ary = (AvREAL(tasks) && !SvMAGICAL(tasks)) ? AvARRAY(tasks) : NULL; 1043 0 1043
1272 1043 ▶ SV **fetch_ptr = (task_ary && i <= AvFILL(tasks))
Branch: 1 branch
BranchTrueFalseTotal
SV **fetch_ptr = (task_ary && i <= AvFILL(tasks)) 1043 0 1043
1273 1043 ? &task_ary[i]
1274 2086 ▶ : av_fetch(tasks, i, 0);
Branch: 1 branch
BranchTrueFalseTotal
: av_fetch(tasks, i, 0); 1043 0 1043
1275 1043 ▶ SV *task_sv = fetch_ptr ? *fetch_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *task_sv = fetch_ptr ? *fetch_ptr : NULL; 1043 0 1043
1276  
1277 2078 ▶ if (IS_PVCV(task_sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(task_sv)) { 1042 1 1043
if (IS_PVCV(task_sv)) { 1041 1 1042
if (IS_PVCV(task_sv)) { 1041 0 1041
1278 1041 CV *cv = NULL;
1279 1041 ▶ if (!unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unsafe) { 1033 8 1041
1280 1033 cv = newXS(NULL, race_task_done, __FILE__);
1281 1033 CvXSUBANY(cv).any_ptr = ctx;
1282 1033 ctx->cvs[i] = (CV*)SvREFCNT_inc((SV*)cv);
1283 }
1284  
1285 1041 ENTER;
1286 1041 SAVETMPS;
1287 1041 ▶ if (!unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unsafe) { 1033 8 1041
1288 1033 done_rv = sv_2mortal(newRV_noinc((SV*)cv));
1289 }
1290 1041 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1041 1041
1291 1041 ▶ XPUSHs(done_rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(done_rv); 0 1041 1041
1292 1041 PUTBACK;
1293  
1294 1041 call_sv(task_sv, flags);
1295 1039 SPAGAIN;
1296 1039 ▶ if (!unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unsafe) { 1033 6 1039
1297 1033 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 1033 0 1033
if (SvTRUE(ERRSV)) { 4 1029 1033
1298 4 ▶ SV *err = sv_mortalcopy(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_mortalcopy(ERRSV); 4 0 4
1299 4 ▶ if (!*is_freed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!*is_freed) { 3 1 4
1300 3 race_cleanup(aTHX_ &ctx);
1301 }
1302 4 croak_sv(err);
1303 }
1304 }
1305  
1306 1035 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1029 6 1035
1307 1035 LEAVE;
1308 } else {
1309 2 ctx->settled = 1;
1310 2 SV *cb = ctx->final_cb;
1311 2 ▶ if (IS_PVCV(cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_PVCV(cb)) { 2 0 2
if (IS_PVCV(cb)) { 2 0 2
if (IS_PVCV(cb)) { 2 0 2
1312 2 ENTER;
1313 2 SAVETMPS;
1314 2 SvREFCNT_inc(cb);
1315 2 sv_2mortal(cb);
1316 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
1317 2 PUTBACK;
1318 2 race_cleanup(aTHX_ &ctx);
1319 2 call_sv(cb, G_DISCARD | G_VOID);
1320 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
1321 2 LEAVE;
1322 } else {
1323 0 race_cleanup(aTHX_ &ctx);
1324 }
1325 2 break;
1326 }
1327 }
1328 1023 guard->completed = 1;
1329 1023 LEAVE;
1330 1023 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 6 1017 1023
1331 6 ST(0) = sv_2mortal(evf_handle_wrap(aTHX_ h));
1332 6 XSRETURN(1);
1333 }
1334  
1335 MODULE = EV::Future PACKAGE = EV::Future::Handle
1336  
1337 void
1338 DESTROY(self)
1339 SV *self
1340 CODE:
1341 59 ▶ if (self && SvROK(self)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self && SvROK(self)) { 59 0 59
if (self && SvROK(self)) { 58 1 59
1342 58 MAGIC *mg = mg_findext(SvRV(self), PERL_MAGIC_ext, &evf_handle_vtbl);
1343 58 ▶ if (mg && mg->mg_ptr) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && mg->mg_ptr) { 53 5 58
if (mg && mg->mg_ptr) { 51 2 53
1344 51 evf_handle *h = (evf_handle *)mg->mg_ptr;
1345 51 mg->mg_ptr = NULL;
1346 51 evf_handle_dec(aTHX_ h);
1347 }
1348 }
1349  
1350 void
1351 cancel(self, ...)
1352 SV *self
1353 CODE:
1354 29 evf_handle *h = evf_handle_from_sv(aTHX_ self);
1355 29 ▶ int fire = (items > 1 && SvTRUE(ST(1)));
Branch: 2 branches
BranchTrueFalseTotal
int fire = (items > 1 && SvTRUE(ST(1))); 8 21 29
int fire = (items > 1 && SvTRUE(ST(1))); 8 0 8
1356 29 evf_handle_cancel(aTHX_ h, fire);
1357  
1358 IV
1359 pending(self)
1360 SV *self
1361 CODE:
1362 26 RETVAL = evf_handle_count(aTHX_ evf_handle_from_sv(aTHX_ self), 0);
1363 OUTPUT:
1364 RETVAL
1365  
1366 IV
1367 active(self)
1368 SV *self
1369 CODE:
1370 19 RETVAL = evf_handle_count(aTHX_ evf_handle_from_sv(aTHX_ self), 1);
1371 OUTPUT:
1372 RETVAL
↑ Summary blib/lib/EV/Future.pm »