]> git.vpit.fr Git - perl/modules/Sub-Op.git/commitdiff
Replace check functions in a thread-safe way
authorVincent Pit <vince@profvince.com>
Thu, 22 Aug 2013 21:22:05 +0000 (18:22 -0300)
committerVincent Pit <vince@profvince.com>
Thu, 22 Aug 2013 21:22:05 +0000 (18:22 -0300)
Op.xs

diff --git a/Op.xs b/Op.xs
index 44e09161c202aa34779166e0536374faae26df79..24ab0dc2166a69db3ac65154b0eeb95808256fbe 100644 (file)
--- a/Op.xs
+++ b/Op.xs
 # define MY_CXT_CLONE NOOP
 #endif
 
+#if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
+# define SO_CHECK_MUTEX_LOCK   OP_CHECK_MUTEX_LOCK
+# define SO_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
+#else
+# define SO_CHECK_MUTEX_LOCK   OP_REFCNT_LOCK
+# define SO_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
+#endif
+
+typedef OP *(*so_ck_t)(pTHX_ OP *);
+
+#ifdef wrap_op_checker
+
+# define so_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
+
+#else
+
+STATIC void so_ck_replace(pTHX_ OPCODE type, so_ck_t new_ck, so_ck_t *old_ck_p){
+#define so_ck_replace(T, NC, OCP) so_ck_replace(aTHX_ (T), (NC), (OCP))
+ SO_CHECK_MUTEX_LOCK;
+ if (!*old_ck_p) {
+  *old_ck_p      = PL_check[type];
+  PL_check[type] = new_ck;
+ }
+ SO_CHECK_MUTEX_UNLOCK;
+}
+
+#endif
+
+STATIC void so_ck_restore(pTHX_ OPCODE type, so_ck_t *old_ck_p) {
+#define so_ck_restore(T, OCP) so_ck_restore(aTHX_ (T), (OCP))
+ SO_CHECK_MUTEX_LOCK;
+ if (*old_ck_p) {
+  PL_check[type] = *old_ck_p;
+  *old_ck_p      = 0;
+ }
+ SO_CHECK_MUTEX_UNLOCK;
+}
+
 /* --- Global data --------------------------------------------------------- */
 
 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
@@ -417,12 +455,9 @@ BOOT:
  MY_CXT.owner       = aTHX;
 #endif /* SO_THREADSAFE */
 
- so_old_ck_entersub    = PL_check[OP_ENTERSUB];
- PL_check[OP_ENTERSUB] = so_ck_entersub;
- so_old_ck_refgen      = PL_check[OP_REFGEN];
- PL_check[OP_REFGEN]   = so_ck_refgen;
- so_old_ck_gelem       = PL_check[OP_GELEM];
- PL_check[OP_GELEM]    = so_ck_gelem;
+ so_ck_replace(OP_ENTERSUB, so_ck_entersub, &so_old_ck_entersub);
+ so_ck_replace(OP_REFGEN,   so_ck_refgen,   &so_old_ck_refgen);
+ so_ck_replace(OP_GELEM,    so_ck_gelem,    &so_old_ck_gelem);
 }
 
 #if SO_THREADSAFE