[前][次][番号順一覧][スレッド一覧]

ruby-changes:60912

From: Nobuyoshi <ko1@a...>
Date: Sun, 26 Apr 2020 21:46:42 +0900 (JST)
Subject: [ruby-changes:60912] 1760c60174 (master): Clarified return value of atomic macros

https://git.ruby-lang.org/ruby.git/commit/?id=1760c60174

From 1760c60174b80e837371a19ec13b1148629262cb Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sun, 26 Apr 2020 19:34:07 +0900
Subject: Clarified return value of atomic macros


diff --git a/ruby_atomic.h b/ruby_atomic.h
index 0742791..eaa9fcd 100644
--- a/ruby_atomic.h
+++ b/ruby_atomic.h
@@ -1,12 +1,17 @@ https://github.com/ruby/ruby/blob/trunk/ruby_atomic.h#L1
 #ifndef RUBY_ATOMIC_H
 #define RUBY_ATOMIC_H
 
+/*
+ * - ATOMIC_CAS, ATOMIC_EXCHANGE, ATOMIC_FETCH_*:
+ *   return the old * value.
+ * - ATOMIC_ADD, ATOMIC_SUB, ATOMIC_INC, ATOMIC_DEC, ATOMIC_OR, ATOMIC_SET:
+ *   may be void.
+ */
 #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_FETCH_ADD(var, val) __atomic_fetch_add(&(var), (val), __ATOMIC_SEQ_CST)
+# define ATOMIC_FETCH_SUB(var, val) __atomic_fetch_sub(&(var), (val), __ATOMIC_SEQ_CST)
 # define ATOMIC_OR(var, val) __atomic_fetch_or(&(var), (val), __ATOMIC_SEQ_CST)
 # define ATOMIC_EXCHANGE(var, val) __atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST)
 # define ATOMIC_CAS(var, oldval, newval) RB_GNUC_EXTENSION_BLOCK( \
@@ -14,9 +19,6 @@ typedef unsigned int rb_atomic_t; https://github.com/ruby/ruby/blob/trunk/ruby_atomic.h#L19
    __atomic_compare_exchange_n(&(var), &oldvaldup, (newval), 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
    oldvaldup )
 
-# 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 RUBY_ATOMIC_GENERIC_MACRO 1
 
 #elif defined HAVE_GCC_SYNC_BUILTINS
@@ -25,16 +27,12 @@ typedef unsigned int rb_atomic_t; https://github.com/ruby/ruby/blob/trunk/ruby_atomic.h#L27
  * http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html */
 
 typedef unsigned int rb_atomic_t; /* Anything OK */
-# define ATOMIC_SET(var, val)  (void)__sync_lock_test_and_set(&(var), (val))
-# define ATOMIC_INC(var) __sync_fetch_and_add(&(var), 1)
-# define ATOMIC_DEC(var) __sync_fetch_and_sub(&(var), 1)
+# define ATOMIC_FETCH_ADD(var, val) __sync_fetch_and_add(&(var), (val))
+# define ATOMIC_FETCH_SUB(var, var) __sync_fetch_and_sub(&(var), (val))
 # define ATOMIC_OR(var, val) __sync_fetch_and_or(&(var), (val))
 # define ATOMIC_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
 # define ATOMIC_CAS(var, oldval, newval) __sync_val_compare_and_swap(&(var), (oldval), (newval))
 
-# 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 RUBY_ATOMIC_GENERIC_MACRO 1
 
 #elif defined _WIN32
@@ -46,6 +44,8 @@ typedef LONG rb_atomic_t; https://github.com/ruby/ruby/blob/trunk/ruby_atomic.h#L44
 # define ATOMIC_SET(var, val) InterlockedExchange(&(var), (val))
 # define ATOMIC_INC(var) InterlockedIncrement(&(var))
 # define ATOMIC_DEC(var) InterlockedDecrement(&(var))
