+/* ... 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
+