From: Vincent Pit Date: Sat, 20 Sep 2008 21:12:37 +0000 (+0200) Subject: Clone wizards across threads X-Git-Tag: v0.20~9 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=23438375d18947cd5f9a8a055063e10af478c318;p=perl%2Fmodules%2FVariable-Magic.git Clone wizards across threads --- diff --git a/Magic.xs b/Magic.xs index f4ec292..cbd65dc 100644 --- a/Magic.xs +++ b/Magic.xs @@ -155,23 +155,6 @@ typedef struct { START_MY_CXT -STATIC void vmg_cxt_init -#if defined(pMY_CXT) && defined(aMY_CXT) - (pTHX_ pMY_CXT) { -# define vmg_cxt_init() vmg_cxt_init(aTHX_ aMY_CXT) -#else - (pTHX) { - dMY_CXT; -# define vmg_cxt_init() vmg_cxt_init(aTHX) -#endif - MY_CXT.wizz = newHV(); -#ifdef USE_ITHREADS - HvSHAREKEYS_off(MY_CXT.wizz); -#endif - MY_CXT.count = 0; - return; -} - /* --- Signatures ---------------------------------------------------------- */ #define SIG_MIN ((U16) (1u << 8)) @@ -214,6 +197,9 @@ typedef struct { #if VMG_UVAR SV *cb_fetch, *cb_store, *cb_exists, *cb_delete; #endif /* VMG_UVAR */ +#if VMG_MULTIPLICITY + tTHX owner; +#endif /* VMG_MULTIPLICITY */ } MGWIZ; #define MGWIZ2SV(W) (newSVuv(PTR2UV(W))) @@ -644,6 +630,11 @@ STATIC int vmg_wizard_free(pTHX_ SV *wiz, MAGIC *mg) { return 0; w = SV2MGWIZ(wiz); +#if VMG_MULTIPLICITY + if (w->owner != aTHX) + return 0; + w->owner = NULL; +#endif /* VMG_MULTIPLICITY */ if (hv_delete(MY_CXT.wizz, buf, sprintf(buf, "%u", w->sig), 0)) { --MY_CXT.count; @@ -672,6 +663,7 @@ STATIC int vmg_wizard_free(pTHX_ SV *wiz, MAGIC *mg) { if (w->cb_exists != NULL) { SvREFCNT_dec(SvRV(w->cb_exists)); } if (w->cb_delete != NULL) { SvREFCNT_dec(SvRV(w->cb_delete)); } #endif /* VMG_UVAR */ + Safefree(w->vtbl); Safefree(w); @@ -735,6 +727,61 @@ STATIC U16 vmg_sv2sig(pTHX_ SV *sv) { w->cb_ ## N = NULL; \ } +#if VMG_MULTIPLICITY + +STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) { +#define vmg_clone(P, O) vmg_clone(aTHX_ (P), (O)) + CLONE_PARAMS param; + param.stashes = NULL; /* don't need it unless sv is a PVHV */ + param.flags = 0; + param.proto_perl = owner; + return sv_dup(sv, ¶m); +} + +#define VMG_CLONE_CB(N) \ + z->cb_ ## N = (w->cb_ ## N) ? newRV_noinc(vmg_clone(SvRV(w->cb_ ## N), \ + w->owner)) \ + : NULL; + +STATIC MGWIZ *vmg_wizard_clone(pTHX_ const MGWIZ *w) { +#define vmg_wizard_clone(W) vmg_wizard_clone(aTHX_ (W)) + MGVTBL *t; + MGWIZ *z; + + Newx(t, 1, MGVTBL); + Copy(w->vtbl, t, 1, MGVTBL); + + Newx(z, 1, MGWIZ); + VMG_CLONE_CB(data); + VMG_CLONE_CB(get); + VMG_CLONE_CB(set); + VMG_CLONE_CB(len); + VMG_CLONE_CB(clear); + VMG_CLONE_CB(free); +#if MGf_COPY + VMG_CLONE_CB(copy); +#endif /* MGf_COPY */ +#if MGf_DUP + VMG_CLONE_CB(dup); +#endif /* MGf_DUP */ +#if MGf_LOCAL + VMG_CLONE_CB(local); +#endif /* MGf_LOCAL */ +#if VMG_UVAR + VMG_CLONE_CB(fetch); + VMG_CLONE_CB(store); + VMG_CLONE_CB(exists); + VMG_CLONE_CB(delete); +#endif /* VMG_UVAR */ + z->owner = aTHX; + z->vtbl = t; + z->sig = w->sig; + z->uvar = w->uvar; + + return z; +} + +#endif /* VMG_MULTIPLICITY */ /* --- XS ------------------------------------------------------------------ */ @@ -746,7 +793,8 @@ BOOT: { HV *stash; MY_CXT_INIT; - vmg_cxt_init(); + MY_CXT.wizz = newHV(); + MY_CXT.count = 0; stash = gv_stashpv(__PACKAGE__, 1); newCONSTSUB(stash, "SIG_MIN", newSVuv(SIG_MIN)); newCONSTSUB(stash, "SIG_MAX", newSVuv(SIG_MAX)); @@ -768,11 +816,36 @@ BOOT: void CLONE(...) PROTOTYPE: DISABLE +PREINIT: + HV *hv; + U16 count; CODE: -#ifdef MY_CXT_CLONE - MY_CXT_CLONE; - vmg_cxt_init(); -#endif +#if VMG_THREADSAFE + { + HE *key; + dMY_CXT; + count = MY_CXT.count; + hv = newHV(); + hv_iterinit(MY_CXT.wizz); + while (key = hv_iternext(MY_CXT.wizz)) { + STRLEN len; + char *sig = HePV(key, len); + SV *sv; + MAGIC *mg; + MGWIZ *w; + sv = MGWIZ2SV(vmg_wizard_clone(SV2MGWIZ(HeVAL(key)))); + mg = sv_magicext(sv, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0); + mg->mg_private = SIG_WIZ; + SvREADONLY_on(sv); + hv_store(hv, sig, len, sv, HeHASH(key)); + } + } + { + MY_CXT_CLONE; + MY_CXT.wizz = hv; + MY_CXT.count = count; + } +#endif /* VMG_THREADSAFE */ SV *_wizard(...) PROTOTYPE: DISABLE @@ -844,6 +917,9 @@ CODE: VMG_SET_CB(ST(i++), exists); VMG_SET_CB(ST(i++), delete); #endif /* VMG_UVAR */ +#if VMG_MULTIPLICITY + w->owner = aTHX; +#endif /* VMG_MULTIPLICITY */ w->vtbl = t; w->sig = sig;