]> git.vpit.fr Git - perl/modules/indirect.git/commitdiff
Revamp the setup boot logic
authorVincent Pit <vince@profvince.com>
Tue, 24 Mar 2015 15:34:23 +0000 (12:34 -0300)
committerVincent Pit <vince@profvince.com>
Tue, 24 Mar 2015 15:34:23 +0000 (12:34 -0300)
This will solve crashes when the module is loaded simultaneously in
different threads.

indirect.xs

index 57a0ea79dbf1289a8a756d6e70420c5161403176..40aebea36f97c0d56e583f19220bdf70bfc58c13 100644 (file)
@@ -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