xs/future.xs

filter: Statementstmt 94.8% 73 / 77 Branchbran 73.2% 63 / 86 (2 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.4% 136 / 163 (2 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/ratelimit.xs ↑ Summary include/punk/punk_cookie.h »
1 MODULE = Punk PACKAGE = Punk::Future
2  
3 PROTOTYPES: DISABLE
4  
5 # Punk::Future, in C (punk_future.h). A Future.pm-compatible async result that
6 # runs on the Hyperman loop when live and blocks otherwise. lib/Punk/Future.pm
7 # is documentation. (Core stage: create / settle / state / callbacks / get.)
8  
9 SV *
10 new(class)
11 SV *class
12 CODE:
13 138 RETVAL = pf_bless(aTHX_ pf_new(aTHX), pf_class_of(aTHX_ class));
14 OUTPUT:
15 RETVAL
16  
17 # done(@values) / fail(@failure): settle a pending future. Chains.
18 SV *
19 done(self, ...)
20 SV *self
21 ALIAS:
22 fail = 1
23 AWAIT_DONE = 2
24 AWAIT_FAIL = 3
25 CODE:
26 {
27 76 punk_future *pf = pf_of(aTHX_ self);
28 76 AV *vals = newAV();
29 int i;
30 148 ▶ for (i = 1; i < items; i++) av_push(vals, newSVsv(ST(i)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) av_push(vals, newSVsv(ST(i))); 72 76 148
31 /* the low bit of ix is the outcome, so the AWAIT_ spellings pair off
32 * with the ones they alias */
33 76 ▶ pf_settle(aTHX_ pf, self, (ix & 1) ? PF_FAILED : PF_DONE, vals);
Branch: 1 branch
BranchTrueFalseTotal
pf_settle(aTHX_ pf, self, (ix & 1) ? PF_FAILED : PF_DONE, vals); 18 58 76
34 76 RETVAL = newSVsv(self);
35 }
36 OUTPUT:
37 RETVAL
38  
39 IV
40 is_ready(self)
41 SV *self
42 ALIAS:
43 is_done = 1
44 is_failed = 2
45 is_cancelled = 3
46 AWAIT_IS_READY = 4
47 AWAIT_IS_CANCELLED = 5
48 CODE:
49 {
50 128 int st = pf_of(aTHX_ self)->state;
51 /* a switch, not a ternary chain: the chain's last arm was a
52 * fallthrough that would quietly answer for any ix added later */
53 127 switch (ix) {
54 17 case 1: RETVAL = (st == PF_DONE); break;
55 45 case 2: RETVAL = (st == PF_FAILED); break;
56 21 case 3: case 5: RETVAL = (st == PF_CANCELLED); break;
57 44 default: RETVAL = (st != PF_PENDING); break; /* 0, 4 */
58 }
59 }
60 OUTPUT:
61 RETVAL
62  
63 IV
64 state(self)
65 SV *self
66 CODE:
67 2 ▶ RETVAL = pf_of(aTHX_ self)->state;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = pf_of(aTHX_ self)->state; 0 2 2
68 OUTPUT:
69 RETVAL
70  
71 # on_ready($cb) fires $cb->($future) on settle; on_done/on_fail fire
72 # $cb->(@values) for the matching outcome. Fire at once if already settled.
73 SV *
74 on_ready(self, thing)
75 SV *self
76 SV *thing
77 ALIAS:
78 on_done = 1
79 on_fail = 2
80 on_cancel = 3
81 AWAIT_ON_READY = 4
82 CODE:
83 {
84 /* ix stops being the reaction kind here. It was PFR_* by coincidence
85 * of declaration order, which is not something an alias list should
86 * have to know: an alias landing on the wrong number would register a
87 * reaction that never fires, and a never-fired reaction is a hang with
88 * nothing to report. */
89 static const int kind[] = {
90 PFR_ON_READY, /* 0 on_ready */
91 PFR_ON_DONE, /* 1 on_done */
92 PFR_ON_FAIL, /* 2 on_fail */
93 PFR_ON_CANCEL, /* 3 on_cancel */
94 PFR_ON_READY /* 4 AWAIT_ON_READY */
95 };
96 108 pf_react(aTHX_ pf_of(aTHX_ self), self, kind[ix], thing);
97 108 RETVAL = newSVsv(self);
98 }
99 OUTPUT:
100 RETVAL
101  
102 # The protocol's two cancel registrations. They are a separate body from
103 # on_cancel rather than two more aliases because they must return NOTHING:
104 # these are called by Future::AsyncAwait on the future it is about to hand
105 # back, and replacing the invocant on the argument stack with a fresh copy of
106 # it - which is what returning `$self` does here - corrupts that future.
107 # The protocol defines no return value for either, so there is none.
108 void
109 AWAIT_ON_CANCEL(self, thing)
110 SV *self
111 SV *thing
112 ALIAS:
113 AWAIT_CHAIN_CANCEL = 1
114 CODE:
115 PERL_UNUSED_VAR(ix);
116 12 pf_react(aTHX_ pf_of(aTHX_ self), self, PFR_ON_CANCEL, thing);
117  
118 # get / result: block until ready, then return the values (or die the failure).
119 # AWAIT_WAIT is the same thing - it is what a toplevel await resolves to.
120 # AWAIT_GET is defined only on a future that is already ready, so it reads
121 # where the others wait; the two croaks stay distinguishable on purpose.
122 void
123 get(self)
124 SV *self
125 ALIAS:
126 result = 1
127 AWAIT_WAIT = 2
128 AWAIT_GET = 3
129 PPCODE:
130 {
131 275 punk_future *pf = pf_of(aTHX_ self);
132 SSize_t i, n;
133 275 ▶ if (ix == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 3) { 25 250 275
134 25 ▶ if (pf->state == PF_PENDING)
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_PENDING) 1 24 25
135 1 croak("Punk::Future: AWAIT_GET on a future that is not ready");
136 }
137 250 ▶ else if (pf->state == PF_PENDING) pf_await(aTHX_ self); /* pump/sleep */
Branch: 1 branch
BranchTrueFalseTotal
else if (pf->state == PF_PENDING) pf_await(aTHX_ self); /* pump/sleep */ 177 73 250
138 /* pf_await ran the loop, and anything on it may have grown the value
139 * stack; a grown stack is reallocated, so the SP captured on entry
140 * points into freed memory. Re-derive it before pushing. */
141 271 SP = PL_stack_base + ax - 1;
142 271 ▶ if (pf->state == PF_CANCELLED)
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_CANCELLED) 1 270 271
143 1 croak("Punk::Future: get on a cancelled future");
144 270 ▶ if (pf->state == PF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_FAILED) { 16 254 270
145 16 ▶ SV **f = pf->vals ? av_fetch(pf->vals, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **f = pf->vals ? av_fetch(pf->vals, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 16 0 16
146 16 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 16 0 16
# uncoverable condition right count:1 class:ignore_covered_err 16 0 16
147 # uncoverable condition right count:2 class:ignore_covered_err
148 254 ▶ croak_sv(f && *f ? *f : sv_2mortal(newSVpvs("Punk::Future failed")));
Branch: 1 branch
BranchTrueFalseTotal
croak_sv(f && *f ? *f : sv_2mortal(newSVpvs("Punk::Future failed"))); 254 0 254
149 }
150 n = pf->vals ? av_len(pf->vals) + 1 : 0;
151 /* Scalar context hands back the FIRST value, as Future.pm does. An
152 * XSUB otherwise returns whatever was pushed last, which made a
153 254 ▶ * multi-value future yield its tail - and punk_coerce and
Branch: 1 branch
BranchTrueFalseTotal
* multi-value future yield its tail - and punk_coerce and 77 177 254
154 77 ▶ * _finish_future both call this in scalar context. */
Branch: 1 branch
BranchTrueFalseTotal
* _finish_future both call this in scalar context. */ 74 3 77
155 77 ▶ if (GIMME_V != G_ARRAY) {
Branch: 3 branches
BranchTrueFalseTotal
if (GIMME_V != G_ARRAY) { 0 77 77
if (GIMME_V != G_ARRAY) { 74 3 77
if (GIMME_V != G_ARRAY) { 74 0 74
156 SV **f = n ? av_fetch(pf->vals, 0, 0) : NULL;
157 XPUSHs(f && *f ? sv_2mortal(newSVsv(*f)) : &PL_sv_undef); /* # uncoverable condition right count:2 class:ignore_covered_err */
158 177 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 177 0 177
} 0 177 177
159 361 ▶ else {
Branch: 1 branch
BranchTrueFalseTotal
else { 184 177 361
160 184 EXTEND(SP, n);
161 for (i = 0; i < n; i++)
162 PUSHs(sv_2mortal(newSVsv(*av_fetch(pf->vals, i, 0))));
163 }
164 }
165  
166 # the first failure value of a failed future, else undef
167 SV *
168 failure(self)
169 SV *self
170 CODE:
171 {
172 58 punk_future *pf = pf_of(aTHX_ self);
173 5 ▶ SV **f = (pf->state == PF_FAILED && pf->vals)
Branch: 1 branch
BranchTrueFalseTotal
SV **f = (pf->state == PF_FAILED && pf->vals) 5 0 5
174 63 ▶ ? av_fetch(pf->vals, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? av_fetch(pf->vals, 0, 0) : NULL; 5 53 58
175 58 ▶ RETVAL = (f && *f) ? newSVsv(*f) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (f && *f) ? newSVsv(*f) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 53 58
RETVAL = (f && *f) ? newSVsv(*f) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
176 }
177 OUTPUT:
178 RETVAL
179  
180 # then($on_done, $on_fail?): a new future the callback's result settles.
181 # The else/catch($on_fail) pair takes the failure branch only, and
182 # followed_by($cb) calls $cb->($self) on any outcome. A callback that
183 # returns a future is adopted.
184 #
185 # Keep 'else' off the start of a comment line here: xsubpp matches
186 # /^#[ \t]*(if|ifn?def|elif|else|endif)\b/ before it decides a '#' line is a
187 # comment, so '# else/catch(...)' parsed as a bare #else and failed the build
188 # with "'else' with no matching 'if'".
189 SV *
190 then(self, on_done, on_fail = &PL_sv_undef)
191 SV *self
192 SV *on_done
193 SV *on_fail
194 CODE:
195 50 RETVAL = pf_make_chain(aTHX_ self, on_done, on_fail, 0);
196 OUTPUT:
197 RETVAL
198  
199 # named _pf_else because an XSUB literally named `else` makes xsubpp emit a
200 # bare C `else`; the public names are the aliases.
201 SV *
202 _pf_else(self, on_fail)
203 SV *self
204 SV *on_fail
205 ALIAS:
206 else = 1
207 catch = 2
208 CODE:
209 PERL_UNUSED_VAR(ix);
210 4 RETVAL = pf_make_chain(aTHX_ self, &PL_sv_undef, on_fail, 0);
211 OUTPUT:
212 RETVAL
213  
214 SV *
215 followed_by(self, cb)
216 SV *self
217 SV *cb
218 CODE:
219 2 RETVAL = pf_make_chain(aTHX_ self, cb, &PL_sv_undef, 1);
220 OUTPUT:
221 RETVAL
222  
223 # A new pending future of the same class, carrying the source's loop. This is
224 # how an async sub gets the future it returns - see pf_clone for why the loop
225 # travels with it.
226 SV *
227 AWAIT_CLONE(self)
228 SV *self
229 CODE:
230 11 RETVAL = pf_clone(aTHX_ self);
231 OUTPUT:
232 RETVAL
233  
234 # done_future(@v) / fail_future(@v): an already-settled future.
235 SV *
236 done_future(class, ...)
237 SV *class
238 ALIAS:
239 fail_future = 1
240 AWAIT_NEW_DONE = 2
241 AWAIT_NEW_FAIL = 3
242 CODE:
243 {
244 40 punk_future *pf = pf_new(aTHX);
245 40 AV *vals = newAV();
246 int i;
247 40 RETVAL = pf_bless(aTHX_ pf, pf_class_of(aTHX_ class));
248 81 ▶ for (i = 1; i < items; i++) av_push(vals, newSVsv(ST(i)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) av_push(vals, newSVsv(ST(i))); 41 40 81
249 /* The protocol never calls AWAIT_NEW_FAIL with no message, but a
250 * failure with no failure value is not a failure. Only the AWAIT_
251 * spelling gets the default; fail_future's own behaviour is left
252 * exactly as it was. */
253 40 ▶ if (ix == 3 && items < 2) av_push(vals, newSVpvs("Failed\n"));
Branch: 2 branches
BranchTrueFalseTotal
if (ix == 3 && items < 2) av_push(vals, newSVpvs("Failed\n")); 3 37 40
if (ix == 3 && items < 2) av_push(vals, newSVpvs("Failed\n")); 1 2 3
254 40 ▶ pf_settle(aTHX_ pf, RETVAL, (ix & 1) ? PF_FAILED : PF_DONE, vals);
Branch: 1 branch
BranchTrueFalseTotal
pf_settle(aTHX_ pf, RETVAL, (ix & 1) ? PF_FAILED : PF_DONE, vals); 10 30 40
255 }
256 OUTPUT:
257 RETVAL
258  
259 # needs_all/needs_any/wait_all/wait_any(@futures), and all/any aliases.
260 SV *
261 needs_all(class, ...)
262 SV *class
263 ALIAS:
264 needs_any = 1
265 wait_all = 2
266 wait_any = 3
267 all = 4
268 any = 5
269 CODE:
270 {
271 19 int mode = (ix == 4) ? PFC_NEEDS_ALL
272 19 ▶ : (ix == 5) ? PFC_NEEDS_ANY : (int)ix;
Branch: 2 branches
BranchTrueFalseTotal
: (ix == 5) ? PFC_NEEDS_ANY : (int)ix; 15 4 19
: (ix == 5) ? PFC_NEEDS_ANY : (int)ix; 11 4 15
273 19 int n = items - 1, i;
274 SV **inputs, *G;
275 19 G = pf_bless(aTHX_ pf_new(aTHX), pf_class_of(aTHX_ class));
276 19 ▶ Newx(inputs, n > 0 ? n : 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(inputs, n > 0 ? n : 1, SV *); 15 4 19
277 51 ▶ for (i = 0; i < n; i++) inputs[i] = ST(i + 1);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) inputs[i] = ST(i + 1); 32 19 51
278 19 pf_combine(aTHX_ G, mode, inputs, n);
279 19 Safefree(inputs);
280 19 RETVAL = G;
281 }
282 OUTPUT:
283 RETVAL
284  
285 # timer($secs): a future that settles after $secs (a loop timer, or a sleep
286 # off-loop). defer($cb): run $cb on the next tick; the future settles with its
287 # result. await: block until ready (pumping the loop), returning the future.
288 SV *
289 timer(class, secs)
290 SV *class
291 NV secs
292 CODE:
293 PERL_UNUSED_VAR(class);
294 3 RETVAL = pf_make_timer(aTHX_ (double)secs);
295 OUTPUT:
296 RETVAL
297  
298 SV *
299 defer(class, cb)
300 SV *class
301 SV *cb
302 CODE:
303 PERL_UNUSED_VAR(class);
304 2 RETVAL = pf_make_defer(aTHX_ cb);
305 OUTPUT:
306 RETVAL
307  
308 SV *
309 # uncoverable statement
310 # uncoverable subroutine
311 await(self)
312 SV *self
313 CODE:
314 0 pf_await(aTHX_ self); /* # uncoverable statement */
315 0 RETVAL = newSVsv(self); /* # uncoverable statement */
316 OUTPUT:
317 RETVAL
318  
319 # cancel: settle a pending future cancelled (its on_ready callbacks and any
320 # then-chain fire, the chain propagating the cancellation onward). Chains.
321 SV *
322 cancel(self)
323 SV *self
324 CODE:
325 {
326 17 punk_future *pf = pf_of(aTHX_ self);
327 17 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 17 0 17
328 17 # uncoverable condition right count:1 class:ignore_covered_err
329 if (pf->state == PF_PENDING) pf_settle(aTHX_ pf, self, PF_CANCELLED, NULL);
330 RETVAL = newSVsv(self);
331 }
332 OUTPUT:
333 RETVAL
334  
335 void
336 DESTROY(self)
337 SV *self
338 CODE:
339 758 pf_free(aTHX_ pf_of(aTHX_ self));
340  
341 MODULE = Punk PACKAGE = Punk::Context
342  
343 # Per-request conveniences: $c->promise (a new pending future), $c->timer($s) /
344 # $c->after($s) (a future settling after $s), and $c->await($f) (block for $f's
345 # result). They ignore $c - they exist where handler code already has it.
346 SV *
347 promise(self)
348 SV *self
349 CODE:
350 PERL_UNUSED_VAR(self);
351 8 RETVAL = pf_bless(aTHX_ pf_new(aTHX), "Punk::Future");
352 OUTPUT:
353 RETVAL
354  
355 SV *
356 timer(self, secs)
357 SV *self
358 NV secs
359 ALIAS:
360 after = 1
361 CODE:
362 PERL_UNUSED_VAR(self); PERL_UNUSED_VAR(ix);
363 24 RETVAL = pf_make_timer(aTHX_ (double)secs);
364 OUTPUT:
365 RETVAL
366  
367 void
368 await(self, f)
369 SV *self
370 SV *f
371 PPCODE:
372 {
373 punk_future *pf;
374 SSize_t i, n;
375 PERL_UNUSED_VAR(self);
376 269 pf = pf_of(aTHX_ f);
377 269 ▶ if (pf->state == PF_PENDING) pf_await(aTHX_ f);
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_PENDING) pf_await(aTHX_ f); 4 265 269
378 269 SP = PL_stack_base + ax - 1; /* the loop ran: see get above */
379 269 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 269 269
380 269 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 269 269
381 0 ▶ if (pf->state == PF_CANCELLED) croak("Punk::Future: await on a cancelled future");
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_CANCELLED) croak("Punk::Future: await on a cancelled future"); 0 0 0
382 0 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 0 0 0
# uncoverable branch true 0 0 0
383 # uncoverable condition left count:1 class:ignore_covered_err
384 269 ▶ if (pf->state == PF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (pf->state == PF_FAILED) { 269 0 269
385 269 ▶ # uncoverable statement
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable statement 269 0 269
# uncoverable statement 0 269 269
386 537 ▶ # uncoverable condition left count:1
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 268 269 537
387 # uncoverable condition right count:1
388 SV **fl = pf->vals ? av_fetch(pf->vals, 0, 0) : NULL;
389 # uncoverable statement
390 # uncoverable condition left count:1
391 # uncoverable condition right count:1
392 # uncoverable condition left count:2
393 # uncoverable condition right count:2
394 croak_sv(fl && *fl ? *fl : sv_2mortal(newSVpvs("Punk::Future failed")));
395 }
396 n = pf->vals ? av_len(pf->vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
397 EXTEND(SP, n);
398 for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSVsv(*av_fetch(pf->vals, i, 0))));
399 }
« xs/ratelimit.xs ↑ Summary include/punk/punk_cookie.h »