From: Vincent Pit Date: Thu, 22 Aug 2013 21:22:05 +0000 (-0300) Subject: Replace check functions in a thread-safe way X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=623a5f3a3bb3565b7b646cad7997bcd74a1fa67b;p=perl%2Fmodules%2FSub-Op.git Replace check functions in a thread-safe way --- diff --git a/Op.xs b/Op.xs index 44e0916..24ab0dc 100644 --- a/Op.xs +++ b/Op.xs @@ -53,6 +53,44 @@ # 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