Ruby  2.5.0dev(2017-10-22revision60238)
ruby_atomic.h
Go to the documentation of this file.
1 #ifndef RUBY_ATOMIC_H
2 #define RUBY_ATOMIC_H
3 
4 #if 0
5 #elif defined HAVE_GCC_ATOMIC_BUILTINS
6 typedef unsigned int rb_atomic_t;
7 # define ATOMIC_SET(var, val) (void)__atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST)
8 # define ATOMIC_INC(var) __atomic_fetch_add(&(var), 1, __ATOMIC_SEQ_CST)
9 # define ATOMIC_DEC(var) __atomic_fetch_sub(&(var), 1, __ATOMIC_SEQ_CST)
10 # define ATOMIC_OR(var, val) __atomic_fetch_or(&(var), (val), __ATOMIC_SEQ_CST)
11 # define ATOMIC_EXCHANGE(var, val) __atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST)
12 # define ATOMIC_CAS(var, oldval, newval) \
13 ({ __typeof__(var) oldvaldup = (oldval); /* oldval should not be modified */ \
14  __atomic_compare_exchange_n(&(var), &oldvaldup, (newval), 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
15  oldvaldup; })
16 
17 # define ATOMIC_SIZE_ADD(var, val) __atomic_fetch_add(&(var), (val), __ATOMIC_SEQ_CST)
18 # define ATOMIC_SIZE_SUB(var, val) __atomic_fetch_sub(&(var), (val), __ATOMIC_SEQ_CST)
19 
20 # define RUBY_ATOMIC_GENERIC_MACRO 1
21 
22 #elif defined HAVE_GCC_SYNC_BUILTINS
23 /* @shyouhei hack to support atomic operations in case of gcc. Gcc
24  * has its own pseudo-insns to support them. See info, or
25  * http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html */
26 
27 typedef unsigned int rb_atomic_t; /* Anything OK */
28 # define ATOMIC_SET(var, val) (void)__sync_lock_test_and_set(&(var), (val))
29 # define ATOMIC_INC(var) __sync_fetch_and_add(&(var), 1)
30 # define ATOMIC_DEC(var) __sync_fetch_and_sub(&(var), 1)
31 # define ATOMIC_OR(var, val) __sync_fetch_and_or(&(var), (val))
32 # define ATOMIC_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
33 # define ATOMIC_CAS(var, oldval, newval) __sync_val_compare_and_swap(&(var), (oldval), (newval))
34 
35 # define ATOMIC_SIZE_ADD(var, val) __sync_fetch_and_add(&(var), (val))
36 # define ATOMIC_SIZE_SUB(var, val) __sync_fetch_and_sub(&(var), (val))
37 
38 # define RUBY_ATOMIC_GENERIC_MACRO 1
39 
40 #elif defined _WIN32
41 #if defined _MSC_VER && _MSC_VER > 1200
42 #pragma intrinsic(_InterlockedOr)
43 #endif
44 typedef LONG rb_atomic_t;
45 
46 # define ATOMIC_SET(var, val) InterlockedExchange(&(var), (val))
47 # define ATOMIC_INC(var) InterlockedIncrement(&(var))
48 # define ATOMIC_DEC(var) InterlockedDecrement(&(var))
49 #if defined __GNUC__
50 # define ATOMIC_OR(var, val) __asm__("lock\n\t" "orl\t%1, %0" : "=m"(var) : "Ir"(val))
51 #elif defined _MSC_VER && _MSC_VER <= 1200
52 # define ATOMIC_OR(var, val) rb_w32_atomic_or(&(var), (val))
53 static inline void
54 rb_w32_atomic_or(volatile rb_atomic_t *var, rb_atomic_t val)
55 {
56 #ifdef _M_IX86
57  __asm mov eax, var;
58  __asm mov ecx, val;
59  __asm lock or [eax], ecx;
60 #else
61 #error unsupported architecture
62 #endif
63 }
64 #else
65 # define ATOMIC_OR(var, val) _InterlockedOr(&(var), (val))
66 #endif
67 # define ATOMIC_EXCHANGE(var, val) InterlockedExchange(&(var), (val))
68 # define ATOMIC_CAS(var, oldval, newval) InterlockedCompareExchange(&(var), (newval), (oldval))
69 # if defined _MSC_VER && _MSC_VER <= 1200
70 static inline rb_atomic_t
71 rb_w32_atomic_cas(volatile rb_atomic_t *var, rb_atomic_t oldval, rb_atomic_t newval)
72 {
73  return (rb_atomic_t)InterlockedCompareExchange((PVOID *)var, (PVOID)newval, (PVOID)oldval);
74 }
75 # undef ATOMIC_CAS
76 # define ATOMIC_CAS(var, oldval, newval) rb_w32_atomic_cas(&(var), (oldval), (newval))
77 # endif
78 # ifdef _M_AMD64
79 # define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd64((LONG_LONG *)&(var), (val))
80 # define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd64((LONG_LONG *)&(var), -(LONG)(val))
81 # define ATOMIC_SIZE_INC(var) InterlockedIncrement64(&(var))
82 # define ATOMIC_SIZE_DEC(var) InterlockedDecrement64(&(var))
83 # define ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange64(&(var), (val))
84 # define ATOMIC_SIZE_CAS(var, oldval, newval) InterlockedCompareExchange64(&(var), (newval), (oldval))
85 # else
86 # define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd((LONG *)&(var), (val))
87 # define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd((LONG *)&(var), -(LONG)(val))
88 # define ATOMIC_SIZE_INC(var) InterlockedIncrement((LONG *)&(var))
89 # define ATOMIC_SIZE_DEC(var) InterlockedDecrement((LONG *)&(var))
90 # define ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange((LONG *)&(var), (val))
91 # endif
92 
93 #elif defined(__sun) && defined(HAVE_ATOMIC_H)
94 #include <atomic.h>
95 typedef unsigned int rb_atomic_t;
96 
97 # define ATOMIC_SET(var, val) (void)atomic_swap_uint(&(var), (val))
98 # define ATOMIC_INC(var) atomic_inc_uint(&(var))
99 # define ATOMIC_DEC(var) atomic_dec_uint(&(var))
100 # define ATOMIC_OR(var, val) atomic_or_uint(&(var), (val))
101 # define ATOMIC_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
102 # define ATOMIC_CAS(var, oldval, newval) atomic_cas_uint(&(var), (oldval), (newval))
103 
104 # if SIZEOF_SIZE_T == SIZEOF_LONG
105 # define ATOMIC_SIZE_ADD(var, val) atomic_add_long(&(var), (val))
106 # define ATOMIC_SIZE_SUB(var, val) atomic_add_long(&(var), -(val))
107 # define ATOMIC_SIZE_INC(var) atomic_inc_ulong(&(var))
108 # define ATOMIC_SIZE_DEC(var) atomic_dec_ulong(&(var))
109 # define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_ulong(&(var), (val))
110 # define ATOMIC_SIZE_CAS(var, oldval, val) atomic_cas_ulong(&(var), (oldval), (val))
111 # else
112 # define ATOMIC_SIZE_ADD(var, val) atomic_add_int(&(var), (val))
113 # define ATOMIC_SIZE_SUB(var, val) atomic_add_int(&(var), -(val))
114 # define ATOMIC_SIZE_INC(var) atomic_inc_uint(&(var))
115 # define ATOMIC_SIZE_DEC(var) atomic_dec_uint(&(var))
116 # define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
117 # endif
118 
119 #else
120 typedef int rb_atomic_t;
121 #define NEED_RUBY_ATOMIC_OPS
122 extern rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val);
123 extern rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr,
124  rb_atomic_t cmp,
125  rb_atomic_t newval);
126 
127 # define ATOMIC_SET(var, val) (void)((var) = (val))
128 # define ATOMIC_INC(var) ((var)++)
129 # define ATOMIC_DEC(var) ((var)--)
130 # define ATOMIC_OR(var, val) ((var) |= (val))
131 # define ATOMIC_EXCHANGE(var, val) ruby_atomic_exchange(&(var), (val))
132 # define ATOMIC_CAS(var, oldval, newval) ruby_atomic_compare_and_swap(&(var), (oldval), (newval))
133 
134 # define ATOMIC_SIZE_ADD(var, val) (void)((var) += (val))
135 # define ATOMIC_SIZE_SUB(var, val) (void)((var) -= (val))
136 # define ATOMIC_SIZE_EXCHANGE(var, val) ruby_atomic_size_exchange(&(var), (val))
137 static inline size_t
138 ruby_atomic_size_exchange(size_t *ptr, size_t val)
139 {
140  size_t old = *ptr;
141  *ptr = val;
142  return old;
143 }
144 #endif
145 
146 #ifndef ATOMIC_SIZE_INC
147 # define ATOMIC_SIZE_INC(var) ATOMIC_INC(var)
148 #endif
149 #ifndef ATOMIC_SIZE_DEC
150 # define ATOMIC_SIZE_DEC(var) ATOMIC_DEC(var)
151 #endif
152 #ifndef ATOMIC_SIZE_EXCHANGE
153 # define ATOMIC_SIZE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
154 #endif
155 #ifndef ATOMIC_SIZE_CAS
156 # define ATOMIC_SIZE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
157 #endif
158 
159 #if RUBY_ATOMIC_GENERIC_MACRO
160 # ifndef ATOMIC_PTR_EXCHANGE
161 # define ATOMIC_PTR_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
162 # endif
163 # ifndef ATOMIC_PTR_CAS
164 # define ATOMIC_PTR_CAS(var, oldval, newval) ATOMIC_CAS(var, oldval, newval)
165 # endif
166 
167 # ifndef ATOMIC_VALUE_EXCHANGE
168 # define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
169 # endif
170 # ifndef ATOMIC_VALUE_CAS
171 # define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
172 # endif
173 #endif
174 
175 #ifndef ATOMIC_PTR_EXCHANGE
176 # if SIZEOF_VOIDP == SIZEOF_SIZE_T
177 # define ATOMIC_PTR_EXCHANGE(var, val) (void *)ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
178 # else
179 # define ATOMIC_PTR_EXCHANGE(var, val) ruby_atomic_ptr_exchange((const void **)&(var), (val))
180 static inline void *
181 ruby_atomic_ptr_exchange(const void **ptr, const void *val)
182 {
183  const void *const old = *ptr;
184  *ptr = val;
185  return (void *)old;
186 }
187 # endif
188 #endif
189 #ifndef ATOMIC_PTR_CAS
190 # if SIZEOF_VOIDP == SIZEOF_SIZE_T
191 # define ATOMIC_PTR_CAS(var, oldval, val) (void *)ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
192 # else
193 # define ATOMIC_PTR_CAS(var, oldval, val) ruby_atomic_ptr_cas(&(var), (oldval), (val))
194 static inline void *
195 ruby_atomic_ptr_cas(const void **ptr, const void *oldval, const void *val)
196 {
197  const void *const old = *ptr;
198  if (old == oldval) *ptr = val;
199  return (void *)old;
200 }
201 # endif
202 #endif
203 
204 #ifndef ATOMIC_VALUE_EXCHANGE
205 # if SIZEOF_VALUE == SIZEOF_SIZE_T
206 # define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
207 # else
208 # define ATOMIC_VALUE_EXCHANGE(var, val) ruby_atomic_value_exchange(&(var), (val))
209 static inline VALUE
210 ruby_atomic_value_exchange(VALUE *ptr, VALUE val)
211 {
212  const VALUE old = *ptr;
213  *ptr = val;
214  return old;
215 }
216 # endif
217 #endif
218 #ifndef ATOMIC_VALUE_CAS
219 # if SIZEOF_VALUE == SIZEOF_SIZE_T
220 # define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
221 # else
222 # define ATOMIC_VALUE_CAS(var, oldval, val) ruby_atomic_value_cas(&(var), (oldval), (val))
223 static inline VALUE
224 ruby_atomic_value_cas(VALUE *ptr, VALUE oldval, VALUE val)
225 {
226  const VALUE old = *ptr;
227  if (old == oldval) *ptr = val;
228  return old;
229 }
230 # endif
231 #endif
232 
233 #endif /* RUBY_ATOMIC_H */
rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr, rb_atomic_t cmp, rb_atomic_t newval)
Definition: signal.c:61
#define val
unsigned long VALUE
Definition: ruby.h:85
int rb_atomic_t
Definition: ruby_atomic.h:120
rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val)
Definition: signal.c:53