include/dbil_hm.h

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 207 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/DBIx/Loop/Loop/Hyperman.pm ↑ Summary xs/loop_hyperman.xs »
1 #ifndef DBIL_HM_H
2 #define DBIL_HM_H
3  
4 /* DBIx::Loop::Loop::Hyperman - the pure-XS loop adapter, over Hyperman's
5 * public C ABI (hm_abi.h, vendored at a pinned HM_ABI_VERSION). The table is
6 * resolved lazily at runtime via Hyperman::_abi_ptr - no link-time coupling,
7 * no build-time Hyperman dependency; when Hyperman is not loaded (or its ABI
8 * is older than ours) the adapter croaks with a clear message and every other
9 * backend/adapter keeps working.
10 *
11 * The adapter implements the Perl seam (add_reader/add_writer/remove/timer/
12 * new_future/await, in xs/loop_hyperman.xs) so the conformance suite drives
13 * it like any other adapter, and fills a dbil_vt (dbil_vt.h) so backends can
14 * bypass the Perl seam entirely: worker-fd readiness runs C to C.
15 *
16 * Requires dbil_pool.h (dbil_closure) before this file. */
17  
18 #include "hm_abi.h"
19  
20 static const hm_abi *DBIL_HM = NULL; /* resolved on first use */
21  
22 0 static const hm_abi *dbil_hm(pTHX) {
23 0 ▶ if (!DBIL_HM) {
Branch: 1 branch
BranchTrueFalseTotal
if (!DBIL_HM) { 0 0 0
24 0 dSP;
25 int count;
26 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 0 0
27 0 count = call_pv("Hyperman::_abi_ptr", G_SCALAR | G_EVAL);
28 0 SPAGAIN;
29 0 ▶ if (!SvTRUE(ERRSV) && count > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) { 0 0 0
if (!SvTRUE(ERRSV) && count > 0) { 0 0 0
if (!SvTRUE(ERRSV) && count > 0) { 0 0 0
30 0 IV p = POPi;
31 0 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 0 0 0
32 0 const hm_abi *a = INT2PTR(const hm_abi *, p);
33 0 ▶ if (a->abi_version >= HM_ABI_VERSION) DBIL_HM = a;
Branch: 1 branch
BranchTrueFalseTotal
if (a->abi_version >= HM_ABI_VERSION) DBIL_HM = a; 0 0 0
34 }
35 0 ▶ } else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
} else if (count > 0) (void)POPs; 0 0 0
36 0 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 0 0
37 }
38 0 return DBIL_HM;
39 }
40  
41 /* per-adapter state; the embedded vtable's ctx points back at this struct */
42 typedef struct dbil_hm_ctx {
43 const hm_abi *A;
44 void *loop; /* opaque hm_loop handle */
45 dbil_vt vt;
46 } dbil_hm_ctx;
47  
48 0 static dbil_hm_ctx *dbil_hm_ctx_of(pTHX_ SV *self) {
49 SV **e;
50 0 ▶ if (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV 0 0 0
if (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV 0 0 0
51 0 ▶ && (e = hv_fetchs((HV *)SvRV(self), "_ctx", 0)) && *e && SvIOK(*e))
Branch: 3 branches
BranchTrueFalseTotal
&& (e = hv_fetchs((HV *)SvRV(self), "_ctx", 0)) && *e && SvIOK(*e)) 0 0 0
&& (e = hv_fetchs((HV *)SvRV(self), "_ctx", 0)) && *e && SvIOK(*e)) 0 0 0
&& (e = hv_fetchs((HV *)SvRV(self), "_ctx", 0)) && *e && SvIOK(*e)) 0 0 0
52 0 return INT2PTR(dbil_hm_ctx *, SvIV(*e));
53 0 croak("DBIx::Loop::Loop::Hyperman: not an initialised adapter");
54 return NULL; /* not reached */
55 }
56  
57 /* ---- trampolines: ABI C callbacks -> stored Perl callbacks ---------------
58 * ABI contract: callbacks must not croak, so Perl callbacks run under G_EVAL
59 * and errors become warnings. */
60  
61 0 static void dbil_hm_io_tramp(pTHX_ int fd, int mask, void *ud) {
62 0 SV *cb = (SV *)ud;
63 0 dSP;
64 PERL_UNUSED_VAR(fd);
65 PERL_UNUSED_VAR(mask);
66 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 0 0
67 0 call_sv(cb, G_DISCARD | G_EVAL);
68 0 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 0 0 0
if (SvTRUE(ERRSV)) 0 0 0
69 0 ▶ warn("DBIx::Loop::Loop::Hyperman: io callback died: %s",
Branch: 1 branch
BranchTrueFalseTotal
warn("DBIx::Loop::Loop::Hyperman: io callback died: %s", 0 0 0
70 SvPV_nolen(ERRSV));
71 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
72 0 }
73  
74 typedef struct { SV *cb; hm_abi_timer *t; } dbil_hm_tmr;
75  
76 0 static void dbil_hm_timer_tramp(pTHX_ void *ud) {
77 0 dbil_hm_tmr *tm = (dbil_hm_tmr *)ud;
78 0 SV *cb = tm->cb;
79 0 dSP;
80 0 Safefree(tm); /* handle is dead once we fire */
81 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 0 0
82 0 call_sv(cb, G_DISCARD | G_EVAL);
83 0 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 0 0 0
if (SvTRUE(ERRSV)) 0 0 0
84 0 ▶ warn("DBIx::Loop::Loop::Hyperman: timer callback died: %s",
Branch: 1 branch
BranchTrueFalseTotal
warn("DBIx::Loop::Loop::Hyperman: timer callback died: %s", 0 0 0
85 SvPV_nolen(ERRSV));
86 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
87 0 SvREFCNT_dec(cb);
88 0 }
89  
90 /* ---- Perl-seam internals (called from xs/loop_hyperman.xs) --------------- */
91  
92 /* Stored Perl io callbacks live in the adapter's _r/_w hashes keyed by fd;
93 * the stored SV itself is the watcher's ud, so replace/remove and the loop
94 * slot stay in lockstep. */
95 0 static HV *dbil_hm_cbh(pTHX_ SV *self, int mask, int create) {
96 0 HV *h = (HV *)SvRV(self);
97 0 ▶ const char *k = (mask & HM_ABI_WRITE) ? "_w" : "_r";
Branch: 1 branch
BranchTrueFalseTotal
const char *k = (mask & HM_ABI_WRITE) ? "_w" : "_r"; 0 0 0
98 0 ▶ SV **e = hv_fetch(h, k, 2, create);
Branch: 1 branch
BranchTrueFalseTotal
SV **e = hv_fetch(h, k, 2, create); 0 0 0
99 0 ▶ if (!e || !*e) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !*e) return NULL; 0 0 0
if (!e || !*e) return NULL; 0 0 0
100 0 ▶ if (!SvROK(*e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(*e)) { 0 0 0
101 0 ▶ if (!create) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!create) return NULL; 0 0 0
102 0 sv_setsv(*e, sv_2mortal(newRV_noinc((SV *)newHV())));
103 }
104 0 return (HV *)SvRV(*e);
105 }
106  
107 0 static void dbil_hm_add_io(pTHX_ SV *self, int fd, int mask, SV *cb) {
108 0 dbil_hm_ctx *c = dbil_hm_ctx_of(aTHX_ self);
109 0 HV *cbs = dbil_hm_cbh(aTHX_ self, mask, 1);
110 0 SV *held = newSVsv(cb);
111 char key[16];
112 0 I32 klen = (I32)sprintf(key, "%d", fd);
113 0 c->A->io_watch(aTHX_ c->loop, fd, mask, dbil_hm_io_tramp, held);
114 0 (void)hv_store(cbs, key, klen, held, 0); /* frees any replaced cb */
115 0 }
116  
117 0 static void dbil_hm_remove(pTHX_ SV *self, int fd) {
118 0 dbil_hm_ctx *c = dbil_hm_ctx_of(aTHX_ self);
119 HV *cbs;
120 char key[16];
121 0 I32 klen = (I32)sprintf(key, "%d", fd);
122 0 c->A->io_unwatch(aTHX_ c->loop, fd, HM_ABI_READ);
123 0 c->A->io_unwatch(aTHX_ c->loop, fd, HM_ABI_WRITE);
124 0 ▶ if ((cbs = dbil_hm_cbh(aTHX_ self, HM_ABI_READ, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((cbs = dbil_hm_cbh(aTHX_ self, HM_ABI_READ, 0))) 0 0 0
125 0 (void)hv_delete(cbs, key, klen, G_DISCARD);
126 0 ▶ if ((cbs = dbil_hm_cbh(aTHX_ self, HM_ABI_WRITE, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((cbs = dbil_hm_cbh(aTHX_ self, HM_ABI_WRITE, 0))) 0 0 0
127 0 (void)hv_delete(cbs, key, klen, G_DISCARD);
128 0 }
129  
130 /* await bridge for foreign futures (DBIx::Loop::Future, ...): on_ready
131 * settles a native Hyperman future the loop can run_until. cap = [bridge] */
132 XS_INTERNAL(dbil_hm_bridge_cb);
133 0 XS_INTERNAL(dbil_hm_bridge_cb) {
134 0 dXSARGS;
135 0 AV *cap = dbil_clos_cap(aTHX_ cv);
136 0 const hm_abi *A = dbil_hm(aTHX);
137 PERL_UNUSED_VAR(items);
138 0 ▶ if (cap && A) {
Branch: 2 branches
BranchTrueFalseTotal
if (cap && A) { 0 0 0
if (cap && A) { 0 0 0
139 0 SV **br = av_fetch(cap, 0, 0);
140 0 ▶ if (br) A->future_done(aTHX_ *br, NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (br) A->future_done(aTHX_ *br, NULL, 0); 0 0 0
141 }
142 0 XSRETURN_EMPTY;
143 }
144  
145 0 static void dbil_hm_await(pTHX_ dbil_hm_ctx *c, SV *f) {
146 0 ▶ if (c->A->is_future(aTHX_ f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->A->is_future(aTHX_ f)) { 0 0 0
147 0 c->A->run_until(aTHX_ c->loop, f);
148 0 return;
149 }
150 { /* foreign future: bridge through its on_ready */
151 0 SV *bridge = c->A->future_new(aTHX);
152 0 AV *cap = newAV();
153 SV *cb;
154 0 av_push(cap, SvREFCNT_inc(bridge));
155 0 cb = sv_2mortal(dbil_closure(aTHX_ dbil_hm_bridge_cb, cap));
156 {
157 0 dSP;
158 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
159 0 ▶ EXTEND(SP, 2); PUSHs(f); PUSHs(cb); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); PUSHs(f); PUSHs(cb); PUTBACK; 0 0 0
160 0 call_method("on_ready", G_VOID | G_DISCARD);
161 0 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 0 0 0
162 }
163 0 c->A->run_until(aTHX_ c->loop, bridge);
164 0 SvREFCNT_dec(bridge);
165 }
166 }
167  
168 /* ---- the dbil_vt implementation (pure C, no Perl frames) ----------------- */
169  
170 0 static void dbil_hm_vt_add_reader(pTHX_ void *vctx, int fd, dbil_io_fn fn,
171 void *ud) {
172 0 dbil_hm_ctx *c = (dbil_hm_ctx *)vctx;
173 0 c->A->io_watch(aTHX_ c->loop, fd, HM_ABI_READ, fn, ud);
174 0 }
175  
176 0 static void dbil_hm_vt_add_writer(pTHX_ void *vctx, int fd, dbil_io_fn fn,
177 void *ud) {
178 0 dbil_hm_ctx *c = (dbil_hm_ctx *)vctx;
179 0 c->A->io_watch(aTHX_ c->loop, fd, HM_ABI_WRITE, fn, ud);
180 0 }
181  
182 0 static void dbil_hm_vt_remove(pTHX_ void *vctx, int fd) {
183 0 dbil_hm_ctx *c = (dbil_hm_ctx *)vctx;
184 0 c->A->io_unwatch(aTHX_ c->loop, fd, HM_ABI_READ);
185 0 c->A->io_unwatch(aTHX_ c->loop, fd, HM_ABI_WRITE);
186 0 }
187  
188 0 static void *dbil_hm_vt_timer(pTHX_ void *vctx, double secs, dbil_timer_fn fn,
189 void *ud) {
190 0 dbil_hm_ctx *c = (dbil_hm_ctx *)vctx;
191 0 return (void *)c->A->timer(aTHX_ c->loop, secs, fn, ud);
192 }
193  
194 0 static void dbil_hm_vt_timer_cancel(pTHX_ void *vctx, void *timer) {
195 0 dbil_hm_ctx *c = (dbil_hm_ctx *)vctx;
196 0 c->A->timer_cancel(aTHX_ c->loop, (hm_abi_timer *)timer);
197 0 }
198  
199 0 static SV *dbil_hm_vt_new_future(pTHX_ void *vctx) {
200 0 dbil_hm_ctx *c = (dbil_hm_ctx *)vctx;
201 0 return c->A->future_new(aTHX);
202 }
203  
204 0 static void dbil_hm_vt_await(pTHX_ void *vctx, SV *f) {
205 0 dbil_hm_await(aTHX_ (dbil_hm_ctx *)vctx, f);
206 0 }
207  
208 0 static dbil_hm_ctx *dbil_hm_ctx_new(const hm_abi *A, void *loop) {
209 dbil_hm_ctx *c;
210 0 Newxz(c, 1, dbil_hm_ctx);
211 0 c->A = A;
212 0 c->loop = loop;
213 0 c->vt.ctx = (void *)c;
214 0 c->vt.add_reader = dbil_hm_vt_add_reader;
215 0 c->vt.add_writer = dbil_hm_vt_add_writer;
216 0 c->vt.remove = dbil_hm_vt_remove;
217 0 c->vt.timer = dbil_hm_vt_timer;
218 0 c->vt.timer_cancel = dbil_hm_vt_timer_cancel;
219 0 c->vt.new_future = dbil_hm_vt_new_future;
220 0 c->vt.await = dbil_hm_vt_await;
221 0 return c;
222 }
223  
224 #endif /* DBIL_HM_H */
« blib/lib/DBIx/Loop/Loop/Hyperman.pm ↑ Summary xs/loop_hyperman.xs »