include/punk/punk_future.h

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