# define SvREFCNT_inc_simple_void_NN(S) ((void) SvREFCNT_inc(S))
#endif
+#ifdef DEBUGGING
+# define LT_ASSERT(C) assert(C)
+#else
+# define LT_ASSERT(C)
+#endif
+
/* ... Thread safety and multiplicity ...................................... */
/* Safe unless stated otherwise in Makefile.PL */
# define aMY_CXT_
#endif
+#if LT_THREADSAFE
+/* We must use preexistent global mutexes or we will never be able to destroy
+ * them. */
+# if LT_HAS_PERL(5, 9, 3)
+# define LT_LOADED_LOCK MUTEX_LOCK(&PL_my_ctx_mutex)
+# define LT_LOADED_UNLOCK MUTEX_UNLOCK(&PL_my_ctx_mutex)
+# else
+# define LT_LOADED_LOCK OP_REFCNT_LOCK
+# define LT_LOADED_UNLOCK OP_REFCNT_UNLOCK
+# endif
+#else
+# define LT_LOADED_LOCK NOOP
+# define LT_LOADED_UNLOCK NOOP
+#endif
+
#if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
-# define LT_CHECK_MUTEX_LOCK OP_CHECK_MUTEX_LOCK
-# define LT_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
+# define LT_CHECK_LOCK OP_CHECK_MUTEX_LOCK
+# define LT_CHECK_UNLOCK OP_CHECK_MUTEX_UNLOCK
+#elif LT_HAS_PERL(5, 9, 3)
+# define LT_CHECK_LOCK OP_REFCNT_LOCK
+# define LT_CHECK_UNLOCK OP_REFCNT_UNLOCK
#else
-# define LT_CHECK_MUTEX_LOCK OP_REFCNT_LOCK
-# define LT_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
+/* Before perl 5.9.3, lt_ck_*() calls are already protected by the LT_LOADED
+ * mutex, which falls back to the OP_REFCNT mutex. Make sure we don't lock it
+ * twice. */
+# define LT_CHECK_LOCK NOOP
+# define LT_CHECK_UNLOCK NOOP
#endif
typedef OP *(*lt_ck_t)(pTHX_ OP *);
static void lt_ck_replace(pTHX_ OPCODE type, lt_ck_t new_ck, lt_ck_t *old_ck_p){
#define lt_ck_replace(T, NC, OCP) lt_ck_replace(aTHX_ (T), (NC), (OCP))
- LT_CHECK_MUTEX_LOCK;
+ LT_CHECK_LOCK;
if (!*old_ck_p) {
*old_ck_p = PL_check[type];
PL_check[type] = new_ck;
}
- LT_CHECK_MUTEX_UNLOCK;
+ LT_CHECK_UNLOCK;
}
#endif
static void lt_ck_restore(pTHX_ OPCODE type, lt_ck_t *old_ck_p) {
#define lt_ck_restore(T, OCP) lt_ck_restore(aTHX_ (T), (OCP))
- LT_CHECK_MUTEX_LOCK;
+ LT_CHECK_LOCK;
if (*old_ck_p) {
PL_check[type] = *old_ck_p;
*old_ck_p = 0;
}
- LT_CHECK_MUTEX_UNLOCK;
+ LT_CHECK_UNLOCK;
}
/* --- Helpers ------------------------------------------------------------- */
+/* ... Check if the module is loaded ....................................... */
+
+static I32 lt_loaded = 0;
+
+#if LT_THREADSAFE
+
+#define PTABLE_NAME ptable_loaded
+#define PTABLE_NEED_DELETE 1
+#define PTABLE_NEED_WALK 0
+
+#include "ptable.h"
+
+#define ptable_loaded_store(T, K, V) ptable_loaded_store(aPTBLMS_ (T), (K), (V))
+#define ptable_loaded_delete(T, K) ptable_loaded_delete(aPTBLMS_ (T), (K))
+#define ptable_loaded_free(T) ptable_loaded_free(aPTBLMS_ (T))
+
+static ptable *lt_loaded_cxts = NULL;
+
+#ifdef DEBUGGING
+
+static int lt_is_loaded(pTHX_ void *cxt) {
+#define lt_is_loaded(C) lt_is_loaded(aTHX_ (C))
+ int res = 0;
+
+ LT_LOADED_LOCK;
+ if (lt_loaded_cxts && ptable_fetch(lt_loaded_cxts, cxt))
+ res = 1;
+ LT_LOADED_UNLOCK;
+
+ return res;
+}
+
+#endif /* DEBUGGING */
+
+static int lt_set_loaded_locked(pTHX_ void *cxt) {
+#define lt_set_loaded_locked(C) lt_set_loaded_locked(aTHX_ (C))
+ int global_setup = 0;
+
+ if (lt_loaded <= 0) {
+ LT_ASSERT(lt_loaded == 0);
+ LT_ASSERT(!lt_loaded_cxts);
+ lt_loaded_cxts = ptable_new();
+ global_setup = 1;
+ }
+ ++lt_loaded;
+ LT_ASSERT(lt_loaded_cxts);
+ ptable_loaded_store(lt_loaded_cxts, cxt, cxt);
+
+ return global_setup;
+}
+
+static int lt_clear_loaded_locked(pTHX_ void *cxt) {
+#define lt_clear_loaded_locked(C) lt_clear_loaded_locked(aTHX_ (C))
+ int global_teardown = 0;
+
+ if (lt_loaded > 1) {
+ LT_ASSERT(lt_loaded_cxts);
+ ptable_loaded_delete(lt_loaded_cxts, cxt);
+ --lt_loaded;
+ } else if (lt_loaded_cxts) {
+ LT_ASSERT(lt_loaded == 1);
+ ptable_loaded_free(lt_loaded_cxts);
+ lt_loaded_cxts = NULL;
+ lt_loaded = 0;
+ global_teardown = 1;
+ }
+
+ return global_teardown;
+}
+
+#else
+
+#define lt_is_loaded(C) (lt_loaded > 0)
+#define lt_set_loaded_locked(C) ((lt_loaded++ <= 0) ? 1 : 0)
+#define lt_clear_loaded_locked(C) ((--lt_loaded <= 0) ? 1 : 0)
+
+#endif
+
/* ... Thread-safe hints ................................................... */
#if LT_WORKAROUND_REQUIRE_PROPAGATION
#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
typedef struct {
+ peep_t old_peep;
+ ptable *seen; /* It really is a ptable_seen */
#if LT_THREADSAFE
- ptable *tbl; /* It really is a ptable_hints */
+ ptable *tbl; /* It really is a ptable_hints */
tTHX owner;
#endif
- ptable *seen; /* It really is a ptable_seen */
SV *default_meth;
} my_cxt_t;
ptable_hints_store(ud->tbl, ent->key, h2);
}
-static void lt_thread_cleanup(pTHX_ void *ud) {
- dMY_CXT;
-
- ptable_hints_free(MY_CXT.tbl);
- MY_CXT.tbl = NULL;
- ptable_seen_free(MY_CXT.seen);
- MY_CXT.seen = NULL;
- SvREFCNT_dec(MY_CXT.default_meth);
- MY_CXT.default_meth = NULL;
-}
-
-static int lt_endav_free(pTHX_ SV *sv, MAGIC *mg) {
- SAVEDESTRUCTOR_X(lt_thread_cleanup, NULL);
-
- return 0;
-}
-
-static MGVTBL lt_endav_vtbl = {
- 0,
- 0,
- 0,
- 0,
- lt_endav_free
-#if MGf_COPY
- , 0
-#endif
-#if MGf_DUP
- , 0
-#endif
-#if MGf_LOCAL
- , 0
-#endif
-};
-
#endif /* LT_THREADSAFE */
/* ... Hint tags ........................................................... */
return LT_HINT_CODE(h);
}
-static U32 lt_hash = 0;
+static VOL U32 lt_hash = 0;
static SV *lt_hint(pTHX) {
#define lt_hint() lt_hint(aTHX)
#endif
-static peep_t lt_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
-
static void lt_peep_rec(pTHX_ OP *o, ptable *seen) {
#define lt_peep_rec(O) lt_peep_rec(aTHX_ (O), seen)
for (; o; o = o->op_next) {
}
static void lt_peep(pTHX_ OP *o) {
+ ptable *seen;
dMY_CXT;
- ptable *seen = MY_CXT.seen;
- lt_old_peep(aTHX_ o);
+ LT_ASSERT(lt_is_loaded(&MY_CXT));
+
+ MY_CXT.old_peep(aTHX_ o);
+ seen = MY_CXT.seen;
if (seen) {
ptable_seen_clear(seen);
lt_peep_rec(o);
}
}
-/* --- Interpreter setup/teardown ------------------------------------------ */
-
-static U32 lt_initialized = 0;
+/* --- Module setup/teardown ----------------------------------------------- */
static void lt_teardown(pTHX_ void *root) {
- if (!lt_initialized)
- return;
+ dMY_CXT;
-#if LT_MULTIPLICITY
- if (aTHX != root)
- return;
+ LT_LOADED_LOCK;
+
+ if (lt_clear_loaded_locked(&MY_CXT)) {
+ lt_ck_restore(OP_PADANY, <_old_ck_padany);
+ lt_ck_restore(OP_PADSV, <_old_ck_padsv);
+
+ ptable_map_free(lt_op_padxv_map);
+ lt_op_padxv_map = NULL;
+
+#if LT_HAS_PERL(5, 17, 6)
+ ptable_map_free(lt_op_padrange_map);
+ lt_op_padrange_map = NULL;
#endif
- {
- dMY_CXT;
-#if LT_THREADSAFE
- ptable_hints_free(MY_CXT.tbl);
- MY_CXT.tbl = NULL;
+#ifdef USE_ITHREADS
+ MUTEX_DESTROY(<_op_map_mutex);
#endif
- ptable_seen_free(MY_CXT.seen);
- MY_CXT.seen = NULL;
- SvREFCNT_dec(MY_CXT.default_meth);
- MY_CXT.default_meth = NULL;
}
- lt_ck_restore(OP_PADANY, <_old_ck_padany);
- lt_ck_restore(OP_PADSV, <_old_ck_padsv);
+ LT_LOADED_UNLOCK;
+ if (MY_CXT.old_peep) {
#if LT_HAS_RPEEP
- PL_rpeepp = lt_old_peep;
+ PL_rpeepp = MY_CXT.old_peep;
#else
- PL_peepp = lt_old_peep;
+ PL_peepp = MY_CXT.old_peep;
+#endif
+ MY_CXT.old_peep = 0;
+ }
+
+ ptable_seen_free(MY_CXT.seen);
+ MY_CXT.seen = NULL;
+
+#if LT_THREADSAFE
+ ptable_hints_free(MY_CXT.tbl);
+ MY_CXT.tbl = NULL;
#endif
- lt_old_peep = 0;
- lt_initialized = 0;
+ SvREFCNT_dec(MY_CXT.default_meth);
+ MY_CXT.default_meth = NULL;
+
+ return;
}
static void lt_setup(pTHX) {
#define lt_setup() lt_setup(aTHX)
- if (lt_initialized)
- return;
+ MY_CXT_INIT; /* Takes/release PL_my_ctx_mutex */
- {
- MY_CXT_INIT;
-#if LT_THREADSAFE
- MY_CXT.tbl = ptable_new();
- MY_CXT.owner = aTHX;
+ LT_LOADED_LOCK;
+
+ if (lt_set_loaded_locked(&MY_CXT)) {
+ PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
+
+ lt_op_padxv_map = ptable_new();
+#if LT_HAS_PERL(5, 17, 6)
+ lt_op_padrange_map = ptable_new();
#endif
- MY_CXT.seen = ptable_new();
- MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
- SvREADONLY_on(MY_CXT.default_meth);
+
+#ifdef USE_ITHREADS
+ MUTEX_INIT(<_op_map_mutex);
+#endif
+
+ lt_ck_replace(OP_PADANY, lt_ck_padany, <_old_ck_padany);
+ lt_ck_replace(OP_PADSV, lt_ck_padsv, <_old_ck_padsv);
}
- lt_ck_replace(OP_PADANY, lt_ck_padany, <_old_ck_padany);
- lt_ck_replace(OP_PADSV, lt_ck_padsv, <_old_ck_padsv);
+ LT_LOADED_UNLOCK;
+
+ {
+ HV *stash;
+
+ stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
+ newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
+ newCONSTSUB(stash, "LT_FORKSAFE", newSVuv(LT_FORKSAFE));
+ }
#if LT_HAS_RPEEP
- lt_old_peep = PL_rpeepp;
- PL_rpeepp = lt_peep;
+ if (PL_rpeepp != lt_peep) {
+ MY_CXT.old_peep = PL_rpeepp;
+ PL_rpeepp = lt_peep;
+ }
#else
- lt_old_peep = PL_peepp;
- PL_peepp = lt_peep;
+ if (PL_peepp != lt_peep) {
+ MY_CXT.old_peep = PL_peepp;
+ PL_peepp = lt_peep;
+ }
#endif
+ else {
+ MY_CXT.old_peep = 0;
+ }
-#if LT_MULTIPLICITY
- call_atexit(lt_teardown, aTHX);
-#else
- call_atexit(lt_teardown, NULL);
+ MY_CXT.seen = ptable_new();
+
+#if LT_THREADSAFE
+ MY_CXT.tbl = ptable_new();
+ MY_CXT.owner = aTHX;
#endif
- lt_initialized = 1;
-}
+ MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
+ SvREADONLY_on(MY_CXT.default_meth);
-static U32 lt_booted = 0;
+ call_atexit(lt_teardown, NULL);
+
+ return;
+}
/* --- XS ------------------------------------------------------------------ */
BOOT:
{
- if (!lt_booted++) {
- HV *stash;
-
- lt_op_padxv_map = ptable_new();
-#if LT_HAS_PERL(5, 17, 6)
- lt_op_padrange_map = ptable_new();
-#endif
-#ifdef USE_ITHREADS
- MUTEX_INIT(<_op_map_mutex);
-#endif
-
- PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
-
- stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
- newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
- newCONSTSUB(stash, "LT_FORKSAFE", newSVuv(LT_FORKSAFE));
- }
-
lt_setup();
}
ptable *t;
ptable *s;
SV *cloned_default_meth;
- GV *gv;
PPCODE:
{
{
}
{
MY_CXT_CLONE;
+ MY_CXT.seen = s;
MY_CXT.tbl = t;
MY_CXT.owner = aTHX;
- MY_CXT.seen = s;
MY_CXT.default_meth = cloned_default_meth;
+ {
+ int global_setup;
+ LT_LOADED_LOCK;
+ global_setup = lt_set_loaded_locked(&MY_CXT);
+ LT_ASSERT(!global_setup);
+ LT_LOADED_UNLOCK;
+ }
}
- gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
- if (gv) {
- CV *cv = GvCV(gv);
- if (!PL_endav)
- PL_endav = newAV();
- SvREFCNT_inc(cv);
- if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
- SvREFCNT_dec(cv);
- sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, <_endav_vtbl, NULL, 0);
- }
- XSRETURN(0);
-
-void
-_THREAD_CLEANUP(...)
-PROTOTYPE: DISABLE
-PPCODE:
- lt_thread_cleanup(aTHX_ NULL);
XSRETURN(0);
#endif