Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

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) RB_GNUC_EXTENSION_BLOCK( \
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 # ifdef InterlockedExchangePointer
94 # define ATOMIC_PTR_EXCHANGE(var, val) InterlockedExchangePointer((PVOID volatile *)&(var), (PVOID)(val))
95 # endif /* See below for definitions of other situations */
96
97 #elif defined(__sun) && defined(HAVE_ATOMIC_H)
98 #include <atomic.h>
99 typedef unsigned int rb_atomic_t;
100
101 # define ATOMIC_SET(var, val) (void)atomic_swap_uint(&(var), (val))
102 # define ATOMIC_INC(var) atomic_inc_uint(&(var))
103 # define ATOMIC_DEC(var) atomic_dec_uint(&(var))
104 # define ATOMIC_OR(var, val) atomic_or_uint(&(var), (val))
105 # define ATOMIC_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
106 # define ATOMIC_CAS(var, oldval, newval) atomic_cas_uint(&(var), (oldval), (newval))
107
108 # if SIZEOF_SIZE_T == SIZEOF_LONG
109 # define ATOMIC_SIZE_ADD(var, val) atomic_add_long(&(var), (val))
110 # define ATOMIC_SIZE_SUB(var, val) atomic_add_long(&(var), -(val))
111 # define ATOMIC_SIZE_INC(var) atomic_inc_ulong(&(var))
112 # define ATOMIC_SIZE_DEC(var) atomic_dec_ulong(&(var))
113 # define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_ulong(&(var), (val))
114 # define ATOMIC_SIZE_CAS(var, oldval, val) atomic_cas_ulong(&(var), (oldval), (val))
115 # else
116 # define ATOMIC_SIZE_ADD(var, val) atomic_add_int(&(var), (val))
117 # define ATOMIC_SIZE_SUB(var, val) atomic_add_int(&(var), -(val))
118 # define ATOMIC_SIZE_INC(var) atomic_inc_uint(&(var))
119 # define ATOMIC_SIZE_DEC(var) atomic_dec_uint(&(var))
120 # define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
121 # endif
122
123 #else
124 typedef int rb_atomic_t;
125 #define NEED_RUBY_ATOMIC_OPS
126 extern rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val);
127 extern rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr,
128 rb_atomic_t cmp,
129 rb_atomic_t newval);
130
131 # define ATOMIC_SET(var, val) (void)((var) = (val))
132 # define ATOMIC_INC(var) ((var)++)
133 # define ATOMIC_DEC(var) ((var)--)
134 # define ATOMIC_OR(var, val) ((var) |= (val))
135 # define ATOMIC_EXCHANGE(var, val) ruby_atomic_exchange(&(var), (val))
136 # define ATOMIC_CAS(var, oldval, newval) ruby_atomic_compare_and_swap(&(var), (oldval), (newval))
137
138 # define ATOMIC_SIZE_ADD(var, val) (void)((var) += (val))
139 # define ATOMIC_SIZE_SUB(var, val) (void)((var) -= (val))
140 # define ATOMIC_SIZE_EXCHANGE(var, val) ruby_atomic_size_exchange(&(var), (val))
141 static inline size_t
142 ruby_atomic_size_exchange(size_t *ptr, size_t val)
143 {
144 size_t old = *ptr;
145 *ptr = val;
146 return old;
147 }
148 #endif
149
150 #ifndef ATOMIC_SIZE_INC
151 # define ATOMIC_SIZE_INC(var) ATOMIC_INC(var)
152 #endif
153
154 #ifndef ATOMIC_SIZE_DEC
155 # define ATOMIC_SIZE_DEC(var) ATOMIC_DEC(var)
156 #endif
157
158 #ifndef ATOMIC_SIZE_EXCHANGE
159 # define ATOMIC_SIZE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
160 #endif
161
162 #ifndef ATOMIC_SIZE_CAS
163 # define ATOMIC_SIZE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
164 #endif
165
166 #if RUBY_ATOMIC_GENERIC_MACRO
167 # ifndef ATOMIC_PTR_EXCHANGE
168 # define ATOMIC_PTR_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
169 # endif
170
171 # ifndef ATOMIC_PTR_CAS
172 # define ATOMIC_PTR_CAS(var, oldval, newval) ATOMIC_CAS(var, oldval, newval)
173 # endif
174
175 # ifndef ATOMIC_VALUE_EXCHANGE
176 # define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
177 # endif
178
179 # ifndef ATOMIC_VALUE_CAS
180 # define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
181 # endif
182 #endif
183
184 #ifndef ATOMIC_PTR_EXCHANGE
185 # if SIZEOF_VOIDP == SIZEOF_SIZE_T
186 # define ATOMIC_PTR_EXCHANGE(var, val) (void *)ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
187 # else
188 # define ATOMIC_PTR_EXCHANGE(var, val) ruby_atomic_ptr_exchange((const void **)&(var), (val))
189 static inline void *
190 ruby_atomic_ptr_exchange(const void **ptr, const void *val)
191 {
192 const void *const old = *ptr;
193 *ptr = val;
194 return (void *)old;
195 }
196 # endif
197 #endif
198
199 #ifndef ATOMIC_PTR_CAS
200 # if SIZEOF_VOIDP == SIZEOF_SIZE_T
201 # define ATOMIC_PTR_CAS(var, oldval, val) (void *)ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
202 # else
203 # define ATOMIC_PTR_CAS(var, oldval, val) ruby_atomic_ptr_cas(&(var), (oldval), (val))
204 static inline void *
205 ruby_atomic_ptr_cas(const void **ptr, const void *oldval, const void *val)
206 {
207 const void *const old = *ptr;
208 if (old == oldval) *ptr = val;
209 return (void *)old;
210 }
211 # endif
212 #endif
213
214 #ifndef ATOMIC_VALUE_EXCHANGE
215 # if SIZEOF_VALUE == SIZEOF_SIZE_T
216 # define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
217 # else
218 # define ATOMIC_VALUE_EXCHANGE(var, val) ruby_atomic_value_exchange(&(var), (val))
219 static inline VALUE
220 ruby_atomic_value_exchange(VALUE *ptr, VALUE val)
221 {
222 const VALUE old = *ptr;
223 *ptr = val;
224 return old;
225 }
226 # endif
227 #endif
228
229 #ifndef ATOMIC_VALUE_CAS
230 # if SIZEOF_VALUE == SIZEOF_SIZE_T
231 # define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
232 # else
233 # define ATOMIC_VALUE_CAS(var, oldval, val) ruby_atomic_value_cas(&(var), (oldval), (val))
234 static inline VALUE
235 ruby_atomic_value_cas(VALUE *ptr, VALUE oldval, VALUE val)
236 {
237 const VALUE old = *ptr;
238 if (old == oldval) *ptr = val;
239 return old;
240 }
241 # endif
242 #endif
243
244 #endif /* RUBY_ATOMIC_H */
ruby_atomic_exchange
rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val)
Definition: signal.c:46
VALUE
unsigned long VALUE
Definition: ruby.h:102
old
VALUE ID VALUE old
Definition: rb_mjit_min_header-2.7.0.h:16133
ptr
struct RIMemo * ptr
Definition: debug.c:74
ruby_atomic_compare_and_swap
rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr, rb_atomic_t cmp, rb_atomic_t newval)
Definition: signal.c:54
rb_atomic_t
int rb_atomic_t
Definition: ruby_atomic.h:124