ruby-changes:40916
From: nobu <ko1@a...>
Date: Wed, 9 Dec 2015 15:48:37 +0900 (JST)
Subject: [ruby-changes:40916] nobu:r52995 (trunk): use atomic operations
nobu 2015-12-09 15:48:15 +0900 (Wed, 09 Dec 2015) New Revision: 52995 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52995 Log: use atomic operations * regcomp.c (onig_chain_link_add): use atomic operation instead of mutex. * regint.h (ONIG_STATE_{INC,DEC}_THREAD): ditto. * regparse.c (PopFreeNode, node_recycle): ditto. Modified files: trunk/ChangeLog trunk/common.mk trunk/regcomp.c trunk/regint.h trunk/regparse.c trunk/ruby_atomic.h Index: regparse.c =================================================================== --- regparse.c (revision 52994) +++ regparse.c (revision 52995) @@ -1031,8 +1031,44 @@ typedef struct _FreeNode { https://github.com/ruby/ruby/blob/trunk/regparse.c#L1031 } FreeNode; static FreeNode* FreeNodeList = (FreeNode* )NULL; + +#define PopFreeNode(n, popped) \ + do { \ + FreeNode* n = FreeNodeList; \ + while (IS_NOT_NULL(n)) { \ + FreeNode* next = n->next; \ + FreeNode *head = ATOMIC_PTR_CAS(FreeNodeList, n, next); \ + if (head == n) { \ + popped; \ + n = next; \ + } \ + else { \ + n = head; /* modified, retry */ \ + } \ + } \ + } while (0) + #endif +static void +node_recycle(Node* node) +{ +#ifdef USE_PARSE_TREE_NODE_RECYCLE + FreeNode* n = (FreeNode* )node; + FreeNode* list = FreeNodeList; + FreeNode* l; + + /* THREAD_ATOMIC_START; */ + do { + n->next = l = list; + list = ATOMIC_PTR_CAS(FreeNodeList, list, n); + } while (list != l); + /* THREAD_ATOMIC_END; */ +#else + xfree(node); +#endif +} + extern void onig_node_free(Node* node) { @@ -1053,18 +1089,7 @@ onig_node_free(Node* node) https://github.com/ruby/ruby/blob/trunk/regparse.c#L1089 { Node* next_node = NCDR(node); -#ifdef USE_PARSE_TREE_NODE_RECYCLE - { - FreeNode* n = (FreeNode* )node; - - THREAD_ATOMIC_START; - n->next = FreeNodeList; - FreeNodeList = n; - THREAD_ATOMIC_END; - } -#else - xfree(node); -#endif + node_recycle(node); node = next_node; goto start; } @@ -1101,32 +1126,15 @@ onig_node_free(Node* node) https://github.com/ruby/ruby/blob/trunk/regparse.c#L1126 break; } -#ifdef USE_PARSE_TREE_NODE_RECYCLE - { - FreeNode* n = (FreeNode* )node; - - THREAD_ATOMIC_START; - n->next = FreeNodeList; - FreeNodeList = n; - THREAD_ATOMIC_END; - } -#else - xfree(node); -#endif + node_recycle(node); } #ifdef USE_PARSE_TREE_NODE_RECYCLE extern int onig_free_node_list(void) { - FreeNode* n; - /* THREAD_ATOMIC_START; */ - while (IS_NOT_NULL(FreeNodeList)) { - n = FreeNodeList; - FreeNodeList = FreeNodeList->next; - xfree(n); - } + PopFreeNode(n, xfree(n)); /* THREAD_ATOMIC_END; */ return 0; } @@ -1138,14 +1146,9 @@ node_new(void) https://github.com/ruby/ruby/blob/trunk/regparse.c#L1146 Node* node; #ifdef USE_PARSE_TREE_NODE_RECYCLE - THREAD_ATOMIC_START; - if (IS_NOT_NULL(FreeNodeList)) { - node = (Node* )FreeNodeList; - FreeNodeList = FreeNodeList->next; - THREAD_ATOMIC_END; - return node; - } - THREAD_ATOMIC_END; + /* THREAD_ATOMIC_START; */ + PopFreeNode(n, return (Node* )n); + /* THREAD_ATOMIC_END; */ #endif node = (Node* )xmalloc(sizeof(Node)); @@ -1155,17 +1158,14 @@ node_new(void) https://github.com/ruby/ruby/blob/trunk/regparse.c#L1158 #if defined(USE_MULTI_THREAD_SYSTEM) && \ defined(USE_SHARED_CCLASS_TABLE) && \ - defined(USE_PARSE_TREE_NODE_RECYCLE) + defined(USE_PARSE_TREE_NODE_RECYCLE) && \ + 0 static Node* node_new_locked(void) { Node* node; - if (IS_NOT_NULL(FreeNodeList)) { - node = (Node* )FreeNodeList; - FreeNodeList = FreeNodeList->next; - return node; - } + PopFreeNode(n, return (Node* )n); node = (Node* )xmalloc(sizeof(Node)); /* xmemset(node, 0, sizeof(Node)); */ @@ -1195,7 +1195,8 @@ node_new_cclass(void) https://github.com/ruby/ruby/blob/trunk/regparse.c#L1195 #if defined(USE_MULTI_THREAD_SYSTEM) && \ defined(USE_SHARED_CCLASS_TABLE) && \ - defined(USE_PARSE_TREE_NODE_RECYCLE) + defined(USE_PARSE_TREE_NODE_RECYCLE) && \ + 0 static Node* node_new_cclass_locked(void) { Index: regcomp.c =================================================================== --- regcomp.c (revision 52994) +++ regcomp.c (revision 52995) @@ -5663,10 +5663,11 @@ onig_transfer(regex_t* to, regex_t* from https://github.com/ruby/ruby/blob/trunk/regcomp.c#L5663 extern void onig_chain_link_add(regex_t* to, regex_t* add) { - THREAD_ATOMIC_START; - REGEX_CHAIN_HEAD(to); - to->chain = add; - THREAD_ATOMIC_END; + /* THREAD_ATOMIC_START; */ + do { + REGEX_CHAIN_HEAD(to); + } while (IS_NOT_NULL(ATOMIC_PTR_CAS(to->chain, (regex_t* )NULL, add))); + /* THREAD_ATOMIC_END; */ } extern void Index: ChangeLog =================================================================== --- ChangeLog (revision 52994) +++ ChangeLog (revision 52995) @@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Dec 9 15:48:12 2015 Nobuyoshi Nakada <nobu@r...> + + * regcomp.c (onig_chain_link_add): use atomic operation instead of + mutex. + + * regint.h (ONIG_STATE_{INC,DEC}_THREAD): ditto. + + * regparse.c (PopFreeNode, node_recycle): ditto. + Wed Dec 9 14:45:27 2015 Koichi Sasada <ko1@a...> * gc.c (gc_mark_stacked_objects): fix typo. Index: ruby_atomic.h =================================================================== --- ruby_atomic.h (revision 52994) +++ ruby_atomic.h (revision 52995) @@ -42,6 +42,7 @@ typedef unsigned int rb_atomic_t; /* Any https://github.com/ruby/ruby/blob/trunk/ruby_atomic.h#L42 #pragma intrinsic(_InterlockedOr) #endif typedef LONG rb_atomic_t; +#define SIZEOF_ATOMIC_T SIZEOF_LONG # define ATOMIC_SET(var, val) InterlockedExchange(&(var), (val)) # define ATOMIC_INC(var) InterlockedIncrement(&(var)) @@ -143,6 +144,10 @@ ruby_atomic_size_exchange(size_t *ptr, s https://github.com/ruby/ruby/blob/trunk/ruby_atomic.h#L144 } #endif +#ifndef SIZEOF_ATOMIC_T +#define SIZEOF_ATOMIC_T SIZEOF_INT +#endif + #ifndef ATOMIC_SIZE_INC # define ATOMIC_SIZE_INC(var) ATOMIC_INC(var) #endif Index: common.mk =================================================================== --- common.mk (revision 52994) +++ common.mk (revision 52995) @@ -1345,6 +1345,7 @@ enc/unicode.$(OBJEXT): {$(VPATH)}missing https://github.com/ruby/ruby/blob/trunk/common.mk#L1345 enc/unicode.$(OBJEXT): {$(VPATH)}oniguruma.h enc/unicode.$(OBJEXT): {$(VPATH)}regenc.h enc/unicode.$(OBJEXT): {$(VPATH)}regint.h +enc/unicode.$(OBJEXT): {$(VPATH)}ruby_atomic.h enc/unicode.$(OBJEXT): {$(VPATH)}st.h enc/unicode.$(OBJEXT): {$(VPATH)}subst.h enc/us_ascii.$(OBJEXT): {$(VPATH)}config.h @@ -1987,6 +1988,7 @@ re.$(OBJEXT): {$(VPATH)}re.h https://github.com/ruby/ruby/blob/trunk/common.mk#L1988 re.$(OBJEXT): {$(VPATH)}regenc.h re.$(OBJEXT): {$(VPATH)}regex.h re.$(OBJEXT): {$(VPATH)}regint.h +re.$(OBJEXT): {$(VPATH)}ruby_atomic.h re.$(OBJEXT): {$(VPATH)}st.h re.$(OBJEXT): {$(VPATH)}subst.h re.$(OBJEXT): {$(VPATH)}util.h @@ -1999,6 +2001,7 @@ regcomp.$(OBJEXT): {$(VPATH)}oniguruma.h https://github.com/ruby/ruby/blob/trunk/common.mk#L2001 regcomp.$(OBJEXT): {$(VPATH)}regcomp.c regcomp.$(OBJEXT): {$(VPATH)}regenc.h regcomp.$(OBJEXT): {$(VPATH)}regint.h +regcomp.$(OBJEXT): {$(VPATH)}ruby_atomic.h regcomp.$(OBJEXT): {$(VPATH)}regparse.h regcomp.$(OBJEXT): {$(VPATH)}st.h regcomp.$(OBJEXT): {$(VPATH)}subst.h @@ -2011,6 +2014,7 @@ regenc.$(OBJEXT): {$(VPATH)}oniguruma.h https://github.com/ruby/ruby/blob/trunk/common.mk#L2014 regenc.$(OBJEXT): {$(VPATH)}regenc.c regenc.$(OBJEXT): {$(VPATH)}regenc.h regenc.$(OBJEXT): {$(VPATH)}regint.h +regenc.$(OBJEXT): {$(VPATH)}ruby_atomic.h regenc.$(OBJEXT): {$(VPATH)}st.h regenc.$(OBJEXT): {$(VPATH)}subst.h regerror.$(OBJEXT): $(hdrdir)/ruby/ruby.h @@ -2022,6 +2026,7 @@ regerror.$(OBJEXT): {$(VPATH)}oniguruma. https://github.com/ruby/ruby/blob/trunk/common.mk#L2026 regerror.$(OBJEXT): {$(VPATH)}regenc.h regerror.$(OBJEXT): {$(VPATH)}regerror.c regerror.$(OBJEXT): {$(VPATH)}regint.h +regerror.$(OBJEXT): {$(VPATH)}ruby_atomic.h regerror.$(OBJEXT): {$(VPATH)}st.h regerror.$(OBJEXT): {$(VPATH)}subst.h regexec.$(OBJEXT): $(hdrdir)/ruby/ruby.h @@ -2033,6 +2038,7 @@ regexec.$(OBJEXT): {$(VPATH)}oniguruma.h https://github.com/ruby/ruby/blob/trunk/common.mk#L2038 regexec.$(OBJEXT): {$(VPATH)}regenc.h regexec.$(OBJEXT): {$(VPATH)}regexec.c regexec.$(OBJEXT): {$(VPATH)}regint.h +regexec.$(OBJEXT): {$(VPATH)}ruby_atomic.h regexec.$(OBJEXT): {$(VPATH)}st.h regexec.$(OBJEXT): {$(VPATH)}subst.h regparse.$(OBJEXT): $(hdrdir)/ruby/ruby.h @@ -2045,6 +2051,7 @@ regparse.$(OBJEXT): {$(VPATH)}regenc.h https://github.com/ruby/ruby/blob/trunk/common.mk#L2051 regparse.$(OBJEXT): {$(VPATH)}regint.h regparse.$(OBJEXT): {$(VPATH)}regparse.c regparse.$(OBJEXT): {$(VPATH)}regparse.h +regparse.$(OBJEXT): {$(VPATH)}ruby_atomic.h regparse.$(OBJEXT): {$(VPATH)}st.h regparse.$(OBJEXT): {$(VPATH)}subst.h regsyntax.$(OBJEXT): $(hdrdir)/ruby/ruby.h @@ -2056,6 +2063,7 @@ regsyntax.$(OBJEXT): {$(VPATH)}oniguruma https://github.com/ruby/ruby/blob/trunk/common.mk#L2063 regsyntax.$(OBJEXT): {$(VPATH)}regenc.h regsyntax.$(OBJEXT): {$(VPATH)}regint.h regsyntax.$(OBJEXT): {$(VPATH)}regsyntax.c +regsyntax.$(OBJEXT): {$(VPATH)}ruby_atomic.h regsyntax.$(OBJEXT): {$(VPATH)}st.h regsyntax.$(OBJEXT): {$(VPATH)}subst.h ruby.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h Index: regint.h =================================================================== --- regint.h (revision 52994) +++ regint.h (revision 52995) @@ -95,6 +95,7 @@ https://github.com/ruby/ruby/blob/trunk/regint.h#L95 #ifndef RUBY_DEFINES_H #include "ruby/ruby.h" +#include "ruby_atomic.h" #undef xmalloc #undef xrealloc #undef xcalloc @@ -238,6 +239,10 @@ extern pthread_mutex_t gOnigMutex; https://github.com/ruby/ruby/blob/trunk/regint.h#L239 #define ONIG_STATE_INC(reg) (reg)->state++ #define ONIG_STATE_DEC(reg) (reg)->state-- +#if SIZEOF_ATOMIC_T == SIZEOF_INT +#define ONIG_STATE_INC_THREAD(reg) (ATOMIC_INC((reg)->state)) +#define ONIG_STATE_DEC_THREAD(reg) (ATOMIC_DEC((reg)->state)) +#else #define ONIG_STATE_INC_THREAD(reg) do {\ THREAD_ATOMIC_START;\ (reg)->state++;\ @@ -248,6 +253,7 @@ extern pthread_mutex_t gOnigMutex; https://github.com/ruby/ruby/blob/trunk/regint.h#L253 (reg)->state--;\ THREAD_ATOMIC_END;\ } while(0) +#endif #else #define ONIG_STATE_INC(reg) /* Nothing */ #define ONIG_STATE_DEC(reg) /* Nothing */ -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/