Future.xs

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