From: Vincent Pit Date: Tue, 24 Mar 2015 15:34:23 +0000 (-0300) Subject: Revamp the setup boot logic X-Git-Tag: rt100068~9 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=48aed0641a91994514808460cdc6297140a2f149;p=perl%2Fmodules%2Findirect.git Revamp the setup boot logic This will solve crashes when the module is loaded simultaneously in different threads. --- diff --git a/indirect.xs b/indirect.xs index 57a0ea7..40aebea 100644 --- a/indirect.xs +++ b/indirect.xs @@ -933,9 +933,11 @@ done: return o; } +/* --- Global setup/teardown ------------------------------------------------ */ + static U32 indirect_initialized = 0; -static void indirect_teardown(pTHX_ void *root) { +static void indirect_global_teardown(pTHX_ void *root) { if (!indirect_initialized) return; @@ -944,16 +946,6 @@ static void indirect_teardown(pTHX_ void *root) { return; #endif - { - dMY_CXT; - ptable_free(MY_CXT.map); - MY_CXT.map = NULL; -#if I_THREADSAFE - ptable_hints_free(MY_CXT.tbl); - MY_CXT.tbl = NULL; -#endif - } - indirect_ck_restore(OP_CONST, &indirect_old_ck_const); indirect_ck_restore(OP_RV2SV, &indirect_old_ck_rv2sv); indirect_ck_restore(OP_PADANY, &indirect_old_ck_padany); @@ -965,22 +957,22 @@ static void indirect_teardown(pTHX_ void *root) { indirect_ck_restore(OP_ENTERSUB, &indirect_old_ck_entersub); indirect_initialized = 0; + + return; } -static void indirect_setup(pTHX) { -#define indirect_setup() indirect_setup(aTHX) +static void indirect_global_setup(pTHX) { +#define indirect_global_setup() indirect_global_setup(aTHX) + HV *stash; + if (indirect_initialized) return; - { - MY_CXT_INIT; -#if I_THREADSAFE - MY_CXT.tbl = ptable_new(); - MY_CXT.owner = aTHX; -#endif - MY_CXT.map = ptable_new(); - MY_CXT.global_code = NULL; - } + PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__); + + stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1); + newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE)); + newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE)); indirect_ck_replace(OP_CONST, indirect_ck_const, &indirect_old_ck_const); indirect_ck_replace(OP_RV2SV, indirect_ck_rv2sv, &indirect_old_ck_rv2sv); @@ -996,15 +988,48 @@ static void indirect_setup(pTHX) { &indirect_old_ck_entersub); #if I_MULTIPLICITY - call_atexit(indirect_teardown, aTHX); + call_atexit(indirect_global_teardown, aTHX); #else - call_atexit(indirect_teardown, NULL); + call_atexit(indirect_global_teardown, NULL); #endif indirect_initialized = 1; + + return; +} + +/* --- Interpreter setup/teardown ------------------------------------------ */ + +static void indirect_local_teardown(pTHX_ void *param) { + dMY_CXT; + + ptable_free(MY_CXT.map); + MY_CXT.map = NULL; + +#if I_THREADSAFE + ptable_hints_free(MY_CXT.tbl); + MY_CXT.tbl = NULL; +#endif + + return; } -static U32 indirect_booted = 0; +static void indirect_local_setup(pTHX) { +#define indirect_local_setup() indirect_local_setup(aTHX) + MY_CXT_INIT; + +#if I_THREADSAFE + MY_CXT.tbl = ptable_new(); + MY_CXT.owner = aTHX; +#endif + + MY_CXT.map = ptable_new(); + MY_CXT.global_code = NULL; + + call_atexit(indirect_local_teardown, NULL); + + return; +} /* --- XS ------------------------------------------------------------------ */ @@ -1014,17 +1039,8 @@ PROTOTYPES: ENABLE BOOT: { - if (!indirect_booted++) { - HV *stash; - - PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__); - - stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1); - newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE)); - newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE)); - } - - indirect_setup(); + indirect_global_setup(); + indirect_local_setup(); } #if I_THREADSAFE