include/punk/punk_future.h

filter: Statementstmt 96.1% 359 / 385 (11 uncoverable) Branchbran 72.0% 233 / 368 (36 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.3% 592 / 753 (47 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_dispatch.h ↑ Summary include/punk/punk_reqid.h »
1 /* punk_future.h - Punk::Future, an async result, in C.
2 *
3 * A Future.pm-compatible value that runs on the Hyperman worker loop when one
4 * is live and blocks (synchronously) anywhere else, so app code can start a
5 * timer or defer a response and hand the future back - Punk's dispatcher
6 * already awaits any Future-compatible return (then / on_ready / get).
7 *
8 * Two backends behind one class: PF_LOOP wraps a companion Hyperman::Future
9 * (via hm_abi.h) purely as a run_until wait-token and arms timers on the loop;
10 * PF_BLOCK is self-contained (a settle + reaction queue, timers that sleep on
11 * get). The future *semantics* - state, the reaction queue, then-chaining and
12 * the combinators - are one implementation over the struct; the backend only
13 * drives settling and awaiting.
14 *
15 * Built in stages; this file currently covers the core (create / done / fail /
16 * state / on_ready|on_done|on_fail / get, block mode).
17 *
18 * Must be included after punk_context.h (pcx_* call helpers) and
19 * punk_wsconn.h (punk_hm, the ABI accessor).
20 */
21  
22 #ifndef PUNK_FUTURE_H
23 #define PUNK_FUTURE_H
24  
25 #include <sys/time.h> /* gettimeofday / select - the block-mode timer */
26  
27 /* settle states - the same integer values as HM_ABI_PENDING.. so a loop-mode
28 * future can mirror its companion's state without translation */
29 enum { PF_PENDING = 0, PF_DONE = 1, PF_FAILED = 2, PF_CANCELLED = 3 };
30  
31 /* reaction kinds queued against a pending future. ON_CANCEL is the odd one:
32 * what it holds may be a future rather than code, because Future.pm's
33 * on_cancel and the await protocol's AWAIT_CHAIN_CANCEL are one registration.
34 * These are internal - pk_abi.h publishes none of them. */
35 enum { PFR_ON_READY = 0, PFR_ON_DONE = 1, PFR_ON_FAIL = 2, PFR_ON_CANCEL = 3 };
36  
37 typedef struct punk_future {
38 int state; /* PF_* */
39 int is_loop; /* PF_LOOP vs PF_BLOCK */
40 AV *vals; /* the done values, or the failure values */
41 AV *reactions; /* flat [kind0, cb0, kind1, cb1, ...], lazily made */
42  
43 /* loop backend (stage D) */
44 const hm_abi *abi;
45 void *loop;
46 hm_abi_timer *timer; /* a live loop timer to cancel on settle/free */
47 SV *self_rv; /* strong self while a loop driver is pending */
48  
49 SV *on_fire; /* a defer callback to run when a loop timer fires */
50 } punk_future;
51  
52 static void pf_detect(pTHX_ punk_future *pf); /* stage D: pick the backend */
53  
54 /* Punk blesses several classes into an IV holding a struct pointer - SSE,
55 * Stream, Config, View, WebSocket - so SvROK && SvIOK alone would take any of
56 * them for a future and reinterpret the wrong struct. The class check is what
57 * makes a mistyped argument a croak rather than a silent misread. */
58 2316 static punk_future *pf_of(pTHX_ SV *self) {
59 /*
60 # uncoverable condition left count:1 class:ignore_covered_err
61 # uncoverable condition left count:2 class:ignore_covered_err */
62 2316 ▶ if (!SvROK(self) || !SvIOK(SvRV(self))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || !SvIOK(SvRV(self)) 2316 0 2316
if (!SvROK(self) || !SvIOK(SvRV(self)) 2316 0 2316
63 2316 ▶ || !sv_derived_from(self, "Punk::Future"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(self, "Punk::Future")) 1 2315 2316
64 1 croak("Punk::Future: not a future");
65 2315 return (punk_future *)INT2PTR(void *, SvIV(SvRV(self)));
66 }
67  
68 758 static punk_future *pf_new(pTHX) {
69 punk_future *pf;
70 758 Newxz(pf, 1, punk_future);
71 758 pf->state = PF_PENDING;
72 758 pf_detect(aTHX_ pf); /* loop-backed when a worker loop is live */
73 758 return pf;
74 }
75  
76 758 static SV *pf_bless(pTHX_ punk_future *pf, const char *class) {
77 758 return sv_setref_iv(newSV(0), class, PTR2IV(pf));
78 }
79  
80 /* The class to bless into, from a class-method or an instance-method
81 * invocant. SvPV_nolen of a blessed reference is "Punk::Future=SCALAR(0x..)",
82 * and sv_setref_iv will bless into a stash of that name quite happily - one
83 * with no DESTROY, so the struct leaks while pf_of still accepts the result.
84 * Every constructor takes its class through here. */
85 208 static const char *pf_class_of(pTHX_ SV *invocant) {
86 208 ▶ if (SvROK(invocant) && SvOBJECT(SvRV(invocant))) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(invocant) && SvOBJECT(SvRV(invocant))) /* # uncoverable condition right count:2 class:ignore_covered_err */ 11 197 208
if (SvROK(invocant) && SvOBJECT(SvRV(invocant))) /* # uncoverable condition right count:2 class:ignore_covered_err */ 11 0 11
87 11 ▶ return HvNAME(SvSTASH(SvRV(invocant)));
Branch: 6 branches
BranchTrueFalseTotal
return HvNAME(SvSTASH(SvRV(invocant))); 11 0 11
return HvNAME(SvSTASH(SvRV(invocant))); 11 0 11
return HvNAME(SvSTASH(SvRV(invocant))); 0 11 11
return HvNAME(SvSTASH(SvRV(invocant))); 0 0 0
return HvNAME(SvSTASH(SvRV(invocant))); 11 0 11
return HvNAME(SvSTASH(SvRV(invocant))); 0 11 11
88 197 return SvPV_nolen(invocant);
89 }
90  
91 /* call $cb->(@argv), trapping a die (a settled callback must not unwind the
92 * loop or the caller); a failure is warned, matching Future's "lost" report. */
93 354 static void pf_invoke(pTHX_ SV *cb, SV **argv, int argc) {
94 354 dSP;
95 int i;
96 354 ENTER; SAVETMPS;
97 354 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 354 354
98 354 ▶ EXTEND(SP, argc);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, argc); 354 0 354
EXTEND(SP, argc); 0 354 354
99 703 ▶ for (i = 0; i < argc; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) PUSHs(argv[i]); 349 354 703
100 354 PUTBACK;
101 354 call_sv(cb, G_VOID | G_EVAL | G_DISCARD);
102 354 SPAGAIN;
103 /*
104 # uncoverable branch true
105 # uncoverable condition right count:1 class:ignore_covered_err
106 # uncoverable condition left count:2 class:ignore_covered_err */
107 354 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 354 0 354
if (SvTRUE(ERRSV)) { 0 354 354
108 0 ▶ STRLEN el; const char *ep = SvPV_const(ERRSV, el); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
STRLEN el; const char *ep = SvPV_const(ERRSV, el); /* # uncoverable statement */ 0 0 0
109 0 ▶ warn("Punk::Future: callback died: %" SVf, /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
warn("Punk::Future: callback died: %" SVf, /* # uncoverable statement */ 0 0 0
110 SVfARG(pl_plain(aTHX_ ep, el, SvUTF8(ERRSV)))); /* one line */ /* # uncoverable statement */
111 }
112 354 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 354 354
113 354 }
114  
115 /* the values to hand a reaction of this kind: on_ready gets [$self], the
116 * others get the settled values. Returns argc; fills *argv (borrowed). */
117 354 static int pf_react_args(pTHX_ punk_future *pf, SV *self, int kind,
118 SV *self_slot[1], SV ***argv) {
119 /* a cancelled future has no values, so on_cancel gets $self as on_ready
120 * does - which is also what Future.pm hands a cancel callback */
121 354 ▶ if (kind == PFR_ON_READY || kind == PFR_ON_CANCEL) {
Branch: 2 branches
BranchTrueFalseTotal
if (kind == PFR_ON_READY || kind == PFR_ON_CANCEL) { 54 300 354
if (kind == PFR_ON_READY || kind == PFR_ON_CANCEL) { 5 49 54
122 305 self_slot[0] = self;
123 305 *argv = self_slot;
124 305 return 1;
125 }
126 49 ▶ *argv = pf->vals ? AvARRAY(pf->vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
*argv = pf->vals ? AvARRAY(pf->vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 49 0 49
127 49 ▶ return pf->vals ? (int)(av_len(pf->vals) + 1) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return pf->vals ? (int)(av_len(pf->vals) + 1) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 49 0 49
128 }
129  
130 /* run a single queued reaction if its kind matches the settled state */
131 368 static void pf_fire_one(pTHX_ punk_future *pf, SV *self, int kind, SV *cb) {
132 368 int run = (kind == PFR_ON_READY)
133 68 ▶ || (kind == PFR_ON_DONE && pf->state == PF_DONE)
Branch: 2 branches
BranchTrueFalseTotal
|| (kind == PFR_ON_DONE && pf->state == PF_DONE) 45 23 68
|| (kind == PFR_ON_DONE && pf->state == PF_DONE) 0 45 45
134 23 ▶ || (kind == PFR_ON_FAIL && pf->state == PF_FAILED)
Branch: 2 branches
BranchTrueFalseTotal
|| (kind == PFR_ON_FAIL && pf->state == PF_FAILED) 6 17 23
|| (kind == PFR_ON_FAIL && pf->state == PF_FAILED) 2 4 6
135 436 ▶ || (kind == PFR_ON_CANCEL && pf->state == PF_CANCELLED);
Branch: 3 branches
BranchTrueFalseTotal
|| (kind == PFR_ON_CANCEL && pf->state == PF_CANCELLED); 68 300 368
|| (kind == PFR_ON_CANCEL && pf->state == PF_CANCELLED); 17 2 19
|| (kind == PFR_ON_CANCEL && pf->state == PF_CANCELLED); 10 7 17
136 368 ▶ if (!run) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!run) return; 9 359 368
137 /* on_cancel holds code OR a future: a future is cancelled, not called.
138 * Its cancel goes out as a method rather than through pf_of, so a future
139 * of any class works and nothing the caller handed us is ever taken for
140 * one of ours. */
141 359 ▶ if (kind == PFR_ON_CANCEL && !(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (kind == PFR_ON_CANCEL && !(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 10 349 359
if (kind == PFR_ON_CANCEL && !(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 10 0 10
if (kind == PFR_ON_CANCEL && !(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 5 5 10
142 /*
143 # uncoverable branch false
144 # uncoverable condition right count:1 class:ignore_covered_err
145 # uncoverable condition right count:2 class:ignore_covered_err
146 # uncoverable condition right count:3 class:ignore_covered_err */
147 5 ▶ if (SvROK(cb) && SvOBJECT(SvRV(cb)) && pcx_can(aTHX_ cb, "cancel")) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(cb) && SvOBJECT(SvRV(cb)) && pcx_can(aTHX_ cb, "cancel")) { 5 0 5
if (SvROK(cb) && SvOBJECT(SvRV(cb)) && pcx_can(aTHX_ cb, "cancel")) { 5 0 5
if (SvROK(cb) && SvOBJECT(SvRV(cb)) && pcx_can(aTHX_ cb, "cancel")) { 5 0 5
148 5 SV *r = pcx_call_meth(aTHX_ cb, "cancel", NULL, 0, 0);
149 /*
150 # uncoverable branch true
151 # uncoverable condition left count:1 class:ignore_covered_err */
152 5 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 5 5
153 }
154 5 return;
155 }
156 {
157 SV *self_slot[1], **argv;
158 354 int argc = pf_react_args(aTHX_ pf, self, kind, self_slot, &argv);
159 354 pf_invoke(aTHX_ cb, argv, argc);
160 }
161 }
162  
163 /* drain the reaction queue after settling */
164 694 static void pf_fire(pTHX_ punk_future *pf, SV *self) {
165 694 AV *q = pf->reactions;
166 SSize_t i, n;
167 694 ▶ if (!q) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!q) return; 360 334 694
168 334 pf->reactions = NULL; /* detach: reactions added while firing
169 are fired immediately, not requeued */
170 334 n = av_len(q) + 1;
171 683 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 349 334 683
172 349 SV **kp = av_fetch(q, i, 0);
173 349 SV **cp = av_fetch(q, i + 1, 0);
174 /*
175 # uncoverable branch false
176 # uncoverable condition right count:1 class:ignore_covered_err
177 # uncoverable condition right count:2 class:ignore_covered_err
178 # uncoverable condition right count:3 class:ignore_covered_err
179 # uncoverable condition right count:4 class:ignore_covered_err */
180 349 ▶ if (kp && *kp && cp && *cp)
Branch: 4 branches
BranchTrueFalseTotal
if (kp && *kp && cp && *cp) 349 0 349
if (kp && *kp && cp && *cp) 349 0 349
if (kp && *kp && cp && *cp) 349 0 349
if (kp && *kp && cp && *cp) 349 0 349
181 349 pf_fire_one(aTHX_ pf, self, (int)SvIV(*kp), *cp);
182 }
183 334 SvREFCNT_dec((SV *)q);
184 }
185  
186 /* Settle a pending future (no-op if already settled, like the Hyperman ABI).
187 * Takes ownership of vals (an AV, or NULL for none). self is borrowed. */
188 694 static void pf_settle(pTHX_ punk_future *pf, SV *self, int state, AV *vals) {
189 /*
190 # uncoverable branch true
191 # uncoverable condition left count:1 class:ignore_covered_err
192 # uncoverable condition left count:2 class:ignore_covered_err
193 # uncoverable condition right count:2 class:ignore_covered_err */
194 694 ▶ if (pf->state != PF_PENDING) { if (vals) SvREFCNT_dec((SV *)vals); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (pf->state != PF_PENDING) { if (vals) SvREFCNT_dec((SV *)vals); return; } 0 694 694
if (pf->state != PF_PENDING) { if (vals) SvREFCNT_dec((SV *)vals); return; } 0 0 0
195 694 pf->state = state;
196 694 ▶ pf->vals = vals ? vals : newAV();
Branch: 1 branch
BranchTrueFalseTotal
pf->vals = vals ? vals : newAV(); 21 673 694
197 /*
198 # uncoverable branch true
199 # uncoverable condition left count:1 class:ignore_covered_err
200 # uncoverable condition left count:2 class:ignore_covered_err
201 # uncoverable condition right count:2 class:ignore_covered_err
202 # uncoverable condition left count:3 class:ignore_covered_err
203 # uncoverable condition right count:3 class:ignore_covered_err */
204 694 ▶ if (pf->timer && pf->abi && pf->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (pf->timer && pf->abi && pf->loop) { 0 694 694
if (pf->timer && pf->abi && pf->loop) { 0 0 0
if (pf->timer && pf->abi && pf->loop) { 0 0 0
205 0 pf->abi->timer_cancel(aTHX_ pf->loop, pf->timer); /* # uncoverable statement */
206 0 pf->timer = NULL; /* # uncoverable statement */
207 }
208 694 pf_fire(aTHX_ pf, self);
209 694 ▶ if (pf->self_rv) { SV *s = pf->self_rv; pf->self_rv = NULL; SvREFCNT_dec(s); }
Branch: 1 branch
BranchTrueFalseTotal
if (pf->self_rv) { SV *s = pf->self_rv; pf->self_rv = NULL; SvREFCNT_dec(s); } 21 673 694
210 }
211  
212 /* register a reaction; fire it at once if the future is already settled */
213 373 static void pf_react(pTHX_ punk_future *pf, SV *self, int kind, SV *cb) {
214 373 ▶ if (pf->state != PF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state != PF_PENDING) { 19 354 373
215 19 pf_fire_one(aTHX_ pf, self, kind, cb);
216 19 return;
217 }
218 354 ▶ if (!pf->reactions) pf->reactions = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!pf->reactions) pf->reactions = newAV(); 339 15 354
219 354 av_push(pf->reactions, newSViv(kind));
220 354 av_push(pf->reactions, newSVsv(cb));
221 }
222  
223 758 static void pf_free(pTHX_ punk_future *pf) {
224 /*
225 # uncoverable branch true
226 # uncoverable condition left count:1 class:ignore_covered_err */
227 758 ▶ if (!pf) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pf) return; 0 758 758
228 /*
229 # uncoverable branch true
230 # uncoverable condition left count:1 class:ignore_covered_err
231 # uncoverable condition left count:2 class:ignore_covered_err
232 # uncoverable condition right count:2 class:ignore_covered_err
233 # uncoverable condition left count:3 class:ignore_covered_err
234 # uncoverable condition right count:3 class:ignore_covered_err */
235 758 ▶ if (pf->timer && pf->abi && pf->loop)
Branch: 3 branches
BranchTrueFalseTotal
if (pf->timer && pf->abi && pf->loop) 0 758 758
if (pf->timer && pf->abi && pf->loop) 0 0 0
if (pf->timer && pf->abi && pf->loop) 0 0 0
236 0 pf->abi->timer_cancel(aTHX_ pf->loop, pf->timer); /* # uncoverable statement */
237 758 ▶ if (pf->vals) SvREFCNT_dec((SV *)pf->vals);
Branch: 1 branch
BranchTrueFalseTotal
if (pf->vals) SvREFCNT_dec((SV *)pf->vals); 694 64 758
238 758 ▶ if (pf->reactions) SvREFCNT_dec((SV *)pf->reactions);
Branch: 1 branch
BranchTrueFalseTotal
if (pf->reactions) SvREFCNT_dec((SV *)pf->reactions); 5 753 758
239 /*
240 # uncoverable branch true
241 # uncoverable condition left count:1 class:ignore_covered_err */
242 758 ▶ if (pf->self_rv) SvREFCNT_dec(pf->self_rv);
Branch: 1 branch
BranchTrueFalseTotal
if (pf->self_rv) SvREFCNT_dec(pf->self_rv); 0 758 758
243 /*
244 # uncoverable branch true
245 # uncoverable condition left count:1 class:ignore_covered_err */
246 758 ▶ if (pf->on_fire) SvREFCNT_dec(pf->on_fire);
Branch: 1 branch
BranchTrueFalseTotal
if (pf->on_fire) SvREFCNT_dec(pf->on_fire); 0 758 758
247 758 Safefree(pf);
248 }
249  
250 /* ---- chaining: then / else / followed_by (stage B) ------------------------ *
251 * Each returns a new future the callback's result settles; a callback that
252 * returns a future is adopted (the new future mirrors it). Reactions are
253 * ordinary on_ready callbacks - the chain logic is a captured C closure
254 * (punk_static.h's punk_closure), so no new reaction machinery is needed. */
255  
256 /* settle g (a Punk::Future) with argc borrowed values */
257 247 static void pf_settle_argv(pTHX_ SV *g, int state, SV **argv, int argc) {
258 247 AV *v = newAV();
259 int i;
260 492 ▶ for (i = 0; i < argc; i++) av_push(v, newSVsv(argv[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) av_push(v, newSVsv(argv[i])); 245 247 492
261 247 pf_settle(aTHX_ pf_of(aTHX_ g), g, state, v);
262 247 }
263  
264 /* blessed and future-compatible (has on_ready)? */
265 57 static int pf_is_future(pTHX_ SV *sv) {
266 57 ▶ return SvROK(sv) && SvOBJECT(SvRV(sv)) && pcx_can(aTHX_ sv, "on_ready"); /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
return SvROK(sv) && SvOBJECT(SvRV(sv)) && pcx_can(aTHX_ sv, "on_ready"); /* # uncoverable condition right count:3 class:ignore_covered_err */ 43 14 57
return SvROK(sv) && SvOBJECT(SvRV(sv)) && pcx_can(aTHX_ sv, "on_ready"); /* # uncoverable condition right count:3 class:ignore_covered_err */ 23 20 43
return SvROK(sv) && SvOBJECT(SvRV(sv)) && pcx_can(aTHX_ sv, "on_ready"); /* # uncoverable condition right count:3 class:ignore_covered_err */ 23 0 23
267 }
268  
269 /* call $cb->(@argv) in list context under a trap; push results onto out.
270 * Returns 1 if it died (ERRSV holds the error), else 0. */
271 49 static int pf_call_list(pTHX_ SV *cb, SV **argv, int argc, AV *out) {
272 49 dSP; int count, i, died;
273 49 ENTER; SAVETMPS;
274 49 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 49 49
275 49 ▶ EXTEND(SP, argc);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, argc); 49 0 49
EXTEND(SP, argc); 0 49 49
276 90 ▶ for (i = 0; i < argc; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) PUSHs(argv[i]); 41 49 90
277 49 PUTBACK;
278 49 count = call_sv(cb, G_ARRAY | G_EVAL);
279 49 SPAGAIN;
280 49 ▶ died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 49 0 49
281 93 ▶ if (!died) for (i = 0; i < count; i++) av_push(out, newSVsv(SP[1 - count + i]));
Branch: 2 branches
BranchTrueFalseTotal
if (!died) for (i = 0; i < count; i++) av_push(out, newSVsv(SP[1 - count + i])); 46 3 49
if (!died) for (i = 0; i < count; i++) av_push(out, newSVsv(SP[1 - count + i])); 44 46 90
282 49 SP -= count; PUTBACK;
283 49 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 47 2 49
284 49 return died;
285 }
286  
287 /* when a settled source R is adopted, mirror its outcome into g. Capture [g]. */
288 XS_INTERNAL(pf_adopt_cb);
289 11 XS_INTERNAL(pf_adopt_cb) {
290 11 dXSARGS;
291 11 AV *cap = punk_clos_cap(aTHX_ cv);
292 11 SV *g = *av_fetch(cap, 0, 0);
293 11 ▶ SV *R = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *R = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 11 0 11
294 11 SV *isf = pcx_call_meth(aTHX_ R, "is_failed", NULL, 0, 1);
295 11 ▶ SV *isd = isf && SvTRUE(isf) ? NULL
Branch: 1 branch
BranchTrueFalseTotal
SV *isd = isf && SvTRUE(isf) ? NULL 9 2 11
296 22 ▶ : pcx_call_meth(aTHX_ R, "is_done", NULL, 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
: pcx_call_meth(aTHX_ R, "is_done", NULL, 0, 1); 11 0 11
297 13 ▶ if (isf && SvTRUE(isf)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (isf && SvTRUE(isf)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 11 0 11
if (isf && SvTRUE(isf)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 9 11
298 2 SV *fl = pcx_call_meth(aTHX_ R, "failure", NULL, 0, 1);
299 2 ▶ SV *a = fl ? fl : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *a = fl ? fl : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 0 2
300 2 pf_settle_argv(aTHX_ g, PF_FAILED, &a, 1);
301 /*
302 # uncoverable branch false
303 # uncoverable condition right count:1 class:ignore_covered_err */
304 2 ▶ if (fl) SvREFCNT_dec(fl);
Branch: 1 branch
BranchTrueFalseTotal
if (fl) SvREFCNT_dec(fl); 2 0 2
305 }
306 /*
307 # uncoverable branch false
308 # uncoverable condition right count:1 class:ignore_covered_err
309 # uncoverable condition right count:2 class:ignore_covered_err */
310 18 ▶ else if (isd && SvTRUE(isd)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (isd && SvTRUE(isd)) { 9 0 9
else if (isd && SvTRUE(isd)) { 9 0 9
311 9 dSP; int count, i; AV *out = (AV *)sv_2mortal((SV *)newAV());
312 int n, j; SV **av2;
313 9 ENTER; SAVETMPS;
314 9 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(R); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(R); PUTBACK; 0 9 9
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(R); PUTBACK; 0 9 9
315 9 count = call_method("get", G_ARRAY | G_EVAL);
316 9 SPAGAIN;
317 /*
318 # uncoverable branch false
319 # uncoverable condition right count:1 class:ignore_covered_err
320 # uncoverable condition right count:2 class:ignore_covered_err */
321 9 ▶ if (!SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) 9 0 9
if (!SvTRUE(ERRSV)) 9 0 9
322 18 ▶ for (i = 0; i < count; i++) av_push(out, newSVsv(SP[1 - count + i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) av_push(out, newSVsv(SP[1 - count + i])); 9 9 18
323 9 ▶ SP -= count; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SP -= count; PUTBACK; FREETMPS; LEAVE; 9 0 9
324 9 n = (int)(av_len(out) + 1);
325 9 ▶ Newx(av2, n > 0 ? n : 1, SV *); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
Newx(av2, n > 0 ? n : 1, SV *); /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 0 9
326 18 ▶ for (j = 0; j < n; j++) av2[j] = *av_fetch(out, j, 0);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) av2[j] = *av_fetch(out, j, 0); 9 9 18
327 9 pf_settle_argv(aTHX_ g, PF_DONE, av2, n);
328 9 Safefree(av2);
329 }
330 else {
331 0 pf_settle(aTHX_ pf_of(aTHX_ g), g, PF_CANCELLED, NULL); /* # uncoverable statement */
332 }
333 /*
334 # uncoverable branch false
335 # uncoverable condition right count:1 class:ignore_covered_err */
336 11 ▶ if (isf) SvREFCNT_dec(isf);
Branch: 1 branch
BranchTrueFalseTotal
if (isf) SvREFCNT_dec(isf); 11 0 11
337 11 ▶ if (isd) SvREFCNT_dec(isd);
Branch: 1 branch
BranchTrueFalseTotal
if (isd) SvREFCNT_dec(isd); 9 2 11
338 11 XSRETURN_EMPTY;
339 }
340  
341 /* g adopts R: register R->on_ready to mirror R into g (fires now if settled) */
342 11 static void pf_adopt(pTHX_ SV *g, SV *R) {
343 11 AV *cap = newAV();
344 SV *clos, *argv[1], *r;
345 11 av_push(cap, newSVsv(g));
346 11 clos = sv_2mortal(punk_closure(aTHX_ pf_adopt_cb, cap));
347 11 argv[0] = clos;
348 11 r = pcx_call_meth(aTHX_ R, "on_ready", argv, 1, 0);
349 /*
350 # uncoverable branch true
351 # uncoverable condition left count:1 class:ignore_covered_err */
352 11 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 11 11
353 11 }
354  
355 /* the on_ready reaction a then/else/followed_by installs. Capture
356 * [g, on_done|undef, on_fail|undef, mode] (mode 1 = followed_by). */
357 XS_INTERNAL(pf_chain_cb);
358 56 XS_INTERNAL(pf_chain_cb) {
359 56 dXSARGS;
360 56 AV *cap = punk_clos_cap(aTHX_ cv);
361 56 SV *g = *av_fetch(cap, 0, 0);
362 56 SV *done_cb = *av_fetch(cap, 1, 0);
363 56 SV *fail_cb = *av_fetch(cap, 2, 0);
364 56 int mode = (int)SvIV(*av_fetch(cap, 3, 0));
365 56 ▶ SV *selff = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *selff = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 56 0 56
366 56 punk_future *sf = pf_of(aTHX_ selff);
367 56 SV *cb = NULL, **args = NULL, *self_slot[1];
368 56 int argc = 0;
369  
370 56 ▶ if (sf->state == PF_CANCELLED) {
Branch: 1 branch
BranchTrueFalseTotal
if (sf->state == PF_CANCELLED) { 2 54 56
371 2 pf_settle(aTHX_ pf_of(aTHX_ g), g, PF_CANCELLED, NULL);
372 2 XSRETURN_EMPTY;
373 }
374 54 ▶ if (mode == 1) { /* followed_by($self) */
Branch: 1 branch
BranchTrueFalseTotal
if (mode == 1) { /* followed_by($self) */ 2 52 54
375 2 cb = done_cb; self_slot[0] = selff; args = self_slot; argc = 1;
376 }
377 52 ▶ else if (sf->state == PF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (sf->state == PF_DONE) { 45 7 52
378 45 ▶ if (SvOK(done_cb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(done_cb)) { 43 2 45
379 43 cb = done_cb;
380 43 ▶ args = sf->vals ? AvARRAY(sf->vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
args = sf->vals ? AvARRAY(sf->vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 43 0 43
381 43 ▶ argc = sf->vals ? (int)(av_len(sf->vals) + 1) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
argc = sf->vals ? (int)(av_len(sf->vals) + 1) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 43 0 43
382 }
383 else { /* pass the value through */
384 2 pf_settle_argv(aTHX_ g, PF_DONE,
385 2 ▶ sf->vals ? AvARRAY(sf->vals) : NULL, /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
sf->vals ? AvARRAY(sf->vals) : NULL, /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
386 2 ▶ sf->vals ? (int)(av_len(sf->vals) + 1) : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
sf->vals ? (int)(av_len(sf->vals) + 1) : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
387 2 XSRETURN_EMPTY;
388 }
389 }
390 else { /* PF_FAILED */
391 7 ▶ if (SvOK(fail_cb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(fail_cb)) { 2 5 7
392 2 cb = fail_cb;
393 2 ▶ args = sf->vals ? AvARRAY(sf->vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
args = sf->vals ? AvARRAY(sf->vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
394 2 ▶ argc = sf->vals ? (int)(av_len(sf->vals) + 1) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
argc = sf->vals ? (int)(av_len(sf->vals) + 1) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
395 }
396 else { /* pass the failure through */
397 5 pf_settle_argv(aTHX_ g, PF_FAILED,
398 5 ▶ sf->vals ? AvARRAY(sf->vals) : NULL, /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
sf->vals ? AvARRAY(sf->vals) : NULL, /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
399 5 ▶ sf->vals ? (int)(av_len(sf->vals) + 1) : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
sf->vals ? (int)(av_len(sf->vals) + 1) : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
400 5 XSRETURN_EMPTY;
401 }
402 }
403 {
404 47 AV *out = (AV *)sv_2mortal((SV *)newAV());
405 47 int died = pf_call_list(aTHX_ cb, args, argc, out);
406 47 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 3 44 47
407 3 ▶ SV *e = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *e = sv_2mortal(newSVsv(ERRSV)); 3 0 3
408 3 pf_settle_argv(aTHX_ g, PF_FAILED, &e, 1);
409 }
410 else {
411 44 int n = (int)(av_len(out) + 1);
412 44 ▶ if (n == 1 && pf_is_future(aTHX_ *av_fetch(out, 0, 0)))
Branch: 2 branches
BranchTrueFalseTotal
if (n == 1 && pf_is_future(aTHX_ *av_fetch(out, 0, 0))) 42 2 44
if (n == 1 && pf_is_future(aTHX_ *av_fetch(out, 0, 0))) 11 31 42
413 11 pf_adopt(aTHX_ g, *av_fetch(out, 0, 0));
414 else {
415 int j; SV **av2;
416 33 ▶ Newx(av2, n > 0 ? n : 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(av2, n > 0 ? n : 1, SV *); 31 2 33
417 64 ▶ for (j = 0; j < n; j++) av2[j] = *av_fetch(out, j, 0);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) av2[j] = *av_fetch(out, j, 0); 31 33 64
418 33 pf_settle_argv(aTHX_ g, PF_DONE, av2, n);
419 33 Safefree(av2);
420 }
421 }
422 }
423 47 XSRETURN_EMPTY;
424 }
425  
426 /* build the derived future and wire the chain reaction; returns g (+1) */
427 56 static SV *pf_make_chain(pTHX_ SV *self, SV *done_cb, SV *fail_cb, int mode) {
428 56 punk_future *sf = pf_of(aTHX_ self);
429 56 punk_future *pg = pf_new(aTHX);
430 SV *g, *clos;
431 AV *cap;
432 56 pg->is_loop = sf->is_loop; pg->abi = sf->abi; pg->loop = sf->loop;
433 56 g = pf_bless(aTHX_ pg, "Punk::Future");
434 56 cap = newAV();
435 56 av_push(cap, newSVsv(g));
436 56 ▶ av_push(cap, (done_cb && SvOK(done_cb)) ? newSVsv(done_cb) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
av_push(cap, (done_cb && SvOK(done_cb)) ? newSVsv(done_cb) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 56 0 56
av_push(cap, (done_cb && SvOK(done_cb)) ? newSVsv(done_cb) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 52 4 56
437 56 ▶ av_push(cap, (fail_cb && SvOK(fail_cb)) ? newSVsv(fail_cb) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
av_push(cap, (fail_cb && SvOK(fail_cb)) ? newSVsv(fail_cb) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 56 0 56
av_push(cap, (fail_cb && SvOK(fail_cb)) ? newSVsv(fail_cb) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 50 56
438 56 av_push(cap, newSViv(mode));
439 56 clos = sv_2mortal(punk_closure(aTHX_ pf_chain_cb, cap));
440 56 pf_react(aTHX_ sf, self, PFR_ON_READY, clos);
441 56 return g;
442 }
443  
444 /* A new pending future of the same class. No per-instance state is copied -
445 * except the loop, which is not state but the only thing that can drive what
446 * is cloned from it. Future::AsyncAwait builds the future an async sub
447 * returns by cloning the one it suspended on, so a clone that lost its loop
448 * is a future nothing can settle and pf_await croaks on.
449 *
450 * pf_new has already run pf_detect, which reads the live worker loop - set
451 * for the whole of hm_loop_run - so inside a handler detect and inherit agree.
452 * They differ only at the edges, and each edge wants the opposite answer: a
453 * clone taken outside hm_loop_run needs the source's loop, and a clone of a
454 * future built before the worker loop came up needs the live one. Inheriting
455 * only when the source has one is right in both. */
456 11 static SV *pf_clone(pTHX_ SV *self) {
457 11 punk_future *sf = pf_of(aTHX_ self);
458 11 punk_future *pg = pf_new(aTHX);
459 11 ▶ if (sf->is_loop) { pg->is_loop = 1; pg->abi = sf->abi; pg->loop = sf->loop; }
Branch: 1 branch
BranchTrueFalseTotal
if (sf->is_loop) { pg->is_loop = 1; pg->abi = sf->abi; pg->loop = sf->loop; } 1 10 11
460 11 return pf_bless(aTHX_ pg, pf_class_of(aTHX_ self));
461 }
462  
463 /* ---- combinators: needs_all / needs_any / wait_all / wait_any (stage C) --- *
464 * A control AV [ G, mode, remaining, results, inputs, total ] is shared (by
465 * ref) across one on_ready reaction per input; each settling input updates it
466 * and settles G when the mode's condition is met. G's no-op-if-settled keeps
467 * "first wins" races safe. The control holds every input so none is freed while
468 * the combinator is pending. */
469 enum { PFC_NEEDS_ALL = 0, PFC_NEEDS_ANY = 1, PFC_WAIT_ALL = 2, PFC_WAIT_ANY = 3 };
470  
471 /* input->get in list context (input is settled done); push onto out */
472 16 static void pf_get_list(pTHX_ SV *input, AV *out) {
473 16 dSP; int count, i;
474 16 ENTER; SAVETMPS;
475 16 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(input); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(input); PUTBACK; 0 16 16
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(input); PUTBACK; 0 16 16
476 16 count = call_method("get", G_ARRAY | G_EVAL);
477 16 SPAGAIN;
478 /*
479 # uncoverable branch false
480 # uncoverable condition right count:1 class:ignore_covered_err
481 # uncoverable condition right count:2 class:ignore_covered_err */
482 16 ▶ if (!SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) 16 0 16
if (!SvTRUE(ERRSV)) 16 0 16
483 30 ▶ for (i = 0; i < count; i++) av_push(out, newSVsv(SP[1 - count + i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) av_push(out, newSVsv(SP[1 - count + i])); 14 16 30
484 16 ▶ SP -= count; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SP -= count; PUTBACK; FREETMPS; LEAVE; 16 0 16
485 16 }
486  
487 /* one input of a combinator settled; capture [control_ref, index] */
488 XS_INTERNAL(pf_comb_cb);
489 28 XS_INTERNAL(pf_comb_cb) {
490 28 dXSARGS;
491 28 AV *cap = punk_clos_cap(aTHX_ cv);
492 28 AV *ctl = (AV *)SvRV(*av_fetch(cap, 0, 0));
493 28 int idx = (int)SvIV(*av_fetch(cap, 1, 0));
494 28 SV *G = *av_fetch(ctl, 0, 0);
495 28 int mode = (int)SvIV(*av_fetch(ctl, 1, 0));
496 28 SV *remsv = *av_fetch(ctl, 2, 0);
497 28 ▶ SV *input = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *input = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 28 0 28
498 28 SV *isf = pcx_call_meth(aTHX_ input, "is_failed", NULL, 0, 1);
499 28 ▶ int failed = isf && SvTRUE(isf); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int failed = isf && SvTRUE(isf); /* # uncoverable condition right count:1 class:ignore_covered_err */ 28 0 28
int failed = isf && SvTRUE(isf); /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 18 28
500 /*
501 # uncoverable branch false
502 # uncoverable condition right count:1 class:ignore_covered_err */
503 28 ▶ if (isf) SvREFCNT_dec(isf);
Branch: 1 branch
BranchTrueFalseTotal
if (isf) SvREFCNT_dec(isf); 28 0 28
504  
505 28 ▶ if (mode == PFC_NEEDS_ALL) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == PFC_NEEDS_ALL) { 14 14 28
506 14 ▶ if (failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (failed) { 2 12 14
507 2 SV *fl = pcx_call_meth(aTHX_ input, "failure", NULL, 0, 1);
508 2 ▶ SV *a = fl ? fl : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *a = fl ? fl : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 0 2
509 2 pf_settle_argv(aTHX_ G, PF_FAILED, &a, 1);
510 /*
511 # uncoverable branch false
512 # uncoverable condition right count:1 class:ignore_covered_err */
513 2 ▶ if (fl) SvREFCNT_dec(fl);
Branch: 1 branch
BranchTrueFalseTotal
if (fl) SvREFCNT_dec(fl); 2 0 2
514 }
515 else {
516 12 AV *results = (AV *)SvRV(*av_fetch(ctl, 3, 0));
517 12 AV *v = newAV();
518 int rem;
519 12 pf_get_list(aTHX_ input, v);
520 12 (void)av_store(results, idx, newRV_noinc((SV *)v));
521 12 rem = (int)SvIV(remsv) - 1; sv_setiv(remsv, rem);
522 12 ▶ if (rem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rem == 0) { 5 7 12
523 5 AV *flat = newAV();
524 5 SSize_t i, n = av_len(results) + 1, j, m;
525 17 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12 5 17
526 12 SV **rp = av_fetch(results, i, 0);
527 /*
528 # uncoverable condition right count:1 class:ignore_covered_err
529 # uncoverable condition right count:2 class:ignore_covered_err
530 # uncoverable condition right count:3 class:ignore_covered_err */
531 12 ▶ AV *ra = (rp && *rp && SvROK(*rp)) ? (AV *)SvRV(*rp) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
AV *ra = (rp && *rp && SvROK(*rp)) ? (AV *)SvRV(*rp) : NULL; 12 0 12
AV *ra = (rp && *rp && SvROK(*rp)) ? (AV *)SvRV(*rp) : NULL; 12 0 12
AV *ra = (rp && *rp && SvROK(*rp)) ? (AV *)SvRV(*rp) : NULL; 12 0 12
532 12 ▶ m = ra ? av_len(ra) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
m = ra ? av_len(ra) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 0 12
533 22 ▶ for (j = 0; j < m; j++) av_push(flat, newSVsv(*av_fetch(ra, j, 0)));
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) av_push(flat, newSVsv(*av_fetch(ra, j, 0))); 10 12 22
534 }
535 5 pf_settle(aTHX_ pf_of(aTHX_ G), G, PF_DONE, flat);
536 }
537 }
538 }
539 14 ▶ else if (mode == PFC_NEEDS_ANY) {
Branch: 1 branch
BranchTrueFalseTotal
else if (mode == PFC_NEEDS_ANY) { 8 6 14
540 8 ▶ if (!failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!failed) { 4 4 8
541 4 AV *v = newAV();
542 4 pf_get_list(aTHX_ input, v);
543 4 pf_settle(aTHX_ pf_of(aTHX_ G), G, PF_DONE, v);
544 }
545 else {
546 4 int rem = (int)SvIV(remsv) - 1; sv_setiv(remsv, rem);
547 /*
548 # uncoverable branch true
549 # uncoverable condition left count:1 class:ignore_covered_err */
550 4 ▶ if (rem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rem == 0) { 0 4 4
551 0 SV *fl = pcx_call_meth(aTHX_ input, "failure", NULL, 0, 1); /* # uncoverable statement */
552 /*
553 # uncoverable statement
554 # uncoverable condition left count:1
555 # uncoverable condition right count:1 */
556 0 ▶ SV *a = fl ? fl : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *a = fl ? fl : &PL_sv_undef; 0 0 0
557 0 pf_settle_argv(aTHX_ G, PF_FAILED, &a, 1); /* # uncoverable statement */
558 /*
559 # uncoverable statement
560 # uncoverable branch true
561 # uncoverable branch false
562 # uncoverable condition left count:1
563 # uncoverable condition right count:1 */
564 0 ▶ if (fl) SvREFCNT_dec(fl);
Branch: 1 branch
BranchTrueFalseTotal
if (fl) SvREFCNT_dec(fl); 0 0 0
565 }
566 }
567 }
568 6 ▶ else if (mode == PFC_WAIT_ALL) {
Branch: 1 branch
BranchTrueFalseTotal
else if (mode == PFC_WAIT_ALL) { 4 2 6
569 4 int rem = (int)SvIV(remsv) - 1; sv_setiv(remsv, rem);
570 4 ▶ if (rem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rem == 0) { 2 2 4
571 2 AV *inputs = (AV *)SvRV(*av_fetch(ctl, 4, 0));
572 2 AV *copy = newAV();
573 2 SSize_t i, n = av_len(inputs) + 1;
574 6 ▶ for (i = 0; i < n; i++) av_push(copy, newSVsv(*av_fetch(inputs, i, 0)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(copy, newSVsv(*av_fetch(inputs, i, 0))); 4 2 6
575 2 pf_settle(aTHX_ pf_of(aTHX_ G), G, PF_DONE, copy);
576 }
577 }
578 else { /* PFC_WAIT_ANY */
579 2 SV *a = input;
580 2 pf_settle_argv(aTHX_ G, PF_DONE, &a, 1);
581 }
582 28 XSRETURN_EMPTY;
583 }
584  
585 /* build a combinator over @inputs (borrowed SV** of length n); returns G (+1).
586 * G is created by the caller (so it can pick the backend) and passed in. */
587 19 static void pf_combine(pTHX_ SV *G, int mode, SV **inputs, int n) {
588 19 AV *ctl = newAV();
589 19 AV *inav = newAV(), *results = newAV();
590 int i, rem;
591 51 ▶ for (i = 0; i < n; i++) av_push(inav, newSVsv(inputs[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(inav, newSVsv(inputs[i])); 32 19 51
592 19 ▶ rem = (mode == PFC_WAIT_ANY) ? (n ? 1 : 0) : n;
Branch: 1 branch
BranchTrueFalseTotal
rem = (mode == PFC_WAIT_ANY) ? (n ? 1 : 0) : n; 2 17 19
593 19 av_push(ctl, newSVsv(G)); /* 0 G */
594 19 av_push(ctl, newSViv(mode)); /* 1 mode */
595 19 av_push(ctl, newSViv(rem)); /* 2 remaining */
596 19 av_push(ctl, newRV_noinc((SV *)results)); /* 3 results */
597 19 av_push(ctl, newRV_noinc((SV *)inav)); /* 4 inputs */
598 19 av_push(ctl, newSViv(n)); /* 5 total */
599 19 ▶ if (n == 0) { /* degenerate cases */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { /* degenerate cases */ 4 15 19
600 4 ▶ if (mode == PFC_NEEDS_ANY)
Branch: 1 branch
BranchTrueFalseTotal
if (mode == PFC_NEEDS_ANY) 2 2 4
601 2 pf_settle(aTHX_ pf_of(aTHX_ G), G, PF_FAILED,
602 NULL); /* nothing can succeed */
603 else
604 2 pf_settle(aTHX_ pf_of(aTHX_ G), G, PF_DONE, newAV());
605 4 SvREFCNT_dec((SV *)ctl);
606 4 return;
607 }
608 {
609 15 SV *ctl_rv = sv_2mortal(newRV_noinc((SV *)ctl));
610 47 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 32 15 47
611 32 AV *cap = newAV();
612 SV *clos, *argv[1], *r;
613 32 av_push(cap, newSVsv(ctl_rv));
614 32 av_push(cap, newSViv(i));
615 32 clos = sv_2mortal(punk_closure(aTHX_ pf_comb_cb, cap));
616 32 argv[0] = clos;
617 32 r = pcx_call_meth(aTHX_ inputs[i], "on_ready", argv, 1, 0);
618 /*
619 # uncoverable branch true
620 # uncoverable condition left count:1 class:ignore_covered_err */
621 32 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 32 32
622 }
623 }
624 }
625  
626 /* ---- loop mode, timers and awaiting (stage D) ----------------------------- *
627 * A future is loop-backed when a Hyperman worker loop is live at creation. The
628 * loop only drives things: timers arm through the ABI, and get/await pump the
629 * loop through a companion Hyperman::Future used purely as a run_until token
630 * (the ABI cannot read a future's values back, so the wrapper's own struct
631 * stays authoritative). Off-loop a timer just sleeps and settles at once. */
632  
633 /* Asked once per future created, so it takes the resolved-table accessor
634 * rather than punk_hm, whose env check would be paid per future. */
635 758 static void pf_detect(pTHX_ punk_future *pf) {
636 758 const hm_abi *A = punk_hm_resolved(aTHX);
637 758 ▶ void *loop = A ? A->cur_loop(aTHX) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
void *loop = A ? A->cur_loop(aTHX) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 758 0 758
638 758 ▶ if (loop) { pf->is_loop = 1; pf->abi = A; pf->loop = loop; }
Branch: 1 branch
BranchTrueFalseTotal
if (loop) { pf->is_loop = 1; pf->abi = A; pf->loop = loop; } 318 440 758
639 758 }
640  
641 /*
642 # uncoverable statement
643 # uncoverable subroutine */
644 0 static double pf_now(void) {
645 struct timeval tv;
646 0 gettimeofday(&tv, NULL); /* # uncoverable statement */
647 0 return (double)tv.tv_sec + (double)tv.tv_usec / 1e6; /* # uncoverable statement */
648 }
649 6 static void pf_sleep(double secs) {
650 struct timeval tv;
651 6 ▶ if (secs <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (secs <= 0) return; 1 5 6
652 5 tv.tv_sec = (long)secs;
653 5 tv.tv_usec = (long)((secs - (double)tv.tv_sec) * 1e6);
654 5 (void)select(0, NULL, NULL, NULL, &tv);
655 }
656  
657 /* run $cb->(@args) and settle self with the result: a returned future is
658 * adopted, a die fails self, otherwise self is done with the values */
659 2 static void pf_run_into(pTHX_ SV *self, SV *cb, SV **args, int argc) {
660 2 AV *out = (AV *)sv_2mortal((SV *)newAV());
661 2 int died = pf_call_list(aTHX_ cb, args, argc, out);
662 /*
663 # uncoverable branch true
664 # uncoverable condition left count:1 class:ignore_covered_err */
665 2 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 0 2 2
666 0 ▶ SV *e = sv_2mortal(newSVsv(ERRSV)); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
SV *e = sv_2mortal(newSVsv(ERRSV)); /* # uncoverable statement */ 0 0 0
667 0 pf_settle_argv(aTHX_ self, PF_FAILED, &e, 1); /* # uncoverable statement */
668 }
669 else {
670 2 int n = (int)(av_len(out) + 1);
671 /*
672 # uncoverable branch true
673 # uncoverable condition left count:1 class:ignore_covered_err
674 # uncoverable condition right count:1 class:ignore_covered_err
675 # uncoverable condition left count:2 class:ignore_covered_err */
676 2 ▶ if (n == 1 && pf_is_future(aTHX_ *av_fetch(out, 0, 0)))
Branch: 2 branches
BranchTrueFalseTotal
if (n == 1 && pf_is_future(aTHX_ *av_fetch(out, 0, 0))) 2 0 2
if (n == 1 && pf_is_future(aTHX_ *av_fetch(out, 0, 0))) 0 2 2
677 0 pf_adopt(aTHX_ self, *av_fetch(out, 0, 0)); /* # uncoverable statement */
678 else {
679 int j; SV **a;
680 2 ▶ Newx(a, n > 0 ? n : 1, SV *); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
Newx(a, n > 0 ? n : 1, SV *); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
681 4 ▶ for (j = 0; j < n; j++) a[j] = *av_fetch(out, j, 0);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) a[j] = *av_fetch(out, j, 0); 2 2 4
682 2 pf_settle_argv(aTHX_ self, PF_DONE, a, n);
683 2 Safefree(a);
684 }
685 }
686 2 }
687  
688 /* a loop timer fired: run the defer callback if any, else settle done */
689 21 static void pf_timer_cb_in(pTHX_ void *ud) {
690 21 punk_future *pf = (punk_future *)ud;
691 21 SV *self = pf->self_rv;
692 21 pf->timer = NULL; /* it fired; nothing to cancel */
693 /*
694 # uncoverable branch true
695 # uncoverable condition left count:1 class:ignore_covered_err */
696 21 ▶ if (!self) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self) return; 0 21 21
697 /*
698 # uncoverable branch true
699 # uncoverable condition left count:1 class:ignore_covered_err */
700 21 ▶ if (pf->on_fire) pf_run_into(aTHX_ self, pf->on_fire, NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (pf->on_fire) pf_run_into(aTHX_ self, pf->on_fire, NULL, 0); 0 21 21
701 21 else pf_settle(aTHX_ pf, self, PF_DONE, newAV());
702 }
703  
704 21 static void pf_timer_cb(pTHX_ void *ud) {
705 21 PK_LOOP_ENTER;
706 21 pf_timer_cb_in(aTHX_ ud);
707 21 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 0 21 21
708 21 }
709  
710 21 static void pf_arm_timer(pTHX_ punk_future *pf, SV *self, double secs) {
711 21 pf->self_rv = newSVsv(self); /* keep alive until it fires */
712 21 pf->timer = pf->abi->timer(aTHX_ pf->loop, secs, pf_timer_cb, pf);
713 21 }
714  
715 /* settle a companion Hyperman::Future so a run_until awaiting it returns.
716 * Capture [ $companion ]. */
717 XS_INTERNAL(pf_wake_hf_cb);
718 185 XS_INTERNAL(pf_wake_hf_cb) {
719 185 dXSARGS;
720 185 AV *cap = punk_clos_cap(aTHX_ cv);
721 185 SV *hf = *av_fetch(cap, 0, 0);
722 185 const hm_abi *A = punk_hm(aTHX);
723 PERL_UNUSED_VAR(items);
724 /*
725 # uncoverable branch false
726 # uncoverable condition right count:1 class:ignore_covered_err
727 # uncoverable condition right count:2 class:ignore_covered_err */
728 185 ▶ if (A && A->is_future(aTHX_ hf)) A->future_done(aTHX_ hf, NULL, 0);
Branch: 2 branches
BranchTrueFalseTotal
if (A && A->is_future(aTHX_ hf)) A->future_done(aTHX_ hf, NULL, 0); 185 0 185
if (A && A->is_future(aTHX_ hf)) A->future_done(aTHX_ hf, NULL, 0); 185 0 185
729 185 XSRETURN_EMPTY;
730 }
731  
732 /* block until self settles: loop mode pumps via run_until, off-loop it is an
733 * error (nothing will settle a pending block-mode future but its own code) */
734 187 static void pf_await(pTHX_ SV *self) {
735 187 punk_future *pf = pf_of(aTHX_ self);
736 /*
737 # uncoverable branch true
738 # uncoverable condition left count:1 class:ignore_covered_err */
739 187 ▶ if (pf->state != PF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state != PF_PENDING) return; 0 187 187
740 /*
741 # uncoverable condition right count:2 class:ignore_covered_err
742 # uncoverable condition right count:3 class:ignore_covered_err */
743 371 ▶ if (pf->is_loop && pf->abi && pf->loop) {
Branch: 3 branches
BranchTrueFalseTotal
if (pf->is_loop && pf->abi && pf->loop) { 184 3 187
if (pf->is_loop && pf->abi && pf->loop) { 184 0 184
if (pf->is_loop && pf->abi && pf->loop) { 184 0 184
744 184 SV *hf = pf->abi->future_new(aTHX);
745 184 AV *cap = newAV();
746 SV *clos;
747 184 av_push(cap, newSVsv(hf));
748 184 clos = sv_2mortal(punk_closure(aTHX_ pf_wake_hf_cb, cap));
749 184 pf_react(aTHX_ pf, self, PFR_ON_READY, clos);
750 /*
751 # uncoverable branch false
752 # uncoverable condition right count:1 class:ignore_covered_err */
753 184 ▶ if (pf->state == PF_PENDING)
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_PENDING) 184 0 184
754 184 pf->abi->run_until(aTHX_ pf->loop, hf);
755 184 SvREFCNT_dec(hf);
756 }
757 else {
758 3 croak("Punk::Future: await on a pending future with no event loop "
759 "to drive it");
760 }
761 }
762  
763 /* The first value of a settled future, awaiting a pending one first: what
764 * `->get` yields in scalar context, croaking the same way, but reached
765 * without a perl method dispatch. Declared in punk_context.h, whose coerce
766 * path is the caller. Returns +1. */
767 /*
768 # uncoverable statement
769 # uncoverable subroutine */
770 0 static SV *pcx_pf_get1(pTHX_ SV *f) {
771 0 punk_future *pf = pf_of(aTHX_ f); /* # uncoverable statement */
772 SV **v;
773 /*
774 # uncoverable statement
775 # uncoverable branch true
776 # uncoverable branch false
777 # uncoverable condition left count:1
778 # uncoverable condition right count:1 */
779 0 ▶ if (pf->state == PF_PENDING) pf_await(aTHX_ f);
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_PENDING) pf_await(aTHX_ f); 0 0 0
780 /*
781 # uncoverable statement
782 # uncoverable branch true
783 # uncoverable branch false
784 # uncoverable condition left count:1
785 # uncoverable condition right count:1 */
786 0 ▶ if (pf->state == PF_CANCELLED)
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_CANCELLED) 0 0 0
787 0 croak("Punk::Future: get on a cancelled future"); /* # uncoverable statement */
788 /*
789 # uncoverable statement
790 # uncoverable branch true
791 # uncoverable branch false
792 # uncoverable condition left count:1
793 # uncoverable condition right count:1 */
794 0 ▶ if (pf->state == PF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_FAILED) { 0 0 0
795 /*
796 # uncoverable statement
797 # uncoverable condition left count:1
798 # uncoverable condition right count:1 */
799 0 ▶ SV **e = pf->vals ? av_fetch(pf->vals, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = pf->vals ? av_fetch(pf->vals, 0, 0) : NULL; 0 0 0
800 /*
801 # uncoverable statement
802 # uncoverable condition left count:1
803 # uncoverable condition right count:1
804 # uncoverable condition left count:2
805 # uncoverable condition right count:2 */
806 0 ▶ croak_sv(e && *e ? *e : sv_2mortal(newSVpvs("Punk::Future failed")));
Branch: 2 branches
BranchTrueFalseTotal
croak_sv(e && *e ? *e : sv_2mortal(newSVpvs("Punk::Future failed"))); 0 0 0
croak_sv(e && *e ? *e : sv_2mortal(newSVpvs("Punk::Future failed"))); 0 0 0
807 }
808 /*
809 # uncoverable statement
810 # uncoverable condition left count:1
811 # uncoverable condition right count:1 */
812 0 ▶ v = pf->vals ? av_fetch(pf->vals, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
v = pf->vals ? av_fetch(pf->vals, 0, 0) : NULL; 0 0 0
813 /*
814 # uncoverable statement
815 # uncoverable condition left count:1
816 # uncoverable condition right count:1
817 # uncoverable condition left count:2
818 # uncoverable condition right count:2 */
819 0 ▶ return (v && *v) ? newSVsv(*v) : newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
return (v && *v) ? newSVsv(*v) : newSV(0); 0 0 0
return (v && *v) ? newSVsv(*v) : newSV(0); 0 0 0
820 }
821  
822 /* a future that settles after secs: a loop timer, or a plain sleep off-loop */
823 27 static SV *pf_make_timer(pTHX_ double secs) {
824 27 punk_future *pf = pf_new(aTHX);
825 27 SV *self = pf_bless(aTHX_ pf, "Punk::Future");
826 27 ▶ if (pf->is_loop) pf_arm_timer(aTHX_ pf, self, secs);
Branch: 1 branch
BranchTrueFalseTotal
if (pf->is_loop) pf_arm_timer(aTHX_ pf, self, secs); 21 6 27
827 6 else { pf_sleep(secs); pf_settle(aTHX_ pf, self, PF_DONE, newAV()); }
828 27 return self;
829 }
830  
831 /* run $cb on the next loop tick (a 0s timer), or now off-loop; the future
832 * settles with its result */
833 2 static SV *pf_make_defer(pTHX_ SV *cb) {
834 2 punk_future *pf = pf_new(aTHX);
835 2 SV *self = pf_bless(aTHX_ pf, "Punk::Future");
836 /*
837 # uncoverable branch true
838 # uncoverable condition left count:1 class:ignore_covered_err */
839 2 ▶ if (pf->is_loop) { pf->on_fire = newSVsv(cb); pf_arm_timer(aTHX_ pf, self, 0); }
Branch: 1 branch
BranchTrueFalseTotal
if (pf->is_loop) { pf->on_fire = newSVsv(cb); pf_arm_timer(aTHX_ pf, self, 0); } 0 2 2
840 2 else { pf_run_into(aTHX_ self, cb, NULL, 0); }
841 2 return self;
842 }
843  
844 #endif /* PUNK_FUTURE_H */
« include/punk/punk_dispatch.h ↑ Summary include/punk/punk_reqid.h »