include/fetch/ft_future.h

filter: Statementstmt 67.8% 335 / 494 Branchbran 42.5% 187 / 440 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.8% 522 / 934 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/backend_kqueue.c ↑ Summary xs/response.xs »
1 #ifndef HM_FUTURE_H
2 #define HM_FUTURE_H
3  
4 /* Fetch::Future - native array-slot async primitive.
5 * Slots: 0 state, 1 result/failure AV, 2 callbacks, 3 weak upstream,
6 * 4 owning loop adapter (see hmf_pin_loop).
7 * Continuations are trampolined through a fire queue, so long then-chains
8 * run iteratively (bounded stack) with or without a loop.
9 *
10 * Everything here is C: chaining (then/else/followed_by/transform), the
11 * combinators, and CPAN Future interop are implemented as anonymous XSUBs
12 * carrying their captured state in magic (hm_clos) - no Perl closures. */
13  
14 #define HMF_PENDING 0
15 #define HMF_DONE 1
16 #define HMF_FAILED 2
17 #define HMF_CANCELLED 3
18  
19 static AV *hm_fq; /* pending (cb, future) pairs */
20 static int hm_fq_active;
21  
22 struct hm_loop;
23 static void hm_loop_run(pTHX_ struct hm_loop *loop, SV *until);
24 static struct hm_loop *hm_cur_loop;
25  
26 /* ---- C closures: anon XSUBs with captured SVs attached via magic ------- */
27  
28 typedef struct {
29 SV *a, *b, *c, *d;
30 IV i;
31 UV u;
32 } hm_clos;
33  
34 369 static int hm_clos_free(pTHX_ SV *sv, MAGIC *mg) {
35 369 hm_clos *cl = (hm_clos *)mg->mg_ptr;
36 PERL_UNUSED_VAR(sv);
37 369 ▶ if (cl) {
Branch: 1 branch
BranchTrueFalseTotal
if (cl) { 369 0 369
38 369 ▶ if (cl->a) SvREFCNT_dec(cl->a);
Branch: 1 branch
BranchTrueFalseTotal
if (cl->a) SvREFCNT_dec(cl->a); 209 160 369
39 369 ▶ if (cl->b) SvREFCNT_dec(cl->b);
Branch: 1 branch
BranchTrueFalseTotal
if (cl->b) SvREFCNT_dec(cl->b); 204 165 369
40 369 ▶ if (cl->c) SvREFCNT_dec(cl->c);
Branch: 1 branch
BranchTrueFalseTotal
if (cl->c) SvREFCNT_dec(cl->c); 192 177 369
41 369 ▶ if (cl->d) SvREFCNT_dec(cl->d);
Branch: 1 branch
BranchTrueFalseTotal
if (cl->d) SvREFCNT_dec(cl->d); 0 369 369
42 369 free(cl);
43 369 mg->mg_ptr = NULL;
44 }
45 369 return 0;
46 }
47  
48 static MGVTBL hm_clos_vtbl = { NULL, NULL, NULL, NULL, hm_clos_free,
49 NULL, NULL, NULL };
50  
51 369 static SV *hm_closure(pTHX_ XSUBADDR_t body, SV *a, SV *b, SV *c, SV *d,
52 IV i, UV u) {
53 369 CV *cv = newXS(NULL, body, __FILE__);
54 369 hm_clos *cl = (hm_clos *)calloc(1, sizeof(hm_clos));
55 369 ▶ cl->a = a ? SvREFCNT_inc(a) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cl->a = a ? SvREFCNT_inc(a) : NULL; 209 160 369
56 369 ▶ cl->b = b ? SvREFCNT_inc(b) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cl->b = b ? SvREFCNT_inc(b) : NULL; 204 165 369
57 369 ▶ cl->c = c ? SvREFCNT_inc(c) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cl->c = c ? SvREFCNT_inc(c) : NULL; 192 177 369
58 369 ▶ cl->d = d ? SvREFCNT_inc(d) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cl->d = d ? SvREFCNT_inc(d) : NULL; 0 369 369
59 369 cl->i = i;
60 369 cl->u = u;
61 369 sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &hm_clos_vtbl, (char *)cl, 0);
62 369 return newRV_noinc((SV *)cv);
63 }
64  
65 798 static hm_clos *hm_clos_of(pTHX_ CV *cv) {
66 798 MAGIC *mg = mg_findext((SV *)cv, PERL_MAGIC_ext, &hm_clos_vtbl);
67 798 ▶ return mg ? (hm_clos *)mg->mg_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return mg ? (hm_clos *)mg->mg_ptr : NULL; 798 0 798
68 }
69  
70 /* ---- core state ---------------------------------------------------------- */
71  
72 1683 static IV hmf_state(pTHX_ SV *self) {
73 SV **s;
74 1683 ▶ if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) return HMF_PENDING;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) return HMF_PENDING; 1683 0 1683
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) return HMF_PENDING; 0 1683 1683
75 1683 s = av_fetch((AV *)SvRV(self), 0, 0);
76 1683 ▶ return s ? SvIV(*s) : HMF_PENDING;
Branch: 1 branch
BranchTrueFalseTotal
return s ? SvIV(*s) : HMF_PENDING; 1683 0 1683
77 }
78  
79 173 static int hmf_is_future(pTHX_ SV *sv) {
80 173 ▶ return sv && SvROK(sv) && SvOBJECT(SvRV(sv))
Branch: 2 branches
BranchTrueFalseTotal
return sv && SvROK(sv) && SvOBJECT(SvRV(sv)) 172 1 173
return sv && SvROK(sv) && SvOBJECT(SvRV(sv)) 172 0 172
81 346 ▶ && sv_derived_from(sv, "Fetch::Future");
Branch: 2 branches
BranchTrueFalseTotal
&& sv_derived_from(sv, "Fetch::Future"); 173 0 173
&& sv_derived_from(sv, "Fetch::Future"); 172 0 172
82 }
83  
84 /* Future-compatible: ours, or any object with an on_ready method. */
85 3 static int hm_is_awaitable(pTHX_ SV *sv) {
86 3 ▶ if (hmf_is_future(aTHX_ sv)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_is_future(aTHX_ sv)) return 1; 2 1 3
87 1 ▶ if (sv && SvROK(sv) && SvOBJECT(SvRV(sv))) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && SvROK(sv) && SvOBJECT(SvRV(sv))) { 1 0 1
if (sv && SvROK(sv) && SvOBJECT(SvRV(sv))) { 0 1 1
if (sv && SvROK(sv) && SvOBJECT(SvRV(sv))) { 0 0 0
88 0 HV *stash = SvSTASH(SvRV(sv));
89 0 ▶ 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; 0 0 0
if (stash && gv_fetchmethod_autoload(stash, "on_ready", 0)) return 1; 0 0 0
90 }
91 1 return 0;
92 }
93  
94 403 static AV *hmf_values_av(pTHX_ SV *self) {
95 403 SV **v = av_fetch((AV *)SvRV(self), 1, 0);
96 403 ▶ return (v && SvROK(*v)) ? (AV *)SvRV(*v) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvROK(*v)) ? (AV *)SvRV(*v) : NULL; 403 0 403
return (v && SvROK(*v)) ? (AV *)SvRV(*v) : NULL; 403 0 403
97 }
98  
99 /* ---- the owning loop (slot 4) -------------------------------------------
100 * A pending request future is only ever resolved by the loop its socket is
101 * armed on, so it has to carry that loop with it. Without the pin the await
102 * path had nothing but the process-global $Fetch::Future::AWAIT, which every
103 * Fetch->new overwrites - so two agents on two loops meant awaiting one
104 * agent's future pumped the *other* agent's loop, which has no watcher for
105 * that socket and blocks in the kernel forever.
106 *
107 * Weak, like the upstream link: the loop already owns the watcher that owns
108 * the connection that holds this future, so a strong ref here would close
109 * that ring and strand the loop's descriptors whenever a request is
110 * abandoned mid-flight. A pin that goes undef means the loop is gone, which
111 * is exactly when the await should fall back rather than drive it. */
112 306 static void hmf_pin_loop(pTHX_ SV *self, SV *loop_sv) {
113 SV *w;
114 306 ▶ if (!loop_sv || !SvROK(loop_sv)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!loop_sv || !SvROK(loop_sv)) return; 302 4 306
if (!loop_sv || !SvROK(loop_sv)) return; 0 302 302
115 302 ▶ if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) return; 302 0 302
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) return; 0 302 302
116 302 w = newSVsv(loop_sv);
117 302 sv_rvweaken(w);
118 302 av_store((AV *)SvRV(self), 4, w);
119 }
120  
121 438 static SV *hmf_pinned_loop(pTHX_ SV *self) {
122 SV **s;
123 438 ▶ if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) return NULL; 438 0 438
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVAV)) return NULL; 0 438 438
124 438 s = av_fetch((AV *)SvRV(self), 4, 0);
125 438 ▶ return (s && SvROK(*s)) ? *s : NULL; /* undef once the loop is gone */
Branch: 2 branches
BranchTrueFalseTotal
return (s && SvROK(*s)) ? *s : NULL; /* undef once the loop is gone */ 271 167 438
return (s && SvROK(*s)) ? *s : NULL; /* undef once the loop is gone */ 271 0 271
126 }
127  
128 /* ---- trampoline ---------------------------------------------------------- */
129  
130 796 static void hmf_pump(pTHX) {
131 796 ▶ if (hm_fq_active) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_fq_active) return; 33 763 796
132 763 ENTER;
133 763 SAVEINT(hm_fq_active);
134 763 hm_fq_active = 1;
135 1757 ▶ while (av_len(hm_fq) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (av_len(hm_fq) >= 0) { 231 763 994
136 231 SV *cb = sv_2mortal(av_shift(hm_fq));
137 231 SV *fut = sv_2mortal(av_shift(hm_fq));
138 231 dSP;
139 231 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 231 231
140 231 ▶ XPUSHs(fut);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(fut); 0 231 231
141 231 PUTBACK;
142 /* user code runs here: trap death into a warning so one bad
143 * callback cannot take down the loop (the guard used to live in
144 * ft_call0; C closures are now dispatched directly) */
145 231 call_sv(cb, G_DISCARD | G_EVAL);
146 231 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 231 0 231
if (SvTRUE(ERRSV)) 0 231 231
147 0 ▶ warn("Fetch: future callback died: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("Fetch: future callback died: %s", SvPV_nolen(ERRSV)); 0 0 0
148 }
149 763 LEAVE;
150 }
151  
152 2 static void hmf_enqueue(pTHX_ SV *cb, SV *self) {
153 2 av_push(hm_fq, SvREFCNT_inc(cb));
154 2 av_push(hm_fq, SvREFCNT_inc(self));
155 2 hmf_pump(aTHX);
156 2 }
157  
158 359 static void hmf_fire(pTHX_ SV *self) {
159 359 AV *av = (AV *)SvRV(self);
160 359 SV **slot = av_fetch(av, 2, 0);
161 359 ▶ SV *cbs = (slot && SvOK(*slot)) ? *slot : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *cbs = (slot && SvOK(*slot)) ? *slot : NULL; 205 154 359
SV *cbs = (slot && SvOK(*slot)) ? *slot : NULL; 205 0 205
162 359 ▶ if (!cbs) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbs) return; 154 205 359
163 205 SvREFCNT_inc(cbs);
164 205 av_store(av, 2, &PL_sv_undef);
165 229 ▶ if (SvROK(cbs) && SvTYPE(SvRV(cbs)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cbs) && SvTYPE(SvRV(cbs)) == SVt_PVAV) { 205 0 205
if (SvROK(cbs) && SvTYPE(SvRV(cbs)) == SVt_PVAV) { 24 181 205
166 24 AV *list = (AV *)SvRV(cbs);
167 24 SSize_t i, n = av_len(list) + 1;
168 72 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 48 24 72
169 48 SV **e = av_fetch(list, i, 0);
170 48 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 48 0 48
171 48 av_push(hm_fq, SvREFCNT_inc(*e));
172 48 av_push(hm_fq, SvREFCNT_inc(self));
173 }
174 }
175 } else {
176 181 av_push(hm_fq, SvREFCNT_inc(cbs));
177 181 av_push(hm_fq, SvREFCNT_inc(self));
178 }
179 205 SvREFCNT_dec(cbs);
180 205 hmf_pump(aTHX);
181 }
182  
183 186 static void hmf_settle(pTHX_ SV *self, IV state, SV **vals, SSize_t n) {
184 186 AV *av = (AV *)SvRV(self);
185 186 AV *val = newAV();
186 SSize_t i;
187 364 ▶ 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])); 178 186 364
188 186 av_store(av, 0, newSViv(state));
189 186 av_store(av, 1, newRV_noinc((SV *)val));
190 186 hmf_fire(aTHX_ self);
191 186 }
192  
193 173 static void hmf_settle_av(pTHX_ SV *self, IV state, AV *vals) {
194 173 AV *av = (AV *)SvRV(self);
195 173 av_store(av, 0, newSViv(state));
196 173 av_store(av, 1, newRV_noinc((SV *)vals));
197 173 hmf_fire(aTHX_ self);
198 173 }
199  
200 366 static SV *hmf_new(pTHX_ const char *classname) {
201 366 AV *av = newAV();
202 SV *rv;
203 366 av_extend(av, 3);
204 366 av_store(av, 0, newSViv(HMF_PENDING));
205 366 rv = newRV_noinc((SV *)av);
206 366 sv_bless(rv, gv_stashpv(classname, GV_ADD));
207 366 return rv;
208 }
209  
210 154 static const char *hmf_class_of(pTHX_ SV *self) {
211 154 HV *stash = SvSTASH(SvRV(self));
212 154 ▶ return stash ? HvNAME(stash) : "Fetch::Future";
Branch: 7 branches
BranchTrueFalseTotal
return stash ? HvNAME(stash) : "Fetch::Future"; 154 0 154
return stash ? HvNAME(stash) : "Fetch::Future"; 154 0 154
return stash ? HvNAME(stash) : "Fetch::Future"; 154 0 154
return stash ? HvNAME(stash) : "Fetch::Future"; 0 154 154
return stash ? HvNAME(stash) : "Fetch::Future"; 0 0 0
return stash ? HvNAME(stash) : "Fetch::Future"; 154 0 154
return stash ? HvNAME(stash) : "Fetch::Future"; 0 154 154
213 }
214  
215 /* Resolve a loop-created future with no values (readiness signals). */
216 0 static void hmf_done0(pTHX_ SV *fut) {
217 0 ▶ if (hmf_state(aTHX_ fut) == HMF_PENDING)
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ fut) == HMF_PENDING) 0 0 0
218 0 hmf_settle(aTHX_ fut, HMF_DONE, NULL, 0);
219 0 }
220  
221 233 static void hmf_on_ready(pTHX_ SV *self, SV *cb) {
222 233 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) { 2 231 233
223 2 hmf_enqueue(aTHX_ cb, self);
224 } else {
225 231 AV *av = (AV *)SvRV(self);
226 231 SV **slot = av_fetch(av, 2, 0);
227 231 ▶ if (!slot || !SvOK(*slot)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!slot || !SvOK(*slot)) { 24 207 231
if (!slot || !SvOK(*slot)) { 0 24 24
228 207 av_store(av, 2, newSVsv(cb));
229 24 ▶ } else if (SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 24 0 24
} else if (SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 0 24 24
230 0 av_push((AV *)SvRV(*slot), newSVsv(cb));
231 } else {
232 24 AV *list = newAV();
233 24 av_push(list, SvREFCNT_inc(*slot));
234 24 av_push(list, newSVsv(cb));
235 24 av_store(av, 2, newRV_noinc((SV *)list));
236 }
237 }
238 233 }
239  
240 /* on_ready that works on ours (direct) or any Future-compatible object. */
241 47 static void hm_any_on_ready(pTHX_ SV *f, SV *cb) {
242 47 ▶ if (hmf_is_future(aTHX_ f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_is_future(aTHX_ f)) { 47 0 47
243 47 hmf_on_ready(aTHX_ f, cb);
244 } else {
245 0 dSP;
246 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
247 0 ▶ XPUSHs(f);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(f); 0 0 0
248 0 ▶ XPUSHs(cb);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cb); 0 0 0
249 0 PUTBACK;
250 0 call_method("on_ready", G_DISCARD);
251 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
252 }
253 47 }
254  
255 153 static void hmf_set_upstream(pTHX_ SV *derived, SV *upstream) {
256 153 SV *w = newSVsv(upstream);
257 153 sv_rvweaken(w);
258 153 av_store((AV *)SvRV(derived), 3, w);
259 /* Inherit the loop down the chain. ->get is called on the tail of a
260 * then-chain, but only the head - the request future - knows which loop
261 * the socket is armed on, and this is the one choke point every derived
262 * future passes through. */
263 153 ▶ if (!hmf_pinned_loop(aTHX_ derived))
Branch: 1 branch
BranchTrueFalseTotal
if (!hmf_pinned_loop(aTHX_ derived)) 153 0 153
264 153 hmf_pin_loop(aTHX_ derived, hmf_pinned_loop(aTHX_ upstream));
265 153 }
266  
267 7 static void hmf_cancel(pTHX_ SV *self) {
268 AV *av;
269 SV **up;
270 7 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 0 7 7
271 7 av = (AV *)SvRV(self);
272 7 hmf_settle(aTHX_ self, HMF_CANCELLED, NULL, 0);
273 /* propagate up a then-chain: cancelling a derived future cancels its
274 * still-pending upstream */
275 7 up = av_fetch(av, 3, 0);
276 7 ▶ if (up && SvROK(*up) && hmf_is_future(aTHX_ *up)
Branch: 3 branches
BranchTrueFalseTotal
if (up && SvROK(*up) && hmf_is_future(aTHX_ *up) 0 7 7
if (up && SvROK(*up) && hmf_is_future(aTHX_ *up) 0 0 0
if (up && SvROK(*up) && hmf_is_future(aTHX_ *up) 0 0 0
277 0 ▶ && hmf_state(aTHX_ *up) == HMF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
&& hmf_state(aTHX_ *up) == HMF_PENDING) { 0 0 0
278 0 dSP;
279 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
280 0 ▶ XPUSHs(*up);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*up); 0 0 0
281 0 PUTBACK;
282 0 call_method("cancel", G_DISCARD);
283 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
284 }
285 }
286  
287 /* Await on the loop the future was pinned to. Returns 1 if that loop was
288 * driven (whatever the outcome), 0 if it is a kind we cannot drive, in which
289 * case the caller falls back to hm_cur_loop / $AWAIT as before. */
290 121 static int hmf_await_pinned(pTHX_ SV *lp, SV *self) {
291 121 ▶ if (!sv_isobject(lp)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(lp)) return 0; 0 121 121
292 /* The native loop is a blessed IV holding the ft_loop*; drive it straight
293 * from C rather than paying a method call per await. */
294 121 ▶ if (sv_derived_from(lp, "Fetch::Loop::Standalone")
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(lp, "Fetch::Loop::Standalone") 121 0 121
295 121 ▶ && SvTYPE(SvRV(lp)) == SVt_IV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(lp)) == SVt_IV) { 0 121 121
296 0 hm_loop_run(aTHX_ INT2PTR(struct hm_loop *, SvIV(SvRV(lp))), self);
297 0 return 1;
298 }
299 /* Foreign adapters (Hyperman, IO::Async, AnyEvent, third parties) expose
300 * the same body their install_await installs globally. */
301 121 ▶ if (gv_fetchmethod_autoload(SvSTASH(SvRV(lp)), "_ft_await", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (gv_fetchmethod_autoload(SvSTASH(SvRV(lp)), "_ft_await", 0)) { 121 0 121
302 121 dSP;
303 121 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 121 121
304 121 ▶ XPUSHs(lp);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(lp); 0 121 121
305 121 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 121 121
306 121 PUTBACK;
307 121 call_method("_ft_await", G_DISCARD);
308 121 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 121 0 121
309 121 return 1;
310 }
311 0 return 0;
312 }
313  
314 176 static void hmf_await(pTHX_ SV *self) {
315 SV *lp;
316 176 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 45 131 176
317 /* The pinned loop outranks both hm_cur_loop and the global: it is the
318 * only one guaranteed to be watching this future's socket. */
319 131 ▶ if ((lp = hmf_pinned_loop(aTHX_ self)) && hmf_await_pinned(aTHX_ lp, self)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((lp = hmf_pinned_loop(aTHX_ self)) && hmf_await_pinned(aTHX_ lp, self)) { 121 10 131
if ((lp = hmf_pinned_loop(aTHX_ self)) && hmf_await_pinned(aTHX_ lp, self)) { 121 0 121
320 121 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 121 0 121
321 0 croak("Fetch::Future: loop stopped before the future was ready");
322 }
323 10 ▶ if (hm_cur_loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_cur_loop) { 0 10 10
324 0 hm_loop_run(aTHX_ hm_cur_loop, self);
325 0 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 0 0 0
326 0 croak("Fetch::Future: loop stopped before the future was ready");
327 }
328 {
329 10 SV *aw = get_sv("Fetch::Future::AWAIT", 0);
330 10 ▶ if (aw && SvOK(aw)) {
Branch: 2 branches
BranchTrueFalseTotal
if (aw && SvOK(aw)) { 10 0 10
if (aw && SvOK(aw)) { 9 1 10
331 9 dSP;
332 9 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 9 9
333 9 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 9 9
334 9 PUTBACK;
335 9 call_sv(aw, G_DISCARD);
336 9 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 9 9
337 9 ▶ if (hmf_state(aTHX_ self) != HMF_PENDING) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ self) != HMF_PENDING) return; 9 0 9
338 }
339 }
340 1 croak("Cannot await a pending Fetch::Future without an event loop");
341 }
342  
343 /* ---- foreign-future compatibility --------------------------------------- */
344  
345 /* state of ours (direct) or a foreign Future-like via method calls */
346 77 static IV hm_any_state(pTHX_ SV *f) {
347 77 dSP;
348 77 IV st = HMF_PENDING;
349 int n;
350 77 ▶ 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); 77 0 77
351 0 ENTER; SAVETMPS;
352 0 ▶ PUSHMARK(SP); XPUSHs(f); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 0 0
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 0 0
353 0 n = call_method("is_done", G_SCALAR | G_EVAL);
354 0 SPAGAIN;
355 0 ▶ if (n && SvTRUE(TOPs)) st = HMF_DONE;
Branch: 2 branches
BranchTrueFalseTotal
if (n && SvTRUE(TOPs)) st = HMF_DONE; 0 0 0
if (n && SvTRUE(TOPs)) st = HMF_DONE; 0 0 0
356 0 (void)POPs;
357 0 PUTBACK;
358 0 ▶ if (st == HMF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_PENDING) { 0 0 0
359 0 ▶ PUSHMARK(SP); XPUSHs(f); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 0 0
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 0 0
360 0 n = call_method("is_failed", G_SCALAR | G_EVAL);
361 0 SPAGAIN;
362 0 ▶ if (n && SvTRUE(TOPs)) st = HMF_FAILED;
Branch: 2 branches
BranchTrueFalseTotal
if (n && SvTRUE(TOPs)) st = HMF_FAILED; 0 0 0
if (n && SvTRUE(TOPs)) st = HMF_FAILED; 0 0 0
363 0 (void)POPs;
364 0 PUTBACK;
365 }
366 0 ▶ if (st == HMF_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_PENDING) { 0 0 0
367 0 ▶ PUSHMARK(SP); XPUSHs(f); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 0 0
PUSHMARK(SP); XPUSHs(f); PUTBACK; 0 0 0
368 0 n = call_method("is_cancelled", G_SCALAR | G_EVAL);
369 0 SPAGAIN;
370 0 ▶ if (n && SvTRUE(TOPs)) st = HMF_CANCELLED;
Branch: 2 branches
BranchTrueFalseTotal
if (n && SvTRUE(TOPs)) st = HMF_CANCELLED; 0 0 0
if (n && SvTRUE(TOPs)) st = HMF_CANCELLED; 0 0 0
371 0 (void)POPs;
372 0 PUTBACK;
373 }
374 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
375 0 return st;
376 }
377  
378 /* collect a method call's list results into out; returns 1 if it died */
379 4 static int hm_call_list(pTHX_ SV *invocant, const char *method, SV *cb,
380 SV **args, SSize_t nargs, AV *out) {
381 4 dSP;
382 SSize_t n, i;
383 4 int err = 0;
384 4 ENTER; SAVETMPS;
385 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
386 4 ▶ if (invocant) XPUSHs(invocant);
Branch: 2 branches
BranchTrueFalseTotal
if (invocant) XPUSHs(invocant); 0 4 4
if (invocant) XPUSHs(invocant); 0 0 0
387 8 ▶ for (i = 0; i < nargs; i++) XPUSHs(args[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) XPUSHs(args[i]); 0 4 4
for (i = 0; i < nargs; i++) XPUSHs(args[i]); 4 4 8
388 4 PUTBACK;
389 0 n = method ? call_method(method, G_ARRAY | G_EVAL)
390 4 ▶ : call_sv(cb, G_ARRAY | G_EVAL);
Branch: 1 branch
BranchTrueFalseTotal
: call_sv(cb, G_ARRAY | G_EVAL); 0 4 4
391 4 SPAGAIN;
392 4 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 4 0 4
if (SvTRUE(ERRSV)) { 0 4 4
393 0 err = 1;
394 0 SP -= n;
395 } else {
396 4 SV **base = SP - n + 1;
397 8 ▶ 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])); 4 4 8
398 4 SP -= n;
399 }
400 4 PUTBACK;
401 4 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 4 0 4
402 4 return err;
403 }
404  
405 /* result (done) or failure (failed) values of ours or a foreign future */
406 46 static void hm_any_values(pTHX_ SV *f, IV st, AV *out) {
407 46 ▶ if (hmf_is_future(aTHX_ f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_is_future(aTHX_ f)) { 46 0 46
408 46 AV *vals = hmf_values_av(aTHX_ f);
409 46 ▶ SSize_t i, n = vals ? av_len(vals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = vals ? av_len(vals) + 1 : 0; 46 0 46
410 92 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 46 46 92
411 46 SV **e = av_fetch(vals, i, 0);
412 46 ▶ av_push(out, e ? newSVsv(*e) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(out, e ? newSVsv(*e) : newSV(0)); 46 0 46
413 }
414 0 ▶ } else if (st == HMF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st == HMF_DONE) { 0 0 0
415 0 hm_call_list(aTHX_ f, "get", NULL, NULL, 0, out);
416 0 ▶ } else if (st == HMF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st == HMF_FAILED) { 0 0 0
417 0 hm_call_list(aTHX_ f, "failure", NULL, NULL, 0, out);
418 }
419 46 }
420  
421 /* ---- chaining ------------------------------------------------------------ */
422  
423 XS_INTERNAL(hm_xs_chain_cb);
424 14 XS_INTERNAL(hm_xs_chain_cb) {
425 14 dXSARGS;
426 14 hm_clos *cl = hm_clos_of(aTHX_ cv);
427 14 SV *g = ST(0);
428 SV *next;
429 IV st;
430 14 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 14 0 14
if (!cl || items < 1) XSRETURN_EMPTY; 0 14 14
431 14 next = cl->a;
432 14 st = hm_any_state(aTHX_ g);
433 28 ▶ if (st == HMF_DONE || st == HMF_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (st == HMF_DONE || st == HMF_FAILED) { 0 14 14
if (st == HMF_DONE || st == HMF_FAILED) { 0 0 0
434 14 AV *vals = newAV();
435 14 hm_any_values(aTHX_ g, st, vals);
436 14 hmf_settle_av(aTHX_ next, st, vals);
437 } else {
438 0 hmf_cancel(aTHX_ next);
439 }
440 14 XSRETURN_EMPTY;
441 }
442  
443 /* Perl's _chain_into: a returned future flattens; plain values resolve. */
444 3 static void hmf_chain_into(pTHX_ SV *next, AV *vals) {
445 3 ▶ 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; 3 0 3
446 5 ▶ if (first && hm_is_awaitable(aTHX_ *first)) {
Branch: 2 branches
BranchTrueFalseTotal
if (first && hm_is_awaitable(aTHX_ *first)) { 3 0 3
if (first && hm_is_awaitable(aTHX_ *first)) { 2 1 3
447 2 SV *cb = hm_closure(aTHX_ hm_xs_chain_cb, next, NULL, NULL, NULL, 0, 0);
448 2 hm_any_on_ready(aTHX_ *first, cb);
449 2 SvREFCNT_dec(cb);
450 2 SvREFCNT_dec((SV *)vals);
451 1 ▶ } else if (av_len(vals) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (av_len(vals) < 0) { 0 1 1
452 0 av_push(vals, newSV(0)); /* done(undef), as the reference impl did */
453 0 hmf_settle_av(aTHX_ next, HMF_DONE, vals);
454 } else {
455 1 hmf_settle_av(aTHX_ next, HMF_DONE, vals);
456 }
457 3 }
458  
459 /* then/else/followed_by continuation. i: 0 = then (b=on_done, c=on_fail),
460 * 1 = followed_by (b=cb). */
461 XS_INTERNAL(hm_xs_then_cb);
462 3 XS_INTERNAL(hm_xs_then_cb) {
463 3 dXSARGS;
464 3 hm_clos *cl = hm_clos_of(aTHX_ cv);
465 SV *f, *next;
466 IV st;
467 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
468 3 f = ST(0);
469 3 next = cl->a;
470 3 st = hmf_state(aTHX_ f);
471  
472 3 ▶ if (cl->i == 1) { /* followed_by: cb($f) */
Branch: 1 branch
BranchTrueFalseTotal
if (cl->i == 1) { /* followed_by: cb($f) */ 0 3 3
473 0 AV *out = newAV();
474 0 SV *arg = f;
475 0 ▶ 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)) { 0 0 0
476 0 ▶ SV *err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_2mortal(newSVsv(ERRSV)); 0 0 0
477 0 hmf_settle(aTHX_ next, HMF_FAILED, &err, 1);
478 0 SvREFCNT_dec((SV *)out);
479 } else {
480 0 hmf_chain_into(aTHX_ next, out);
481 }
482 0 XSRETURN_EMPTY;
483 }
484  
485 6 ▶ if (st == HMF_DONE || st == HMF_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (st == HMF_DONE || st == HMF_FAILED) { 1 2 3
if (st == HMF_DONE || st == HMF_FAILED) { 1 0 1
486 3 ▶ SV *user = (st == HMF_DONE) ? cl->b : cl->c;
Branch: 1 branch
BranchTrueFalseTotal
SV *user = (st == HMF_DONE) ? cl->b : cl->c; 2 1 3
487 3 AV *vals = hmf_values_av(aTHX_ f);
488 3 ▶ SSize_t n = vals ? av_len(vals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = vals ? av_len(vals) + 1 : 0; 3 0 3
489 6 ▶ if (user && SvOK(user)) {
Branch: 2 branches
BranchTrueFalseTotal
if (user && SvOK(user)) { 3 0 3
if (user && SvOK(user)) { 3 0 3
490 3 AV *out = newAV();
491 3 ▶ SV **args = vals ? AvARRAY(vals) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **args = vals ? AvARRAY(vals) : NULL; 3 0 3
492 3 ▶ 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)) { 0 3 3
493 0 ▶ SV *err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_2mortal(newSVsv(ERRSV)); 0 0 0
494 0 hmf_settle(aTHX_ next, HMF_FAILED, &err, 1);
495 0 SvREFCNT_dec((SV *)out);
496 } else {
497 3 hmf_chain_into(aTHX_ next, out);
498 }
499 } else {
500 0 AV *copy = newAV();
501 SSize_t i;
502 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
503 0 SV **e = av_fetch(vals, i, 0);
504 0 ▶ av_push(copy, e ? newSVsv(*e) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(copy, e ? newSVsv(*e) : newSV(0)); 0 0 0
505 }
506 0 hmf_settle_av(aTHX_ next, st, copy);
507 }
508 } else {
509 0 hmf_cancel(aTHX_ next);
510 }
511 3 XSRETURN_EMPTY;
512 }
513  
514 /* transform continuation: b = done-mapper, c = fail-mapper */
515 XS_INTERNAL(hm_xs_transform_cb);
516 1 XS_INTERNAL(hm_xs_transform_cb) {
517 1 dXSARGS;
518 1 hm_clos *cl = hm_clos_of(aTHX_ cv);
519 SV *f, *next;
520 IV st;
521 1 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 1 0 1
if (!cl || items < 1) XSRETURN_EMPTY; 0 1 1
522 1 f = ST(0);
523 1 next = cl->a;
524 1 st = hmf_state(aTHX_ f);
525 2 ▶ if (st == HMF_DONE || st == HMF_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (st == HMF_DONE || st == HMF_FAILED) { 0 1 1
if (st == HMF_DONE || st == HMF_FAILED) { 0 0 0
526 1 ▶ SV *mapper = (st == HMF_DONE) ? cl->b : cl->c;
Branch: 1 branch
BranchTrueFalseTotal
SV *mapper = (st == HMF_DONE) ? cl->b : cl->c; 1 0 1
527 1 AV *vals = hmf_values_av(aTHX_ f);
528 1 ▶ SSize_t n = vals ? av_len(vals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = vals ? av_len(vals) + 1 : 0; 1 0 1
529 1 AV *out = newAV();
530 2 ▶ if (mapper && SvOK(mapper)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mapper && SvOK(mapper)) { 1 0 1
if (mapper && SvOK(mapper)) { 1 0 1
531 1 ▶ SV **args = vals ? AvARRAY(vals) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **args = vals ? AvARRAY(vals) : NULL; 1 0 1
532 1 ▶ 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)) { 0 1 1
533 0 ▶ SV *err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_2mortal(newSVsv(ERRSV)); 0 0 0
534 0 hmf_settle(aTHX_ next, HMF_FAILED, &err, 1);
535 0 SvREFCNT_dec((SV *)out);
536 0 XSRETURN_EMPTY;
537 }
538 } else {
539 SSize_t i;
540 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
541 0 SV **e = av_fetch(vals, i, 0);
542 0 ▶ av_push(out, e ? newSVsv(*e) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(out, e ? newSVsv(*e) : newSV(0)); 0 0 0
543 }
544 }
545 1 hmf_settle_av(aTHX_ next, st, out);
546 } else {
547 0 hmf_cancel(aTHX_ next);
548 }
549 1 XSRETURN_EMPTY;
550 }
551  
552 /* on_done / on_fail filter: b = user cb, i = required state */
553 XS_INTERNAL(hm_xs_ondone_cb);
554 3 XS_INTERNAL(hm_xs_ondone_cb) {
555 3 dXSARGS;
556 3 hm_clos *cl = hm_clos_of(aTHX_ cv);
557 SV *f;
558 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
559 3 f = ST(0);
560 3 ▶ if (hmf_state(aTHX_ f) == cl->i) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ f) == cl->i) { 2 1 3
561 2 AV *vals = hmf_values_av(aTHX_ f);
562 2 ▶ SSize_t n = vals ? av_len(vals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = vals ? av_len(vals) + 1 : 0; 2 0 2
563 2 dSP;
564 SSize_t j;
565 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2 2
566 4 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 2 2 4
567 2 SV **e = av_fetch(vals, j, 0);
568 2 ▶ XPUSHs(e ? *e : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(e ? *e : &PL_sv_undef); 0 2 2
XPUSHs(e ? *e : &PL_sv_undef); 2 0 2
569 }
570 2 PUTBACK;
571 2 call_sv(cl->b, G_DISCARD);
572 2 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 2 2
573 }
574 3 XSRETURN_EMPTY;
575 }
576  
577 /* Run one on_cancel target: a code ref is called with the future, anything
578 * else is treated as a future and cancelled. Both spellings are needed
579 * because the await protocol registers each - a code block through
580 * AWAIT_ON_CANCEL, a future through AWAIT_CHAIN_CANCEL. */
581 5 static void hmf_cancel_target(pTHX_ SV *thing, SV *self) {
582 5 dSP;
583 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
584 5 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 5 5
585 5 ▶ if (SvTYPE(SvRV(thing)) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(thing)) == SVt_PVCV) { 4 1 5
586 4 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 4 4
587 4 PUTBACK;
588 4 call_sv(thing, G_DISCARD);
589 } else {
590 1 ▶ XPUSHs(thing);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(thing); 0 1 1
591 1 PUTBACK;
592 1 call_method("cancel", G_DISCARD);
593 }
594 5 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 4 5
595 }
596  
597 /* on_cancel filter: b = target. Cancel callbacks ride the on_ready list
598 * rather than a slot of their own, so this fires on every settlement and
599 * drops everything that is not a cancellation. */
600 XS_INTERNAL(hm_xs_oncancel_cb);
601 6 XS_INTERNAL(hm_xs_oncancel_cb) {
602 6 dXSARGS;
603 6 hm_clos *cl = hm_clos_of(aTHX_ cv);
604 SV *f;
605 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
606 6 f = ST(0);
607 6 ▶ if (hmf_state(aTHX_ f) == HMF_CANCELLED)
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ f) == HMF_CANCELLED) 4 2 6
608 4 hmf_cancel_target(aTHX_ cl->b, f);
609 6 XSRETURN_EMPTY;
610 }
611  
612 /* ---- convergent combinators --------------------------------------------- *
613 * a = result future, b = remaining-count SV, c = subfutures AV ref,
614 * i = mode. */
615 #define HM_COMB_WAIT_ALL 0
616 #define HM_COMB_WAIT_ANY 1
617 #define HM_COMB_NEEDS_ALL 2
618 #define HM_COMB_NEEDS_ANY 3
619  
620 XS_INTERNAL(hm_xs_comb_cb);
621 32 XS_INTERNAL(hm_xs_comb_cb) {
622 32 dXSARGS;
623 32 hm_clos *cl = hm_clos_of(aTHX_ cv);
624 SV *g, *res;
625 AV *list;
626 IV left;
627 32 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 32 0 32
if (!cl || items < 1) XSRETURN_EMPTY; 0 32 32
628 32 g = ST(0);
629 32 res = cl->a;
630 32 list = (AV *)SvRV(cl->c);
631 32 ▶ if (hmf_state(aTHX_ res) != HMF_PENDING) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ res) != HMF_PENDING) XSRETURN_EMPTY; 0 32 32
632  
633 32 switch (cl->i) {
634 0 case HM_COMB_WAIT_ALL: {
635 0 left = SvIV(cl->b) - 1;
636 0 sv_setiv(cl->b, left);
637 0 ▶ if (left == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (left == 0) { 0 0 0
638 0 AV *out = newAV();
639 0 SSize_t i, n = av_len(list) + 1;
640 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
641 0 SV **e = av_fetch(list, i, 0);
642 0 ▶ av_push(out, e ? newSVsv(*e) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(out, e ? newSVsv(*e) : newSV(0)); 0 0 0
643 }
644 0 hmf_settle_av(aTHX_ res, HMF_DONE, out);
645 }
646 0 break;
647 }
648 0 case HM_COMB_WAIT_ANY: {
649 0 IV st = hm_any_state(aTHX_ g);
650 0 AV *out = newAV();
651 0 hm_any_values(aTHX_ g, st, out);
652 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
653 0 hmf_settle_av(aTHX_ res, st, out);
654 else {
655 0 SvREFCNT_dec((SV *)out);
656 0 hmf_cancel(aTHX_ res);
657 }
658 0 break;
659 }
660 31 case HM_COMB_NEEDS_ALL: {
661 31 IV st = hm_any_state(aTHX_ g);
662 31 ▶ if (st == HMF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_FAILED) { 0 31 31
663 0 AV *out = newAV();
664 0 hm_any_values(aTHX_ g, st, out);
665 0 hmf_settle_av(aTHX_ res, HMF_FAILED, out);
666 0 break;
667 }
668 31 ▶ if (st == HMF_CANCELLED) { hmf_cancel(aTHX_ res); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_CANCELLED) { hmf_cancel(aTHX_ res); break; } 0 31 31
669 31 left = SvIV(cl->b) - 1;
670 31 sv_setiv(cl->b, left);
671 31 ▶ if (left == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (left == 0) { 5 26 31
672 5 AV *out = newAV();
673 5 SSize_t i, n = av_len(list) + 1;
674 36 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 31 5 36
675 31 SV **e = av_fetch(list, i, 0);
676 31 ▶ 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); 31 0 31
677 }
678 5 hmf_settle_av(aTHX_ res, HMF_DONE, out);
679 }
680 31 break;
681 }
682 1 case HM_COMB_NEEDS_ANY: {
683 1 IV st = hm_any_state(aTHX_ g);
684 1 ▶ if (st == HMF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_DONE) { 1 0 1
685 1 AV *out = newAV();
686 1 hm_any_values(aTHX_ g, st, out);
687 1 hmf_settle_av(aTHX_ res, HMF_DONE, out);
688 1 break;
689 }
690 0 left = SvIV(cl->b) - 1;
691 0 sv_setiv(cl->b, left);
692 0 ▶ if (left == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (left == 0) { 0 0 0
693 0 AV *out = newAV();
694 0 hm_any_values(aTHX_ g, st, out);
695 0 hmf_settle_av(aTHX_ res, HMF_FAILED, out);
696 }
697 0 break;
698 }
699 }
700 32 XSRETURN_EMPTY;
701 }
702  
703 /* shared combinator builder called from xs/future.xs */
704 6 static SV *hmf_combine(pTHX_ SV *classname, SV **futs, SSize_t n, IV mode) {
705 6 SV *res = hmf_new(aTHX_ SvPV_nolen(classname));
706 6 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 0 6 6
707 0 ▶ if (mode == HM_COMB_NEEDS_ANY) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == HM_COMB_NEEDS_ANY) { 0 0 0
708 0 SV *msg = sv_2mortal(newSVpvs("needs_any on empty list\n"));
709 0 hmf_settle(aTHX_ res, HMF_FAILED, &msg, 1);
710 0 ▶ } else if (mode == HM_COMB_WAIT_ANY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (mode == HM_COMB_WAIT_ANY) { 0 0 0
711 0 SV *msg = sv_2mortal(newSVpvs("wait_any on empty list\n"));
712 0 hmf_settle(aTHX_ res, HMF_FAILED, &msg, 1);
713 } else {
714 0 hmf_settle(aTHX_ res, HMF_DONE, futs, n);
715 }
716 0 return res;
717 }
718 {
719 6 AV *list = newAV();
720 SV *list_rv, *count;
721 SSize_t i;
722 39 ▶ 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])); 33 6 39
723 6 list_rv = sv_2mortal(newRV_noinc((SV *)list));
724 6 count = sv_2mortal(newSViv(n));
725 39 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 33 6 39
726 33 SV *cb = hm_closure(aTHX_ hm_xs_comb_cb, res, count, list_rv,
727 NULL, mode, 0);
728 33 hm_any_on_ready(aTHX_ futs[i], cb);
729 33 SvREFCNT_dec(cb);
730 }
731 }
732 6 return res;
733 }
734  
735 /* ---- CPAN Future interop ------------------------------------------------- */
736  
737 /* mirror our resolution into a CPAN Future (a = the CPAN future) */
738 XS_INTERNAL(hm_xs_tocpan_cb);
739 0 XS_INTERNAL(hm_xs_tocpan_cb) {
740 0 dXSARGS;
741 0 hm_clos *cl = hm_clos_of(aTHX_ cv);
742 SV *f;
743 IV st;
744 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
745 0 f = ST(0);
746 0 st = hmf_state(aTHX_ f);
747 {
748 0 dSP;
749 0 AV *vals = hmf_values_av(aTHX_ f);
750 0 ▶ SSize_t j, n = vals ? av_len(vals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, n = vals ? av_len(vals) + 1 : 0; 0 0 0
751 0 const char *meth = (st == HMF_DONE) ? "done"
752 0 ▶ : (st == HMF_FAILED) ? "fail"
Branch: 2 branches
BranchTrueFalseTotal
: (st == HMF_FAILED) ? "fail" 0 0 0
: (st == HMF_FAILED) ? "fail" 0 0 0
753 : "cancel";
754 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
755 0 ▶ XPUSHs(cl->a);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cl->a); 0 0 0
756 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
757 0 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 0 0 0
758 0 SV **e = av_fetch(vals, j, 0);
759 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
760 }
761 0 PUTBACK;
762 0 call_method(meth, G_DISCARD | G_EVAL);
763 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
764 }
765 0 XSRETURN_EMPTY;
766 }
767  
768 /* mirror a foreign future's resolution into ours (a = ours) */
769 XS_INTERNAL(hm_xs_fromcpan_cb);
770 0 XS_INTERNAL(hm_xs_fromcpan_cb) {
771 0 dXSARGS;
772 0 hm_clos *cl = hm_clos_of(aTHX_ cv);
773 SV *o;
774 IV st;
775 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
776 0 o = ST(0);
777 0 st = hm_any_state(aTHX_ o);
778 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
779 0 AV *out = newAV();
780 0 hm_any_values(aTHX_ o, st, out);
781 0 hmf_settle_av(aTHX_ cl->a, st, out);
782 } else {
783 0 hmf_cancel(aTHX_ cl->a);
784 }
785 0 XSRETURN_EMPTY;
786 }
787  
788 #endif /* HM_FUTURE_H */
« include/fetch/backend_kqueue.c ↑ Summary xs/response.xs »