+# define ATOMIC_FETCH_ADD(var, val) InterlockedExchangeAdd(&(var), (val))
+# define ATOMIC_FETCH_SUB(var, val) InterlockedExchangeAdd(&(var), -(LONG)(val))
 #if defined __GNUC__
 # define ATOMIC_OR(var, val) __asm__("lock\n\t" "orl\t%1, %0" : "=m"(var) : "Ir"(val))
 #elif MSC_VERSION_BEFORE(1300)
@@ -98,13 +98,28 @@ rb_w32_atomic_cas(volatile rb_atomic_t *var, rb_atomic_t oldval, rb_atomic_t new https://github.com/ruby/ruby/blob/trunk/ruby_atomic.h#L98
 #include <atomic.h>
 typedef unsigned int rb_atomic_t;
 
-# define ATOMIC_SET(var, val) (void)atomic_swap_uint(&(var), (val))
 # define ATOMIC_INC(var) atomic_inc_uint(&(var))
 # define ATOMIC_DEC(var) atomic_dec_uint(&(var))
+# define ATOMIC_FETCH_ADD(var, val) rb_atomic_fetch_add(&(var), (val))
+# define ATOMIC_FETCH_SUB(var, val) rb_atomic_fetch_sub(&(var), (val))
+# define ATOMIC_ADD(var, val) atomic_add_uint(&(var), (val))
+# define ATOMIC_SUB(var, val) atomic_sub_uint(&(var), (val))
 # define ATOMIC_OR(var, val) atomic_or_uint(&(var), (val))
 # define ATOMIC_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
 # define ATOMIC_CAS(var, oldval, newval) atomic_cas_uint(&(var), (oldval), (newval))
 
+static inline rb_atomic_t
+rb_atomic_fetch_add(volatile rb_atomic_t *var, rb_atomic_t val)
+{
+    return atomic_add_int_nv(var, val) - val;
+}
+
+static inline rb_atomic_t
+rb_atomic_fetch_sub(volatile rb_atomic_t *var, rb_atomic_t val)
+{
+    return atomic_sub_int_nv(var, val) + val;
+}
+
 # if defined(_LP64) || defined(_I32LPx)
 #  define ATOMIC_SIZE_ADD(var, val) atomic_add_long(&(var), (val))
 #  define ATOMIC_SIZE_SUB(var, val) atomic_add_long(&(var), -(val))
@@ -124,6 +139,26 @@ typedef unsigned int rb_atomic_t; https://github.com/ruby/ruby/blob/trunk/ruby_atomic.h#L139
 # error No atomic operation found
 #endif
 
+#ifndef ATOMIC_SET
+# define ATOMIC_SET(var, val) (void)ATOMIC_EXCHANGE(var, val)
+#endif
+
+#ifndef ATOMIC_ADD
+# define ATOMIC_ADD(var, val) (void)ATOMIC_FETCH_ADD(var, val)
+#endif
+
+#ifndef ATOMIC_SUB
+# define ATOMIC_SUB(var, val) (void)ATOMIC_FETCH_SUB(var, val)
+#endif
+
+#ifndef ATOMIC_INC
+# define ATOMIC_INC(var) ATOMIC_ADD(var, 1)
+#endif
+
+#ifndef ATOMIC_DEC
+# define ATOMIC_DEC(var) ATOMIC_SUB(var, 1)
+#endif
+
 #ifndef ATOMIC_SIZE_INC
 # define ATOMIC_SIZE_INC(var) ATOMIC_INC(var)
 #endif
@@ -140,6 +175,14 @@ typedef unsigned int rb_atomic_t; https://github.com/ruby/ruby/blob/trunk/ruby_atomic.h#L175
 # define ATOMIC_SIZE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
 #endif
 
+#ifndef ATOMIC_SIZE_ADD
+# define ATOMIC_SIZE_ADD(var, val) ATOMIC_ADD(var, val)
+#endif
+
+#ifndef ATOMIC_SIZE_SUB
+# define ATOMIC_SIZE_SUB(var, val) ATOMIC_SUB(var, val)
+#endif
+
 #if RUBY_ATOMIC_GENERIC_MACRO
 # ifndef ATOMIC_PTR_EXCHANGE
 #   define ATOMIC_PTR_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]