| 1 |
|
| #ifndef CONTEXT_VARS_FAA_H |
| 2 |
|
| #define CONTEXT_VARS_FAA_H |
| 3 |
|
| |
| 4 |
|
| /* async subs, under Future::AsyncAwait. |
| 5 |
|
| * |
| 6 |
|
| * An async sub that reaches `await` on a pending future suspends: it leaves |
| 7 |
|
| * by an ordinary return, so its scope closes, and when the future is ready it |
| 8 |
|
| * is resumed from a callback, in whatever context that callback runs in. Left |
| 9 |
|
| * alone, everything it reads after the await comes from there, and every |
| 10 |
|
| * assignment it makes lands there - in somebody else's context. |
| 11 |
|
| * |
| 12 |
|
| * So for each suspended sub this keeps a context of its own: what the sub |
| 13 |
|
| * could see when it first suspended, and everything it assigns from then on. |
| 14 |
|
| * That context is made current for each stretch the sub runs, and put back |
| 15 |
|
| * when the stretch ends, by suspending again or by finishing. |
| 16 |
|
| * |
| 17 |
|
| * Future::AsyncAwait finds these through hooks it calls around every suspend |
| 18 |
|
| * and resume. They are reached at run time, through keys it puts in |
| 19 |
|
| * PL_modglobal, so this dist needs nothing from it to build and nothing from |
| 20 |
|
| * it at all when it is not loaded. The one thing taken on trust is the |
| 21 |
|
| * layout of its hook table, version 2 of its hook interface, which its header |
| 22 |
|
| * marks experimental. |
| 23 |
|
| * |
| 24 |
|
| * Two things about how it treats the save stack shape the code: |
| 25 |
|
| * |
| 26 |
|
| * - The restore is pushed BEFORE the sub's frames are rebuilt. Pushed |
| 27 |
|
| * after, it lands in whatever block the await is in, and closes at the |
| 28 |
|
| * end of that block. |
| 29 |
|
| * - At the next suspend Future::AsyncAwait lifts that entry off the save |
| 30 |
|
| * stack WITHOUT running it, and puts it back at the next resume. So the |
| 31 |
|
| * entry's data is a record that the suspend hook can close by hand, and |
| 32 |
|
| * that closing again does nothing. */ |
| 33 |
|
| |
| 34 |
|
| /* struct AsyncAwaitHookFuncs, from Future::AsyncAwait's AsyncAwait.h. */ |
| 35 |
|
| struct context_vars_faa_hooks { |
| 36 |
|
| U32 flags; |
| 37 |
|
| void (*post_cv_copy)(pTHX_ CV *runcv, CV *cv, HV *modhookdata, void *hookdata); |
| 38 |
|
| void (*pre_suspend) (pTHX_ CV *cv, HV *modhookdata, void *hookdata); |
| 39 |
|
| void (*post_suspend)(pTHX_ CV *cv, HV *modhookdata, void *hookdata); |
| 40 |
|
| void (*pre_resume) (pTHX_ CV *cv, HV *modhookdata, void *hookdata); |
| 41 |
|
| void (*post_resume) (pTHX_ CV *cv, HV *modhookdata, void *hookdata); |
| 42 |
|
| void (*free) (pTHX_ CV *cv, HV *modhookdata, void *hookdata); |
| 43 |
|
| }; |
| 44 |
|
| #define CONTEXT_VARS_FAA_ABI 2 |
| 45 |
|
| |
| 46 |
|
| /* One suspended sub's context. It lives in Future::AsyncAwait's per-sub hash, |
| 47 |
|
| * as the private data of magic on a plain scalar, so that it goes - and lets |
| 48 |
|
| * go of its context - when that hash does. */ |
| 49 |
|
| typedef struct { |
| 50 |
|
| context_vars_ctx *ctx; /* owned */ |
| 51 |
|
| context_vars_ctx *prev; /* what to put back; borrowed */ |
| 52 |
|
| int active; /* ctx is current for this sub now */ |
| 53 |
|
| int pushed; /* a restore for it is on, or lifted off, the save stack */ |
| 54 |
|
| } context_vars_susp; |
| 55 |
|
| |
| 56 |
|
| #define CONTEXT_VARS_SUSP_KEY "Context::Vars/suspended" |
| 57 |
|
| |
| 58 |
|
| static int context_vars_faa_registered = 0; |
| 59 |
|
| |
| 60 |
0 |
| static int context_vars_susp_free(pTHX_ SV *sv, MAGIC *mg) { |
| 61 |
0 |
| context_vars_susp *r = (context_vars_susp *)mg->mg_ptr; |
| 62 |
|
| PERL_UNUSED_ARG(sv); |
| 63 |
0 |
▶ | if (r) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) { |
0 |
0 |
0 |
|
| 64 |
0 |
▶ | if (r->ctx) context_vars_release(aTHX_ r->ctx); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->ctx) context_vars_release(aTHX_ r->ctx); |
0 |
0 |
0 |
|
| 65 |
0 |
| Safefree(r); |
| 66 |
0 |
| mg->mg_ptr = NULL; |
| 67 |
|
| } |
| 68 |
0 |
| return 0; |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| static MGVTBL context_vars_susp_vtbl = { 0, 0, 0, 0, context_vars_susp_free }; |
| 72 |
|
| |
| 73 |
0 |
| static context_vars_susp *context_vars_susp_of(SV *rsv) { |
| 74 |
|
| MAGIC *mg; |
| 75 |
0 |
▶ | for (mg = SvMAGIC(rsv); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(rsv); mg; mg = mg->mg_moremagic) |
0 |
0 |
0 |
|
| 76 |
0 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_susp_vtbl) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_susp_vtbl) |
0 |
0 |
0 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_susp_vtbl) |
0 |
0 |
0 |
|
| 77 |
0 |
| return (context_vars_susp *)mg->mg_ptr; |
| 78 |
0 |
| return NULL; |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
0 |
| static SV *context_vars_susp_sv(pTHX_ HV *mhd) { |
| 82 |
|
| SV **svp; |
| 83 |
0 |
▶ | if (!mhd) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mhd) return NULL; |
0 |
0 |
0 |
|
| 84 |
0 |
| svp = hv_fetchs(mhd, CONTEXT_VARS_SUSP_KEY, 0); |
| 85 |
0 |
▶ | return svp ? *svp : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return svp ? *svp : NULL; |
0 |
0 |
0 |
|
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| /* The end of one stretch: put back what was current before it. Run by the |
| 89 |
|
| * save stack, or by hand from the suspend hook when the entry was lifted. */ |
| 90 |
0 |
| static void context_vars_susp_close(pTHX_ context_vars_susp *r) { |
| 91 |
0 |
▶ | if (r && r->active) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (r && r->active) { |
0 |
0 |
0 |
| if (r && r->active) { |
0 |
0 |
0 |
|
| 92 |
|
| dMY_CXT; |
| 93 |
0 |
| MY_CXT.cur = r->prev; |
| 94 |
0 |
| r->active = 0; |
| 95 |
0 |
| MY_CXT.depth--; |
| 96 |
|
| } |
| 97 |
0 |
| } |
| 98 |
|
| |
| 99 |
0 |
| static void context_vars_susp_restore(pTHX_ void *p) { |
| 100 |
0 |
| SV *rsv = (SV *)p; |
| 101 |
0 |
| context_vars_susp *r = context_vars_susp_of(rsv); |
| 102 |
0 |
| context_vars_susp_close(aTHX_ r); |
| 103 |
0 |
▶ | if (r) r->pushed = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) r->pushed = 0; |
0 |
0 |
0 |
|
| 104 |
0 |
| SvREFCNT_dec(rsv); |
| 105 |
0 |
| } |
| 106 |
|
| |
| 107 |
0 |
| static void context_vars_faa_pre_suspend(pTHX_ CV *cv, HV *mhd, void *hd) { |
| 108 |
|
| dMY_CXT; |
| 109 |
|
| context_vars_susp *r; |
| 110 |
|
| SV *rsv; |
| 111 |
|
| PERL_UNUSED_ARG(cv); PERL_UNUSED_ARG(hd); |
| 112 |
|
| |
| 113 |
0 |
▶ | if (!mhd || context_vars_susp_sv(aTHX_ mhd)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!mhd || context_vars_susp_sv(aTHX_ mhd)) return; |
0 |
0 |
0 |
| if (!mhd || context_vars_susp_sv(aTHX_ mhd)) return; |
0 |
0 |
0 |
|
| 114 |
|
| |
| 115 |
|
| /* The first suspend: keep what the sub can see now, as a context of its |
| 116 |
|
| * own. Never the shared empty one, so that it can be assigned to. */ |
| 117 |
0 |
| Newxz(r, 1, context_vars_susp); |
| 118 |
0 |
| r->ctx = context_vars_ctx_new(aTHX_ |
| 119 |
0 |
| (CONTEXT_VARS_HAMT->map_copy)(aTHX_ MY_CXT.cur->map)); |
| 120 |
0 |
| rsv = newSV(0); |
| 121 |
0 |
| sv_magicext(rsv, NULL, PERL_MAGIC_ext, &context_vars_susp_vtbl, (const char *)r, 0); |
| 122 |
0 |
| (void)hv_stores(mhd, CONTEXT_VARS_SUSP_KEY, rsv); |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
0 |
| static void context_vars_faa_post_suspend(pTHX_ CV *cv, HV *mhd, void *hd) { |
| 126 |
0 |
| SV *rsv = context_vars_susp_sv(aTHX_ mhd); |
| 127 |
|
| PERL_UNUSED_ARG(cv); PERL_UNUSED_ARG(hd); |
| 128 |
0 |
▶ | if (rsv) context_vars_susp_close(aTHX_ context_vars_susp_of(rsv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rsv) context_vars_susp_close(aTHX_ context_vars_susp_of(rsv)); |
0 |
0 |
0 |
|
| 129 |
0 |
| } |
| 130 |
|
| |
| 131 |
0 |
| static void context_vars_faa_pre_resume(pTHX_ CV *cv, HV *mhd, void *hd) { |
| 132 |
|
| dMY_CXT; |
| 133 |
0 |
| SV *rsv = context_vars_susp_sv(aTHX_ mhd); |
| 134 |
|
| context_vars_susp *r; |
| 135 |
|
| PERL_UNUSED_ARG(cv); PERL_UNUSED_ARG(hd); |
| 136 |
|
| |
| 137 |
0 |
▶ | if (!rsv || !(r = context_vars_susp_of(rsv))) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!rsv || !(r = context_vars_susp_of(rsv))) return; |
0 |
0 |
0 |
| if (!rsv || !(r = context_vars_susp_of(rsv))) return; |
0 |
0 |
0 |
|
| 138 |
0 |
▶ | if (!r->pushed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->pushed) { |
0 |
0 |
0 |
|
| 139 |
0 |
| SvREFCNT_inc_simple_void_NN(rsv); |
| 140 |
0 |
| SAVEDESTRUCTOR_X(context_vars_susp_restore, (void *)rsv); |
| 141 |
0 |
| r->pushed = 1; |
| 142 |
|
| } |
| 143 |
0 |
| r->prev = MY_CXT.cur; |
| 144 |
0 |
| MY_CXT.cur = r->ctx; |
| 145 |
0 |
| r->active = 1; |
| 146 |
0 |
| MY_CXT.depth++; |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
|
| static const struct context_vars_faa_hooks context_vars_faa_hookfuncs = { |
| 150 |
|
| 0, |
| 151 |
|
| NULL, |
| 152 |
|
| context_vars_faa_pre_suspend, |
| 153 |
|
| context_vars_faa_post_suspend, |
| 154 |
|
| context_vars_faa_pre_resume, |
| 155 |
|
| NULL, |
| 156 |
|
| NULL |
| 157 |
|
| }; |
| 158 |
|
| |
| 159 |
|
| /* Called once Future::AsyncAwait has loaded, at once if it already has. */ |
| 160 |
0 |
| static void context_vars_faa_enable(pTHX_ void *data) { |
| 161 |
0 |
| SV **min = hv_fetchs(PL_modglobal, "Future::AsyncAwait/ABIVERSION_MIN", 0); |
| 162 |
0 |
| SV **max = hv_fetchs(PL_modglobal, "Future::AsyncAwait/ABIVERSION_MAX", 0); |
| 163 |
0 |
| SV **reg = hv_fetchs(PL_modglobal, "Future::AsyncAwait/register()@2", 0); |
| 164 |
|
| PERL_UNUSED_ARG(data); |
| 165 |
|
| |
| 166 |
0 |
▶ | if (context_vars_faa_registered) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_faa_registered) return; |
0 |
0 |
0 |
|
| 167 |
0 |
▶ | if (!min || !max || !reg || SvIV(*min) > CONTEXT_VARS_FAA_ABI |
Branch: 4 branches
| Branch | True | False | Total |
| if (!min || !max || !reg || SvIV(*min) > CONTEXT_VARS_FAA_ABI |
0 |
0 |
0 |
| if (!min || !max || !reg || SvIV(*min) > CONTEXT_VARS_FAA_ABI |
0 |
0 |
0 |
| if (!min || !max || !reg || SvIV(*min) > CONTEXT_VARS_FAA_ABI |
0 |
0 |
0 |
| if (!min || !max || !reg || SvIV(*min) > CONTEXT_VARS_FAA_ABI |
0 |
0 |
0 |
|
| 168 |
0 |
▶ | || SvIV(*max) < CONTEXT_VARS_FAA_ABI || !SvUV(*reg)) { |
Branch: 2 branches
| Branch | True | False | Total |
| || SvIV(*max) < CONTEXT_VARS_FAA_ABI || !SvUV(*reg)) { |
0 |
0 |
0 |
| || SvIV(*max) < CONTEXT_VARS_FAA_ABI || !SvUV(*reg)) { |
0 |
0 |
0 |
|
| 169 |
0 |
| warn("Context::Vars: this Future::AsyncAwait does not offer version %d of " |
| 170 |
|
| "its hook interface, so an async sub will not keep its variables " |
| 171 |
|
| "across await", CONTEXT_VARS_FAA_ABI); |
| 172 |
0 |
| return; |
| 173 |
|
| } |
| 174 |
|
| { |
| 175 |
0 |
| void (*registerf)(pTHX_ const struct context_vars_faa_hooks *, void *) = |
| 176 |
0 |
| INT2PTR(void (*)(pTHX_ const struct context_vars_faa_hooks *, void *), |
| 177 |
|
| SvUV(*reg)); |
| 178 |
0 |
| (*registerf)(aTHX_ &context_vars_faa_hookfuncs, NULL); |
| 179 |
0 |
| context_vars_faa_registered = 1; |
| 180 |
|
| } |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
37 |
| static void context_vars_faa_boot(pTHX) { |
| 184 |
37 |
| SV **svp = hv_fetchs(PL_modglobal, "Future::AsyncAwait/loaded", 0); |
| 185 |
37 |
▶ | if (svp && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) { |
0 |
37 |
37 |
| if (svp && SvOK(*svp)) { |
0 |
0 |
0 |
|
| 186 |
0 |
| context_vars_faa_enable(aTHX_ NULL); |
| 187 |
0 |
| return; |
| 188 |
|
| } |
| 189 |
|
| { |
| 190 |
|
| AV *av; |
| 191 |
37 |
| svp = hv_fetchs(PL_modglobal, "Future::AsyncAwait/on_loaded", 0); |
| 192 |
37 |
▶ | if (svp && SvROK(*svp) == 0 && SvTYPE(*svp) == SVt_PVAV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (svp && SvROK(*svp) == 0 && SvTYPE(*svp) == SVt_PVAV) |
0 |
37 |
37 |
| if (svp && SvROK(*svp) == 0 && SvTYPE(*svp) == SVt_PVAV) |
0 |
0 |
0 |
| if (svp && SvROK(*svp) == 0 && SvTYPE(*svp) == SVt_PVAV) |
0 |
0 |
0 |
|
| 193 |
0 |
| av = (AV *)*svp; |
| 194 |
|
| else { |
| 195 |
37 |
| av = newAV(); |
| 196 |
37 |
| (void)hv_stores(PL_modglobal, "Future::AsyncAwait/on_loaded", (SV *)av); |
| 197 |
|
| } |
| 198 |
37 |
| av_push(av, newSVuv(PTR2UV(context_vars_faa_enable))); |
| 199 |
37 |
| av_push(av, newSVuv(PTR2UV((void *)NULL))); |
| 200 |
|
| } |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
|
| #endif |