| 1 |
|
| MODULE = DBIx::Loop PACKAGE = DBIx::Loop::Future |
| 2 |
|
| |
| 3 |
|
| # The AWAIT_* methods are the Future::AsyncAwait::Awaitable API, which is how |
| 4 |
|
| # `await $f` reaches one of these inside an async sub. Most are aliases of the |
| 5 |
|
| # method they correspond to, so the protocol costs nothing beyond the XSUB it |
| 6 |
|
| # already resolves to. |
| 7 |
|
| # |
| 8 |
|
| # This future has no cancelled state - see AWAIT_IS_CANCELLED - so the three |
| 9 |
|
| # cancellation methods take the constant-false/no-op form the specification |
| 10 |
|
| # provides for implementations without it. |
| 11 |
|
| |
| 12 |
|
| PROTOTYPES: DISABLE |
| 13 |
|
| |
| 14 |
|
| SV * |
| 15 |
|
| new(class) |
| 16 |
|
| SV *class |
| 17 |
|
| CODE: |
| 18 |
28 |
| RETVAL = dbil_future_new(aTHX_ |
| 19 |
56 |
▶ | SvROK(class) ? sv_reftype(SvRV(class), 1) : SvPV_nolen(class)); |
Branch: 1 branch
| Branch | True | False | Total |
| SvROK(class) ? sv_reftype(SvRV(class), 1) : SvPV_nolen(class)); |
0 |
28 |
28 |
|
| 20 |
|
| OUTPUT: |
| 21 |
|
| RETVAL |
| 22 |
|
| |
| 23 |
|
| # An already-settled future. Nothing can have registered on one this new, so |
| 24 |
|
| # neither settles through fire(): the callback queue is empty by construction. |
| 25 |
|
| SV * |
| 26 |
|
| AWAIT_NEW_DONE(class, ...) |
| 27 |
|
| SV *class |
| 28 |
|
| CODE: |
| 29 |
|
| { |
| 30 |
|
| dbil_future *f; |
| 31 |
2 |
| AV *res = newAV(); |
| 32 |
|
| int i; |
| 33 |
2 |
| RETVAL = dbil_future_new(aTHX_ |
| 34 |
4 |
▶ | SvROK(class) ? sv_reftype(SvRV(class), 1) : SvPV_nolen(class)); |
Branch: 1 branch
| Branch | True | False | Total |
| SvROK(class) ? sv_reftype(SvRV(class), 1) : SvPV_nolen(class)); |
0 |
2 |
2 |
|
| 35 |
2 |
| f = dbil_future_of(aTHX_ RETVAL); |
| 36 |
5 |
▶ | for (i = 1; i < items; i++) av_push(res, newSVsv(ST(i))); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) av_push(res, newSVsv(ST(i))); |
3 |
2 |
5 |
|
| 37 |
2 |
| f->result = newRV_noinc((SV *)res); |
| 38 |
2 |
| f->state = 1; |
| 39 |
|
| } |
| 40 |
|
| OUTPUT: |
| 41 |
|
| RETVAL |
| 42 |
|
| |
| 43 |
|
| SV * |
| 44 |
|
| AWAIT_NEW_FAIL(class, ...) |
| 45 |
|
| SV *class |
| 46 |
|
| CODE: |
| 47 |
|
| { |
| 48 |
|
| dbil_future *f; |
| 49 |
1 |
| RETVAL = dbil_future_new(aTHX_ |
| 50 |
2 |
▶ | SvROK(class) ? sv_reftype(SvRV(class), 1) : SvPV_nolen(class)); |
Branch: 1 branch
| Branch | True | False | Total |
| SvROK(class) ? sv_reftype(SvRV(class), 1) : SvPV_nolen(class)); |
0 |
1 |
1 |
|
| 51 |
1 |
| f = dbil_future_of(aTHX_ RETVAL); |
| 52 |
1 |
▶ | f->error = newSVsv(items > 1 ? ST(1) |
Branch: 1 branch
| Branch | True | False | Total |
| f->error = newSVsv(items > 1 ? ST(1) |
1 |
0 |
1 |
|
| 53 |
|
| : sv_2mortal(newSVpvs("Failed\n"))); |
| 54 |
1 |
| f->state = 2; |
| 55 |
|
| } |
| 56 |
|
| OUTPUT: |
| 57 |
|
| RETVAL |
| 58 |
|
| |
| 59 |
|
| # A new pending future of the same class. The instance is not modified and no |
| 60 |
|
| # per-instance state is copied; this future carries no loop of its own, so |
| 61 |
|
| # there is nothing else to hand on. |
| 62 |
|
| SV * |
| 63 |
|
| AWAIT_CLONE(self) |
| 64 |
|
| SV *self |
| 65 |
|
| CODE: |
| 66 |
1 |
| (void)dbil_future_of(aTHX_ self); /* croak early if not one */ |
| 67 |
1 |
| RETVAL = dbil_future_new(aTHX_ sv_reftype(SvRV(self), 1)); |
| 68 |
|
| OUTPUT: |
| 69 |
|
| RETVAL |
| 70 |
|
| |
| 71 |
|
| SV * |
| 72 |
|
| done(self, ...) |
| 73 |
|
| SV *self |
| 74 |
|
| ALIAS: |
| 75 |
|
| AWAIT_DONE = 1 |
| 76 |
|
| CODE: |
| 77 |
|
| { |
| 78 |
17 |
| dbil_future *f = dbil_future_of(aTHX_ self); |
| 79 |
17 |
| AV *res = newAV(); |
| 80 |
|
| int i; |
| 81 |
|
| PERL_UNUSED_VAR(ix); |
| 82 |
17 |
▶ | if (f->state) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->state) |
1 |
16 |
17 |
|
| 83 |
1 |
| croak("DBIx::Loop::Future: already settled"); |
| 84 |
34 |
▶ | for (i = 1; i < items; i++) av_push(res, newSVsv(ST(i))); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) av_push(res, newSVsv(ST(i))); |
18 |
16 |
34 |
|
| 85 |
16 |
| f->result = newRV_noinc((SV *)res); |
| 86 |
16 |
| f->state = 1; |
| 87 |
16 |
| dbil_future_fire(aTHX_ self, f); |
| 88 |
16 |
| RETVAL = SvREFCNT_inc(self); |
| 89 |
|
| } |
| 90 |
|
| OUTPUT: |
| 91 |
|
| RETVAL |
| 92 |
|
| |
| 93 |
|
| # Declared with a slurpy tail only so AWAIT_FAIL can share the body: the |
| 94 |
|
| # protocol may hand a failure more than one value, where fail() documents |
| 95 |
|
| # exactly one. The arity check below is the one xsubpp generated before. |
| 96 |
|
| SV * |
| 97 |
|
| fail(self, ...) |
| 98 |
|
| SV *self |
| 99 |
|
| ALIAS: |
| 100 |
|
| AWAIT_FAIL = 1 |
| 101 |
|
| CODE: |
| 102 |
|
| { |
| 103 |
|
| dbil_future *f; |
| 104 |
|
| PERL_UNUSED_VAR(ix); |
| 105 |
8 |
▶ | if (items < 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items < 2) |
1 |
7 |
8 |
|
| 106 |
1 |
| croak("Usage: DBIx::Loop::Future::fail(self, error)"); |
| 107 |
7 |
| f = dbil_future_of(aTHX_ self); |
| 108 |
7 |
▶ | if (f->state) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->state) |
0 |
7 |
7 |
|
| 109 |
0 |
| croak("DBIx::Loop::Future: already settled"); |
| 110 |
7 |
| f->error = newSVsv(ST(1)); |
| 111 |
7 |
| f->state = 2; |
| 112 |
7 |
| dbil_future_fire(aTHX_ self, f); |
| 113 |
7 |
| RETVAL = SvREFCNT_inc(self); |
| 114 |
|
| } |
| 115 |
|
| OUTPUT: |
| 116 |
|
| RETVAL |
| 117 |
|
| |
| 118 |
|
| SV * |
| 119 |
|
| on_ready(self, cb) |
| 120 |
|
| SV *self |
| 121 |
|
| SV *cb |
| 122 |
|
| ALIAS: |
| 123 |
|
| AWAIT_ON_READY = 1 |
| 124 |
|
| CODE: |
| 125 |
|
| { |
| 126 |
9 |
| dbil_future *f = dbil_future_of(aTHX_ self); |
| 127 |
|
| PERL_UNUSED_VAR(ix); |
| 128 |
9 |
▶ | if (!SvROK(cb) || SvTYPE(SvRV(cb)) != SVt_PVCV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(cb) || SvTYPE(SvRV(cb)) != SVt_PVCV) |
9 |
0 |
9 |
| if (!SvROK(cb) || SvTYPE(SvRV(cb)) != SVt_PVCV) |
0 |
9 |
9 |
|
| 129 |
0 |
| croak("DBIx::Loop::Future->on_ready: need a coderef"); |
| 130 |
9 |
▶ | if (f->state) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->state) { |
1 |
8 |
9 |
|
| 131 |
1 |
| dSP; |
| 132 |
1 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
1 |
1 |
|
| 133 |
1 |
▶ | EXTEND(SP, 1); PUSHs(self); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); PUSHs(self); PUTBACK; |
0 |
1 |
1 |
|
| 134 |
1 |
| call_sv(cb, G_DISCARD | G_EVAL); |
| 135 |
1 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
1 |
0 |
1 |
| if (SvTRUE(ERRSV)) |
0 |
1 |
1 |
|
| 136 |
0 |
▶ | warn("DBIx::Loop::Future: on_ready callback died: %s", |
Branch: 1 branch
| Branch | True | False | Total |
| warn("DBIx::Loop::Future: on_ready callback died: %s", |
0 |
0 |
0 |
|
| 137 |
|
| SvPV_nolen(ERRSV)); |
| 138 |
1 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
1 |
1 |
|
| 139 |
|
| } else { |
| 140 |
8 |
| av_push(f->cbs, newSVsv(cb)); |
| 141 |
|
| } |
| 142 |
9 |
| RETVAL = SvREFCNT_inc(self); |
| 143 |
|
| } |
| 144 |
|
| OUTPUT: |
| 145 |
|
| RETVAL |
| 146 |
|
| |
| 147 |
|
| # then($on_done [, $on_fail]) / else($on_fail) -> a new future. |
| 148 |
|
| # |
| 149 |
|
| # The continuation is an AV on the callback queue, not a closure, so a chain |
| 150 |
|
| # costs one array per link and settling it runs no Perl frame but the user's |
| 151 |
|
| # own callbacks. `else` is the same thing with only the failure half, which is |
| 152 |
|
| # why it is an ALIAS rather than a second body. |
| 153 |
|
| SV * |
| 154 |
|
| then(self, on_done = &PL_sv_undef, on_fail = &PL_sv_undef) |
| 155 |
|
| SV *self |
| 156 |
|
| SV *on_done |
| 157 |
|
| SV *on_fail |
| 158 |
|
| ALIAS: |
| 159 |
|
| else = 1 |
| 160 |
|
| CODE: |
| 161 |
|
| { |
| 162 |
|
| SV *next; |
| 163 |
|
| /* else($cb): the one argument is the failure handler */ |
| 164 |
2015 |
▶ | if (ix == 1) { on_fail = on_done; on_done = &PL_sv_undef; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix == 1) { on_fail = on_done; on_done = &PL_sv_undef; } |
1 |
2014 |
2015 |
|
| 165 |
2015 |
| (void)dbil_future_of(aTHX_ self); /* croak early if not one */ |
| 166 |
2015 |
| next = dbil_future_new(aTHX_ "DBIx::Loop::Future"); |
| 167 |
2015 |
| dbil_then_attach(aTHX_ self, next, on_done, on_fail); |
| 168 |
2015 |
| RETVAL = next; |
| 169 |
|
| } |
| 170 |
|
| OUTPUT: |
| 171 |
|
| RETVAL |
| 172 |
|
| |
| 173 |
|
| int |
| 174 |
|
| is_ready(self) |
| 175 |
|
| SV *self |
| 176 |
|
| ALIAS: |
| 177 |
|
| AWAIT_IS_READY = 1 |
| 178 |
|
| CODE: |
| 179 |
|
| PERL_UNUSED_VAR(ix); |
| 180 |
1488 |
▶ | RETVAL = dbil_future_of(aTHX_ self)->state != 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = dbil_future_of(aTHX_ self)->state != 0; |
1457 |
31 |
1488 |
|
| 181 |
|
| OUTPUT: |
| 182 |
|
| RETVAL |
| 183 |
|
| |
| 184 |
|
| int |
| 185 |
|
| is_done(self) |
| 186 |
|
| SV *self |
| 187 |
|
| CODE: |
| 188 |
60 |
▶ | RETVAL = dbil_future_of(aTHX_ self)->state == 1; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = dbil_future_of(aTHX_ self)->state == 1; |
31 |
29 |
60 |
|
| 189 |
|
| OUTPUT: |
| 190 |
|
| RETVAL |
| 191 |
|
| |
| 192 |
|
| int |
| 193 |
|
| is_failed(self) |
| 194 |
|
| SV *self |
| 195 |
|
| CODE: |
| 196 |
527 |
▶ | RETVAL = dbil_future_of(aTHX_ self)->state == 2; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = dbil_future_of(aTHX_ self)->state == 2; |
501 |
26 |
527 |
|
| 197 |
|
| OUTPUT: |
| 198 |
|
| RETVAL |
| 199 |
|
| |
| 200 |
|
| # This future settles exactly once, into done or failed; there is no third |
| 201 |
|
| # outcome and no cancel method to reach one. The protocol allows an |
| 202 |
|
| # implementation without cancellation to answer constantly here and to ignore |
| 203 |
|
| # the two registration methods, which is what these do. |
| 204 |
|
| int |
| 205 |
|
| AWAIT_IS_CANCELLED(self) |
| 206 |
|
| SV *self |
| 207 |
|
| CODE: |
| 208 |
3 |
| (void)dbil_future_of(aTHX_ self); /* croak early if not one */ |
| 209 |
3 |
▶ | RETVAL = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = 0; |
0 |
3 |
3 |
|
| 210 |
|
| OUTPUT: |
| 211 |
|
| RETVAL |
| 212 |
|
| |
| 213 |
|
| void |
| 214 |
|
| AWAIT_ON_CANCEL(self, thing) |
| 215 |
|
| SV *self |
| 216 |
|
| SV *thing |
| 217 |
|
| ALIAS: |
| 218 |
|
| AWAIT_CHAIN_CANCEL = 1 |
| 219 |
|
| CODE: |
| 220 |
|
| PERL_UNUSED_ARG(self); |
| 221 |
|
| PERL_UNUSED_ARG(thing); |
| 222 |
|
| PERL_UNUSED_VAR(ix); |
| 223 |
|
| |
| 224 |
|
| SV * |
| 225 |
|
| failure(self) |
| 226 |
|
| SV *self |
| 227 |
|
| CODE: |
| 228 |
|
| { |
| 229 |
22 |
| dbil_future *f = dbil_future_of(aTHX_ self); |
| 230 |
22 |
▶ | RETVAL = (f->state == 2 && f->error) ? newSVsv(f->error) : newSV(0); |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (f->state == 2 && f->error) ? newSVsv(f->error) : newSV(0); |
22 |
0 |
22 |
| RETVAL = (f->state == 2 && f->error) ? newSVsv(f->error) : newSV(0); |
22 |
0 |
22 |
|
| 231 |
|
| } |
| 232 |
|
| OUTPUT: |
| 233 |
|
| RETVAL |
| 234 |
|
| |
| 235 |
|
| # get already reads rather than waits, which is what AWAIT_GET wants. |
| 236 |
|
| # AWAIT_WAIT is meant to run the event system until the future is ready, and |
| 237 |
|
| # this distribution has no blocking wait to run - awaiting is the loop's job - |
| 238 |
|
| # so it is the same method, and a toplevel await on a pending future gets the |
| 239 |
|
| # same advice as a premature get. |
| 240 |
|
| void |
| 241 |
|
| get(self) |
| 242 |
|
| SV *self |
| 243 |
|
| ALIAS: |
| 244 |
|
| AWAIT_GET = 1 |
| 245 |
|
| AWAIT_WAIT = 2 |
| 246 |
|
| PPCODE: |
| 247 |
|
| { |
| 248 |
175 |
| dbil_future *f = dbil_future_of(aTHX_ self); |
| 249 |
|
| PERL_UNUSED_VAR(ix); |
| 250 |
175 |
▶ | if (f->state == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->state == 0) |
3 |
172 |
175 |
|
| 251 |
3 |
| croak("DBIx::Loop::Future->get: future is not ready " |
| 252 |
|
| "(await it on your event loop first)"); |
| 253 |
172 |
▶ | if (f->state == 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->state == 2) |
5 |
167 |
172 |
|
| 254 |
5 |
▶ | croak_sv(f->error ? f->error : sv_2mortal(newSVpvs("failed"))); |
Branch: 1 branch
| Branch | True | False | Total |
| croak_sv(f->error ? f->error : sv_2mortal(newSVpvs("failed"))); |
5 |
0 |
5 |
|
| 255 |
|
| { |
| 256 |
167 |
| AV *res = (AV *)SvRV(f->result); |
| 257 |
167 |
| SSize_t i, n = av_len(res) + 1; |
| 258 |
167 |
▶ | EXTEND(SP, n); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, n); |
167 |
0 |
167 |
| EXTEND(SP, n); |
0 |
167 |
167 |
|
| 259 |
340 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
173 |
167 |
340 |
|
| 260 |
173 |
| SV **e = av_fetch(res, i, 0); |
| 261 |
173 |
▶ | PUSHs(e && *e ? sv_2mortal(newSVsv(*e)) : &PL_sv_undef); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHs(e && *e ? sv_2mortal(newSVsv(*e)) : &PL_sv_undef); |
173 |
0 |
173 |
| PUSHs(e && *e ? sv_2mortal(newSVsv(*e)) : &PL_sv_undef); |
173 |
0 |
173 |
|
| 262 |
|
| } |
| 263 |
|
| } |
| 264 |
|
| } |