include/hyperman/hm_future.h

filter: Statementstmt 95.9% 502 / 523 (2 uncoverable) Branchbran 72.2% 331 / 482 (27 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.5% 833 / 1005 (29 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_sa_bus_perl.h ↑ Summary include/hyperman/hm_compress.h »
1 #ifndef HM_FUTURE_H
2 #define HM_FUTURE_H
3  
4 /* Hyperman::Future - native array-slot async primitive.
5 * Slots: 0 state, 1 result/failure AV, 2 callbacks, 3 weak upstream.
6 * Continuations are trampolined through a fire queue, so long then-chains
7 * run iteratively (bounded stack) with or without a loop.
8 *
9 * Everything here is C: chaining (then/else/followed_by/transform), the
10 * combinators, and CPAN Future interop are implemented as anonymous XSUBs
11 * carrying their captured state in magic (hm_clos) - no Perl closures. */
12  
13 #define HMF_PENDING 0
14 #define HMF_DONE 1
15 #define HMF_FAILED 2
16 #define HMF_CANCELLED 3
17  
18 static AV *hm_fq; /* pending (cb, future) pairs */
19 static int hm_fq_active;
20  
21 struct hm_loop;
22 static void hm_loop_run(pTHX_ struct hm_loop *loop, SV *until);
23 static struct hm_loop *hm_cur_loop;
24  
25 /* ---- C closures: anon XSUBs with captured SVs attached via magic ------- */
26  
27 typedef struct {
28 SV *a, *b, *c, *d;
29 IV i;
30 UV u;
31 } hm_clos;
32  
33 52140 static int hm_clos_free(pTHX_ SV *sv, MAGIC *mg) {
34 52140 hm_clos *cl = (hm_clos *)mg->mg_ptr;
35 PERL_UNUSED_VAR(sv);
36 /*
37 # uncoverable branch false class:ignore_covered_err
38 # uncoverable condition right count:1 class:ignore_covered_err */
39 52140 ▶ if (cl) {
Branch: 1 branch
BranchTrueFalseTotal
if (cl) { 52140 0 52140
40 52140 ▶ if (cl->a) SvREFCNT_dec(cl->a);
Branch: 1 branch
BranchTrueFalseTotal
if (cl->a) SvREFCNT_dec(cl->a); 50082 2058 52140
41 52140 ▶ if (cl->b) SvREFCNT_dec(cl->b);
Branch: 1 branch
BranchTrueFalseTotal
if (cl->b) SvREFCNT_dec(cl->b); 52071 69 52140
42 52140 ▶ if (cl->c) SvREFCNT_dec(cl->c);
Branch: 1 branch
BranchTrueFalseTotal
if (cl->c) SvREFCNT_dec(cl->c); 32 52108 52140
43 52140 ▶ if (cl->d) SvREFCNT_dec(cl->d);
Branch: 1 branch
BranchTrueFalseTotal
if (cl->d) SvREFCNT_dec(cl->d); 0 52140 52140
44 52140 free(cl);
45 52140 mg->mg_ptr = NULL;
46 }
47 52140 return 0;
48 }
49  
50 static MGVTBL hm_clos_vtbl = { NULL, NULL, NULL, NULL, hm_clos_free,
51 NULL, NULL, NULL };
52  
53 52141 static SV *hm_closure(pTHX_ XSUBADDR_t body, SV *a, SV *b, SV *c, SV *d,
54 IV i, UV u) {
55 52141 CV *cv = newXS(NULL, body, __FILE__);
56 52141 hm_clos *cl = (hm_clos *)hm_xcalloc(1, sizeof(hm_clos));
57 52141 ▶ cl->a = a ? SvREFCNT_inc(a) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cl->a = a ? SvREFCNT_inc(a) : NULL; 50083 2058 52141
58 52141 ▶ cl->b = b ? SvREFCNT_inc(b) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cl->b = b ? SvREFCNT_inc(b) : NULL; 52071 70 52141
59 52141 ▶ cl->c = c ? SvREFCNT_inc(c) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cl->c = c ? SvREFCNT_inc(c) : NULL; 32 52109 52141
60 52141 ▶ cl->d = d ? SvREFCNT_inc(d) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cl->d = d ? SvREFCNT_inc(d) : NULL; 0 52141 52141
61 52141 cl->i = i;
62 52141 cl->u = u;
63 52141 sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &hm_clos_vtbl, (char *)cl, 0);
64 52141 return newRV_noinc((SV *)cv);
65 }
66  
67 52128 static hm_clos *hm_clos_of(pTHX_ CV *cv) {
68 52128 MAGIC *mg = mg_findext((SV *)cv, PERL_MAGIC_ext, &hm_clos_vtbl);
69 52128 ▶ return mg ? (hm_clos *)mg->mg_ptr : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return mg ? (hm_clos *)mg->mg_ptr : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 52128 0 52128
70 }
71  
72 /* ---- core state ---------------------------------------------------------- */
73  
74 263107 static IV hmf_state(pTHX_ SV *self) {
75 SV **s;
76 /* A croak, not PENDING: every writer below dereferences the array, so a
77 * class name or an unblessed scalar here was a segfault one call later
78 * (Hyperman::Future->done(1)), and a ->get on one pumped the loop for
79 * ever. Callers that may legitimately hold something else ask
80 * hmf_is_future first. */
81 263107 ▶ if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 263107 0 263107
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 263103 4 263107
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 263101 263103
82 6 croak("Hyperman::Future: not a future object");
83 263101 s = av_fetch((AV *)SvRV(self), 0, 0);
84 263101 ▶ return s ? SvIV(*s) : HMF_PENDING; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return s ? SvIV(*s) : HMF_PENDING; /* # uncoverable condition right count:1 class:ignore_covered_err */ 263101 0 263101
85 }
86  
87 51164 static int hmf_is_future(pTHX_ SV *sv) {
88 51164 ▶ return sv && SvROK(sv) && SvOBJECT(SvRV(sv))
Branch: 2 branches
BranchTrueFalseTotal
return sv && SvROK(sv) && SvOBJECT(SvRV(sv)) 1158 50006 51164
return sv && SvROK(sv) && SvOBJECT(SvRV(sv)) 295 863 1158
89 102328 ▶ && sv_derived_from(sv, "Hyperman::Future");
Branch: 2 branches
BranchTrueFalseTotal
&& sv_derived_from(sv, "Hyperman::Future"); 51164 0 51164
&& sv_derived_from(sv, "Hyperman::Future"); 248 47 295
90 }
91  
92 /* Future-compatible: ours, or any object with an on_ready method. */
93 50885 static int hm_is_awaitable(pTHX_ SV *sv) {
94 50885 ▶ if (hmf_is_future(aTHX_ sv)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_is_future(aTHX_ sv)) return 1; 17 50868 50885
95 50868 ▶ if (sv && SvROK(sv) && SvOBJECT(SvRV(sv))) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (sv && SvROK(sv) && SvOBJECT(SvRV(sv))) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 50868 0 50868
if (sv && SvROK(sv) && SvOBJECT(SvRV(sv))) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 863 50005 50868
if (sv && SvROK(sv) && SvOBJECT(SvRV(sv))) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 860 863
96 3 HV *stash = SvSTASH(SvRV(sv));
97 /*
98 # uncoverable branch false class:ignore_covered_err
99 # uncoverable condition right count:1 class:ignore_covered_err
100 # uncoverable condition right count:2 class:ignore_covered_err */
101 3 ▶ if (stash && gv_fetchmethod_autoload(stash, "on_ready", 0)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (stash && gv_fetchmethod_autoload(stash, "on_ready", 0)) return 1; 3 0 3
if (stash && gv_fetchmethod_autoload(stash, "on_ready", 0)) return 1; 3 0 3
102 }
103 50865 return 0;
104 }
105  
106 52109 static AV *hmf_values_av(pTHX_ SV *self) {
107 52109 SV **v = av_fetch((AV *)SvRV(self), 1, 0);
108 /*
109 # uncoverable condition right count:1 class:ignore_covered_err
110 # uncoverable condition right count:2 class:ignore_covered_err */
111 52109 ▶ return (v && SvROK(*v)) ? (AV *)SvRV(*v) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvROK(*v)) ? (AV *)SvRV(*v) : NULL; 52109 0 52109
return (v && SvROK(*v)) ? (AV *)SvRV(*v) : NULL; 52109 0 52109
112 }
113  
114 /* ---- trampoline ---------------------------------------------------------- */
115  
116 /* SAVETMPS/FREETMPS around the drain, and they are the difference between a
117 * server that runs for a week and one the kernel kills.
118 *
119 * The pair below is mortalised out of the queue, so without a temps frame of
120 * its own the pump hangs them on whichever frame it was entered from. For a
121 * timer that is the worker's event loop, which does not return while the
122 * server is up: every Future ever resolved, and every callback attached to
123 * one, is then held for the life of the process. A caller that reschedules
124 * from its own callback - the shape every clock in an application has -
125 * leaks one Future and one closure per tick, for ever.
126 *
127 * SAVETMPS sets a new floor rather than freeing what the caller mortalised
128 * before calling in, so a caller's own temporaries are untouched. */
129 #include <stdarg.h>
130  
131 /* The server's own messages: a dying handler, a refused response, a wedged
132 * worker, a TLS reload that failed. One writer, so `error_log` can point them
133 * all somewhere other than the inherited stderr. A whole line per call,
134 * stamped like the access log and with the pid, written with one write(2) so
135 * lines from several workers do not interleave. -1 means stderr. */
136 static int hm_err_fd = -1;
137 26549 static void hm_errlog(const char *fmt, ...) {
138 char buf[1024];
139 26549 size_t n = 0;
140 struct tm tm;
141 26549 time_t t = time(NULL);
142 va_list ap;
143 int k;
144 /*
145 # uncoverable branch false class:ignore_covered_err
146 # uncoverable condition right count:1 class:ignore_covered_err */
147 26549 ▶ if (hm_localtime_r(&t, &tm))
Branch: 1 branch
BranchTrueFalseTotal
if (hm_localtime_r(&t, &tm)) 26549 0 26549
148 26549 n = strftime(buf, sizeof(buf), "[%d/%b/%Y:%H:%M:%S %z] ", &tm);
149 26549 k = snprintf(buf + n, sizeof(buf) - n, "[%d] ", hm_os_getpid());
150 /*
151 # uncoverable branch false class:ignore_covered_err
152 # uncoverable condition right count:1 class:ignore_covered_err */
153 26549 ▶ if (k > 0) n += (size_t)k;
Branch: 1 branch
BranchTrueFalseTotal
if (k > 0) n += (size_t)k; 26549 0 26549
154 26549 va_start(ap, fmt);
155 26549 k = vsnprintf(buf + n, sizeof(buf) - n, fmt, ap);
156 26549 va_end(ap);
157 /*
158 # uncoverable branch false class:ignore_covered_err
159 # uncoverable condition right count:1 class:ignore_covered_err */
160 26549 ▶ if (k > 0) n += (size_t)k;
Branch: 1 branch
BranchTrueFalseTotal
if (k > 0) n += (size_t)k; 26549 0 26549
161 /*
162 # uncoverable branch true class:ignore_covered_err
163 # uncoverable condition left count:1 class:ignore_covered_err */
164 26549 ▶ if (n > sizeof(buf) - 2) n = sizeof(buf) - 2;
Branch: 1 branch
BranchTrueFalseTotal
if (n > sizeof(buf) - 2) n = sizeof(buf) - 2; 0 26549 26549
165 /*
166 # uncoverable condition right count:1 class:ignore_covered_err
167 # uncoverable condition left count:3 class:ignore_covered_err */
168 53078 ▶ while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) n--;
Branch: 3 branches
BranchTrueFalseTotal
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) n--; 53078 0 53078
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) n--; 26529 26549 53078
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) n--; 0 26549 26549
169 /* One line per call: a die message quoting request data must not be
170 * able to write a second, forged line into the error log. */
171 {
172 size_t i;
173 2469574 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 2443025 26549 2469574
174 /*
175 # uncoverable branch true class:ignore_covered_err
176 # uncoverable condition left count:1 class:ignore_covered_err
177 # uncoverable condition left count:2 class:ignore_covered_err */
178 2443025 ▶ if (buf[i] == '\n' || buf[i] == '\r') buf[i] = ' ';
Branch: 2 branches
BranchTrueFalseTotal
if (buf[i] == '\n' || buf[i] == '\r') buf[i] = ' '; 2443025 0 2443025
if (buf[i] == '\n' || buf[i] == '\r') buf[i] = ' '; 0 2443025 2443025
179 }
180 26549 buf[n++] = '\n';
181 {
182 26549 ▶ int fd = hm_err_fd >= 0 ? hm_err_fd : 2;
Branch: 1 branch
BranchTrueFalseTotal
int fd = hm_err_fd >= 0 ? hm_err_fd : 2; 26518 31 26549
183 ssize_t w;
184 /*
185 # uncoverable condition left count:1 class:ignore_covered_err
186 # uncoverable condition left count:2 class:ignore_covered_err
187 # uncoverable condition right count:2 class:ignore_covered_err */
188 26549 ▶ do { w = hm_os_write_file(fd, buf, n); } while (w < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
do { w = hm_os_write_file(fd, buf, n); } while (w < 0 && errno == EINTR); 0 26549 26549
do { w = hm_os_write_file(fd, buf, n); } while (w < 0 && errno == EINTR); 0 0 0
189 (void)w;
190 }
191 26549 }
192  
193 /* After a G_EVAL call: say what died, on the error log. Every callback
194 * the loop runs is trapped, so an exception never unwinds through the C
195 * loop (which would skip loop->running-- and leave this queue half drained)
196 * - but it must not be silent either, or an app bug becomes a 500 with no
197 * line in any log. */
198 53011 static void hm_warn_died(pTHX_ const char *what) {
199 STRLEN l;
200 const char *m;
201 53011 ▶ if (!SvTRUE(ERRSV)) return; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) return; /* # uncoverable condition right count:1 class:ignore_covered_err */ 53011 0 53011
if (!SvTRUE(ERRSV)) return; /* # uncoverable condition right count:1 class:ignore_covered_err */ 53005 6 53011
202 6 ▶ m = SvPV(ERRSV, l);
Branch: 1 branch
BranchTrueFalseTotal
m = SvPV(ERRSV, l); 6 0 6
203 6 hm_errlog("Hyperman: %s died: %.*s", what, (int)l, m);
204 }
205  
206 52087 static void hmf_pump(pTHX) {
207 52087 ▶ if (hm_fq_active) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_fq_active) return; 50018 2069 52087
208 2069 ENTER;
209 2069 SAVETMPS;
210 2069 SAVEINT(hm_fq_active);
211 2069 hm_fq_active = 1;
212 54160 ▶ while (av_len(hm_fq) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (av_len(hm_fq) >= 0) { 52091 2069 54160
213 52091 SV *cb = sv_2mortal(av_shift(hm_fq));
214 52091 SV *fut = sv_2mortal(av_shift(hm_fq));
215 52091 dSP;
216 52091 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 52091 52091
217 52091 ▶ XPUSHs(fut);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(fut); 0 52091 52091
218 52091 PUTBACK;
219 52091 call_sv(cb, G_DISCARD | G_EVAL);
220 52091 hm_warn_died(aTHX_ "on_ready callback");
221 }
222 2069 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2069 0 2069
223 2069 LEAVE;
224 }
225  
226 10 static void hmf_enqueue(pTHX_ SV *cb, SV *self) {
227 10 av_push(hm_fq, SvREFCNT_inc(cb));
228 10 av_push(hm_fq, SvREFCNT_inc(self));
229 10 hmf_pump(aTHX);
230 10 }
231  
232 52228 static void hmf_fire(pTHX_ SV *self) {
233 52228 AV *av = (AV *)SvRV(self);
234 52228 SV **slot = av_fetch(av, 2, 0);
235 52228 ▶ SV *cbs = (slot && SvOK(*slot)) ? *slot : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
SV *cbs = (slot && SvOK(*slot)) ? *slot : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 52077 151 52228
SV *cbs = (slot && SvOK(*slot)) ? *slot : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 52077 0 52077
236 52228 ▶ if (!cbs) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbs) return; 151 52077 52228
237 52077 SvREFCNT_inc(cbs);
238 52077 av_store(av, 2, &PL_sv_undef);
239 52080 ▶ if (SvROK(cbs) && SvTYPE(SvRV(cbs)) == SVt_PVAV) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cbs) && SvTYPE(SvRV(cbs)) == SVt_PVAV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 52077 0 52077
if (SvROK(cbs) && SvTYPE(SvRV(cbs)) == SVt_PVAV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 52074 52077
240 3 AV *list = (AV *)SvRV(cbs);
241 3 SSize_t i, n = av_len(list) + 1;
242 10 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7 3 10
243 7 SV **e = av_fetch(list, i, 0);
244 /*
245 # uncoverable branch false class:ignore_covered_err
246 # uncoverable condition right count:1 class:ignore_covered_err */
247 7 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 7 0 7
248 7 av_push(hm_fq, SvREFCNT_inc(*e));
249 7 av_push(hm_fq, SvREFCNT_inc(self));
250 }
251 }
252 } else {
253 52074 av_push(hm_fq, SvREFCNT_inc(cbs));
254 52074 av_push(hm_fq, SvREFCNT_inc(self));
255 }
256 52077 SvREFCNT_dec(cbs);
257 52077 hmf_pump(aTHX);
258 }
259  
260 /* A future settles ONCE. The check is here, at the one place a state is
261 * written, rather than at each caller: a continuation that ran a user
262 * callback and then found its future cancelled meanwhile (the callback
263 * closed the connection, say) used to flip CANCELLED to DONE, and every
264 * callback registered after the cancel fired a second time. */
265 2191 static void hmf_settle(pTHX_ SV *self, IV state, SV **vals, SSize_t n) {
266 AV *av;
267 AV *val;
268 SSize_t i;
269 /*
270 # uncoverable branch true
271 # uncoverable condition left count:1 class:ignore_covered_err */
272 2191 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 0 2191 2191
273 2191 av = (AV *)SvRV(self);
274 2191 val = newAV();
275 2277 ▶ for (i = 0; i < n; i++) av_push(val, newSVsv(vals[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(val, newSVsv(vals[i])); 86 2191 2277
276 2191 av_store(av, 0, newSViv(state));
277 2191 av_store(av, 1, newRV_noinc((SV *)val));
278 2191 hmf_fire(aTHX_ self);
279 }
280  
281 /* Takes ownership of vals either way. */
282 50037 static void hmf_settle_av(pTHX_ SV *self, IV state, AV *vals) {
283 AV *av;
284 /*
285 # uncoverable branch true
286 # uncoverable condition left count:1 class:ignore_covered_err */
287 50037 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) { SvREFCNT_dec((SV *)vals); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) { SvREFCNT_dec((SV *)vals); return; } 0 50037 50037
288 50037 av = (AV *)SvRV(self);
289 50037 av_store(av, 0, newSViv(state));
290 50037 av_store(av, 1, newRV_noinc((SV *)vals));
291 50037 hmf_fire(aTHX_ self);
292 }
293  
294 52245 static SV *hmf_new(pTHX_ const char *classname) {
295 52245 AV *av = newAV();
296 SV *rv;
297 52245 av_extend(av, 3);
298 52245 av_store(av, 0, newSViv(HMF_PENDING));
299 52245 rv = newRV_noinc((SV *)av);
300 52245 sv_bless(rv, gv_stashpv(classname, GV_ADD));
301 52245 return rv;
302 }
303  
304 50040 static const char *hmf_class_of(pTHX_ SV *self) {
305 /*
306 # uncoverable condition right count:1 class:ignore_covered_err
307 # uncoverable condition right count:2 class:ignore_covered_err */
308 50040 ▶ HV *stash = (SvROK(self) && SvOBJECT(SvRV(self))) ? SvSTASH(SvRV(self)) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *stash = (SvROK(self) && SvOBJECT(SvRV(self))) ? SvSTASH(SvRV(self)) : NULL; 50040 0 50040
HV *stash = (SvROK(self) && SvOBJECT(SvRV(self))) ? SvSTASH(SvRV(self)) : NULL; 50040 0 50040
309 /*
310 # uncoverable condition right count:1 class:ignore_covered_err
311 # uncoverable condition left count:2 class:ignore_covered_err */
312 50040 ▶ return stash ? HvNAME(stash) : "Hyperman::Future";
Branch: 7 branches
BranchTrueFalseTotal
return stash ? HvNAME(stash) : "Hyperman::Future"; 50040 0 50040
return stash ? HvNAME(stash) : "Hyperman::Future"; 50040 0 50040
return stash ? HvNAME(stash) : "Hyperman::Future"; 50040 0 50040
return stash ? HvNAME(stash) : "Hyperman::Future"; 0 50040 50040
return stash ? HvNAME(stash) : "Hyperman::Future"; 0 0 0
return stash ? HvNAME(stash) : "Hyperman::Future"; 50040 0 50040
return stash ? HvNAME(stash) : "Hyperman::Future"; 0 50040 50040
313 }
314  
315 /* Resolve a loop-created future with no values (readiness signals). */
316 2028 static void hmf_done0(pTHX_ SV *fut) {
317 /*
318 # uncoverable branch false class:ignore_covered_err
319 # uncoverable condition right count:1 class:ignore_covered_err */
320 2028 ▶ if (hmf_state(aTHX_ fut) == HMF_PENDING)
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ fut) == HMF_PENDING) 2028 0 2028
321 2028 hmf_settle(aTHX_ fut, HMF_DONE, NULL, 0);
322 2028 }
323  
324 52096 static void hmf_on_ready(pTHX_ SV *self, SV *cb) {
325 52096 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) { 10 52084 52094
326 10 hmf_enqueue(aTHX_ cb, self);
327 } else {
328 52084 AV *av = (AV *)SvRV(self);
329 52084 SV **slot = av_fetch(av, 2, 0);
330 52084 ▶ if (!slot || !SvOK(*slot)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!slot || !SvOK(*slot)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 4 52080 52084
if (!slot || !SvOK(*slot)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 4 4
331 52080 av_store(av, 2, newSVsv(cb));
332 4 ▶ } else if (SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
} else if (SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 3 4
333 1 av_push((AV *)SvRV(*slot), newSVsv(cb));
334 } else {
335 3 AV *list = newAV();
336 3 av_push(list, SvREFCNT_inc(*slot));
337 3 av_push(list, newSVsv(cb));
338 3 av_store(av, 2, newRV_noinc((SV *)list));
339 }
340 }
341 52094 }
342  
343 /* on_ready that works on ours (direct) or any Future-compatible object. */
344 51 static void hm_any_on_ready(pTHX_ SV *f, SV *cb) {
345 51 ▶ if (hmf_is_future(aTHX_ f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_is_future(aTHX_ f)) { 38 13 51
346 38 hmf_on_ready(aTHX_ f, cb);
347 } else {
348 13 dSP;
349 13 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 13 13
350 13 ▶ XPUSHs(f);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(f); 0 13 13
351 13 ▶ XPUSHs(cb);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cb); 0 13 13
352 13 PUTBACK;
353 13 call_method("on_ready", G_DISCARD);
354 13 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 13 0 13
355 }
356 51 }
357  
358 50052 static void hmf_set_upstream(pTHX_ SV *derived, SV *upstream) {
359 SV *w;
360 50052 (void)hmf_state(aTHX_ derived); /* croaks on a non-future */
361 50052 w = newSVsv(upstream);
362 50052 sv_rvweaken(w);
363 50052 av_store((AV *)SvRV(derived), 3, w);
364 50052 }
365  
366 static void hmf_cancel(pTHX_ SV *self);
367 static IV hm_any_state(pTHX_ SV *f);
368  
369 /* Cancel one awaitable of any class: ours directly, a foreign one through
370 * its method, trapped so a class without `cancel` cannot unwind the loop. */
371 9 static void hm_any_cancel(pTHX_ SV *f) {
372 9 ▶ if (hmf_is_future(aTHX_ f)) { hmf_cancel(aTHX_ f); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_is_future(aTHX_ f)) { hmf_cancel(aTHX_ f); return; } 8 1 9
373 /*
374 # uncoverable branch true class:ignore_covered_err
375 # uncoverable condition left count:1 class:ignore_covered_err
376 # uncoverable condition left count:2 class:ignore_covered_err */
377 1 ▶ if (!(SvROK(f) && SvOBJECT(SvRV(f)))) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(f) && SvOBJECT(SvRV(f)))) return; 1 0 1
if (!(SvROK(f) && SvOBJECT(SvRV(f)))) return; 0 1 1
378 {
379 1 dSP;
380 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
381 1 ▶ XPUSHs(f);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(f); 0 1 1
382 1 PUTBACK;
383 1 call_method("cancel", G_DISCARD | G_EVAL);
384 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
385 }
386 }
387  
388 /* Cancel every still-pending member of a combinator's input list, over a
389 * snapshot: a cancellation fires that member's callbacks, one of which is
390 * the combinator's own, which clears this very list. */
391 16 static void hmf_cancel_list(pTHX_ AV *list) {
392 16 SSize_t i, n = av_len(list) + 1;
393 SV **snap;
394 /*
395 # uncoverable branch true
396 # uncoverable condition left count:1 class:ignore_covered_err */
397 16 ▶ if (n <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return; 0 16 16
398 16 snap = (SV **)hm_xmalloc(sizeof(SV *) * (size_t)n);
399 47 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 31 16 47
400 31 SV **e = av_fetch(list, i, 0);
401 /*
402 # uncoverable condition right count:1 class:ignore_covered_err
403 # uncoverable condition right count:2 class:ignore_covered_err */
404 31 ▶ snap[i] = e && *e ? SvREFCNT_inc(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
snap[i] = e && *e ? SvREFCNT_inc(*e) : NULL; 31 0 31
snap[i] = e && *e ? SvREFCNT_inc(*e) : NULL; 31 0 31
405 }
406 47 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 31 16 47
407 31 ▶ if (snap[i] && hm_any_state(aTHX_ snap[i]) == HMF_PENDING) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (snap[i] && hm_any_state(aTHX_ snap[i]) == HMF_PENDING) /* # uncoverable condition right count:1 class:ignore_covered_err */ 31 0 31
if (snap[i] && hm_any_state(aTHX_ snap[i]) == HMF_PENDING) /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 22 31
408 9 hm_any_cancel(aTHX_ snap[i]);
409 }
410 47 ▶ for (i = 0; i < n; i++) if (snap[i]) SvREFCNT_dec(snap[i]); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) if (snap[i]) SvREFCNT_dec(snap[i]); /* # uncoverable condition right count:1 class:ignore_covered_err */ 31 0 31
for (i = 0; i < n; i++) if (snap[i]) SvREFCNT_dec(snap[i]); /* # uncoverable condition right count:1 class:ignore_covered_err */ 31 16 47
411 16 free(snap);
412 }
413  
414 41 static void hmf_cancel(pTHX_ SV *self) {
415 AV *av;
416 SV **up;
417 41 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 1 40 41
418 40 av = (AV *)SvRV(self);
419 40 hmf_settle(aTHX_ self, HMF_CANCELLED, NULL, 0);
420 /* propagate up a then-chain: cancelling a derived future cancels its
421 * still-pending upstream - one future, or a combinator's whole list */
422 40 up = av_fetch(av, 3, 0);
423 40 ▶ if (!up || !SvROK(*up)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!up || !SvROK(*up)) return; 14 26 40
if (!up || !SvROK(*up)) return; 1 13 14
424 /* a future is a blessed array too: ask that first, and only an UNBLESSED
425 * array is a combinator's input list */
426 13 ▶ if (!hmf_is_future(aTHX_ *up)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hmf_is_future(aTHX_ *up)) { 4 9 13
427 4 ▶ if (SvTYPE(SvRV(*up)) == SVt_PVAV && !SvOBJECT(SvRV(*up))) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(SvRV(*up)) == SVt_PVAV && !SvOBJECT(SvRV(*up))) /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 1 4
if (SvTYPE(SvRV(*up)) == SVt_PVAV && !SvOBJECT(SvRV(*up))) /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
428 3 hmf_cancel_list(aTHX_ (AV *)SvRV(*up));
429 4 return;
430 }
431 9 ▶ if (hmf_state(aTHX_ *up) == HMF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ *up) == HMF_PENDING) { 5 4 9
432 5 dSP;
433 5 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 5 5
434 5 ▶ XPUSHs(*up);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*up); 0 5 5
435 5 PUTBACK;
436 5 call_method("cancel", G_DISCARD);
437 5 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 5 0 5
438 }
439 }
440  
441 48 static void hmf_await(pTHX_ SV *self) {
442 48 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 23 23 46
443 23 ▶ if (hm_cur_loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_cur_loop) { 21 2 23
444 21 hm_loop_run(aTHX_ hm_cur_loop, self);
445 21 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 15 6 21
446 6 croak("Hyperman::Future: loop stopped before the future was ready");
447 }
448 {
449 2 SV *aw = get_sv("Hyperman::Future::AWAIT", 0);
450 2 ▶ if (aw && SvOK(aw)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (aw && SvOK(aw)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
if (aw && SvOK(aw)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 1 2
451 1 dSP;
452 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
453 1 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 1 1
454 1 PUTBACK;
455 1 call_sv(aw, G_DISCARD);
456 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 1 1
457 /*
458 # uncoverable branch false
459 # uncoverable condition right count:1 class:ignore_covered_err */
460 1 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 1 0 1
461 }
462 }
463 1 croak("Cannot await a pending Hyperman::Future without an event loop");
464 }
465  
466 /* ---- foreign-future compatibility --------------------------------------- */
467  
468 /* state of ours (direct) or a foreign Future-like via method calls */
469 68 static IV hm_any_state(pTHX_ SV *f) {
470 68 dSP;
471 68 IV st = HMF_PENDING;
472 int n;
473 68 ▶ if (hmf_is_future(aTHX_ f)) return hmf_state(aTHX_ f);
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_is_future(aTHX_ f)) return hmf_state(aTHX_ f); 46 22 68
474 22 ENTER; SAVETMPS;
475 22 ▶ PUSHMARK(SP); XPUSHs(f); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 22 22
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 22 22
476 22 n = call_method("is_done", G_SCALAR | G_EVAL);
477 22 SPAGAIN;
478 22 ▶ if (n && SvTRUE(TOPs)) st = HMF_DONE; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n && SvTRUE(TOPs)) st = HMF_DONE; /* # uncoverable condition right count:1 class:ignore_covered_err */ 22 0 22
if (n && SvTRUE(TOPs)) st = HMF_DONE; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 15 22
479 22 (void)POPs;
480 22 PUTBACK;
481 22 ▶ if (st == HMF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_PENDING) { 15 7 22
482 15 ▶ PUSHMARK(SP); XPUSHs(f); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 15 15
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 15 15
483 15 n = call_method("is_failed", G_SCALAR | G_EVAL);
484 15 SPAGAIN;
485 15 ▶ if (n && SvTRUE(TOPs)) st = HMF_FAILED; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n && SvTRUE(TOPs)) st = HMF_FAILED; /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 0 15
if (n && SvTRUE(TOPs)) st = HMF_FAILED; /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 9 15
486 15 (void)POPs;
487 15 PUTBACK;
488 }
489 22 ▶ if (st == HMF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_PENDING) { 9 13 22
490 9 ▶ PUSHMARK(SP); XPUSHs(f); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 9 9
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 9 9
491 9 n = call_method("is_cancelled", G_SCALAR | G_EVAL);
492 9 SPAGAIN;
493 9 ▶ if (n && SvTRUE(TOPs)) st = HMF_CANCELLED; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n && SvTRUE(TOPs)) st = HMF_CANCELLED; /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 0 9
if (n && SvTRUE(TOPs)) st = HMF_CANCELLED; /* # uncoverable condition right count:1 class:ignore_covered_err */ 8 1 9
494 9 (void)POPs;
495 9 PUTBACK;
496 }
497 22 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 22 0 22
498 22 return st;
499 }
500  
501 /* collect a method call's list results into out; returns 1 if it died */
502 50036 static int hm_call_list(pTHX_ SV *invocant, const char *method, SV *cb,
503 SV **args, SSize_t nargs, AV *out) {
504 50036 dSP;
505 SSize_t n, i;
506 50036 int err = 0;
507 50036 ENTER; SAVETMPS;
508 50036 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 50036 50036
509 50036 ▶ if (invocant) XPUSHs(invocant); /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (invocant) XPUSHs(invocant); /* # uncoverable condition left count:2 class:ignore_covered_err */ 6 50030 50036
if (invocant) XPUSHs(invocant); /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 6 6
510 100060 ▶ for (i = 0; i < nargs; i++) XPUSHs(args[i]); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) XPUSHs(args[i]); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 50024 50024
for (i = 0; i < nargs; i++) XPUSHs(args[i]); /* # uncoverable condition left count:1 class:ignore_covered_err */ 50024 50036 100060
511 50036 PUTBACK;
512 6 n = method ? call_method(method, G_ARRAY | G_EVAL)
513 50036 ▶ : call_sv(cb, G_ARRAY | G_EVAL);
Branch: 1 branch
BranchTrueFalseTotal
: call_sv(cb, G_ARRAY | G_EVAL); 6 50030 50036
514 50036 SPAGAIN;
515 50036 ▶ if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 50036 0 50036
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 50031 50036
516 5 err = 1;
517 5 SP -= n;
518 } else {
519 50031 SV **base = SP - n + 1;
520 100064 ▶ for (i = 0; i < n; i++) av_push(out, newSVsv(base[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(out, newSVsv(base[i])); 50033 50031 100064
521 50031 SP -= n;
522 }
523 50036 PUTBACK;
524 50036 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 50035 1 50036
525 50036 return err;
526 }
527  
528 /* result (done) or failure (failed) values of ours or a foreign future */
529 23 static void hm_any_values(pTHX_ SV *f, IV st, AV *out) {
530 23 ▶ if (hmf_is_future(aTHX_ f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_is_future(aTHX_ f)) { 16 7 23
531 16 AV *vals = hmf_values_av(aTHX_ f);
532 16 ▶ SSize_t i, n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 16 0 16
533 32 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 16 16 32
534 16 SV **e = av_fetch(vals, i, 0);
535 16 ▶ av_push(out, e ? newSVsv(*e) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(out, e ? newSVsv(*e) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 16 0 16
536 }
537 7 ▶ } else if (st == HMF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st == HMF_DONE) { 3 4 7
538 3 hm_call_list(aTHX_ f, "get", NULL, NULL, 0, out);
539 4 ▶ } else if (st == HMF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st == HMF_FAILED) { 3 1 4
540 3 hm_call_list(aTHX_ f, "failure", NULL, NULL, 0, out);
541 }
542 23 }
543  
544 /* ---- chaining ------------------------------------------------------------ */
545  
546 XS_INTERNAL(hm_xs_chain_cb);
547 11 XS_INTERNAL(hm_xs_chain_cb) {
548 11 dXSARGS;
549 11 hm_clos *cl = hm_clos_of(aTHX_ cv);
550 11 SV *g = ST(0);
551 SV *next;
552 IV st;
553 /*
554 # uncoverable branch true class:ignore_covered_err
555 # uncoverable condition left count:1 class:ignore_covered_err
556 # uncoverable condition left count:2 class:ignore_covered_err */
557 11 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 11 0 11
if (!cl || items < 1) XSRETURN_EMPTY; 0 11 11
558 11 next = cl->a;
559 /* Cancelled while the inner future was still running: nothing to
560 * deliver into, and settling it again would flip CANCELLED to DONE. */
561 11 ▶ if (hmf_state(aTHX_ next) != HMF_PENDING) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ next) != HMF_PENDING) XSRETURN_EMPTY; 2 9 11
562 9 st = hm_any_state(aTHX_ g);
563 17 ▶ if (st == HMF_DONE || st == HMF_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (st == HMF_DONE || st == HMF_FAILED) { 2 7 9
if (st == HMF_DONE || st == HMF_FAILED) { 1 1 2
564 8 AV *vals = newAV();
565 8 hm_any_values(aTHX_ g, st, vals);
566 8 hmf_settle_av(aTHX_ next, st, vals);
567 } else {
568 1 hmf_cancel(aTHX_ next);
569 }
570 9 XSRETURN_EMPTY;
571 }
572  
573 /* Perl's _chain_into: a returned future flattens; plain values resolve. */
574 50021 static void hmf_chain_into(pTHX_ SV *next, AV *vals) {
575 50021 ▶ SV **first = av_len(vals) >= 0 ? av_fetch(vals, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **first = av_len(vals) >= 0 ? av_fetch(vals, 0, 0) : NULL; 50020 1 50021
576 50033 ▶ if (first && hm_is_awaitable(aTHX_ *first)) {
Branch: 2 branches
BranchTrueFalseTotal
if (first && hm_is_awaitable(aTHX_ *first)) { 50020 1 50021
if (first && hm_is_awaitable(aTHX_ *first)) { 12 50008 50020
577 12 SV *cb = hm_closure(aTHX_ hm_xs_chain_cb, next, NULL, NULL, NULL, 0, 0);
578 /* The returned future is now what `next` waits on, so cancelling
579 * `next` must reach it - the same link then() made to its input. */
580 12 hmf_set_upstream(aTHX_ next, *first);
581 12 hm_any_on_ready(aTHX_ *first, cb);
582 12 SvREFCNT_dec(cb);
583 12 SvREFCNT_dec((SV *)vals);
584 50009 ▶ } else if (av_len(vals) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (av_len(vals) < 0) { 1 50008 50009
585 1 av_push(vals, newSV(0)); /* done(undef), as the reference impl did */
586 1 hmf_settle_av(aTHX_ next, HMF_DONE, vals);
587 } else {
588 50008 hmf_settle_av(aTHX_ next, HMF_DONE, vals);
589 }
590 50021 }
591  
592 /* then/else/followed_by continuation. i: 0 = then (b=on_done, c=on_fail),
593 * 1 = followed_by (b=cb). */
594 XS_INTERNAL(hm_xs_then_cb);
595 50032 XS_INTERNAL(hm_xs_then_cb) {
596 50032 dXSARGS;
597 50032 hm_clos *cl = hm_clos_of(aTHX_ cv);
598 SV *f, *next;
599 IV st;
600 /*
601 # uncoverable branch true class:ignore_covered_err
602 # uncoverable condition left count:1 class:ignore_covered_err
603 # uncoverable condition left count:2 class:ignore_covered_err */
604 50032 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 50032 0 50032
if (!cl || items < 1) XSRETURN_EMPTY; 0 50032 50032
605 50032 f = ST(0);
606 50032 next = cl->a;
607 50032 st = hmf_state(aTHX_ f);
608  
609 /* `next` was cancelled after this fire was queued (the upstream had
610 * already settled, so the cancel had nothing to propagate to): the
611 * user's callback must not run and the state must not change. Checked
612 * before BOTH branches; followed_by once ran its callback regardless. */
613 50032 ▶ if (hmf_state(aTHX_ next) != HMF_PENDING) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ next) != HMF_PENDING) XSRETURN_EMPTY; 4 50028 50032
614  
615 50028 ▶ if (cl->i == 1) { /* followed_by: cb($f) */
Branch: 1 branch
BranchTrueFalseTotal
if (cl->i == 1) { /* followed_by: cb($f) */ 7 50021 50028
616 7 AV *out = newAV();
617 7 SV *arg = f;
618 7 ▶ if (hm_call_list(aTHX_ NULL, NULL, cl->b, &arg, 1, out)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_call_list(aTHX_ NULL, NULL, cl->b, &arg, 1, out)) { 1 6 7
619 1 ▶ SV *err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_2mortal(newSVsv(ERRSV)); 1 0 1
620 1 hmf_settle(aTHX_ next, HMF_FAILED, &err, 1);
621 1 SvREFCNT_dec((SV *)out);
622 } else {
623 6 hmf_chain_into(aTHX_ next, out);
624 }
625 7 XSRETURN_EMPTY;
626 }
627  
628 100041 ▶ if (st == HMF_DONE || st == HMF_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (st == HMF_DONE || st == HMF_FAILED) { 3 50018 50021
if (st == HMF_DONE || st == HMF_FAILED) { 2 1 3
629 50020 ▶ SV *user = (st == HMF_DONE) ? cl->b : cl->c;
Branch: 1 branch
BranchTrueFalseTotal
SV *user = (st == HMF_DONE) ? cl->b : cl->c; 50018 2 50020
630 50020 AV *vals = hmf_values_av(aTHX_ f);
631 50020 ▶ SSize_t n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 50020 0 50020
632 100038 ▶ if (user && SvOK(user)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (user && SvOK(user)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 50018 2 50020
if (user && SvOK(user)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 50018 0 50018
633 50018 AV *out = newAV();
634 50018 ▶ SV **args = vals ? AvARRAY(vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **args = vals ? AvARRAY(vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 50018 0 50018
635 50018 ▶ if (hm_call_list(aTHX_ NULL, NULL, user, args, n, out)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_call_list(aTHX_ NULL, NULL, user, args, n, out)) { 3 50015 50018
636 3 ▶ SV *err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_2mortal(newSVsv(ERRSV)); 3 0 3
637 3 hmf_settle(aTHX_ next, HMF_FAILED, &err, 1);
638 3 SvREFCNT_dec((SV *)out);
639 } else {
640 50015 hmf_chain_into(aTHX_ next, out);
641 }
642 } else {
643 2 AV *copy = newAV();
644 SSize_t i;
645 5 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 2 5
646 3 SV **e = av_fetch(vals, i, 0);
647 3 ▶ av_push(copy, e ? newSVsv(*e) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(copy, e ? newSVsv(*e) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
648 }
649 2 hmf_settle_av(aTHX_ next, st, copy);
650 }
651 } else {
652 1 hmf_cancel(aTHX_ next);
653 }
654 50021 XSRETURN_EMPTY;
655 }
656  
657 /* transform continuation: b = done-mapper, c = fail-mapper */
658 XS_INTERNAL(hm_xs_transform_cb);
659 7 XS_INTERNAL(hm_xs_transform_cb) {
660 7 dXSARGS;
661 7 hm_clos *cl = hm_clos_of(aTHX_ cv);
662 SV *f, *next;
663 IV st;
664 /*
665 # uncoverable branch true class:ignore_covered_err
666 # uncoverable condition left count:1 class:ignore_covered_err
667 # uncoverable condition left count:2 class:ignore_covered_err */
668 7 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 7 0 7
if (!cl || items < 1) XSRETURN_EMPTY; 0 7 7
669 7 f = ST(0); /* # uncoverable statement class:ignore_covered_err */
670 7 next = cl->a; /* # uncoverable statement class:ignore_covered_err */
671 /*
672 # uncoverable statement class:ignore_covered_err
673 # uncoverable branch true
674 # uncoverable condition left count:1 */
675 7 ▶ if (hmf_state(aTHX_ next) != HMF_PENDING) XSRETURN_EMPTY; /* cancelled */
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ next) != HMF_PENDING) XSRETURN_EMPTY; /* cancelled */ 0 7 7
676 7 st = hmf_state(aTHX_ f);
677 12 ▶ if (st == HMF_DONE || st == HMF_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (st == HMF_DONE || st == HMF_FAILED) { 3 4 7
if (st == HMF_DONE || st == HMF_FAILED) { 2 1 3
678 6 ▶ SV *mapper = (st == HMF_DONE) ? cl->b : cl->c;
Branch: 1 branch
BranchTrueFalseTotal
SV *mapper = (st == HMF_DONE) ? cl->b : cl->c; 4 2 6
679 6 AV *vals = hmf_values_av(aTHX_ f);
680 6 ▶ SSize_t n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 0 6
681 6 AV *out = newAV();
682 10 ▶ if (mapper && SvOK(mapper)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (mapper && SvOK(mapper)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 0 6
if (mapper && SvOK(mapper)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 1 6
683 5 ▶ SV **args = vals ? AvARRAY(vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **args = vals ? AvARRAY(vals) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
684 5 ▶ if (hm_call_list(aTHX_ NULL, NULL, mapper, args, n, out)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_call_list(aTHX_ NULL, NULL, mapper, args, n, out)) { 1 4 5
685 1 ▶ SV *err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_2mortal(newSVsv(ERRSV)); 1 0 1
686 1 hmf_settle(aTHX_ next, HMF_FAILED, &err, 1);
687 1 SvREFCNT_dec((SV *)out);
688 1 XSRETURN_EMPTY;
689 }
690 } else {
691 SSize_t i;
692 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 1 3
693 2 SV **e = av_fetch(vals, i, 0);
694 2 ▶ av_push(out, e ? newSVsv(*e) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(out, e ? newSVsv(*e) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
695 }
696 }
697 5 hmf_settle_av(aTHX_ next, st, out);
698 } else {
699 1 hmf_cancel(aTHX_ next);
700 }
701 6 XSRETURN_EMPTY;
702 }
703  
704 /* on_done / on_fail filter: b = user cb, i = required state */
705 XS_INTERNAL(hm_xs_ondone_cb);
706 2002 XS_INTERNAL(hm_xs_ondone_cb) {
707 2002 dXSARGS;
708 2002 hm_clos *cl = hm_clos_of(aTHX_ cv);
709 SV *f;
710 /*
711 # uncoverable branch true class:ignore_covered_err
712 # uncoverable condition left count:1 class:ignore_covered_err
713 # uncoverable condition left count:2 class:ignore_covered_err */
714 2002 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 2002 0 2002
if (!cl || items < 1) XSRETURN_EMPTY; 0 2002 2002
715 2002 f = ST(0);
716 /*
717 # uncoverable branch false class:ignore_covered_err
718 # uncoverable condition right count:1 class:ignore_covered_err */
719 2002 ▶ if (hmf_state(aTHX_ f) == cl->i) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ f) == cl->i) { 2002 0 2002
720 2002 AV *vals = hmf_values_av(aTHX_ f);
721 2002 ▶ SSize_t n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2002 0 2002
722 2002 dSP;
723 SSize_t j;
724 2002 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2002 2002
725 2007 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 5 2002 2007
726 5 SV **e = av_fetch(vals, j, 0);
727 /*
728 # uncoverable condition left count:1 class:ignore_covered_err
729 # uncoverable condition right count:2 class:ignore_covered_err */
730 5 ▶ XPUSHs(e ? *e : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(e ? *e : &PL_sv_undef); 0 5 5
XPUSHs(e ? *e : &PL_sv_undef); 5 0 5
731 }
732 2002 PUTBACK;
733 2002 call_sv(cl->b, G_DISCARD);
734 2002 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 2002 2002
735 }
736 2002 XSRETURN_EMPTY;
737 }
738  
739 /* Run one on_cancel target: a code ref is called with the future, anything
740 * else is treated as a future and cancelled. Both spellings are needed
741 * because the await protocol registers each - a code block through
742 * AWAIT_ON_CANCEL, a future through AWAIT_CHAIN_CANCEL. */
743 5 static void hmf_cancel_target(pTHX_ SV *thing, SV *self) {
744 5 dSP;
745 /*
746 # uncoverable branch true class:ignore_covered_err
747 # uncoverable condition left count:1 class:ignore_covered_err
748 # uncoverable condition left count:2 class:ignore_covered_err */
749 5 ▶ if (!(thing && SvROK(thing))) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(thing && SvROK(thing))) return; 5 0 5
if (!(thing && SvROK(thing))) return; 0 5 5
750 5 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 5 5
751 5 ▶ if (SvTYPE(SvRV(thing)) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(thing)) == SVt_PVCV) { 4 1 5
752 4 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 4 4
753 4 PUTBACK;
754 4 call_sv(thing, G_DISCARD);
755 } else {
756 1 ▶ XPUSHs(thing);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(thing); 0 1 1
757 1 PUTBACK;
758 1 call_method("cancel", G_DISCARD);
759 }
760 5 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 4 5
761 }
762  
763 /* on_cancel filter: b = target. Cancel callbacks ride the on_ready list
764 * rather than a slot of their own - a future is already four slots wide and
765 * two of them are allocated per parked request - so this fires on every
766 * settlement and drops everything that is not a cancellation. */
767 XS_INTERNAL(hm_xs_oncancel_cb);
768 6 XS_INTERNAL(hm_xs_oncancel_cb) {
769 6 dXSARGS;
770 6 hm_clos *cl = hm_clos_of(aTHX_ cv);
771 SV *f;
772 /*
773 # uncoverable branch true class:ignore_covered_err
774 # uncoverable condition left count:1 class:ignore_covered_err
775 # uncoverable condition left count:2 class:ignore_covered_err */
776 6 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 6 0 6
if (!cl || items < 1) XSRETURN_EMPTY; 0 6 6
777 6 f = ST(0);
778 6 ▶ if (hmf_state(aTHX_ f) == HMF_CANCELLED)
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ f) == HMF_CANCELLED) 4 2 6
779 4 hmf_cancel_target(aTHX_ cl->b, f);
780 6 XSRETURN_EMPTY;
781 }
782  
783 /* ---- convergent combinators --------------------------------------------- *
784 * a = result future, b = remaining-count SV, c = subfutures AV ref,
785 * i = mode. */
786 #define HM_COMB_WAIT_ALL 0
787 #define HM_COMB_WAIT_ANY 1
788 #define HM_COMB_NEEDS_ALL 2
789 #define HM_COMB_NEEDS_ANY 3
790  
791 XS_INTERNAL(hm_xs_comb_cb);
792 28 XS_INTERNAL(hm_xs_comb_cb) {
793 28 dXSARGS;
794 28 hm_clos *cl = hm_clos_of(aTHX_ cv);
795 SV *g, *res;
796 AV *list;
797 IV left;
798 /*
799 # uncoverable branch true class:ignore_covered_err
800 # uncoverable condition left count:1 class:ignore_covered_err
801 # uncoverable condition left count:2 class:ignore_covered_err */
802 28 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 28 0 28
if (!cl || items < 1) XSRETURN_EMPTY; 0 28 28
803 28 g = ST(0);
804 28 res = cl->a;
805 28 list = (AV *)SvRV(cl->c);
806 28 ▶ if (hmf_state(aTHX_ res) != HMF_PENDING) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ res) != HMF_PENDING) XSRETURN_EMPTY; 9 19 28
807  
808 19 switch (cl->i) { /* # uncoverable branch true count:5 class:ignore_covered_err */
809 3 case HM_COMB_WAIT_ALL: {
810 3 left = SvIV(cl->b) - 1;
811 3 sv_setiv(cl->b, left);
812 3 ▶ if (left == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (left == 0) { 1 2 3
813 1 AV *out = newAV();
814 1 SSize_t i, n = av_len(list) + 1;
815 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 1 3
816 2 SV **e = av_fetch(list, i, 0);
817 2 ▶ av_push(out, e ? newSVsv(*e) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(out, e ? newSVsv(*e) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
818 }
819 1 hmf_settle_av(aTHX_ res, HMF_DONE, out);
820 }
821 3 break;
822 }
823 5 case HM_COMB_WAIT_ANY: {
824 5 IV st = hm_any_state(aTHX_ g);
825 5 AV *out = newAV();
826 5 hm_any_values(aTHX_ g, st, out);
827 5 ▶ if (st == HMF_DONE || st == HMF_FAILED)
Branch: 2 branches
BranchTrueFalseTotal
if (st == HMF_DONE || st == HMF_FAILED) 2 3 5
if (st == HMF_DONE || st == HMF_FAILED) 1 1 2
828 4 hmf_settle_av(aTHX_ res, st, out);
829 else {
830 1 SvREFCNT_dec((SV *)out);
831 1 hmf_cancel(aTHX_ res);
832 }
833 5 break;
834 }
835 6 case HM_COMB_NEEDS_ALL: {
836 6 IV st = hm_any_state(aTHX_ g);
837 6 ▶ if (st == HMF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_FAILED) { 2 4 6
838 2 AV *out = newAV();
839 2 hm_any_values(aTHX_ g, st, out);
840 2 hmf_settle_av(aTHX_ res, HMF_FAILED, out);
841 2 break;
842 }
843 4 ▶ if (st == HMF_CANCELLED) { hmf_cancel(aTHX_ res); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_CANCELLED) { hmf_cancel(aTHX_ res); break; } 1 3 4
844 3 left = SvIV(cl->b) - 1;
845 3 sv_setiv(cl->b, left);
846 3 ▶ if (left == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (left == 0) { 1 2 3
847 1 AV *out = newAV();
848 1 SSize_t i, n = av_len(list) + 1;
849 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 1 3
850 2 SV **e = av_fetch(list, i, 0);
851 /*
852 # uncoverable branch false class:ignore_covered_err
853 # uncoverable condition right count:1 class:ignore_covered_err */
854 2 ▶ if (e) hm_any_values(aTHX_ *e, hm_any_state(aTHX_ *e), out);
Branch: 1 branch
BranchTrueFalseTotal
if (e) hm_any_values(aTHX_ *e, hm_any_state(aTHX_ *e), out); 2 0 2
855 }
856 1 hmf_settle_av(aTHX_ res, HMF_DONE, out);
857 }
858 3 break;
859 }
860 5 case HM_COMB_NEEDS_ANY: {
861 5 IV st = hm_any_state(aTHX_ g);
862 5 ▶ if (st == HMF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_DONE) { 2 3 5
863 2 AV *out = newAV();
864 2 hm_any_values(aTHX_ g, st, out);
865 2 hmf_settle_av(aTHX_ res, HMF_DONE, out);
866 2 break;
867 }
868 3 left = SvIV(cl->b) - 1;
869 3 sv_setiv(cl->b, left);
870 3 ▶ if (left == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (left == 0) { 1 2 3
871 1 AV *out = newAV();
872 1 hm_any_values(aTHX_ g, st, out);
873 1 hmf_settle_av(aTHX_ res, HMF_FAILED, out);
874 }
875 3 break;
876 }
877 }
878 /* The result has settled: the inputs still running lost (wait_any's
879 * others, the rest after needs_all's failure or needs_any's success)
880 * and are cancelled, as CPAN Future does; and the list is emptied, which
881 * breaks the cycle input -> callback -> list -> input that otherwise
882 * kept every input alive for as long as the slowest one. */
883 19 ▶ if (hmf_state(aTHX_ res) != HMF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ res) != HMF_PENDING) { 13 6 19
884 13 hmf_cancel_list(aTHX_ list);
885 13 av_clear(list);
886 }
887 19 XSRETURN_EMPTY;
888 }
889  
890 /* shared combinator builder called from xs/future.xs */
891 17 static SV *hmf_combine(pTHX_ SV *classname, SV **futs, SSize_t n, IV mode) {
892 17 SV *res = hmf_new(aTHX_ SvPV_nolen(classname));
893 17 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 3 14 17
894 3 ▶ if (mode == HM_COMB_NEEDS_ANY) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == HM_COMB_NEEDS_ANY) { 1 2 3
895 1 SV *msg = sv_2mortal(newSVpvs("needs_any on empty list\n"));
896 1 hmf_settle(aTHX_ res, HMF_FAILED, &msg, 1);
897 2 ▶ } else if (mode == HM_COMB_WAIT_ANY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (mode == HM_COMB_WAIT_ANY) { 1 1 2
898 1 SV *msg = sv_2mortal(newSVpvs("wait_any on empty list\n"));
899 1 hmf_settle(aTHX_ res, HMF_FAILED, &msg, 1);
900 } else {
901 1 hmf_settle(aTHX_ res, HMF_DONE, futs, n);
902 }
903 3 return res;
904 }
905 {
906 14 AV *list = newAV();
907 SV *list_rv, *count;
908 SSize_t i;
909 42 ▶ for (i = 0; i < n; i++) av_push(list, SvREFCNT_inc(futs[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(list, SvREFCNT_inc(futs[i])); 28 14 42
910 14 list_rv = sv_2mortal(newRV_noinc((SV *)list));
911 14 count = sv_2mortal(newSViv(n));
912 /* cancelling the result cancels what it waits on (hmf_cancel) */
913 14 av_store((AV *)SvRV(res), 3, newSVsv(list_rv));
914 42 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 28 14 42
915 28 SV *cb = hm_closure(aTHX_ hm_xs_comb_cb, res, count, list_rv,
916 NULL, mode, 0);
917 28 hm_any_on_ready(aTHX_ futs[i], cb);
918 28 SvREFCNT_dec(cb);
919 }
920 }
921 14 return res;
922 }
923  
924 /* ---- CPAN Future interop ------------------------------------------------- */
925  
926 /* mirror our resolution into a CPAN Future (a = the CPAN future) */
927 XS_INTERNAL(hm_xs_tocpan_cb);
928 0 XS_INTERNAL(hm_xs_tocpan_cb) {
929 0 dXSARGS;
930 0 hm_clos *cl = hm_clos_of(aTHX_ cv);
931 SV *f;
932 IV st;
933 /*
934 # uncoverable branch true class:ignore_covered_err
935 # uncoverable condition left count:1 class:ignore_covered_err
936 # uncoverable condition left count:2 class:ignore_covered_err */
937 0 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 0 0 0
if (!cl || items < 1) XSRETURN_EMPTY; 0 0 0
938 0 f = ST(0);
939 0 st = hmf_state(aTHX_ f);
940 {
941 0 dSP;
942 0 AV *vals = hmf_values_av(aTHX_ f);
943 0 ▶ SSize_t j, n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, n = vals ? av_len(vals) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 0 0
944 0 const char *meth = (st == HMF_DONE) ? "done"
945 0 ▶ : (st == HMF_FAILED) ? "fail"
Branch: 2 branches
BranchTrueFalseTotal
: (st == HMF_FAILED) ? "fail" 0 0 0
: (st == HMF_FAILED) ? "fail" 0 0 0
946 : "cancel";
947 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
948 0 ▶ XPUSHs(cl->a);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cl->a); 0 0 0
949 0 ▶ if (st == HMF_DONE || st == HMF_FAILED)
Branch: 2 branches
BranchTrueFalseTotal
if (st == HMF_DONE || st == HMF_FAILED) 0 0 0
if (st == HMF_DONE || st == HMF_FAILED) 0 0 0
950 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
951 0 SV **e = av_fetch(vals, j, 0);
952 /*
953 # uncoverable condition left count:1 class:ignore_covered_err
954 # uncoverable condition right count:2 class:ignore_covered_err */
955 0 ▶ XPUSHs(e ? *e : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(e ? *e : &PL_sv_undef); 0 0 0
XPUSHs(e ? *e : &PL_sv_undef); 0 0 0
956 }
957 0 PUTBACK;
958 0 call_method(meth, G_DISCARD | G_EVAL);
959 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
960 }
961 0 XSRETURN_EMPTY;
962 }
963  
964 /* mirror a foreign future's resolution into ours (a = ours) */
965 XS_INTERNAL(hm_xs_fromcpan_cb);
966 3 XS_INTERNAL(hm_xs_fromcpan_cb) {
967 3 dXSARGS;
968 3 hm_clos *cl = hm_clos_of(aTHX_ cv);
969 SV *o;
970 IV st;
971 /*
972 # uncoverable branch true class:ignore_covered_err
973 # uncoverable condition left count:1 class:ignore_covered_err
974 # uncoverable condition left count:2 class:ignore_covered_err */
975 3 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 3 0 3
if (!cl || items < 1) XSRETURN_EMPTY; 0 3 3
976 3 o = ST(0); /* # uncoverable statement class:ignore_covered_err */
977 /*
978 # uncoverable statement class:ignore_covered_err
979 # uncoverable branch true
980 # uncoverable condition left count:1 */
981 3 ▶ if (hmf_state(aTHX_ cl->a) != HMF_PENDING) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ cl->a) != HMF_PENDING) XSRETURN_EMPTY; 0 3 3
982 3 st = hm_any_state(aTHX_ o);
983 5 ▶ if (st == HMF_DONE || st == HMF_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (st == HMF_DONE || st == HMF_FAILED) { 2 1 3
if (st == HMF_DONE || st == HMF_FAILED) { 1 1 2
984 2 AV *out = newAV();
985 2 hm_any_values(aTHX_ o, st, out);
986 2 hmf_settle_av(aTHX_ cl->a, st, out);
987 } else {
988 1 hmf_cancel(aTHX_ cl->a);
989 }
990 3 XSRETURN_EMPTY;
991 }
992  
993 #endif /* HM_FUTURE_H */
« include/hyperman/hm_sa_bus_perl.h ↑ Summary include/hyperman/hm_compress.h »