| 1 |
|
| MODULE = DBIx::Loop PACKAGE = DBIx::Loop::Loop::Hyperman |
| 2 |
|
| |
| 3 |
|
| # The pure-XS Hyperman loop adapter (dbil_hm.h). Implements the Perl seam |
| 4 |
|
| # (add_reader / add_writer / remove / timer / new_future / await) over |
| 5 |
|
| # Hyperman's C ABI, plus cancel_timer, and carries the dbil_vt C vtable so |
| 6 |
|
| # backends dispatch readiness C-to-C with no Perl call frame. |
| 7 |
|
| |
| 8 |
|
| SV * |
| 9 |
|
| new(class, ...) |
| 10 |
|
| SV *class |
| 11 |
|
| CODE: |
| 12 |
|
| { |
| 13 |
0 |
| const hm_abi *A = dbil_hm(aTHX); |
| 14 |
0 |
| SV *loop_sv = NULL; |
| 15 |
|
| void *loop; |
| 16 |
|
| dbil_hm_ctx *c; |
| 17 |
|
| HV *self; |
| 18 |
|
| int i; |
| 19 |
0 |
▶ | const char *cls = (SvROK(class) && SvOBJECT(SvRV(class))) |
Branch: 1 branch
| Branch | True | False | Total |
| const char *cls = (SvROK(class) && SvOBJECT(SvRV(class))) |
0 |
0 |
0 |
|
| 20 |
0 |
▶ | ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); |
Branch: 7 branches
| Branch | True | False | Total |
| ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); |
0 |
0 |
0 |
| ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); |
0 |
0 |
0 |
| ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); |
0 |
0 |
0 |
| ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); |
0 |
0 |
0 |
| ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); |
0 |
0 |
0 |
| ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); |
0 |
0 |
0 |
| ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); |
0 |
0 |
0 |
|
| 21 |
0 |
▶ | if (!A) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!A) |
0 |
0 |
0 |
|
| 22 |
0 |
| croak("DBIx::Loop::Loop::Hyperman: no Hyperman C ABI version %d - " |
| 23 |
|
| "either Hyperman is not loaded (use Hyperman first) or it " |
| 24 |
|
| "predates the ABI (needs Hyperman 0.10 or newer)", |
| 25 |
|
| HM_ABI_VERSION); |
| 26 |
0 |
▶ | for (i = 1; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) { |
0 |
0 |
0 |
|
| 27 |
0 |
| STRLEN kl; const char *k = SvPV_const(ST(i), kl); |
| 28 |
0 |
▶ | if (kl == 4 && memEQ(k, "loop", 4) && SvOK(ST(i + 1))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (kl == 4 && memEQ(k, "loop", 4) && SvOK(ST(i + 1))) |
0 |
0 |
0 |
| if (kl == 4 && memEQ(k, "loop", 4) && SvOK(ST(i + 1))) |
0 |
0 |
0 |
| if (kl == 4 && memEQ(k, "loop", 4) && SvOK(ST(i + 1))) |
0 |
0 |
0 |
|
| 29 |
0 |
| loop_sv = ST(i + 1); |
| 30 |
|
| } |
| 31 |
0 |
▶ | if (loop_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop_sv) { |
0 |
0 |
0 |
|
| 32 |
0 |
| loop = A->loop_of_sv(aTHX_ loop_sv); /* croaks if not one */ |
| 33 |
0 |
| loop_sv = newSVsv(loop_sv); |
| 34 |
0 |
▶ | } else if ((loop = A->cur_loop(aTHX))) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((loop = A->cur_loop(aTHX))) { |
0 |
0 |
0 |
|
| 35 |
0 |
| loop_sv = A->sv_of_loop(aTHX_ loop); /* +1, we own it */ |
| 36 |
|
| } else { |
| 37 |
|
| /* no loop given, none running: make one (IO::Async parity) */ |
| 38 |
0 |
| dSP; int n; |
| 39 |
0 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
0 |
0 |
|
| 40 |
0 |
▶ | EXTEND(SP, 1); mPUSHp("Hyperman::Loop", 14); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); mPUSHp("Hyperman::Loop", 14); PUTBACK; |
0 |
0 |
0 |
|
| 41 |
0 |
| n = call_method("new", G_SCALAR); |
| 42 |
0 |
| SPAGAIN; |
| 43 |
0 |
▶ | if (n > 0) { SV *s = POPs; loop_sv = newSVsv(s); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { SV *s = POPs; loop_sv = newSVsv(s); } |
0 |
0 |
0 |
|
| 44 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 45 |
0 |
▶ | if (!loop_sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!loop_sv) |
0 |
0 |
0 |
|
| 46 |
0 |
| croak("DBIx::Loop::Loop::Hyperman: Hyperman::Loop->new " |
| 47 |
|
| "returned nothing"); |
| 48 |
0 |
| loop = A->loop_of_sv(aTHX_ loop_sv); |
| 49 |
|
| } |
| 50 |
0 |
| c = dbil_hm_ctx_new(A, loop); |
| 51 |
0 |
| self = newHV(); |
| 52 |
0 |
| (void)hv_stores(self, "loop", loop_sv); |
| 53 |
0 |
| (void)hv_stores(self, "_ctx", newSViv(PTR2IV(c))); |
| 54 |
0 |
| (void)hv_stores(self, "_vt", newSViv(PTR2IV(&c->vt))); |
| 55 |
0 |
| RETVAL = sv_bless(newRV_noinc((SV *)self), gv_stashpv(cls, GV_ADD)); |
| 56 |
|
| } |
| 57 |
|
| OUTPUT: |
| 58 |
|
| RETVAL |
| 59 |
|
| |
| 60 |
|
| # the underlying Hyperman::Loop object |
| 61 |
|
| SV * |
| 62 |
|
| loop(self) |
| 63 |
|
| SV *self |
| 64 |
|
| CODE: |
| 65 |
|
| { |
| 66 |
|
| SV **e; |
| 67 |
0 |
| (void)dbil_hm_ctx_of(aTHX_ self); /* validates */ |
| 68 |
0 |
| e = hv_fetchs((HV *)SvRV(self), "loop", 0); |
| 69 |
0 |
▶ | RETVAL = (e && *e) ? newSVsv(*e) : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (e && *e) ? newSVsv(*e) : &PL_sv_undef; |
0 |
0 |
0 |
| RETVAL = (e && *e) ? newSVsv(*e) : &PL_sv_undef; |
0 |
0 |
0 |
|
| 70 |
|
| } |
| 71 |
|
| OUTPUT: |
| 72 |
|
| RETVAL |
| 73 |
|
| |
| 74 |
|
| void |
| 75 |
|
| add_reader(self, fd, cb) |
| 76 |
|
| SV *self |
| 77 |
|
| int fd |
| 78 |
|
| SV *cb |
| 79 |
|
| ALIAS: |
| 80 |
|
| add_writer = 1 |
| 81 |
|
| CODE: |
| 82 |
0 |
▶ | dbil_hm_add_io(aTHX_ self, fd, ix ? HM_ABI_WRITE : HM_ABI_READ, cb); |
Branch: 1 branch
| Branch | True | False | Total |
| dbil_hm_add_io(aTHX_ self, fd, ix ? HM_ABI_WRITE : HM_ABI_READ, cb); |
0 |
0 |
0 |
|
| 83 |
|
| |
| 84 |
|
| void |
| 85 |
|
| remove(self, fd) |
| 86 |
|
| SV *self |
| 87 |
|
| int fd |
| 88 |
|
| CODE: |
| 89 |
0 |
| dbil_hm_remove(aTHX_ self, fd); |
| 90 |
|
| |
| 91 |
|
| # One-shot timer; returns a handle for cancel_timer. The handle dies when the |
| 92 |
|
| # callback fires - only cancel a timer that has not fired. |
| 93 |
|
| SV * |
| 94 |
|
| timer(self, secs, cb) |
| 95 |
|
| SV *self |
| 96 |
|
| double secs |
| 97 |
|
| SV *cb |
| 98 |
|
| CODE: |
| 99 |
|
| { |
| 100 |
0 |
| dbil_hm_ctx *c = dbil_hm_ctx_of(aTHX_ self); |
| 101 |
|
| dbil_hm_tmr *tm; |
| 102 |
0 |
| Newxz(tm, 1, dbil_hm_tmr); |
| 103 |
0 |
| tm->cb = newSVsv(cb); |
| 104 |
0 |
| tm->t = c->A->timer(aTHX_ c->loop, secs, dbil_hm_timer_tramp, tm); |
| 105 |
0 |
| RETVAL = newSViv(PTR2IV(tm)); |
| 106 |
|
| } |
| 107 |
|
| OUTPUT: |
| 108 |
|
| RETVAL |
| 109 |
|
| |
| 110 |
|
| void |
| 111 |
|
| cancel_timer(self, handle) |
| 112 |
|
| SV *self |
| 113 |
|
| SV *handle |
| 114 |
|
| CODE: |
| 115 |
|
| { |
| 116 |
0 |
| dbil_hm_ctx *c = dbil_hm_ctx_of(aTHX_ self); |
| 117 |
|
| dbil_hm_tmr *tm; |
| 118 |
0 |
▶ | if (!SvIOK(handle)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvIOK(handle)) |
0 |
0 |
0 |
|
| 119 |
0 |
| croak("cancel_timer: not a timer handle"); |
| 120 |
0 |
| tm = INT2PTR(dbil_hm_tmr *, SvIV(handle)); |
| 121 |
0 |
| c->A->timer_cancel(aTHX_ c->loop, tm->t); |
| 122 |
0 |
| SvREFCNT_dec(tm->cb); |
| 123 |
0 |
| Safefree(tm); |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
|
| # a pending Hyperman::Future (the ecosystem-native future) |
| 127 |
|
| SV * |
| 128 |
|
| new_future(self) |
| 129 |
|
| SV *self |
| 130 |
|
| CODE: |
| 131 |
0 |
| RETVAL = dbil_hm_ctx_of(aTHX_ self)->A->future_new(aTHX); |
| 132 |
|
| OUTPUT: |
| 133 |
|
| RETVAL |
| 134 |
|
| |
| 135 |
|
| # run the loop until $future is ready; takes Hyperman::Future natively and |
| 136 |
|
| # anything with on_ready (DBIx::Loop::Future) through a C bridge |
| 137 |
|
| SV * |
| 138 |
|
| await(self, future) |
| 139 |
|
| SV *self |
| 140 |
|
| SV *future |
| 141 |
|
| CODE: |
| 142 |
0 |
| dbil_hm_await(aTHX_ dbil_hm_ctx_of(aTHX_ self), future); |
| 143 |
0 |
| RETVAL = newSVsv(future); |
| 144 |
|
| OUTPUT: |
| 145 |
|
| RETVAL |
| 146 |
|
| |
| 147 |
|
| void |
| 148 |
|
| DESTROY(self) |
| 149 |
|
| SV *self |
| 150 |
|
| CODE: |
| 151 |
|
| { |
| 152 |
|
| HV *h; |
| 153 |
|
| SV **e; |
| 154 |
0 |
▶ | if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) XSRETURN_EMPTY; |
0 |
0 |
0 |
| if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) XSRETURN_EMPTY; |
0 |
0 |
0 |
|
| 155 |
0 |
| h = (HV *)SvRV(self); |
| 156 |
0 |
| e = hv_fetchs(h, "_ctx", 0); |
| 157 |
0 |
▶ | if (e && *e && SvIOK(*e)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvIOK(*e)) { |
0 |
0 |
0 |
| if (e && *e && SvIOK(*e)) { |
0 |
0 |
0 |
| if (e && *e && SvIOK(*e)) { |
0 |
0 |
0 |
|
| 158 |
0 |
| dbil_hm_ctx *c = INT2PTR(dbil_hm_ctx *, SvIV(*e)); |
| 159 |
|
| /* drop any still-registered Perl-seam watchers so the loop never |
| 160 |
|
| * fires into freed callbacks; skip during global destruction */ |
| 161 |
0 |
▶ | if (!PL_dirty) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PL_dirty) { |
0 |
0 |
0 |
|
| 162 |
|
| int m; |
| 163 |
0 |
▶ | for (m = 0; m < 2; m++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (m = 0; m < 2; m++) { |
0 |
0 |
0 |
|
| 164 |
0 |
▶ | HV *cbs = dbil_hm_cbh(aTHX_ self, |
Branch: 1 branch
| Branch | True | False | Total |
| HV *cbs = dbil_hm_cbh(aTHX_ self, |
0 |
0 |
0 |
|
| 165 |
|
| m ? HM_ABI_WRITE : HM_ABI_READ, 0); |
| 166 |
0 |
▶ | if (cbs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cbs) { |
0 |
0 |
0 |
|
| 167 |
|
| HE *he; |
| 168 |
0 |
| hv_iterinit(cbs); |
| 169 |
0 |
▶ | while ((he = hv_iternext(cbs))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(cbs))) { |
0 |
0 |
0 |
|
| 170 |
|
| I32 kl; |
| 171 |
0 |
| const char *k = hv_iterkey(he, &kl); |
| 172 |
0 |
▶ | c->A->io_unwatch(aTHX_ c->loop, atoi(k), |
Branch: 1 branch
| Branch | True | False | Total |
| c->A->io_unwatch(aTHX_ c->loop, atoi(k), |
0 |
0 |
0 |
|
| 173 |
|
| m ? HM_ABI_WRITE : HM_ABI_READ); |
| 174 |
|
| } |
| 175 |
|
| } |
| 176 |
|
| } |
| 177 |
|
| } |
| 178 |
0 |
| Safefree(c); |
| 179 |
0 |
| (void)hv_delete(h, "_ctx", 4, G_DISCARD); |
| 180 |
0 |
| (void)hv_delete(h, "_vt", 3, G_DISCARD); |
| 181 |
|
| } |
| 182 |
|
| } |