From 0791c940ad9c4749e72accb91c7e22e684fde668 Mon Sep 17 00:00:00 2001 From: glass Date: Tue, 26 Nov 2013 02:45:53 +0000 Subject: [PATCH] * ruby_atomic.h: use __atomic builtin functions supported by GCC. __sync family are legacy functions now and it is recommended that new code use the __atomic functions. http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html * configure.in: check existence of __atomic functions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43851 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++++++ configure.in | 25 ++++++++++++++++++++----- ruby_atomic.h | 20 ++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2f48ce3277..6b69e2144d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Tue Nov 26 11:43:19 2013 Masaki Matsushita + + * ruby_atomic.h: use __atomic builtin functions supported by GCC. + __sync family are legacy functions now and it is recommended + that new code use the __atomic functions. + http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html + + * configure.in: check existence of __atomic functions. + Tue Nov 26 10:57:49 2013 Nobuyoshi Nakada * ext/bigdecimal/bigdecimal.gemspec: revert Gem::Specification#date diff --git a/configure.in b/configure.in index 1e0550d187..1a6379cb4a 100644 --- a/configure.in +++ b/configure.in @@ -1480,7 +1480,22 @@ if test "$GCC" = yes; then AC_DEFINE_UNQUOTED([RUBY_ALIAS_FUNCTION_VOID(prot, name, args)], [RUBY_ALIAS_FUNCTION_TYPE(void, prot, name, args)]) fi - AC_CACHE_CHECK([for atomic builtins], [rb_cv_gcc_atomic_builtins], [ + + AC_CACHE_CHECK([for __atomic builtins], [rb_cv_gcc_atomic_builtins], [ + AC_TRY_LINK([unsigned char atomic_var;], + [ + __atomic_exchange_n(&atomic_var, 0, __ATOMIC_SEQ_CST); + __atomic_exchange_n(&atomic_var, 1, __ATOMIC_SEQ_CST); + __atomic_fetch_add(&atomic_var, 1, __ATOMIC_SEQ_CST); + __atomic_fetch_sub(&atomic_var, 1, __ATOMIC_SEQ_CST); + ], + [rb_cv_gcc_atomic_builtins=yes], + [rb_cv_gcc_atomic_builtins=no])]) + if test "$rb_cv_gcc_atomic_builtins" = yes; then + AC_DEFINE(HAVE_GCC_ATOMIC_BUILTINS) + fi + + AC_CACHE_CHECK([for __sync builtins], [rb_cv_gcc_sync_builtins], [ AC_TRY_LINK([unsigned char atomic_var;], [ __sync_lock_test_and_set(&atomic_var, 0); @@ -1488,10 +1503,10 @@ if test "$GCC" = yes; then __sync_fetch_and_add(&atomic_var, 1); __sync_fetch_and_sub(&atomic_var, 1); ], - [rb_cv_gcc_atomic_builtins=yes], - [rb_cv_gcc_atomic_builtins=no])]) - if test "$rb_cv_gcc_atomic_builtins" = yes; then - AC_DEFINE(HAVE_GCC_ATOMIC_BUILTINS) + [rb_cv_gcc_sync_builtins=yes], + [rb_cv_gcc_sync_builtins=no])]) + if test "$rb_cv_gcc_sync_builtins" = yes; then + AC_DEFINE(HAVE_GCC_SYNC_BUILTINS) fi AC_CACHE_CHECK(for __builtin_unreachable, rb_cv_func___builtin_unreachable, diff --git a/ruby_atomic.h b/ruby_atomic.h index 7e16d287f8..091a608011 100644 --- a/ruby_atomic.h +++ b/ruby_atomic.h @@ -3,6 +3,26 @@ #if 0 #elif defined HAVE_GCC_ATOMIC_BUILTINS +typedef unsigned int rb_atomic_t; +# define ATOMIC_SET(var, val) (void)__atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST) +# define ATOMIC_INC(var) __atomic_fetch_add(&(var), 1, __ATOMIC_SEQ_CST) +# define ATOMIC_DEC(var) __atomic_fetch_sub(&(var), 1, __ATOMIC_SEQ_CST) +# define ATOMIC_OR(var, val) __atomic_or_fetch(&(var), (val), __ATOMIC_SEQ_CST) +# define ATOMIC_EXCHANGE(var, val) __atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST) +# define ATOMIC_CAS(var, oldval, newval) \ +({ __typeof__(__typeof__(var) *) ptr = &(var); /* expression var may have side effects */ \ + __typeof__(oldval) oldvaldup = (oldval); /* oldval should not be modified */ \ + __typeof__(var) tmp = *ptr; \ + __atomic_compare_exchange_n(ptr, &oldvaldup, (newval), 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \ + tmp; }) + +# define ATOMIC_SIZE_ADD(var, val) __atomic_fetch_add(&(var), (val), __ATOMIC_SEQ_CST) +# define ATOMIC_SIZE_SUB(var, val) __atomic_fetch_sub(&(var), (val), __ATOMIC_SEQ_CST) +# define ATOMIC_SIZE_INC(var) __atomic_fetch_add(&(var), 1, __ATOMIC_SEQ_CST) +# define ATOMIC_SIZE_DEC(var) __atomic_fetch_sub(&(var), 1, __ATOMIC_SEQ_CST) +# define ATOMIC_SIZE_EXCHANGE(var, val) __atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST) + +#elif defined HAVE_GCC_SYNC_BUILTINS /* @shyouhei hack to support atomic operations in case of gcc. Gcc * has its own pseudo-insns to support them. See info, or * http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html */