summaryrefslogtreecommitdiff
path: root/ruby_atomic.h
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-08-06 01:44:03 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-08-06 01:44:03 (GMT)
commitec10c033a7f6ba4819b7a65eb31eba432028f28a (patch)
treed46a845820f797fae3eed46958c6dee3ba6ed3a6 /ruby_atomic.h
parentff7eb4c656b0d7629ff00366878fed5fbf86eb93 (diff)
ruby_atomic.h: atomic VALUE operations
* ruby_atomic.h (ATOMIC_VALUE_EXCHANGE, ATOMIC_VALUE_CAS): add atomic operations for VALUE. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51498 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ruby_atomic.h')
-rw-r--r--ruby_atomic.h73
1 files changed, 67 insertions, 6 deletions
diff --git a/ruby_atomic.h b/ruby_atomic.h
index ad7d77a..cd2fbce 100644
--- a/ruby_atomic.h
+++ b/ruby_atomic.h
@@ -17,8 +17,7 @@ typedef unsigned int rb_atomic_t;
# 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_PTR_EXCHANGE(var, val) __atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST)
-# define ATOMIC_PTR_CAS(var, oldval, newval) ATOMIC_CAS(var, oldval, newval)
+# define RUBY_ATOMIC_GENERIC_MACRO 1
#elif defined HAVE_GCC_SYNC_BUILTINS
/* @shyouhei hack to support atomic operations in case of gcc. Gcc
@@ -36,8 +35,7 @@ typedef unsigned int rb_atomic_t; /* Anything OK */
# define ATOMIC_SIZE_ADD(var, val) __sync_fetch_and_add(&(var), (val))
# define ATOMIC_SIZE_SUB(var, val) __sync_fetch_and_sub(&(var), (val))
-# define ATOMIC_PTR_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
-# define ATOMIC_PTR_CAS(var, oldval, newval) ATOMIC_CAS(var, oldval, newval)
+# define RUBY_ATOMIC_GENERIC_MACRO 1
#elif defined _WIN32
#if defined _MSC_VER && _MSC_VER > 1200
@@ -135,9 +133,9 @@ extern rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr,
# define ATOMIC_SIZE_ADD(var, val) (void)((var) += (val))
# define ATOMIC_SIZE_SUB(var, val) (void)((var) -= (val))
-# define ATOMIC_SIZE_EXCHANGE(var, val) atomic_size_exchange(&(var), (val))
+# define ATOMIC_SIZE_EXCHANGE(var, val) ruby_atomic_size_exchange(&(var), (val))
static inline size_t
-atomic_size_exchange(size_t *ptr, size_t val)
+ruby_atomic_size_exchange(size_t *ptr, size_t val)
{
size_t old = *ptr;
*ptr = val;
@@ -158,14 +156,77 @@ atomic_size_exchange(size_t *ptr, size_t val)
# define ATOMIC_SIZE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
#endif
+#if RUBY_ATOMIC_GENERIC_MACRO
+# ifndef ATOMIC_PTR_EXCHANGE
+# define ATOMIC_PTR_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
+# endif
+# ifndef ATOMIC_PTR_CAS
+# define ATOMIC_PTR_CAS(var, oldval, newval) ATOMIC_CAS(var, oldval, newval)
+# endif
+
+# ifndef ATOMIC_VALUE_EXCHANGE
+# define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
+# endif
+# ifndef ATOMIC_VALUE_CAS
+# define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, newval)
+# endif
+#endif
+
#ifndef ATOMIC_PTR_EXCHANGE
# if SIZEOF_VOIDP == SIZEOF_SIZE_T
# define ATOMIC_PTR_EXCHANGE(var, val) (void *)ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
+# else
+# define ATOMIC_PTR_EXCHANGE(var, val) ruby_atomic_ptr_exchange((const void **)&(var), (val))
+static inline void *
+ruby_atomic_ptr_exchange(const void **ptr, const void *val)
+{
+ const void *const old = *ptr;
+ *ptr = val;
+ return (void *)old;
+}
# endif
#endif
#ifndef ATOMIC_PTR_CAS
# if SIZEOF_VOIDP == SIZEOF_SIZE_T
# define ATOMIC_PTR_CAS(var, oldval, val) (void *)ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
+# else
+# define ATOMIC_PTR_CAS(var, oldval, val) ruby_atomic_ptr_cas(&(var), (oldval), (val))
+static inline void *
+ruby_atomic_ptr_cas(const void **ptr, const void *oldval, const void *val)
+{
+ const void *const old = *ptr;
+ if (old == oldval) *ptr = val;
+ return (void *)old;
+}
+# endif
+#endif
+
+#ifndef ATOMIC_VALUE_EXCHANGE
+# if SIZEOF_VALUE == SIZEOF_SIZE_T
+# define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
+# else
+# define ATOMIC_VALUE_EXCHANGE(var, val) ruby_atomic_value_exchange(&(var), (val))
+static inline VALUE
+ruby_atomic_value_exchange(VALUE *ptr, VALUE val)
+{
+ const VALUE old = *ptr;
+ *ptr = val;
+ return old;
+}
+# endif
+#endif
+#ifndef ATOMIC_VALUE_CAS
+# if SIZEOF_VALUE == SIZEOF_SIZE_T
+# define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
+# else
+# define ATOMIC_VALUE_CAS(var, oldval, val) ruby_atomic_value_cas(&(var), (oldval), (val))
+static inline VALUE
+ruby_atomic_value_cas(VALUE *ptr, VALUE oldval, VALUE val)
+{
+ const VALUE old = *ptr;
+ if (old == oldval) *ptr = val;
+ return old;
+}
# endif
#endif