From: Vincent Pit Date: Fri, 30 Mar 2012 20:00:18 +0000 (+0200) Subject: Protect the global linked list by a mutex X-Git-Tag: v0.02~13 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2Fre-engine-Hooks.git;a=commitdiff_plain;h=13fa2bf6c6176277832bb2de593252165b5151ed Protect the global linked list by a mutex --- diff --git a/Hooks.xs b/Hooks.xs index 30ee8ca..eddf6ad 100644 --- a/Hooks.xs +++ b/Hooks.xs @@ -17,6 +17,14 @@ # define SvPV_const(S, L) SvPV(S, L) #endif +#ifdef USE_ITHREADS +# define REH_LOCK(M) MUTEX_LOCK(M) +# define REH_UNLOCK(M) MUTEX_UNLOCK(M) +#else +# define REH_LOCK(M) NOOP +# define REH_UNLOCK(M) NOOP +#endif + /* --- Lexical hints ------------------------------------------------------- */ STATIC U32 reh_hash = 0; @@ -58,6 +66,12 @@ typedef struct reh_action { STATIC reh_action *reh_action_list = 0; +#ifdef USE_ITHREADS + +STATIC perl_mutex reh_action_list_mutex; + +#endif /* USE_ITHREADS */ + #undef reh_register void reh_register(pTHX_ const char *key, reh_comp_hook comp, reh_exec_hook exec) { reh_action *a; @@ -73,13 +87,15 @@ void reh_register(pTHX_ const char *key, reh_comp_hook comp, reh_exec_hook exec) key_dup[len] = '\0'; a = PerlMemShared_malloc(sizeof *a); - a->next = reh_action_list; a->comp = comp; a->exec = exec; a->key = key_dup; a->klen = len; + REH_LOCK(&reh_action_list_mutex); + a->next = reh_action_list; reh_action_list = a; + REH_UNLOCK(&reh_action_list_mutex); return; } @@ -94,7 +110,11 @@ void reh_call_comp_hook(pTHX_ regexp *rx, regnode *node) { const char *keys = SvPV_const(hint, len); reh_action *a; - for (a = reh_action_list; a; a = a->next) { + REH_LOCK(&reh_action_list_mutex); + a = reh_action_list; + REH_UNLOCK(&reh_action_list_mutex); + + for (; a; a = a->next) { if (a->comp) { char *p = strstr(keys, a->key); @@ -113,7 +133,11 @@ void reh_call_exec_hook(pTHX_ regexp *rx, regnode *node, regmatch_info *reginfo, const char *keys = SvPV_const(hint, len); reh_action *a; - for (a = reh_action_list; a; a = a->next) { + REH_LOCK(&reh_action_list_mutex); + a = reh_action_list; + REH_UNLOCK(&reh_action_list_mutex); + + for (; a; a = a->next) { if (a->exec) { char *p = strstr(keys, a->key); @@ -201,6 +225,13 @@ MODULE = re::engine::Hooks PACKAGE = re::engine::Hooks PROTOTYPES: ENABLE +BOOT: +{ +#ifdef USE_ITHREADS + MUTEX_INIT(&reh_action_list_mutex); +#endif +} + void _ENGINE() PROTOTYPE: @@ -212,10 +243,13 @@ _registered(SV *key) PROTOTYPE: $ PREINIT: SV *ret = NULL; - reh_action *a = reh_action_list; + reh_action *a; STRLEN len; const char *s; PPCODE: + REH_LOCK(&reh_action_list_mutex); + a = reh_action_list; + REH_UNLOCK(&reh_action_list_mutex); s = SvPV_const(key, len); while (a && !ret) { if (a->klen == len && memcmp(a->key, s, len) == 0)