| 1 |
|
| #ifndef FT_HM_H |
| 2 |
|
| #define FT_HM_H |
| 3 |
|
| |
| 4 |
|
| /* Hyperman's public C ABI (hm_abi.h, vendored at a pinned HM_ABI_VERSION), |
| 5 |
|
| * resolved lazily at runtime via Hyperman::_abi_ptr - the same pattern as |
| 6 |
|
| * ft_json.h's frj table. When a UA drives a Hyperman loop, the connection |
| 7 |
|
| * state machine arms fd interest and deadlines straight through this table: |
| 8 |
|
| * readiness and timeouts reach ft_conn with no Perl call frame and no |
| 9 |
|
| * per-arm method dispatch. When Hyperman is absent (or its ABI is older |
| 10 |
|
| * than ours) ft_hm() returns NULL and the Perl adapter seam |
| 11 |
|
| * (Fetch::Loop::Hyperman's _ft_arm/_ft_timer) keeps working unchanged. */ |
| 12 |
|
| |
| 13 |
|
| #include "hm_abi.h" |
| 14 |
|
| |
| 15 |
|
| static const hm_abi *FT_HM = NULL; /* resolved on first use */ |
| 16 |
|
| |
| 17 |
0 |
| static const hm_abi *ft_hm(pTHX) { |
| 18 |
0 |
▶ | if (!FT_HM) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!FT_HM) { |
0 |
0 |
0 |
|
| 19 |
0 |
| dSP; |
| 20 |
|
| int count; |
| 21 |
0 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
0 |
0 |
|
| 22 |
0 |
| count = call_pv("Hyperman::_abi_ptr", G_SCALAR | G_EVAL); |
| 23 |
0 |
| SPAGAIN; |
| 24 |
0 |
▶ | if (!SvTRUE(ERRSV) && count > 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && count > 0) { |
0 |
0 |
0 |
| if (!SvTRUE(ERRSV) && count > 0) { |
0 |
0 |
0 |
| if (!SvTRUE(ERRSV) && count > 0) { |
0 |
0 |
0 |
|
| 25 |
0 |
| IV p = POPi; |
| 26 |
0 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
0 |
0 |
0 |
|
| 27 |
0 |
| const hm_abi *a = INT2PTR(const hm_abi *, p); |
| 28 |
0 |
▶ | if (a->abi_version >= HM_ABI_VERSION) FT_HM = a; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->abi_version >= HM_ABI_VERSION) FT_HM = a; |
0 |
0 |
0 |
|
| 29 |
|
| } |
| 30 |
0 |
▶ | } else if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (count > 0) (void)POPs; |
0 |
0 |
0 |
|
| 31 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 32 |
|
| } |
| 33 |
0 |
| return FT_HM; |
| 34 |
|
| } |
| 35 |
|
| |
| 36 |
|
| /* ---- C await: $Fetch::Future::AWAIT over run_until ----------------------- |
| 37 |
|
| * |
| 38 |
|
| * Awaiting a pending Fetch::Future on a Hyperman loop bridges it to a |
| 39 |
|
| * Hyperman::Future (created and settled only through the ABI - no layout |
| 40 |
|
| * assumption crosses the dist boundary; the Fetch future is touched only |
| 41 |
|
| * with Fetch's own hmf_* helpers) and pumps the loop with run_until. This |
| 42 |
|
| * replaces the Perl AWAIT sub (is_ready + on_ready(sub { $loop->stop }) + |
| 43 |
|
| * $loop->run per await), and its stop flag: each nested await returns |
| 44 |
|
| * exactly when ITS bridge settles, so an inner await can never break an |
| 45 |
|
| * outer pump the way a mis-aimed ->stop could. */ |
| 46 |
|
| |
| 47 |
|
| /* settles the bridge (cl->a) when the awaited Fetch::Future readies */ |
| 48 |
|
| XS_INTERNAL(ft_hm_bridge_cb); |
| 49 |
0 |
| XS_INTERNAL(ft_hm_bridge_cb) { |
| 50 |
0 |
| dVAR; dXSARGS; |
| 51 |
0 |
| hm_clos *cl = hm_clos_of(aTHX_ cv); |
| 52 |
0 |
| const hm_abi *A = ft_hm(aTHX); |
| 53 |
|
| PERL_UNUSED_VAR(items); |
| 54 |
0 |
▶ | if (cl && cl->a && A) A->future_done(aTHX_ cl->a, NULL, 0); |
Branch: 3 branches
| Branch | True | False | Total |
| if (cl && cl->a && A) A->future_done(aTHX_ cl->a, NULL, 0); |
0 |
0 |
0 |
| if (cl && cl->a && A) A->future_done(aTHX_ cl->a, NULL, 0); |
0 |
0 |
0 |
| if (cl && cl->a && A) A->future_done(aTHX_ cl->a, NULL, 0); |
0 |
0 |
0 |
|
| 55 |
0 |
| XSRETURN_EMPTY; |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
|
| /* the AWAIT body: cl->b = the Fetch::Loop::Hyperman adapter (refcounted, so |
| 59 |
|
| * the Hyperman loop outlives the global), cl->u = the opaque loop handle */ |
| 60 |
|
| XS_INTERNAL(ft_hm_await_cb); |
| 61 |
0 |
| XS_INTERNAL(ft_hm_await_cb) { |
| 62 |
0 |
| dVAR; dXSARGS; |
| 63 |
0 |
| hm_clos *cl = hm_clos_of(aTHX_ cv); |
| 64 |
0 |
| const hm_abi *A = ft_hm(aTHX); |
| 65 |
|
| SV *f, *bridge, *cb; |
| 66 |
0 |
▶ | if (!cl || !A || items < 1) XSRETURN_EMPTY; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cl || !A || items < 1) XSRETURN_EMPTY; |
0 |
0 |
0 |
| if (!cl || !A || items < 1) XSRETURN_EMPTY; |
0 |
0 |
0 |
| if (!cl || !A || items < 1) XSRETURN_EMPTY; |
0 |
0 |
0 |
|
| 67 |
0 |
| f = ST(0); |
| 68 |
0 |
▶ | if (hmf_state(aTHX_ f) != HMF_PENDING) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hmf_state(aTHX_ f) != HMF_PENDING) XSRETURN_EMPTY; |
0 |
0 |
0 |
|
| 69 |
0 |
| bridge = A->future_new(aTHX); |
| 70 |
0 |
| cb = hm_closure(aTHX_ ft_hm_bridge_cb, bridge, NULL, NULL, NULL, 0, 0); |
| 71 |
0 |
| hmf_on_ready(aTHX_ f, cb); |
| 72 |
0 |
| SvREFCNT_dec(cb); |
| 73 |
0 |
| A->run_until(aTHX_ INT2PTR(void *, cl->u), bridge); |
| 74 |
0 |
| SvREFCNT_dec(bridge); |
| 75 |
0 |
| XSRETURN_EMPTY; |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
|
| /* overwrite whatever install_await put in $Fetch::Future::AWAIT */ |
| 79 |
0 |
| static void ft_hm_install_await(pTHX_ SV *adapter, void *hm_loop) { |
| 80 |
0 |
| SV *cb = hm_closure(aTHX_ ft_hm_await_cb, NULL, adapter, NULL, NULL, |
| 81 |
|
| 0, PTR2UV(hm_loop)); |
| 82 |
0 |
| SV *aw = get_sv("Fetch::Future::AWAIT", GV_ADD); |
| 83 |
0 |
| sv_setsv(aw, cb); |
| 84 |
0 |
| SvREFCNT_dec(cb); |
| 85 |
0 |
| } |
| 86 |
|
| |
| 87 |
|
| #endif /* FT_HM_H */ |