src/future.c

filter: Statementstmt 1.4% 13 / 929 Branchbran 0.6% 4 / 598 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 1.1% 17 / 1527 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/Future/XS.xs ↑ Summary blib/lib/Future/XS.pm »
1 #define PERL_NO_GET_CONTEXT
2  
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6  
7 #include "future.h"
8  
9 #include "perl-backcompat.c.inc"
10  
11 #include "av-utils.c.inc"
12 #include "cv_set_anysv_refcounted.c.inc"
13  
14 #if !HAVE_PERL_VERSION(5, 16, 0)
15 # define false FALSE
16 # define true TRUE
17 #endif
18  
19 #ifdef HAVE_DMD_HELPER
20 # define WANT_DMD_API_044
21 # include "DMD_helper.h"
22 #endif
23  
24 #if !HAVE_PERL_VERSION(5, 16, 0)
25 # define XS_INTERNAL(name) static XS(name)
26 #endif
27  
28 #define mPUSHpvs(s) mPUSHp("" s "", sizeof(s)-1)
29  
30 static bool future_debug;
31 static bool capture_times;
32  
33 /* There's no reason these have to match those in Future.pm but for now we
34 * might as well just copy the same values
35 */
36 enum {
37 CB_DONE = (1<<0),
38 CB_FAIL = (1<<1),
39 CB_CANCEL = (1<<2),
40 CB_ALWAYS = CB_DONE|CB_FAIL|CB_CANCEL,
41  
42 CB_SELF = (1<<3),
43 CB_RESULT = (1<<4),
44  
45 CB_SEQ_READY = (1<<5),
46 CB_SEQ_CANCEL = (1<<6),
47 CB_SEQ_ANY = CB_SEQ_READY|CB_SEQ_CANCEL,
48  
49 CB_SEQ_IMDONE = (1<<7),
50 CB_SEQ_IMFAIL = (1<<8),
51  
52 CB_SEQ_STRICT = (1<<9),
53  
54 CB_IS_FUTURE = (1<<10),
55 };
56  
57 // TODO: Consider using different struct types to save memory? Or maybe it's
58 // so small a difference it doesn't matter
59 struct FutureXSCallback
60 {
61 unsigned int flags;
62 union {
63 SV *code; /* if !(flags & CB_SEQ_ANY) */
64 struct { /* if (flags & CB_SEQ_ANY) */
65 SV *thencode;
66 SV *elsecode;
67 HV *catches;
68 SV *f;
69 } seq;
70 };
71 };
72  
73 struct FutureXSRevocation
74 {
75 SV *precedent_f;
76 SV *toclear_sv_at;
77 };
78  
79 #define CB_NONSEQ_CODE(cb) \
80 ({ if((cb)->flags & CB_SEQ_ANY) croak("ARGH: CB_NONSEQ_CODE on SEQ"); (cb)->code;})
81  
82 enum {
83 SUBFLAG_NO_CANCEL = (1<<0),
84 };
85  
86 struct FutureXS
87 {
88 unsigned int ready : 1;
89 unsigned int cancelled : 1;
90 unsigned int reported : 1;
91 SV *label;
92 AV *result; // implies done
93 AV *failure; // implies fail
94 AV *callbacks; // values are struct FutureXSCallback ptrs directly. TODO: custom ptr/fill/max
95 AV *on_cancel; // values are CVs directly
96 AV *revoke_when_ready; // values are struct FutureXSRevocation ptrs directly.
97 int empty_revocation_slots;
98  
99 HV *udata;
100  
101 struct timeval btime, rtime;
102 SV *constructed_at;
103  
104 /* For convergents
105 * TODO: consider making this an optional extra part of the body, only
106 * allocated when required
107 */
108 AV *subs;
109 U8 *subflags;
110 Size_t pending_subs;
111  
112 /* For without_cancel, purely to keep a strongref */
113 SV *precedent_f;
114 };
115  
116 #ifdef USE_ITHREADS
117 static int future_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param);
118  
119 static MGVTBL vtbl = {
120 .svt_dup = &future_dup,
121 };
122 #endif
123  
124 0 bool Future_sv_is_future(pTHX_ SV *sv)
125 {
126 0 ▶ if(!SvROK(sv) || !SvOBJECT(SvRV(sv)))
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(sv) || !SvOBJECT(SvRV(sv))) 0 0 0
if(!SvROK(sv) || !SvOBJECT(SvRV(sv))) 0 0 0
127 0 return false;
128  
129 0 ▶ if(sv_derived_from(sv, "Future") || sv_derived_from(sv, "Future::XS"))
Branch: 2 branches
BranchTrueFalseTotal
if(sv_derived_from(sv, "Future") || sv_derived_from(sv, "Future::XS")) 0 0 0
if(sv_derived_from(sv, "Future") || sv_derived_from(sv, "Future::XS")) 0 0 0
130 0 return true;
131  
132 0 return false;
133 }
134  
135 #define get_future(sv) S_get_future(aTHX_ sv, FALSE)
136 #define maybe_get_future(sv) S_get_future(aTHX_ sv, TRUE)
137 0 static struct FutureXS *S_get_future(pTHX_ SV *sv, bool nullok)
138 {
139 assert(sv);
140 assert(SvROK(sv) && SvOBJECT(SvRV(sv)));
141 // TODO: Add some safety checking about class
142 0 struct FutureXS *self = INT2PTR(struct FutureXS *, SvIV(SvRV(sv)));
143 0 ▶ if(self || nullok)
Branch: 2 branches
BranchTrueFalseTotal
if(self || nullok) 0 0 0
if(self || nullok) 0 0 0
144 0 return self;
145 0 croak("Future::XS instance %" SVf " is not available in this thread", SVfARG(sv));
146 }
147  
148 0 SV *Future_new(pTHX_ const char *cls)
149 {
150 0 ▶ if(!cls)
Branch: 1 branch
BranchTrueFalseTotal
if(!cls) 0 0 0
151 0 cls = "Future::XS";
152  
153 struct FutureXS *self;
154 0 Newx(self, 1, struct FutureXS);
155  
156 0 self->ready = false;
157 0 self->cancelled = false;
158 0 self->reported = false;
159  
160 0 self->label = NULL;
161  
162 0 ▶ if(capture_times)
Branch: 1 branch
BranchTrueFalseTotal
if(capture_times) 0 0 0
163 0 gettimeofday(&self->btime, NULL);
164 else
165 0 self->btime = (struct timeval){ 0 };
166  
167 0 self->rtime = (struct timeval){ 0 };
168  
169 0 ▶ if(future_debug)
Branch: 1 branch
BranchTrueFalseTotal
if(future_debug) 0 0 0
170 0 ▶ self->constructed_at = newSVpvf("constructed at %s line %d", CopFILE(PL_curcop), CopLINE(PL_curcop));
Branch: 1 branch
BranchTrueFalseTotal
self->constructed_at = newSVpvf("constructed at %s line %d", CopFILE(PL_curcop), CopLINE(PL_curcop)); 0 0 0
171 else
172 0 self->constructed_at = NULL;
173  
174 0 self->result = NULL;
175 0 self->failure = NULL;
176  
177 0 self->callbacks = NULL;
178 0 self->on_cancel = NULL;
179 0 self->revoke_when_ready = NULL;
180 0 self->empty_revocation_slots = 0;
181  
182 0 self->udata = NULL;
183  
184 0 self->subs = NULL;
185 0 self->subflags = NULL;
186  
187 0 self->precedent_f = NULL;
188  
189 0 SV *ret = newSV(0);
190 0 sv_setref_pv(ret, cls, self);
191  
192 #ifdef USE_ITHREADS
193 MAGIC *mg = sv_magicext(SvRV(ret), SvRV(ret), PERL_MAGIC_ext, &vtbl, NULL, 0);
194 mg->mg_flags |= MGf_DUP;
195 #endif
196  
197 0 return ret;
198 }
199  
200 #define future_new_proto(f1) Future_new_proto(aTHX_ f1)
201 0 SV *Future_new_proto(pTHX_ SV *f1)
202 {
203 assert(f1 && SvROK(f1) && SvRV(f1));
204 // TODO Shortcircuit in the common case that f1 is a Future instance
205 // return future_new(HvNAME(SvSTASH(SvRV(f1))));
206  
207 0 dSP;
208 0 ENTER;
209 0 SAVETMPS;
210  
211 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
212 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
213 0 PUSHs(sv_mortalcopy(f1));
214 0 PUTBACK;
215  
216 0 call_method("new", G_SCALAR);
217  
218 0 SPAGAIN;
219  
220 0 SV *ret = SvREFCNT_inc(POPs);
221  
222 0 PUTBACK;
223 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
224 0 LEAVE;
225  
226 0 return ret;
227 }
228  
229 #ifdef USE_ITHREADS
230  
231 static int future_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
232 {
233 /* We don't currently support duplicating a Future instance across thread
234 * creation/return. For now just zero out the pointer and complain if anyone
235 * tries to access it.
236 * This at least means that incidental Future instances that happen to exist
237 * in main thread memory won't be disturbed when sidecar threads are joined.
238 */
239 sv_setiv(mg->mg_obj, 0);
240 }
241 #endif
242  
243 #define clear_callback(cb) S_clear_callback(aTHX_ cb)
244 0 static void S_clear_callback(pTHX_ struct FutureXSCallback *cb)
245 {
246 0 int flags = cb->flags;
247 0 ▶ if(flags & CB_SEQ_ANY) {
Branch: 1 branch
BranchTrueFalseTotal
if(flags & CB_SEQ_ANY) { 0 0 0
248 0 SvREFCNT_dec(cb->seq.thencode);
249 0 SvREFCNT_dec(cb->seq.elsecode);
250 0 SvREFCNT_dec(cb->seq.catches);
251 0 SvREFCNT_dec(cb->seq.f);
252 }
253 else {
254 0 ▶ SvREFCNT_dec(CB_NONSEQ_CODE(cb));
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec(CB_NONSEQ_CODE(cb)); 0 0 0
255 }
256 0 }
257  
258 #define destroy_callbacks(self) S_destroy_callbacks(aTHX_ self)
259 0 static void S_destroy_callbacks(pTHX_ struct FutureXS *self)
260 {
261 0 AV *callbacksav = self->callbacks;
262 0 ▶ while(callbacksav && AvFILLp(callbacksav) > -1) {
Branch: 2 branches
BranchTrueFalseTotal
while(callbacksav && AvFILLp(callbacksav) > -1) { 0 0 0
while(callbacksav && AvFILLp(callbacksav) > -1) { 0 0 0
263 0 struct FutureXSCallback *cb = (struct FutureXSCallback *)AvARRAY(self->callbacks)[AvFILLp(callbacksav)--];
264 0 clear_callback(cb);
265 0 Safefree(cb);
266 }
267 0 }
268  
269 #define future_mortal_selfstr(f) Future_mortal_selfstr(aTHX_ f)
270 0 static SV *Future_mortal_selfstr(pTHX_ SV *f)
271 {
272 0 struct FutureXS *self = get_future(f);
273  
274 0 SV *ret = newSVpvf("%" SVf, SVfARG(f));
275 0 ▶ if(self->label)
Branch: 1 branch
BranchTrueFalseTotal
if(self->label) 0 0 0
276 0 sv_catpvf(ret, " (\"%" SVf "\")", SVfARG(self->label));
277 0 ▶ if(future_debug)
Branch: 1 branch
BranchTrueFalseTotal
if(future_debug) 0 0 0
278 0 sv_catpvf(ret, " (%" SVf ")", SVfARG(self->constructed_at));
279 0 return sv_2mortal(ret);
280 }
281  
282 0 void Future_destroy(pTHX_ SV *f)
283 {
284 #ifdef DEBUGGING
285 // Every pointer in this function ought to have been uniquely held
286 # define UNREF(p) \
287 do { \
288 if(p) assert(SvREFCNT(p) == 1); \
289 SvREFCNT_dec((SV *)p); \
290 (p) = (void *)0xAA55AA55; \
291 } while(0)
292 #else
293 # define UNREF(p) SvREFCNT_dec((SV *)p)
294 #endif
295  
296 /* Defend against being run during global destruction */
297 0 ▶ if(!f || !SvROK(f))
Branch: 2 branches
BranchTrueFalseTotal
if(!f || !SvROK(f)) 0 0 0
if(!f || !SvROK(f)) 0 0 0
298 0 return;
299 0 struct FutureXS *self = maybe_get_future(f);
300 0 ▶ if(!self)
Branch: 1 branch
BranchTrueFalseTotal
if(!self) 0 0 0
301 0 return;
302  
303 0 ▶ if(future_debug &&
Branch: 1 branch
BranchTrueFalseTotal
if(future_debug && 0 0 0
304 0 ▶ (!self->ready || (self->failure && !self->reported))) {
Branch: 3 branches
BranchTrueFalseTotal
(!self->ready || (self->failure && !self->reported))) { 0 0 0
(!self->ready || (self->failure && !self->reported))) { 0 0 0
(!self->ready || (self->failure && !self->reported))) { 0 0 0
305 0 ▶ if(!self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ready) 0 0 0
306 0 ▶ warn("%" SVf " was lost near %s line %d before it was ready\n",
Branch: 1 branch
BranchTrueFalseTotal
warn("%" SVf " was lost near %s line %d before it was ready\n", 0 0 0
307 SVfARG(future_mortal_selfstr(f)),
308 CopFILE(PL_curcop), CopLINE(PL_curcop));
309 else {
310 0 SV *failure = AvARRAY(self->failure)[0];
311 0 ▶ warn("%" SVf " was lost near %s line %d with an unreported failure of: %" SVf "\n",
Branch: 1 branch
BranchTrueFalseTotal
warn("%" SVf " was lost near %s line %d with an unreported failure of: %" SVf "\n", 0 0 0
312 SVfARG(future_mortal_selfstr(f)),
313 CopFILE(PL_curcop), CopLINE(PL_curcop),
314 SVfARG(failure));
315 }
316 }
317  
318 0 UNREF(self->label);
319  
320 0 UNREF(self->result);
321  
322 0 UNREF(self->failure);
323  
324 0 destroy_callbacks(self);
325 0 UNREF(self->callbacks);
326  
327 0 UNREF(self->on_cancel);
328  
329 0 AV *revocationsav = self->revoke_when_ready;
330 0 ▶ while(revocationsav && AvFILLp(revocationsav) > -1) {
Branch: 2 branches
BranchTrueFalseTotal
while(revocationsav && AvFILLp(revocationsav) > -1) { 0 0 0
while(revocationsav && AvFILLp(revocationsav) > -1) { 0 0 0
331 0 struct FutureXSRevocation *rev = (struct FutureXSRevocation *)AvARRAY(revocationsav)[AvFILLp(revocationsav)--];
332 0 UNREF(rev->precedent_f);
333 0 UNREF(rev->toclear_sv_at);
334 0 Safefree(rev);
335 }
336 0 UNREF(self->revoke_when_ready);
337  
338 0 UNREF(self->udata);
339  
340 0 UNREF(self->constructed_at);
341  
342 0 UNREF(self->subs);
343 0 Safefree(self->subflags);
344  
345 0 UNREF(self->precedent_f);
346  
347 0 Safefree(self);
348  
349 #undef UNREF
350 }
351  
352 0 bool Future_is_ready(pTHX_ SV *f)
353 {
354 0 struct FutureXS *self = get_future(f);
355 0 return self->ready;
356 }
357  
358 0 bool Future_is_done(pTHX_ SV *f)
359 {
360 0 struct FutureXS *self = get_future(f);
361 0 ▶ return self->ready && !self->failure && !self->cancelled;
Branch: 3 branches
BranchTrueFalseTotal
return self->ready && !self->failure && !self->cancelled; 0 0 0
return self->ready && !self->failure && !self->cancelled; 0 0 0
return self->ready && !self->failure && !self->cancelled; 0 0 0
362 }
363  
364 0 bool Future_is_failed(pTHX_ SV *f)
365 {
366 0 struct FutureXS *self = get_future(f);
367 0 ▶ return self->ready && self->failure;
Branch: 2 branches
BranchTrueFalseTotal
return self->ready && self->failure; 0 0 0
return self->ready && self->failure; 0 0 0
368 }
369  
370 0 bool Future_is_cancelled(pTHX_ SV *f)
371 {
372 0 struct FutureXS *self = get_future(f);
373 0 return self->cancelled;
374 }
375  
376 #define clear_on_cancel(self) S_clear_on_cancel(aTHX_ self)
377 0 static void S_clear_on_cancel(pTHX_ struct FutureXS *self)
378 {
379 0 ▶ if(!self->on_cancel)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->on_cancel) 0 0 0
380 0 return;
381  
382 0 AV *on_cancel = self->on_cancel;
383 0 self->on_cancel = NULL;
384  
385 0 SvREFCNT_dec(on_cancel);
386 }
387  
388 #define push_callback(self, cb) S_push_callback(aTHX_ self, cb)
389 0 static void S_push_callback(pTHX_ struct FutureXS *self, struct FutureXSCallback *cb)
390 {
391 struct FutureXSCallback *new;
392 0 Newx(new, 1, struct FutureXSCallback);
393  
394 0 new->flags = cb->flags;
395 0 ▶ if(cb->flags & CB_SEQ_ANY) {
Branch: 1 branch
BranchTrueFalseTotal
if(cb->flags & CB_SEQ_ANY) { 0 0 0
396 0 new->seq.thencode = cb->seq.thencode;
397 0 new->seq.elsecode = cb->seq.elsecode;
398 0 new->seq.catches = cb->seq.catches;
399 0 new->seq.f = cb->seq.f;
400 }
401 else {
402 0 ▶ new->code = CB_NONSEQ_CODE(cb);
Branch: 1 branch
BranchTrueFalseTotal
new->code = CB_NONSEQ_CODE(cb); 0 0 0
403 }
404  
405 0 ▶ if(!self->callbacks)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->callbacks) 0 0 0
406 0 self->callbacks = newAV();
407  
408 0 av_push(self->callbacks, (SV *)new);
409 0 }
410  
411 #define wrap_cb(f, name, cv) S_wrap_cb(aTHX_ f, name, cv)
412 0 static SV *S_wrap_cb(pTHX_ SV *f, const char *name, SV *cv)
413 {
414 // TODO: This is quite the speed bump having to do this, in the common case
415 // that it isn't overridden
416 0 dSP;
417 0 ENTER;
418 0 SAVETMPS;
419  
420 0 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 0 0
421 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
422 0 PUSHs(sv_mortalcopy(f));
423 0 mPUSHp(name, strlen(name));
424 0 PUSHs(sv_mortalcopy(cv));
425 0 PUTBACK;
426  
427 0 call_method("wrap_cb", G_SCALAR);
428  
429 0 SPAGAIN;
430 0 SV *ret = newSVsv(POPs);
431  
432 0 PUTBACK;
433 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
434 0 LEAVE;
435  
436 0 return ret;
437 }
438  
439 #define invoke_seq_callback(self, selfsv, cb) S_invoke_seq_callback(aTHX_ self, selfsv, cb)
440 0 static SV *S_invoke_seq_callback(pTHX_ struct FutureXS *self, SV *selfsv, struct FutureXSCallback *cb)
441 {
442 0 int flags = cb->flags;
443  
444 0 bool is_fail = cBOOL(self->failure);
445 0 ▶ bool is_done = !self->cancelled && !is_fail;
Branch: 2 branches
BranchTrueFalseTotal
bool is_done = !self->cancelled && !is_fail; 0 0 0
bool is_done = !self->cancelled && !is_fail; 0 0 0
446  
447 0 ▶ AV *result = (is_done) ? self->result :
Branch: 1 branch
BranchTrueFalseTotal
AV *result = (is_done) ? self->result : 0 0 0
448 0 ▶ (is_fail) ? self->failure :
Branch: 1 branch
BranchTrueFalseTotal
(is_fail) ? self->failure : 0 0 0
449 NULL;
450  
451 0 ▶ SV *code = (is_done) ? cb->seq.thencode :
Branch: 1 branch
BranchTrueFalseTotal
SV *code = (is_done) ? cb->seq.thencode : 0 0 0
452 0 ▶ (is_fail) ? cb->seq.elsecode :
Branch: 1 branch
BranchTrueFalseTotal
(is_fail) ? cb->seq.elsecode : 0 0 0
453 NULL;
454  
455 0 ▶ if(is_fail && result && av_count(result) > 1 && cb->seq.catches) {
Branch: 4 branches
BranchTrueFalseTotal
if(is_fail && result && av_count(result) > 1 && cb->seq.catches) { 0 0 0
if(is_fail && result && av_count(result) > 1 && cb->seq.catches) { 0 0 0
if(is_fail && result && av_count(result) > 1 && cb->seq.catches) { 0 0 0
if(is_fail && result && av_count(result) > 1 && cb->seq.catches) { 0 0 0
456 0 SV *category = AvARRAY(result)[1];
457 0 ▶ if(SvOK(category)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(category)) { 0 0 0
458 0 HE *he = hv_fetch_ent(cb->seq.catches, category, 0, 0);
459 0 ▶ if(he && HeVAL(he))
Branch: 2 branches
BranchTrueFalseTotal
if(he && HeVAL(he)) 0 0 0
if(he && HeVAL(he)) 0 0 0
460 0 code = HeVAL(he);
461 }
462 }
463  
464 0 ▶ if(!code || !SvOK(code))
Branch: 2 branches
BranchTrueFalseTotal
if(!code || !SvOK(code)) 0 0 0
if(!code || !SvOK(code)) 0 0 0
465 0 return newSVsv(selfsv);
466  
467 0 dSP;
468  
469 0 ENTER;
470 0 SAVETMPS;
471  
472 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
473 0 ▶ if(flags & CB_SELF)
Branch: 1 branch
BranchTrueFalseTotal
if(flags & CB_SELF) 0 0 0
474 0 ▶ XPUSHs(selfsv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(selfsv); 0 0 0
475 0 ▶ if(flags & CB_RESULT)
Branch: 1 branch
BranchTrueFalseTotal
if(flags & CB_RESULT) 0 0 0
476 0 ▶ XPUSHs_from_AV(result);
Branch: 3 branches
BranchTrueFalseTotal
XPUSHs_from_AV(result); 0 0 0
XPUSHs_from_AV(result); 0 0 0
XPUSHs_from_AV(result); 0 0 0
477 0 PUTBACK;
478  
479 assert(SvOK(code));
480 0 call_sv(code, G_SCALAR|G_EVAL);
481  
482 0 SPAGAIN;
483  
484 0 ▶ if(SvROK(ERRSV) || SvTRUE(ERRSV)) {
Branch: 4 branches
BranchTrueFalseTotal
if(SvROK(ERRSV) || SvTRUE(ERRSV)) { 0 0 0
if(SvROK(ERRSV) || SvTRUE(ERRSV)) { 0 0 0
if(SvROK(ERRSV) || SvTRUE(ERRSV)) { 0 0 0
if(SvROK(ERRSV) || SvTRUE(ERRSV)) { 0 0 0
485 0 POPs;
486  
487 0 SV *fseq = cb->seq.f;
488  
489 0 ▶ if(!fseq)
Branch: 1 branch
BranchTrueFalseTotal
if(!fseq) 0 0 0
490 0 fseq = future_new_proto(selfsv);
491  
492 0 ▶ future_failv(fseq, &ERRSV, 1);
Branch: 1 branch
BranchTrueFalseTotal
future_failv(fseq, &ERRSV, 1); 0 0 0
493  
494 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
495 0 LEAVE;
496  
497 0 return fseq;
498 }
499  
500 0 SV *f2 = POPs;
501 0 SvREFCNT_inc(f2);
502  
503 0 PUTBACK;
504 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
505 0 LEAVE;
506  
507 0 ▶ if(!sv_is_future(f2)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!sv_is_future(f2)) { 0 0 0
508 0 SV *result = f2;
509  
510 // TODO: strictness check
511  
512 0 f2 = future_new_proto(selfsv);
513 0 future_donev(f2, &result, 1);
514 }
515  
516 0 return f2;
517 }
518  
519 #define invoke_callback(self, selfsv, cb) S_invoke_callback(aTHX_ self, selfsv, cb)
520 0 static void S_invoke_callback(pTHX_ struct FutureXS *self, SV *selfsv, struct FutureXSCallback *cb)
521 {
522 0 int flags = cb->flags;
523  
524 0 bool is_cancelled = self->cancelled;
525 0 bool is_fail = cBOOL(self->failure);
526 0 ▶ bool is_done = !is_cancelled && !is_fail;
Branch: 2 branches
BranchTrueFalseTotal
bool is_done = !is_cancelled && !is_fail; 0 0 0
bool is_done = !is_cancelled && !is_fail; 0 0 0
527  
528 0 ▶ AV *result = (is_done) ? self->result :
Branch: 1 branch
BranchTrueFalseTotal
AV *result = (is_done) ? self->result : 0 0 0
529 0 ▶ (is_fail) ? self->failure :
Branch: 1 branch
BranchTrueFalseTotal
(is_fail) ? self->failure : 0 0 0
530 NULL;
531  
532 0 ▶ if(is_done && !(flags & CB_DONE))
Branch: 2 branches
BranchTrueFalseTotal
if(is_done && !(flags & CB_DONE)) 0 0 0
if(is_done && !(flags & CB_DONE)) 0 0 0
533 0 return;
534 0 ▶ if(is_fail && !(flags & CB_FAIL))
Branch: 2 branches
BranchTrueFalseTotal
if(is_fail && !(flags & CB_FAIL)) 0 0 0
if(is_fail && !(flags & CB_FAIL)) 0 0 0
535 0 return;
536 0 ▶ if(is_cancelled && !(flags & CB_CANCEL))
Branch: 2 branches
BranchTrueFalseTotal
if(is_cancelled && !(flags & CB_CANCEL)) 0 0 0
if(is_cancelled && !(flags & CB_CANCEL)) 0 0 0
537 0 return;
538  
539 0 ▶ if(flags & CB_IS_FUTURE) {
Branch: 1 branch
BranchTrueFalseTotal
if(flags & CB_IS_FUTURE) { 0 0 0
540 0 dSP;
541  
542 0 ENTER;
543 0 SAVETMPS;
544  
545 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
546 0 ▶ XPUSHs(CB_NONSEQ_CODE(cb)); // really a Future RV
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(CB_NONSEQ_CODE(cb)); // really a Future RV 0 0 0
XPUSHs(CB_NONSEQ_CODE(cb)); // really a Future RV 0 0 0
547 0 ▶ if(result)
Branch: 1 branch
BranchTrueFalseTotal
if(result) 0 0 0
548 0 ▶ XPUSHs_from_AV(result);
Branch: 3 branches
BranchTrueFalseTotal
XPUSHs_from_AV(result); 0 0 0
XPUSHs_from_AV(result); 0 0 0
XPUSHs_from_AV(result); 0 0 0
549  
550 0 PUTBACK;
551 0 ▶ if(is_done)
Branch: 1 branch
BranchTrueFalseTotal
if(is_done) 0 0 0
552 0 call_method("done", G_VOID);
553 0 ▶ else if(is_fail)
Branch: 1 branch
BranchTrueFalseTotal
else if(is_fail) 0 0 0
554 0 call_method("fail", G_VOID);
555 else
556 0 call_method("cancel", G_VOID);
557  
558 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
559 0 LEAVE;
560 }
561 0 ▶ else if(flags & CB_SEQ_ANY) {
Branch: 1 branch
BranchTrueFalseTotal
else if(flags & CB_SEQ_ANY) { 0 0 0
562 0 SV *fseq = cb->seq.f;
563  
564 0 ▶ if(!SvOK(fseq)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(fseq)) { 0 0 0
565 0 warn("%" SVf " lost a sequence Future",
566 SVfARG(future_mortal_selfstr(selfsv)));
567 0 return;
568 }
569  
570 0 SV *f2 = invoke_seq_callback(self, selfsv, cb);
571 0 ▶ if(f2 == fseq)
Branch: 1 branch
BranchTrueFalseTotal
if(f2 == fseq) 0 0 0
572 /* immediate fail */
573 0 return;
574  
575 0 future_on_cancel(fseq, f2);
576  
577 0 ▶ if(future_is_ready(f2)) {
Branch: 1 branch
BranchTrueFalseTotal
if(future_is_ready(f2)) { 0 0 0
578 0 ▶ if(!future_is_cancelled(f2))
Branch: 1 branch
BranchTrueFalseTotal
if(!future_is_cancelled(f2)) 0 0 0
579 0 future_on_ready(f2, fseq);
580 0 ▶ else if(flags & CB_CANCEL)
Branch: 1 branch
BranchTrueFalseTotal
else if(flags & CB_CANCEL) 0 0 0
581 0 future_cancel(fseq);
582 }
583 else {
584 0 struct FutureXS *f2self = get_future(f2);
585 0 struct FutureXSCallback cb2 = {
586 .flags = CB_DONE|CB_FAIL|CB_IS_FUTURE,
587 0 .code = sv_rvweaken(newSVsv(fseq)),
588 };
589 0 push_callback(f2self, &cb2);
590 }
591  
592 assert(SvREFCNT(f2) == 1);
593 0 SvREFCNT_dec(f2);
594 }
595 else {
596 0 ▶ SV *code = CB_NONSEQ_CODE(cb);
Branch: 1 branch
BranchTrueFalseTotal
SV *code = CB_NONSEQ_CODE(cb); 0 0 0
597  
598 0 dSP;
599  
600 0 ENTER;
601 0 SAVETMPS;
602  
603 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
604 0 ▶ if(flags & CB_SELF)
Branch: 1 branch
BranchTrueFalseTotal
if(flags & CB_SELF) 0 0 0
605 0 ▶ XPUSHs(selfsv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(selfsv); 0 0 0
606 0 ▶ if((flags & CB_RESULT) && result)
Branch: 2 branches
BranchTrueFalseTotal
if((flags & CB_RESULT) && result) 0 0 0
if((flags & CB_RESULT) && result) 0 0 0
607 0 ▶ XPUSHs_from_AV(result);
Branch: 3 branches
BranchTrueFalseTotal
XPUSHs_from_AV(result); 0 0 0
XPUSHs_from_AV(result); 0 0 0
XPUSHs_from_AV(result); 0 0 0
608  
609 0 PUTBACK;
610 assert(SvOK(code));
611 0 call_sv(code, G_VOID);
612  
613 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
614 0 LEAVE;
615 }
616 }
617  
618 #define revoke_on_cancel(rev) S_revoke_on_cancel(aTHX_ rev)
619 0 static void S_revoke_on_cancel(pTHX_ struct FutureXSRevocation *rev)
620 {
621 0 ▶ if(rev->toclear_sv_at && SvROK(rev->toclear_sv_at)) {
Branch: 2 branches
BranchTrueFalseTotal
if(rev->toclear_sv_at && SvROK(rev->toclear_sv_at)) { 0 0 0
if(rev->toclear_sv_at && SvROK(rev->toclear_sv_at)) { 0 0 0
622 assert(SvTYPE(rev->toclear_sv_at) <= SVt_PVMG);
623 assert(SvROK(rev->toclear_sv_at));
624 0 sv_set_undef(SvRV(rev->toclear_sv_at));
625 0 SvREFCNT_dec(rev->toclear_sv_at);
626 0 rev->toclear_sv_at = NULL;
627 }
628  
629 0 ▶ if(!SvOK(rev->precedent_f))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(rev->precedent_f)) 0 0 0
630 0 return;
631  
632 0 struct FutureXS *self = get_future(rev->precedent_f);
633  
634 0 self->empty_revocation_slots++;
635  
636 0 AV *on_cancel = self->on_cancel;
637 0 ▶ if(self->empty_revocation_slots >= 8 && on_cancel &&
Branch: 2 branches
BranchTrueFalseTotal
if(self->empty_revocation_slots >= 8 && on_cancel && 0 0 0
if(self->empty_revocation_slots >= 8 && on_cancel && 0 0 0
638 0 ▶ self->empty_revocation_slots >= AvFILL(on_cancel)/2) {
Branch: 1 branch
BranchTrueFalseTotal
self->empty_revocation_slots >= AvFILL(on_cancel)/2) { 0 0 0
639  
640 // Squash up the array to contain only defined values
641 0 SV **wrsv = AvARRAY(on_cancel),
642 0 **rdsv = AvARRAY(on_cancel),
643 0 **end = AvARRAY(on_cancel) + AvFILL(on_cancel);
644  
645 0 ▶ while(rdsv <= end) {
Branch: 1 branch
BranchTrueFalseTotal
while(rdsv <= end) { 0 0 0
646 0 ▶ if(SvOK(*rdsv))
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(*rdsv)) 0 0 0
647 // Keep this one
648 0 *(wrsv++) = *rdsv;
649 else
650 // Free this one
651 0 SvREFCNT_dec(*rdsv);
652  
653 0 rdsv++;
654 }
655 0 AvFILLp(on_cancel) = wrsv - AvARRAY(on_cancel) - 1;
656  
657 0 self->empty_revocation_slots = 0;
658 }
659 }
660  
661 #define mark_ready(self, selfsv, state) S_mark_ready(aTHX_ self, selfsv, state)
662 0 static void S_mark_ready(pTHX_ struct FutureXS *self, SV *selfsv, const char *state)
663 {
664 0 self->ready = true;
665 // TODO: self->ready_at
666 0 ▶ if(capture_times)
Branch: 1 branch
BranchTrueFalseTotal
if(capture_times) 0 0 0
667 0 gettimeofday(&self->rtime, NULL);
668  
669 /* Make sure self doesn't disappear during this function */
670 0 SvREFCNT_inc(SvRV(selfsv));
671 0 SAVEFREESV(SvRV(selfsv));
672  
673 0 ▶ if(self->precedent_f) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->precedent_f) { 0 0 0
674 0 SvREFCNT_dec(self->precedent_f);
675 0 self->precedent_f = NULL;
676 }
677  
678 0 clear_on_cancel(self);
679 0 ▶ if(self->revoke_when_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->revoke_when_ready) { 0 0 0
680 0 AV *revocations = self->revoke_when_ready;
681 0 ▶ for(size_t i = 0; i < av_count(revocations); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(size_t i = 0; i < av_count(revocations); i++) { 0 0 0
682 0 struct FutureXSRevocation *rev = (struct FutureXSRevocation *)AvARRAY(revocations)[i];
683 0 revoke_on_cancel(rev);
684  
685 0 SvREFCNT_dec(rev->precedent_f);
686 0 Safefree(rev);
687 }
688 0 AvFILLp(revocations) = -1;
689 0 SvREFCNT_dec(revocations);
690  
691 0 self->revoke_when_ready = NULL;
692 }
693  
694 0 ▶ if(!self->callbacks)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->callbacks) 0 0 0
695 0 return;
696  
697 0 AV *callbacks = self->callbacks;
698  
699 0 struct FutureXSCallback **cbs = (struct FutureXSCallback **)AvARRAY(callbacks);
700 0 size_t i, n = av_count(callbacks);
701 0 ▶ for(i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < n; i++) { 0 0 0
702 0 struct FutureXSCallback *cb = cbs[i];
703 0 invoke_callback(self, selfsv, cb);
704 }
705  
706 0 destroy_callbacks(self);
707 }
708  
709 #define make_sequence_weak(f1, cb) S_make_sequence(aTHX_ f1, cb, true)
710 #define make_sequence_strong(f1, cb) S_make_sequence(aTHX_ f1, cb, false)
711 0 static SV *S_make_sequence(pTHX_ SV *f1, struct FutureXSCallback *cb, bool weaken)
712 {
713 0 struct FutureXS *self = get_future(f1);
714  
715 0 int flags = cb->flags;
716  
717 0 ▶ if(self->ready) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->ready) { 0 0 0
718 // TODO: CB_SEQ_IM*
719  
720 0 SV *f2 = invoke_seq_callback(self, f1, cb);
721 0 clear_callback(cb);
722 0 return f2;
723 }
724  
725 0 SV *fseq = future_new_proto(f1);
726 0 ▶ if(cb->flags & CB_SEQ_CANCEL)
Branch: 1 branch
BranchTrueFalseTotal
if(cb->flags & CB_SEQ_CANCEL) 0 0 0
727 0 future_on_cancel(fseq, f1);
728  
729 0 cb->flags |= CB_DONE|CB_FAIL;
730 0 ▶ if(cb->seq.thencode)
Branch: 1 branch
BranchTrueFalseTotal
if(cb->seq.thencode) 0 0 0
731 0 cb->seq.thencode = wrap_cb(f1, "sequence", sv_2mortal(cb->seq.thencode));
732 0 ▶ if(cb->seq.elsecode)
Branch: 1 branch
BranchTrueFalseTotal
if(cb->seq.elsecode) 0 0 0
733 0 cb->seq.elsecode = wrap_cb(f1, "sequence", sv_2mortal(cb->seq.elsecode));
734 0 cb->seq.f = newSVsv(fseq);
735 0 ▶ if(weaken)
Branch: 1 branch
BranchTrueFalseTotal
if(weaken) 0 0 0
736 0 sv_rvweaken(cb->seq.f);
737  
738 0 push_callback(self, cb);
739  
740 0 return fseq;
741 }
742  
743 // TODO: move to a hax/ file
744 #define CvNAME_FILE_LINE(cv) S_CvNAME_FILE_LINE(aTHX_ cv)
745 0 static SV *S_CvNAME_FILE_LINE(pTHX_ CV *cv)
746 {
747 0 ▶ if(!CvANON(cv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!CvANON(cv)) { 0 0 0
748 0 SV *ret = newSVpvf("HvNAME::GvNAME");
749 0 return ret;
750 }
751  
752 0 OP *cop = CvSTART(cv);
753 0 ▶ while(cop && OP_CLASS(cop) != OA_COP)
Branch: 4 branches
BranchTrueFalseTotal
while(cop && OP_CLASS(cop) != OA_COP) 0 0 0
while(cop && OP_CLASS(cop) != OA_COP) 0 0 0
while(cop && OP_CLASS(cop) != OA_COP) 0 0 0
while(cop && OP_CLASS(cop) != OA_COP) 0 0 0
754 0 cop = cop->op_next;
755  
756 0 ▶ if(!cop)
Branch: 1 branch
BranchTrueFalseTotal
if(!cop) 0 0 0
757 0 return newSVpvs("__ANON__");
758  
759 0 ▶ return newSVpvf("__ANON__(%s line %d)", CopFILE((COP *)cop), CopLINE((COP *)cop));
Branch: 1 branch
BranchTrueFalseTotal
return newSVpvf("__ANON__(%s line %d)", CopFILE((COP *)cop), CopLINE((COP *)cop)); 0 0 0
760 }
761  
762 0 static const char *statestr(struct FutureXS *self)
763 {
764 0 ▶ if(!self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ready) 0 0 0
765 0 return "pending";
766 0 ▶ if(self->cancelled)
Branch: 1 branch
BranchTrueFalseTotal
if(self->cancelled) 0 0 0
767 0 return "cancelled";
768 0 ▶ if(self->failure)
Branch: 1 branch
BranchTrueFalseTotal
if(self->failure) 0 0 0
769 0 return "failed";
770  
771 0 return "done";
772 }
773  
774 0 void Future_donev(pTHX_ SV *f, SV **svp, size_t n)
775 {
776 0 struct FutureXS *self = get_future(f);
777  
778 0 ▶ if(self->cancelled)
Branch: 1 branch
BranchTrueFalseTotal
if(self->cancelled) 0 0 0
779 0 return;
780  
781 0 ▶ if(self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(self->ready) 0 0 0
782 0 croak("%" SVf " is already %s and cannot be ->done",
783 SVfARG(f), statestr(self));
784 // TODO: test subs
785  
786 0 self->result = newAV_svn_dup(svp, n);
787 0 mark_ready(self, f, "done");
788 }
789  
790 0 void Future_failv(pTHX_ SV *f, SV **svp, size_t n)
791 {
792 0 struct FutureXS *self = get_future(f);
793  
794 0 ▶ if(self->cancelled)
Branch: 1 branch
BranchTrueFalseTotal
if(self->cancelled) 0 0 0
795 0 return;
796  
797 0 ▶ if(self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(self->ready) 0 0 0
798 0 croak("%" SVf " is already %s and cannot be ->fail'ed",
799 SVfARG(f), statestr(self));
800  
801 0 ▶ if(n == 1 &&
Branch: 1 branch
BranchTrueFalseTotal
if(n == 1 && 0 0 0
802 0 ▶ SvROK(svp[0]) && SvOBJECT(SvRV(svp[0])) &&
Branch: 1 branch
BranchTrueFalseTotal
SvROK(svp[0]) && SvOBJECT(SvRV(svp[0])) && 0 0 0
803 0 sv_derived_from(svp[0], "Future::Exception")) {
804 0 SV *exception = svp[0];
805 0 AV *failure = self->failure = newAV();
806  
807 0 dSP;
808  
809 {
810 0 ENTER;
811 0 SAVETMPS;
812  
813 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
814 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
815 0 PUSHs(sv_mortalcopy(exception));
816 0 PUTBACK;
817  
818 0 call_method("message", G_SCALAR);
819  
820 0 SPAGAIN;
821  
822 0 av_push(failure, SvREFCNT_inc(POPs));
823  
824 0 PUTBACK;
825 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
826 0 LEAVE;
827 }
828  
829 {
830 0 ENTER;
831 0 SAVETMPS;
832  
833 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
834 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
835 0 PUSHs(sv_mortalcopy(exception));
836 0 PUTBACK;
837  
838 0 call_method("category", G_SCALAR);
839  
840 0 SPAGAIN;
841  
842 0 av_push(failure, SvREFCNT_inc(POPs));
843  
844 0 PUTBACK;
845 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
846 0 LEAVE;
847 }
848  
849 {
850 0 ENTER;
851 0 SAVETMPS;
852  
853 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
854 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
855 0 PUSHs(sv_mortalcopy(exception));
856 0 PUTBACK;
857  
858 0 SSize_t count = call_method("details", G_LIST);
859  
860 0 SPAGAIN;
861  
862 0 SV **retp = SP - count + 1;
863  
864 0 ▶ for(SSize_t i = 0; i < count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(SSize_t i = 0; i < count; i++) 0 0 0
865 0 av_push(failure, SvREFCNT_inc(retp[i]));
866 0 SP -= count;
867  
868 0 PUTBACK;
869 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
870 0 LEAVE;
871 }
872 }
873 else {
874 0 self->failure = newAV_svn_dup(svp, n);
875 }
876  
877 0 mark_ready(self, f, "failed");
878 }
879  
880 #define future_failp(f, s) Future_failp(aTHX_ f, s)
881 0 void Future_failp(pTHX_ SV *f, const char *s)
882 {
883 0 struct FutureXS *self = get_future(f);
884  
885 0 ▶ if(self->cancelled)
Branch: 1 branch
BranchTrueFalseTotal
if(self->cancelled) 0 0 0
886 0 return;
887  
888 0 ▶ if(self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(self->ready) 0 0 0
889 0 croak("%" SVf " is already %s and cannot be ->fail'ed",
890 SVfARG(f), statestr(self));
891  
892 0 self->failure = newAV();
893 0 av_push(self->failure, newSVpv(s, strlen(s)));
894 0 mark_ready(self, f, "failed");
895 }
896  
897 0 void Future_on_cancel(pTHX_ SV *f, SV *code)
898 {
899 0 struct FutureXS *self = get_future(f);
900  
901 0 ▶ if(self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(self->ready) 0 0 0
902 0 return;
903  
904 0 bool is_future = sv_is_future(code);
905 // TODO: is_future or callable(code) or croak
906  
907 0 ▶ if(!self->on_cancel)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->on_cancel) 0 0 0
908 0 self->on_cancel = newAV();
909  
910 0 SV *rv = newSVsv((SV *)code);
911 0 av_push(self->on_cancel, rv);
912  
913 0 ▶ if(is_future) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_future) { 0 0 0
914 struct FutureXSRevocation *rev;
915 0 Newx(rev, 1, struct FutureXSRevocation);
916  
917 0 rev->precedent_f = sv_rvweaken(newSVsv(f));
918 0 rev->toclear_sv_at = sv_rvweaken(newRV_inc(rv));
919  
920 0 struct FutureXS *codeself = get_future(code);
921 0 ▶ if(!codeself->revoke_when_ready)
Branch: 1 branch
BranchTrueFalseTotal
if(!codeself->revoke_when_ready) 0 0 0
922 0 codeself->revoke_when_ready = newAV();
923  
924 0 av_push(codeself->revoke_when_ready, (SV *)rev);
925 }
926 }
927  
928 0 void Future_on_ready(pTHX_ SV *f, SV *code)
929 {
930 0 struct FutureXS *self = get_future(f);
931  
932 0 bool is_future = sv_is_future(code);
933 // TODO: is_future or callable(code) or croak
934  
935 0 int flags = CB_ALWAYS|CB_SELF;
936 0 ▶ if(is_future)
Branch: 1 branch
BranchTrueFalseTotal
if(is_future) 0 0 0
937 0 flags |= CB_IS_FUTURE;
938  
939 0 struct FutureXSCallback cb = {
940 .flags = flags,
941 .code = code,
942 };
943  
944 0 ▶ if(self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(self->ready) 0 0 0
945 0 invoke_callback(self, f, &cb);
946 else {
947 0 cb.code = wrap_cb(f, "on_ready", cb.code);
948 0 push_callback(self, &cb);
949 }
950 0 }
951  
952 0 void Future_on_done(pTHX_ SV *f, SV *code)
953 {
954 0 struct FutureXS *self = get_future(f);
955  
956 0 bool is_future = sv_is_future(code);
957 // TODO: is_future or callable(code) or croak
958  
959 0 int flags = CB_DONE|CB_RESULT;
960 0 ▶ if(is_future)
Branch: 1 branch
BranchTrueFalseTotal
if(is_future) 0 0 0
961 0 flags |= CB_IS_FUTURE;
962  
963 0 struct FutureXSCallback cb = {
964 .flags = flags,
965 .code = code,
966 };
967  
968 0 ▶ if(self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(self->ready) 0 0 0
969 0 invoke_callback(self, f, &cb);
970 else {
971 0 cb.code = wrap_cb(f, "on_done", cb.code);
972 0 push_callback(self, &cb);
973 }
974 0 }
975  
976 0 void Future_on_fail(pTHX_ SV *f, SV *code)
977 {
978 0 struct FutureXS *self = get_future(f);
979  
980 0 bool is_future = sv_is_future(code);
981 // TODO: is_future or callable(code) or croak
982  
983 0 int flags = CB_FAIL|CB_RESULT;
984 0 ▶ if(is_future)
Branch: 1 branch
BranchTrueFalseTotal
if(is_future) 0 0 0
985 0 flags |= CB_IS_FUTURE;
986  
987 0 struct FutureXSCallback cb = {
988 .flags = flags,
989 .code = code,
990 };
991  
992 0 ▶ if(self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(self->ready) 0 0 0
993 0 invoke_callback(self, f, &cb);
994 else {
995 0 cb.code = wrap_cb(f, "on_fail", cb.code);
996 0 push_callback(self, &cb);
997 }
998 0 }
999  
1000 #define future_await(f) Future_await(aTHX_ f)
1001 0 static void Future_await(pTHX_ SV *f)
1002 {
1003 0 dSP;
1004  
1005 0 ENTER;
1006 0 SAVETMPS;
1007  
1008 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1009 0 ▶ mXPUSHs(newSVsv(f));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVsv(f)); 0 0 0
1010 0 PUTBACK;
1011  
1012 0 call_method("await", G_VOID);
1013  
1014 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1015 0 LEAVE;
1016 0 }
1017  
1018 0 AV *Future_get_result_av(pTHX_ SV *f, bool await)
1019 {
1020 0 struct FutureXS *self = get_future(f);
1021  
1022 0 ▶ if(await && !self->ready)
Branch: 2 branches
BranchTrueFalseTotal
if(await && !self->ready) 0 0 0
if(await && !self->ready) 0 0 0
1023 0 future_await(f);
1024  
1025 0 ▶ if(!self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ready) 0 0 0
1026 0 croak("%" SVf " is not yet ready", SVfARG(f));
1027  
1028 0 ▶ if(self->failure) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->failure) { 0 0 0
1029 0 self->reported = true;
1030  
1031 0 SV *exception = AvARRAY(self->failure)[0];
1032 0 ▶ if(av_count(self->failure) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(av_count(self->failure) > 1) { 0 0 0
1033 0 dSP;
1034 0 ENTER;
1035 0 SAVETMPS;
1036  
1037 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1038 0 ▶ EXTEND(SP, 1 + av_count(self->failure));
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1 + av_count(self->failure)); 0 0 0
1039 0 mPUSHpvs("Future::Exception");
1040 0 ▶ for(SSize_t i = 0; i < av_count(self->failure); i++)
Branch: 1 branch
BranchTrueFalseTotal
for(SSize_t i = 0; i < av_count(self->failure); i++) 0 0 0
1041 0 PUSHs(sv_mortalcopy(AvARRAY(self->failure)[i]));
1042 0 PUTBACK;
1043  
1044 0 call_method("new", G_SCALAR);
1045  
1046 0 SPAGAIN;
1047  
1048 0 exception = SvREFCNT_inc(POPs);
1049  
1050 0 PUTBACK;
1051 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1052 0 LEAVE;
1053  
1054 0 sv_2mortal(exception);
1055 }
1056  
1057 0 ▶ if(SvROK(exception) || SvPV_nolen(exception)[SvCUR(exception)-1] == '\n')
Branch: 2 branches
BranchTrueFalseTotal
if(SvROK(exception) || SvPV_nolen(exception)[SvCUR(exception)-1] == '\n') 0 0 0
if(SvROK(exception) || SvPV_nolen(exception)[SvCUR(exception)-1] == '\n') 0 0 0
1058 0 die_sv(exception);
1059 else {
1060 /* We'd like to call Carp::croak to do the @CARP_NOT logic, but it gets
1061 * confused about a missing callframe first because this is XS. We'll
1062 * reïmplement the logic here
1063 */
1064 I32 cxix;
1065 0 ▶ for(cxix = cxstack_ix; cxix; cxix--) {
Branch: 1 branch
BranchTrueFalseTotal
for(cxix = cxstack_ix; cxix; cxix--) { 0 0 0
1066 0 ▶ if(CxTYPE(&cxstack[cxix]) != CXt_SUB)
Branch: 1 branch
BranchTrueFalseTotal
if(CxTYPE(&cxstack[cxix]) != CXt_SUB) 0 0 0
1067 0 continue;
1068  
1069 0 const CV *cv = cxstack[cxix].blk_sub.cv;
1070 0 ▶ if(!cv)
Branch: 1 branch
BranchTrueFalseTotal
if(!cv) 0 0 0
1071 0 continue;
1072  
1073 0 ▶ const char *stashname = HvNAME(CvSTASH(cv));
Branch: 6 branches
BranchTrueFalseTotal
const char *stashname = HvNAME(CvSTASH(cv)); 0 0 0
const char *stashname = HvNAME(CvSTASH(cv)); 0 0 0
const char *stashname = HvNAME(CvSTASH(cv)); 0 0 0
const char *stashname = HvNAME(CvSTASH(cv)); 0 0 0
const char *stashname = HvNAME(CvSTASH(cv)); 0 0 0
const char *stashname = HvNAME(CvSTASH(cv)); 0 0 0
1074 0 ▶ if(!stashname)
Branch: 1 branch
BranchTrueFalseTotal
if(!stashname) 0 0 0
1075 0 continue;
1076  
1077 // The essence of the @CARP_NOT logic
1078 0 ▶ if(strEQ(stashname, "Future::_base"))
Branch: 1 branch
BranchTrueFalseTotal
if(strEQ(stashname, "Future::_base")) 0 0 0
1079 0 continue;
1080  
1081 0 ▶ const COP *cop = cxix < cxstack_ix ? cxstack[cxix+1].blk_oldcop : PL_curcop;
Branch: 1 branch
BranchTrueFalseTotal
const COP *cop = cxix < cxstack_ix ? cxstack[cxix+1].blk_oldcop : PL_curcop; 0 0 0
1082  
1083 0 ▶ sv_catpvf(exception, " at %s line %d.\n", CopFILE(cop), CopLINE(cop));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(exception, " at %s line %d.\n", CopFILE(cop), CopLINE(cop)); 0 0 0
1084 0 break;
1085 }
1086  
1087 0 die_sv(exception);
1088 }
1089 }
1090  
1091 0 ▶ if(self->cancelled)
Branch: 1 branch
BranchTrueFalseTotal
if(self->cancelled) 0 0 0
1092 0 croak("%" SVf " was cancelled",
1093 SVfARG(future_mortal_selfstr(f)));
1094  
1095 0 ▶ if(!self->result)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->result) 0 0 0
1096 0 self->result = newAV();
1097  
1098 0 return self->result;
1099 }
1100  
1101 0 AV *Future_get_failure_av(pTHX_ SV *f)
1102 {
1103 0 struct FutureXS *self = get_future(f);
1104  
1105 0 ▶ if(!self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ready) 0 0 0
1106 0 future_await(f);
1107  
1108 0 ▶ if(!self->failure)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->failure) 0 0 0
1109 0 return NULL;
1110  
1111 0 return self->failure;
1112 }
1113  
1114 0 void Future_cancel(pTHX_ SV *f)
1115 {
1116 /* Specifically don't make it an error to ->cancel a future instance not
1117 * available in this thread; as it often appears in defer / DESTROY / etc
1118 */
1119 0 struct FutureXS *self = maybe_get_future(f);
1120 0 ▶ if(!self)
Branch: 1 branch
BranchTrueFalseTotal
if(!self) 0 0 0
1121 0 return;
1122  
1123 0 ▶ if(self->ready)
Branch: 1 branch
BranchTrueFalseTotal
if(self->ready) 0 0 0
1124 0 return;
1125  
1126 0 self->cancelled = true;
1127 0 AV *on_cancel = self->on_cancel;
1128  
1129 0 ▶ if(self->subs) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->subs) { 0 0 0
1130 0 ▶ for(Size_t i = 0; i < av_count(self->subs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < av_count(self->subs); i++) { 0 0 0
1131 0 U8 flags = self->subflags[i];
1132 0 ▶ if(!(flags & SUBFLAG_NO_CANCEL))
Branch: 1 branch
BranchTrueFalseTotal
if(!(flags & SUBFLAG_NO_CANCEL)) 0 0 0
1133 0 future_cancel(AvARRAY(self->subs)[i]);
1134 }
1135 }
1136  
1137 // TODO: maybe we need to clear these out from self before we do this, in
1138 // case of recursion?
1139  
1140 0 ▶ for(int i = on_cancel ? AvFILL(on_cancel) : -1; i >= 0; i--) {
Branch: 2 branches
BranchTrueFalseTotal
for(int i = on_cancel ? AvFILL(on_cancel) : -1; i >= 0; i--) { 0 0 0
for(int i = on_cancel ? AvFILL(on_cancel) : -1; i >= 0; i--) { 0 0 0
1141 0 SV *code = AvARRAY(on_cancel)[i];
1142 0 ▶ if(!SvOK(code))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(code)) 0 0 0
1143 0 continue;
1144  
1145 0 ▶ if(sv_is_future(code)) {
Branch: 1 branch
BranchTrueFalseTotal
if(sv_is_future(code)) { 0 0 0
1146 0 dSP;
1147  
1148 0 ENTER;
1149 0 SAVETMPS;
1150  
1151 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1152 0 PUSHs(code);
1153 0 PUTBACK;
1154  
1155 0 call_method("cancel", G_VOID);
1156  
1157 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1158 0 LEAVE;
1159 }
1160 else {
1161 0 dSP;
1162  
1163 0 ENTER;
1164 0 SAVETMPS;
1165  
1166 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1167 0 PUSHs(f);
1168 0 PUTBACK;
1169  
1170 assert(SvOK(code));
1171 0 call_sv(code, G_VOID);
1172  
1173 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1174 0 LEAVE;
1175 }
1176 }
1177  
1178 0 mark_ready(self, f, "cancel");
1179 }
1180  
1181 0 SV *Future_without_cancel(pTHX_ SV *f)
1182 {
1183 0 struct FutureXSCallback cb = {
1184 .flags = CB_SEQ_READY|CB_CANCEL, /* without CB_SEQ_CANCEL */
1185 /* no code */
1186 };
1187  
1188 0 SV *ret = make_sequence_strong(f, &cb);
1189 0 struct FutureXS *self = get_future(ret);
1190  
1191 0 self->precedent_f = newSVsv(f);
1192  
1193 0 return ret;
1194 }
1195  
1196 0 SV *Future_then(pTHX_ SV *f, U32 flags, SV *thencode, SV *elsecode)
1197 {
1198 0 struct FutureXSCallback cb = {
1199 .flags = CB_SEQ_ANY|CB_RESULT,
1200 .seq.thencode = thencode,
1201 .seq.elsecode = elsecode,
1202 };
1203 0 ▶ if(flags & FUTURE_THEN_WITH_F)
Branch: 1 branch
BranchTrueFalseTotal
if(flags & FUTURE_THEN_WITH_F) 0 0 0
1204 0 cb.flags |= CB_SELF;
1205  
1206 0 return make_sequence_weak(f, &cb);
1207 }
1208  
1209 0 SV *Future_followed_by(pTHX_ SV *f, SV *code)
1210 {
1211 0 struct FutureXSCallback cb = {
1212 .flags = CB_SEQ_ANY|CB_SELF,
1213 .seq.thencode = code,
1214 0 .seq.elsecode = SvREFCNT_inc(code),
1215 };
1216  
1217 0 return make_sequence_weak(f, &cb);
1218 }
1219  
1220 0 SV *Future_thencatch(pTHX_ SV *f, U32 flags, SV *thencode, HV *catches, SV *elsecode)
1221 {
1222 0 struct FutureXSCallback cb = {
1223 .flags = CB_SEQ_ANY|CB_RESULT,
1224 .seq.thencode = thencode,
1225 .seq.elsecode = elsecode,
1226 .seq.catches = catches,
1227 };
1228 0 ▶ if(flags & FUTURE_THEN_WITH_F)
Branch: 1 branch
BranchTrueFalseTotal
if(flags & FUTURE_THEN_WITH_F) 0 0 0
1229 0 cb.flags |= CB_SELF;
1230  
1231 0 return make_sequence_weak(f, &cb);
1232 }
1233  
1234 #define future_new_subsv(cls, subs, n) S_future_new_subsv(aTHX_ cls, subs, n)
1235 0 static SV *S_future_new_subsv(pTHX_ const char *cls, SV **subs, size_t n)
1236 {
1237 0 HV *future_stash = get_hv("Future::", 0);
1238 assert(future_stash);
1239  
1240 /* Find the best prototype; pick the first derived instance if there is
1241 * one */
1242 0 SV *proto = NULL;
1243 0 size_t subcount = 0;
1244 0 ▶ for(Size_t i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < n; i++) { 0 0 0
1245 0 ▶ if(!SvROK(subs[i]) && SvPOK(subs[i]) && strEQ(SvPVX(subs[i]), "also"))
Branch: 3 branches
BranchTrueFalseTotal
if(!SvROK(subs[i]) && SvPOK(subs[i]) && strEQ(SvPVX(subs[i]), "also")) 0 0 0
if(!SvROK(subs[i]) && SvPOK(subs[i]) && strEQ(SvPVX(subs[i]), "also")) 0 0 0
if(!SvROK(subs[i]) && SvPOK(subs[i]) && strEQ(SvPVX(subs[i]), "also")) 0 0 0
1246 0 i++;
1247  
1248 0 ▶ if(!SvROK(subs[i]) || !SvOBJECT(SvRV(subs[i])))
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(subs[i]) || !SvOBJECT(SvRV(subs[i]))) 0 0 0
if(!SvROK(subs[i]) || !SvOBJECT(SvRV(subs[i]))) 0 0 0
1249 0 croak("Expected a Future, got %" SVf, SVfARG(subs[i]));
1250  
1251 0 subcount++;
1252  
1253 0 ▶ if(!proto && SvSTASH(SvRV(subs[i])) != future_stash)
Branch: 2 branches
BranchTrueFalseTotal
if(!proto && SvSTASH(SvRV(subs[i])) != future_stash) 0 0 0
if(!proto && SvSTASH(SvRV(subs[i])) != future_stash) 0 0 0
1254 0 proto = subs[i];
1255 }
1256  
1257 0 ▶ SV *f = proto ? future_new_proto(proto) : future_new(cls);
Branch: 1 branch
BranchTrueFalseTotal
SV *f = proto ? future_new_proto(proto) : future_new(cls); 0 0 0
1258 0 struct FutureXS *self = get_future(f);
1259  
1260 0 ▶ if(!self->subs)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->subs) 0 0 0
1261 0 self->subs = newAV();
1262 0 av_extend(self->subs, subcount);
1263 0 ▶ if(!self->subflags)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->subflags) 0 0 0
1264 0 Newx(self->subflags, subcount, U8);
1265  
1266 0 ▶ for(Size_t i = 0, subi = 0; i < n; i++, subi++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0, subi = 0; i < n; i++, subi++) { 0 0 0
1267 0 U8 flags = 0;
1268 0 ▶ if(!SvROK(subs[i]) && SvPOK(subs[i]) && strEQ(SvPVX(subs[i]), "also"))
Branch: 3 branches
BranchTrueFalseTotal
if(!SvROK(subs[i]) && SvPOK(subs[i]) && strEQ(SvPVX(subs[i]), "also")) 0 0 0
if(!SvROK(subs[i]) && SvPOK(subs[i]) && strEQ(SvPVX(subs[i]), "also")) 0 0 0
if(!SvROK(subs[i]) && SvPOK(subs[i]) && strEQ(SvPVX(subs[i]), "also")) 0 0 0
1269 0 flags |= SUBFLAG_NO_CANCEL, i++;
1270  
1271 0 av_store(self->subs, subi, newSVsv(subs[i]));
1272 0 self->subflags[subi] = flags;
1273 }
1274  
1275 0 return f;
1276 }
1277  
1278 #define copy_result(self, src) S_copy_result(aTHX_ self, src)
1279 0 static void S_copy_result(pTHX_ struct FutureXS *self, SV *src)
1280 {
1281 /* TODO: Handle non-Future::XS instances too */
1282 0 struct FutureXS *srcself = get_future(src);
1283  
1284 assert(srcself->ready);
1285 assert(!srcself->cancelled);
1286  
1287 0 ▶ if(srcself->failure) {
Branch: 1 branch
BranchTrueFalseTotal
if(srcself->failure) { 0 0 0
1288 0 self->failure = newAV_svn_dup(AvARRAY(srcself->failure), av_count(srcself->failure));
1289 }
1290 else {
1291 assert(srcself->result);
1292 0 self->result = newAV_svn_dup(AvARRAY(srcself->result), av_count(srcself->result));
1293 }
1294 0 }
1295  
1296 #define cancel_pending_subs(self) S_cancel_pending_subs(aTHX_ self)
1297 0 static void S_cancel_pending_subs(pTHX_ struct FutureXS *self)
1298 {
1299 0 ▶ if(!self->subs)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->subs) 0 0 0
1300 0 return;
1301  
1302 0 ▶ for(Size_t i = 0; i < av_count(self->subs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < av_count(self->subs); i++) { 0 0 0
1303 0 SV *sub = AvARRAY(self->subs)[i];
1304 0 U8 flags = self->subflags[i];
1305  
1306 0 ▶ if(!(flags & SUBFLAG_NO_CANCEL) && !future_is_ready(sub))
Branch: 2 branches
BranchTrueFalseTotal
if(!(flags & SUBFLAG_NO_CANCEL) && !future_is_ready(sub)) 0 0 0
if(!(flags & SUBFLAG_NO_CANCEL) && !future_is_ready(sub)) 0 0 0
1307 0 future_cancel(sub);
1308 }
1309 }
1310  
1311 0 XS_INTERNAL(sub_on_ready_waitall)
1312 {
1313 0 dXSARGS;
1314  
1315 0 SV *f = XSANY_sv;
1316 0 ▶ if(!SvOK(f))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(f)) 0 0 0
1317 0 return;
1318  
1319 /* Make sure self doesn't disappear during this function */
1320 0 SvREFCNT_inc(SvRV(f));
1321 0 SAVEFREESV(SvRV(f));
1322  
1323 0 struct FutureXS *self = get_future(f);
1324  
1325 0 self->pending_subs--;
1326  
1327 0 ▶ if(self->pending_subs)
Branch: 1 branch
BranchTrueFalseTotal
if(self->pending_subs) 0 0 0
1328 0 XSRETURN(0);
1329  
1330 /* TODO: This is really just newAVav() */
1331 0 self->result = newAV_svn_dup(AvARRAY(self->subs), av_count(self->subs));
1332 0 mark_ready(self, f, "wait_all");
1333 }
1334  
1335 0 SV *Future_new_waitallv(pTHX_ const char *cls, SV **subs, size_t n)
1336 {
1337 0 SV *f = future_new_subsv(cls, subs, n);
1338 0 struct FutureXS *self = get_future(f);
1339  
1340 /* Reïnit subs + n */
1341 0 subs = AvARRAY(self->subs);
1342 0 n = av_count(self->subs);
1343  
1344 0 self->pending_subs = 0;
1345 0 ▶ for(Size_t i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < n; i++) { 0 0 0
1346 /* TODO: This should probably use some API function to make it transparent */
1347 0 ▶ if(!future_is_ready(subs[i]))
Branch: 1 branch
BranchTrueFalseTotal
if(!future_is_ready(subs[i])) 0 0 0
1348 0 self->pending_subs++;
1349 }
1350  
1351 0 ▶ if(!self->pending_subs) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->pending_subs) { 0 0 0
1352 0 self->result = newAV_svn_dup(subs, n);
1353 0 mark_ready(self, f, "wait_all");
1354  
1355 0 return f;
1356 }
1357  
1358 0 CV *sub_on_ready = newXS(NULL, sub_on_ready_waitall, __FILE__);
1359 0 cv_set_anysv_refcounted(sub_on_ready, newSVsv(f));
1360 0 sv_rvweaken(CvXSUBANY_sv(sub_on_ready));
1361  
1362 0 GV *gv = gv_fetchpvs("Future::XS::(wait_all callback)", GV_ADDMULTI, SVt_PVCV);
1363 0 CvGV_set(sub_on_ready, gv);
1364 0 CvANON_off(sub_on_ready);
1365  
1366 0 ▶ for(Size_t i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < n; i++) { 0 0 0
1367 0 ▶ if(!future_is_ready(subs[i]))
Branch: 1 branch
BranchTrueFalseTotal
if(!future_is_ready(subs[i])) 0 0 0
1368 0 future_on_ready(subs[i], sv_2mortal(newRV_inc((SV *)sub_on_ready)));
1369 }
1370  
1371 0 SvREFCNT_dec(sub_on_ready);
1372  
1373 0 return f;
1374 }
1375  
1376 0 XS_INTERNAL(sub_on_ready_waitany)
1377 {
1378 0 dXSARGS;
1379 0 SV *thissub = ST(0);
1380  
1381 0 SV *f = XSANY_sv;
1382 0 ▶ if(!SvOK(f))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(f)) 0 0 0
1383 0 return;
1384  
1385 /* Make sure self doesn't disappear during this function */
1386 0 SvREFCNT_inc(SvRV(f));
1387 0 SAVEFREESV(SvRV(f));
1388  
1389 0 struct FutureXS *self = get_future(f);
1390  
1391 0 ▶ if(self->result || self->failure)
Branch: 2 branches
BranchTrueFalseTotal
if(self->result || self->failure) 0 0 0
if(self->result || self->failure) 0 0 0
1392 0 return;
1393  
1394 0 self->pending_subs--;
1395  
1396 0 bool this_cancelled = future_is_cancelled(thissub);
1397  
1398 0 ▶ if(self->pending_subs && this_cancelled)
Branch: 2 branches
BranchTrueFalseTotal
if(self->pending_subs && this_cancelled) 0 0 0
if(self->pending_subs && this_cancelled) 0 0 0
1399 0 return;
1400  
1401 0 ▶ if(this_cancelled) {
Branch: 1 branch
BranchTrueFalseTotal
if(this_cancelled) { 0 0 0
1402 0 future_failp(f, "All component futures were cancelled");
1403 0 return;
1404 }
1405 else
1406 0 copy_result(self, thissub);
1407  
1408 0 cancel_pending_subs(self);
1409  
1410 0 mark_ready(self, f, "wait_any");
1411 }
1412  
1413 0 SV *Future_new_waitanyv(pTHX_ const char *cls, SV **subs, size_t n)
1414 {
1415 0 SV *f = future_new_subsv(cls, subs, n);
1416 0 struct FutureXS *self = get_future(f);
1417  
1418 /* Reïnit subs + n */
1419 0 subs = AvARRAY(self->subs);
1420 0 n = av_count(self->subs);
1421  
1422 0 ▶ if(!n) {
Branch: 1 branch
BranchTrueFalseTotal
if(!n) { 0 0 0
1423 0 future_failp(f, "Cannot ->wait_any with no subfutures");
1424 0 return f;
1425 }
1426  
1427 0 SV *immediate_ready = NULL;
1428 0 ▶ for(Size_t i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < n; i++) { 0 0 0
1429 /* TODO: This should probably use some API function to make it transparent */
1430 0 ▶ if(future_is_ready(subs[i]) && !future_is_cancelled(subs[i])) {
Branch: 2 branches
BranchTrueFalseTotal
if(future_is_ready(subs[i]) && !future_is_cancelled(subs[i])) { 0 0 0
if(future_is_ready(subs[i]) && !future_is_cancelled(subs[i])) { 0 0 0
1431 0 immediate_ready = subs[i];
1432 0 break;
1433 }
1434 }
1435  
1436 0 ▶ if(immediate_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if(immediate_ready) { 0 0 0
1437 0 copy_result(self, immediate_ready);
1438  
1439 0 cancel_pending_subs(self);
1440  
1441 0 mark_ready(self, f, "wait_any");
1442  
1443 0 return f;
1444 }
1445  
1446 0 self->pending_subs = 0;
1447  
1448 0 CV *sub_on_ready = newXS(NULL, sub_on_ready_waitany, __FILE__);
1449 0 cv_set_anysv_refcounted(sub_on_ready, newSVsv(f));
1450 0 sv_rvweaken(CvXSUBANY_sv(sub_on_ready));
1451  
1452 0 GV *gv = gv_fetchpvs("Future::XS::(wait_any callback)", GV_ADDMULTI, SVt_PVCV);
1453 0 CvGV_set(sub_on_ready, gv);
1454 0 CvANON_off(sub_on_ready);
1455  
1456 0 ▶ for(Size_t i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < n; i++) { 0 0 0
1457 0 ▶ if(future_is_cancelled(subs[i]))
Branch: 1 branch
BranchTrueFalseTotal
if(future_is_cancelled(subs[i])) 0 0 0
1458 0 continue;
1459  
1460 0 future_on_ready(subs[i], sv_2mortal(newRV_inc((SV *)sub_on_ready)));
1461 0 self->pending_subs++;
1462 }
1463  
1464 0 SvREFCNT_dec(sub_on_ready);
1465  
1466 0 return f;
1467 }
1468  
1469 #define compose_needsall_result(self) S_compose_needsall_result(aTHX_ self)
1470 0 static void S_compose_needsall_result(pTHX_ struct FutureXS *self)
1471 {
1472 0 AV *result = self->result = newAV();
1473 0 ▶ for(Size_t i = 0; i < av_count(self->subs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < av_count(self->subs); i++) { 0 0 0
1474 0 SV *sub = AvARRAY(self->subs)[i];
1475 0 struct FutureXS *subself = get_future(sub);
1476 assert(subself->result);
1477 0 av_push_svn(result, AvARRAY(subself->result), av_count(subself->result));
1478 }
1479 0 }
1480  
1481 0 XS_INTERNAL(sub_on_ready_needsall)
1482 {
1483 0 dXSARGS;
1484 0 SV *thissub = ST(0);
1485  
1486 0 SV *f = XSANY_sv;
1487 0 ▶ if(!SvOK(f))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(f)) 0 0 0
1488 0 return;
1489  
1490 /* Make sure self doesn't disappear during this function */
1491 0 SvREFCNT_inc(SvRV(f));
1492 0 SAVEFREESV(SvRV(f));
1493  
1494 0 struct FutureXS *self = get_future(f);
1495  
1496 0 ▶ if(self->result || self->failure)
Branch: 2 branches
BranchTrueFalseTotal
if(self->result || self->failure) 0 0 0
if(self->result || self->failure) 0 0 0
1497 0 return;
1498  
1499 0 ▶ if(future_is_cancelled(thissub)) {
Branch: 1 branch
BranchTrueFalseTotal
if(future_is_cancelled(thissub)) { 0 0 0
1500 0 future_failp(f, "A component future was cancelled");
1501 0 cancel_pending_subs(self);
1502 0 return;
1503 }
1504 0 ▶ else if(future_is_failed(thissub)) {
Branch: 1 branch
BranchTrueFalseTotal
else if(future_is_failed(thissub)) { 0 0 0
1505 0 copy_result(self, thissub);
1506 0 cancel_pending_subs(self);
1507 0 mark_ready(self, f, "needs_all");
1508 }
1509 else {
1510 0 self->pending_subs--;
1511 0 ▶ if(self->pending_subs)
Branch: 1 branch
BranchTrueFalseTotal
if(self->pending_subs) 0 0 0
1512 0 return;
1513 0 compose_needsall_result(self);
1514 0 mark_ready(self, f, "needs_all");
1515 }
1516 }
1517  
1518 0 SV *Future_new_needsallv(pTHX_ const char *cls, SV **subs, size_t n)
1519 {
1520 0 SV *f = future_new_subsv(cls, subs, n);
1521 0 struct FutureXS *self = get_future(f);
1522  
1523 /* Reïnit subs + n */
1524 0 subs = AvARRAY(self->subs);
1525 0 n = av_count(self->subs);
1526  
1527 0 ▶ if(!n) {
Branch: 1 branch
BranchTrueFalseTotal
if(!n) { 0 0 0
1528 0 future_donev(f, NULL, 0);
1529 0 return f;
1530 }
1531  
1532 0 SV *immediate_fail = NULL;
1533 0 ▶ for(Size_t i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < n; i++) { 0 0 0
1534 0 ▶ if(future_is_cancelled(subs[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if(future_is_cancelled(subs[i])) { 0 0 0
1535 0 future_failp(f, "A component future was cancelled");
1536 0 cancel_pending_subs(self);
1537 0 return f;
1538 }
1539 0 ▶ if(future_is_failed(subs[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if(future_is_failed(subs[i])) { 0 0 0
1540 0 immediate_fail = subs[i];
1541 0 break;
1542 }
1543 }
1544  
1545 0 ▶ if(immediate_fail) {
Branch: 1 branch
BranchTrueFalseTotal
if(immediate_fail) { 0 0 0
1546 0 copy_result(self, immediate_fail);
1547 0 cancel_pending_subs(self);
1548 0 mark_ready(self, f, "needs_all");
1549 0 return f;
1550 }
1551  
1552 0 self->pending_subs = 0;
1553  
1554 0 CV *sub_on_ready = newXS(NULL, sub_on_ready_needsall, __FILE__);
1555 0 cv_set_anysv_refcounted(sub_on_ready, newSVsv(f));
1556 0 sv_rvweaken(CvXSUBANY_sv(sub_on_ready));
1557  
1558 0 GV *gv = gv_fetchpvs("Future::XS::(needs_all callback)", GV_ADDMULTI, SVt_PVCV);
1559 0 CvGV_set(sub_on_ready, gv);
1560 0 CvANON_off(sub_on_ready);
1561  
1562 0 ▶ for(Size_t i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < n; i++) { 0 0 0
1563 0 ▶ if(future_is_ready(subs[i]))
Branch: 1 branch
BranchTrueFalseTotal
if(future_is_ready(subs[i])) 0 0 0
1564 0 continue;
1565  
1566 0 future_on_ready(subs[i], sv_2mortal(newRV_inc((SV *)sub_on_ready)));
1567 0 self->pending_subs++;
1568 }
1569  
1570 0 ▶ if(!self->pending_subs) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->pending_subs) { 0 0 0
1571 0 compose_needsall_result(self);
1572 0 mark_ready(self, f, "needs_all");
1573 }
1574  
1575 0 SvREFCNT_dec(sub_on_ready);
1576  
1577 0 return f;
1578 }
1579  
1580 0 XS_INTERNAL(sub_on_ready_needsany)
1581 {
1582 0 dXSARGS;
1583 0 SV *thissub = ST(0);
1584  
1585 0 SV *f = XSANY_sv;
1586 0 ▶ if(!SvOK(f))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(f)) 0 0 0
1587 0 return;
1588  
1589 /* Make sure self doesn't disappear during this function */
1590 0 SvREFCNT_inc(SvRV(f));
1591 0 SAVEFREESV(SvRV(f));
1592  
1593 0 struct FutureXS *self = get_future(f);
1594  
1595 0 ▶ if(self->result || self->failure)
Branch: 2 branches
BranchTrueFalseTotal
if(self->result || self->failure) 0 0 0
if(self->result || self->failure) 0 0 0
1596 0 return;
1597  
1598 0 self->pending_subs--;
1599  
1600 0 bool this_cancelled = future_is_cancelled(thissub);
1601  
1602 0 ▶ if(self->pending_subs && this_cancelled)
Branch: 2 branches
BranchTrueFalseTotal
if(self->pending_subs && this_cancelled) 0 0 0
if(self->pending_subs && this_cancelled) 0 0 0
1603 0 return;
1604  
1605 0 ▶ if(this_cancelled) {
Branch: 1 branch
BranchTrueFalseTotal
if(this_cancelled) { 0 0 0
1606 0 future_failp(f, "All component futures were cancelled");
1607 }
1608 0 ▶ else if(future_is_failed(thissub)) {
Branch: 1 branch
BranchTrueFalseTotal
else if(future_is_failed(thissub)) { 0 0 0
1609 0 ▶ if(self->pending_subs)
Branch: 1 branch
BranchTrueFalseTotal
if(self->pending_subs) 0 0 0
1610 0 return;
1611  
1612 0 copy_result(self, thissub);
1613 0 mark_ready(self, f, "needs_any");
1614 }
1615 else {
1616 0 copy_result(self, thissub);
1617 0 cancel_pending_subs(self);
1618 0 mark_ready(self, f, "needs_any");
1619 }
1620 }
1621  
1622 0 SV *Future_new_needsanyv(pTHX_ const char *cls, SV **subs, size_t n)
1623 {
1624 0 SV *f = future_new_subsv(cls, subs, n);
1625 0 struct FutureXS *self = get_future(f);
1626  
1627 /* Reïnit subs + n */
1628 0 subs = AvARRAY(self->subs);
1629 0 n = av_count(self->subs);
1630  
1631 0 ▶ if(!n) {
Branch: 1 branch
BranchTrueFalseTotal
if(!n) { 0 0 0
1632 0 future_failp(f, "Cannot ->needs_any with no subfutures");
1633 0 return f;
1634 }
1635  
1636 0 SV *immediate_done = NULL;
1637 0 ▶ for(Size_t i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < n; i++) { 0 0 0
1638 0 ▶ if(future_is_done(subs[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if(future_is_done(subs[i])) { 0 0 0
1639 0 immediate_done = subs[i];
1640 0 break;
1641 }
1642 }
1643  
1644 0 ▶ if(immediate_done) {
Branch: 1 branch
BranchTrueFalseTotal
if(immediate_done) { 0 0 0
1645 0 copy_result(self, immediate_done);
1646 0 cancel_pending_subs(self);
1647 0 mark_ready(self, f, "needs_any");
1648 0 return f;
1649 }
1650  
1651 0 self->pending_subs = 0;
1652  
1653 0 CV *sub_on_ready = newXS(NULL, sub_on_ready_needsany, __FILE__);
1654 0 cv_set_anysv_refcounted(sub_on_ready, newSVsv(f));
1655 0 sv_rvweaken(CvXSUBANY_sv(sub_on_ready));
1656  
1657 0 GV *gv = gv_fetchpvs("Future::XS::(needs_any callback)", GV_ADDMULTI, SVt_PVCV);
1658 0 CvGV_set(sub_on_ready, gv);
1659 0 CvANON_off(sub_on_ready);
1660  
1661 0 ▶ for(Size_t i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(Size_t i = 0; i < n; i++) { 0 0 0
1662 0 ▶ if(future_is_ready(subs[i]))
Branch: 1 branch
BranchTrueFalseTotal
if(future_is_ready(subs[i])) 0 0 0
1663 0 continue;
1664  
1665 0 future_on_ready(subs[i], sv_2mortal(newRV_inc((SV *)sub_on_ready)));
1666 0 self->pending_subs++;
1667 }
1668  
1669 0 ▶ if(!self->pending_subs) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->pending_subs) { 0 0 0
1670 0 copy_result(self, subs[n-1]);
1671 0 mark_ready(self, f, "needs_any");
1672 }
1673  
1674 0 SvREFCNT_dec(sub_on_ready);
1675  
1676 0 return f;
1677 }
1678  
1679 0 Size_t Future_mPUSH_subs(pTHX_ SV *f, enum FutureSubFilter filter)
1680 {
1681 0 dSP;
1682  
1683 0 struct FutureXS *self = get_future(f);
1684  
1685 0 Size_t ret = 0;
1686 0 ▶ for(Size_t i = 0; self->subs && i < av_count(self->subs); i++) {
Branch: 2 branches
BranchTrueFalseTotal
for(Size_t i = 0; self->subs && i < av_count(self->subs); i++) { 0 0 0
for(Size_t i = 0; self->subs && i < av_count(self->subs); i++) { 0 0 0
1687 0 SV *sub = AvARRAY(self->subs)[i];
1688  
1689 bool want;
1690 0 switch(filter) {
1691 0 case FUTURE_SUBS_PENDING:
1692 0 want = !future_is_ready(sub);
1693 0 break;
1694  
1695 0 case FUTURE_SUBS_READY:
1696 0 want = future_is_ready(sub);
1697 0 break;
1698  
1699 0 case FUTURE_SUBS_DONE:
1700 0 want = future_is_done(sub);
1701 0 break;
1702  
1703 0 case FUTURE_SUBS_FAILED:
1704 0 want = future_is_failed(sub);
1705 0 break;
1706  
1707 0 case FUTURE_SUBS_CANCELLED:
1708 0 want = future_is_cancelled(sub);
1709 0 break;
1710 }
1711  
1712 0 ▶ if(want) {
Branch: 1 branch
BranchTrueFalseTotal
if(want) { 0 0 0
1713 0 ▶ XPUSHs(sv_mortalcopy(sub));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_mortalcopy(sub)); 0 0 0
1714 0 ret++;
1715 }
1716 }
1717  
1718 0 PUTBACK;
1719 0 return ret;
1720 }
1721  
1722 0 struct timeval Future_get_btime(pTHX_ SV *f)
1723 {
1724 0 struct FutureXS *self = get_future(f);
1725 0 return self->btime;
1726 }
1727  
1728 0 struct timeval Future_get_rtime(pTHX_ SV *f)
1729 {
1730 0 struct FutureXS *self = get_future(f);
1731 0 return self->rtime;
1732 }
1733  
1734 0 void Future_set_label(pTHX_ SV *f, SV *label)
1735 {
1736 0 struct FutureXS *self = get_future(f);
1737  
1738 0 ▶ if(self->label)
Branch: 1 branch
BranchTrueFalseTotal
if(self->label) 0 0 0
1739 0 SvREFCNT_dec(label);
1740  
1741 0 self->label = newSVsv(label);
1742 0 }
1743  
1744 0 SV *Future_get_label(pTHX_ SV *f)
1745 {
1746 0 struct FutureXS *self = get_future(f);
1747  
1748 0 return self->label;
1749 }
1750  
1751 0 void Future_set_udata(pTHX_ SV *f, SV *key, SV *value)
1752 {
1753 0 struct FutureXS *self = get_future(f);
1754  
1755 0 ▶ if(!self->udata)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->udata) 0 0 0
1756 0 self->udata = newHV();
1757  
1758 0 hv_store_ent(self->udata, key, newSVsv(value), 0);
1759 0 }
1760  
1761 0 SV *Future_get_udata(pTHX_ SV *f, SV *key)
1762 {
1763 0 struct FutureXS *self = get_future(f);
1764  
1765 0 ▶ if(!self->udata)
Branch: 1 branch
BranchTrueFalseTotal
if(!self->udata) 0 0 0
1766 0 return &PL_sv_undef;
1767  
1768 0 HE *he = hv_fetch_ent(self->udata, key, 0, 0);
1769 0 ▶ return he ? HeVAL(he) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return he ? HeVAL(he) : &PL_sv_undef; 0 0 0
1770 }
1771  
1772 /* DMD_HELPER assistants */
1773  
1774 #ifdef HAVE_DMD_HELPER
1775 static int dumpstruct_callback(pTHX_ DMDContext *ctx, struct FutureXSCallback *cb)
1776 {
1777 if(!(cb->flags & CB_SEQ_ANY))
1778 DMD_DUMP_STRUCT(ctx, "Future::XS/FutureXSCallback", cb, sizeof(struct FutureXSCallback),
1779 /* Some cheating here, to claim the "code" is either a CV or a Future,
1780 * depending on the CB_IS_FUTURE flag */
1781 3, ((const DMDNamedField []){
1782 {"flags", DMD_FIELD_UINT, .n = cb->flags},
1783 {"the code CV", DMD_FIELD_PTR, .ptr = (cb->flags & CB_IS_FUTURE) ? NULL : cb->code},
1784 {"the Future SV", DMD_FIELD_PTR, .ptr = (cb->flags & CB_IS_FUTURE) ? cb->code : NULL },
1785 })
1786 );
1787 else
1788 DMD_DUMP_STRUCT(ctx, "Future::XS/FutureXSCallback(CB_SEQ)", cb, sizeof(struct FutureXSCallback),
1789 4, ((const DMDNamedField []){
1790 {"flags", DMD_FIELD_UINT, .n = cb->flags},
1791 {"the then code CV", DMD_FIELD_PTR, .ptr = cb->seq.thencode},
1792 {"the else code CV", DMD_FIELD_PTR, .ptr = cb->seq.elsecode},
1793 {"the sequence future SV", DMD_FIELD_PTR, .ptr = cb->seq.f},
1794 })
1795 );
1796  
1797 return 0;
1798 }
1799  
1800 static int dumpstruct_revocation(pTHX_ DMDContext *ctx, struct FutureXSRevocation *rev)
1801 {
1802 DMD_DUMP_STRUCT(ctx, "Future::XS/FutureXSRevocation", rev, sizeof(struct FutureXSRevocation),
1803 2, ((const DMDNamedField []){
1804 {"the precedent future SV", DMD_FIELD_PTR, .ptr = rev->precedent_f},
1805 {"the SV to clear RV", DMD_FIELD_PTR, .ptr = rev->toclear_sv_at},
1806 })
1807 );
1808  
1809 return 0;
1810 }
1811  
1812 static int dumpstruct(pTHX_ DMDContext *ctx, const SV *sv)
1813 {
1814 int ret = 0;
1815  
1816 // TODO: Add some safety checking
1817 struct FutureXS *self = INT2PTR(struct FutureXS *, SvIV((SV *)sv));
1818  
1819 DMD_DUMP_STRUCT(ctx, "Future::XS/FutureXS", self, sizeof(struct FutureXS),
1820 12, ((const DMDNamedField []){
1821 {"ready", DMD_FIELD_BOOL, .b = self->ready},
1822 {"cancelled", DMD_FIELD_BOOL, .b = self->cancelled},
1823 {"the label SV", DMD_FIELD_PTR, .ptr = self->label},
1824 {"the result AV", DMD_FIELD_PTR, .ptr = self->result},
1825 {"the failure AV", DMD_FIELD_PTR, .ptr = self->failure},
1826 {"the callbacks AV", DMD_FIELD_PTR, .ptr = self->callbacks},
1827 {"the on_cancel AV", DMD_FIELD_PTR, .ptr = self->on_cancel},
1828 {"the revoke_when_ready AV", DMD_FIELD_PTR, .ptr = self->revoke_when_ready},
1829 {"the udata HV", DMD_FIELD_PTR, .ptr = self->udata},
1830 {"the constructed-at SV", DMD_FIELD_PTR, .ptr = self->constructed_at},
1831 {"the subs AV", DMD_FIELD_PTR, .ptr = self->subs},
1832 {"the pending sub count", DMD_FIELD_UINT, .n = self->pending_subs},
1833 })
1834 );
1835  
1836 for(size_t i = 0; self->callbacks && i < av_count(self->callbacks); i++) {
1837 struct FutureXSCallback *cb = (struct FutureXSCallback *)AvARRAY(self->callbacks)[i];
1838 ret += dumpstruct_callback(aTHX_ ctx, cb);
1839 }
1840  
1841 for(size_t i = 0; self->revoke_when_ready && i < av_count(self->revoke_when_ready); i++) {
1842 struct FutureXSRevocation *rev = (struct FutureXSRevocation *)AvARRAY(self->revoke_when_ready)[i];
1843 ret += dumpstruct_revocation(aTHX_ ctx, rev);
1844 }
1845  
1846 ret += DMD_ANNOTATE_SV(sv, (SV *)self, "the FutureXS structure");
1847  
1848 return ret;
1849 }
1850 #endif
1851  
1852 #define getenv_bool(key) S_getenv_bool(aTHX_ key)
1853 6 static bool S_getenv_bool(pTHX_ const char *key)
1854 {
1855 6 const char *val = getenv(key);
1856 6 ▶ if(!val || !val[0])
Branch: 2 branches
BranchTrueFalseTotal
if(!val || !val[0]) 0 6 6
if(!val || !val[0]) 0 0 0
1857 6 return false;
1858 0 ▶ if(val[0] == '0' && strlen(val) == 1)
Branch: 2 branches
BranchTrueFalseTotal
if(val[0] == '0' && strlen(val) == 1) 0 0 0
if(val[0] == '0' && strlen(val) == 1) 0 0 0
1859 0 return false;
1860 0 return true;
1861 }
1862  
1863 #ifndef newSVbool
1864 # define newSVbool(b) newSVsv(b ? &PL_sv_yes : &PL_sv_no)
1865 #endif
1866  
1867 3 void Future_reread_environment(pTHX)
1868 {
1869 3 future_debug = getenv_bool("PERL_FUTURE_DEBUG");
1870  
1871 3 ▶ capture_times = future_debug || getenv_bool("PERL_FUTURE_TIMES");
Branch: 2 branches
BranchTrueFalseTotal
capture_times = future_debug || getenv_bool("PERL_FUTURE_TIMES"); 3 0 3
capture_times = future_debug || getenv_bool("PERL_FUTURE_TIMES"); 0 3 3
1872 3 ▶ sv_setsv(get_sv("Future::TIMES", GV_ADDMULTI), capture_times ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(get_sv("Future::TIMES", GV_ADDMULTI), capture_times ? &PL_sv_yes : &PL_sv_no); 0 3 3
1873 3 }
1874  
1875 3 void Future_boot(pTHX)
1876 {
1877 #ifdef HAVE_DMD_HELPER
1878 DMD_SET_PACKAGE_HELPER("Future::XS", dumpstruct);
1879 #endif
1880  
1881 3 Future_reread_environment(aTHX);
1882  
1883 // We can only do this once
1884 3 newCONSTSUB(gv_stashpvn("Future::XS", 10, TRUE), "DEBUG", newSVbool(future_debug));
1885 3 }
« lib/Future/XS.xs ↑ Summary blib/lib/Future/XS.pm »