Ruby: include/ruby/atomic.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

atomic.h
Go to the documentation of this file.
1#ifndef RUBY_ATOMIC_H
2#define RUBY_ATOMIC_H
3
4/*
5 * - RUBY_ATOMIC_CAS, RUBY_ATOMIC_EXCHANGE, RUBY_ATOMIC_FETCH_*:
6 * return the old * value.
7 * - RUBY_ATOMIC_ADD, RUBY_ATOMIC_SUB, RUBY_ATOMIC_INC, RUBY_ATOMIC_DEC, RUBY_ATOMIC_OR, RUBY_ATOMIC_SET:
8 * may be void.
9 */
10#if 0
11#elif defined HAVE_GCC_ATOMIC_BUILTINS
12typedef unsigned int rb_atomic_t;
13# define RUBY_ATOMIC_FETCH_ADD(var, val) __atomic_fetch_add(&(var), (val), __ATOMIC_SEQ_CST)
14# define RUBY_ATOMIC_FETCH_SUB(var, val) __atomic_fetch_sub(&(var), (val), __ATOMIC_SEQ_CST)
15# define RUBY_ATOMIC_OR(var, val) __atomic_fetch_or(&(var), (val), __ATOMIC_SEQ_CST)
16# define RUBY_ATOMIC_EXCHANGE(var, val) __atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST)
17# define RUBY_ATOMIC_CAS(var, oldval, newval) RB_GNUC_EXTENSION_BLOCK( \
18 __typeof__(var) oldvaldup = (oldval); /* oldval should not be modified */ \
19 __atomic_compare_exchange_n(&(var), &oldvaldup, (newval), 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
20 oldvaldup )
21
22# define RUBY_ATOMIC_GENERIC_MACRO 1
23
24#elif defined HAVE_GCC_SYNC_BUILTINS
25/* @shyouhei hack to support atomic operations in case of gcc. Gcc
26 * has its own pseudo-insns to support them. See info, or
27 * http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html */
28
29typedef unsigned int rb_atomic_t; /* Anything OK */
30# define RUBY_ATOMIC_FETCH_ADD(var, val) __sync_fetch_and_add(&(var), (val))
31# define RUBY_ATOMIC_FETCH_SUB(var, val) __sync_fetch_and_sub(&(var), (val))
32# define RUBY_ATOMIC_OR(var, val) __sync_fetch_and_or(&(var), (val))
33# define RUBY_ATOMIC_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
34# define RUBY_ATOMIC_CAS(var, oldval, newval) __sync_val_compare_and_swap(&(var), (oldval), (newval))
35
36# define RUBY_ATOMIC_GENERIC_MACRO 1
37
38#elif defined _WIN32
39#if RBIMPL_COMPILER_SINCE(MSVC, 13, 0, 0)
40#pragma intrinsic(_InterlockedOr)
41#endif
42typedef LONG rb_atomic_t;
43
44# define RUBY_ATOMIC_SET(var, val) InterlockedExchange(&(var), (val))
45# define RUBY_ATOMIC_INC(var) InterlockedIncrement(&(var))
46# define RUBY_ATOMIC_DEC(var) InterlockedDecrement(&(var))
47# define RUBY_ATOMIC_FETCH_ADD(var, val) InterlockedExchangeAdd(&(var), (val))
48# define RUBY_ATOMIC_FETCH_SUB(var, val) InterlockedExchangeAdd(&(var), -(LONG)(val))
49#if defined __GNUC__
50# define RUBY_ATOMIC_OR(var, val) __asm__("lock\n\t" "orl\t%1, %0" : "=m"(var) : "Ir"(val))
51#elif RBIMPL_COMPILER_BEFORE(MSVC, 13, 0, 0)
52# define RUBY_ATOMIC_OR(var, val) rb_w32_atomic_or(&(var), (val))
53static inline void
54rb_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 RUBY_ATOMIC_OR(var, val) _InterlockedOr(&(var), (val))
66#endif
67# define RUBY_ATOMIC_EXCHANGE(var, val) InterlockedExchange(&(var), (val))
68# define RUBY_ATOMIC_CAS(var, oldval, newval) InterlockedCompareExchange(&(var), (newval), (oldval))
69# if RBIMPL_COMPILER_BEFORE(MSVC, 13, 0, 0)
70static inline rb_atomic_t
71rb_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 RUBY_ATOMIC_CAS
76# define RUBY_ATOMIC_CAS(var, oldval, newval) rb_w32_atomic_cas(&(var), (oldval), (newval))
77# endif
78# ifdef _M_AMD64
79# define RUBY_ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd64((LONG_LONG *)&(var), (val))
80# define RUBY_ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd64((LONG_LONG *)&(var), -(LONG)(val))
81# define RUBY_ATOMIC_SIZE_INC(var) InterlockedIncrement64(&(var))
82# define RUBY_ATOMIC_SIZE_DEC(var) InterlockedDecrement64(&(var))
83# define RUBY_ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange64(&(var), (val))
84# define RUBY_ATOMIC_SIZE_CAS(var, oldval, newval) InterlockedCompareExchange64(&(var), (newval), (oldval))
85# else
86# define RUBY_ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd((LONG *)&(var), (val))
87# define RUBY_ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd((LONG *)&(var), -(LONG)(val))
88# define RUBY_ATOMIC_SIZE_INC(var) InterlockedIncrement((LONG *)&(var))
89# define RUBY_ATOMIC_SIZE_DEC(var) InterlockedDecrement((LONG *)&(var))
90# define RUBY_ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange((LONG *)&(var), (val))
91# endif
92
93# ifdef InterlockedExchangePointer
94# define RUBY_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>
99typedef unsigned int rb_atomic_t;
100
101# define RUBY_ATOMIC_INC(var) atomic_inc_uint(&(var))
102# define RUBY_ATOMIC_DEC(var) atomic_dec_uint(&(var))
103# define RUBY_ATOMIC_FETCH_ADD(var, val) rb_atomic_fetch_add(&(var), (val))
104# define RUBY_ATOMIC_FETCH_SUB(var, val) rb_atomic_fetch_sub(&(var), (val))
105# define RUBY_ATOMIC_ADD(var, val) atomic_add_uint(&(var), (val))
106# define RUBY_ATOMIC_SUB(var, val) atomic_sub_uint(&(var), (val))
107# define RUBY_ATOMIC_OR(var, val) atomic_or_uint(&(var), (val))
108# define RUBY_ATOMIC_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
109# define RUBY_ATOMIC_CAS(var, oldval, newval) atomic_cas_uint(&(var), (oldval), (newval))
110
111static inline rb_atomic_t
112rb_atomic_fetch_add(volatile rb_atomic_t *var, rb_atomic_t val)
113{
114 return atomic_add_int_nv(var, val) - val;
115}
116
117static inline rb_atomic_t
118rb_atomic_fetch_sub(volatile rb_atomic_t *var, rb_atomic_t val)
119{
120 return atomic_add_int_nv(var, (rb_atomic_t)(-(int)val)) + val;
121}
122
123# if defined(_LP64) || defined(_I32LPx)
124# define RUBY_ATOMIC_SIZE_ADD(var, val) atomic_add_long(&(var), (val))
125# define RUBY_ATOMIC_SIZE_SUB(var, val) atomic_add_long(&(var), -(val))
126# define RUBY_ATOMIC_SIZE_INC(var) atomic_inc_ulong(&(var))
127# define RUBY_ATOMIC_SIZE_DEC(var) atomic_dec_ulong(&(var))
128# define RUBY_ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_ulong(&(var), (val))
129# define RUBY_ATOMIC_SIZE_CAS(var, oldval, val) atomic_cas_ulong(&(var), (oldval), (val))
130# else
131# define RUBY_ATOMIC_SIZE_ADD(var, val) atomic_add_int(&(var), (val))
132# define RUBY_ATOMIC_SIZE_SUB(var, val) atomic_add_int(&(var), -(val))
133# define RUBY_ATOMIC_SIZE_INC(var) atomic_inc_uint(&(var))
134# define RUBY_ATOMIC_SIZE_DEC(var) atomic_dec_uint(&(var))
135# define RUBY_ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
136# endif
137
138#else
139# error No atomic operation found
140#endif
141
142#ifndef RUBY_ATOMIC_SET
143# define RUBY_ATOMIC_SET(var, val) (void)RUBY_ATOMIC_EXCHANGE(var, val)
144#endif
145
146#ifndef RUBY_ATOMIC_ADD
147# define RUBY_ATOMIC_ADD(var, val) (void)RUBY_ATOMIC_FETCH_ADD(var, val)
148#endif
149
150#ifndef RUBY_ATOMIC_SUB
151# define RUBY_ATOMIC_SUB(var, val) (void)RUBY_ATOMIC_FETCH_SUB(var, val)
152#endif
153
154#ifndef RUBY_ATOMIC_INC
155# define RUBY_ATOMIC_INC(var) RUBY_ATOMIC_ADD(var, 1)
156#endif
157
158#ifndef RUBY_ATOMIC_DEC
159# define RUBY_ATOMIC_DEC(var) RUBY_ATOMIC_SUB(var, 1)
160#endif
161
162#ifndef RUBY_ATOMIC_SIZE_INC
163# define RUBY_ATOMIC_SIZE_INC(var) RUBY_ATOMIC_INC(var)
164#endif
165
166#ifndef RUBY_ATOMIC_SIZE_DEC
167# define RUBY_ATOMIC_SIZE_DEC(var) RUBY_ATOMIC_DEC(var)
168#endif
169
170#ifndef RUBY_ATOMIC_SIZE_EXCHANGE
171# define RUBY_ATOMIC_SIZE_EXCHANGE(var, val) RUBY_ATOMIC_EXCHANGE(var, val)
172#endif
173
174#ifndef RUBY_ATOMIC_SIZE_CAS
175# define RUBY_ATOMIC_SIZE_CAS(var, oldval, val) RUBY_ATOMIC_CAS(var, oldval, val)
176#endif
177
178#ifndef RUBY_ATOMIC_SIZE_ADD
179# define RUBY_ATOMIC_SIZE_ADD(var, val) RUBY_ATOMIC_ADD(var, val)
180#endif
181
182#ifndef RUBY_ATOMIC_SIZE_SUB
183# define RUBY_ATOMIC_SIZE_SUB(var, val) RUBY_ATOMIC_SUB(var, val)
184#endif
185
186#if RUBY_ATOMIC_GENERIC_MACRO
187# ifndef RUBY_ATOMIC_PTR_EXCHANGE
188# define RUBY_ATOMIC_PTR_EXCHANGE(var, val) RUBY_ATOMIC_EXCHANGE(var, val)
189# endif
190
191# ifndef RUBY_ATOMIC_PTR_CAS
192# define RUBY_ATOMIC_PTR_CAS(var, oldval, newval) RUBY_ATOMIC_CAS(var, oldval, newval)
193# endif
194
195# ifndef RUBY_ATOMIC_VALUE_EXCHANGE
196# define RUBY_ATOMIC_VALUE_EXCHANGE(var, val) RUBY_ATOMIC_EXCHANGE(var, val)
197# endif
198
199# ifndef RUBY_ATOMIC_VALUE_CAS
200# define RUBY_ATOMIC_VALUE_CAS(var, oldval, val) RUBY_ATOMIC_CAS(var, oldval, val)
201# endif
202#endif
203
204#ifndef RUBY_ATOMIC_PTR_EXCHANGE
205# if SIZEOF_VOIDP == SIZEOF_SIZE_T
206# define RUBY_ATOMIC_PTR_EXCHANGE(var, val) (void *)RUBY_ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
207# else
208# error No atomic exchange for void*
209# endif
210#endif
211
212#ifndef RUBY_ATOMIC_PTR_CAS
213# if SIZEOF_VOIDP == SIZEOF_SIZE_T
214# define RUBY_ATOMIC_PTR_CAS(var, oldval, val) (void *)RUBY_ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
215# else
216# error No atomic compare-and-set for void*
217# endif
218#endif
219
220#ifndef RUBY_ATOMIC_VALUE_EXCHANGE
221# if SIZEOF_VALUE == SIZEOF_SIZE_T
222# define RUBY_ATOMIC_VALUE_EXCHANGE(var, val) RUBY_ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
223# else
224# error No atomic exchange for VALUE
225# endif
226#endif
227
228#ifndef RUBY_ATOMIC_VALUE_CAS
229# if SIZEOF_VALUE == SIZEOF_SIZE_T
230# define RUBY_ATOMIC_VALUE_CAS(var, oldval, val) RUBY_ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
231# else
232# error No atomic compare-and-set for VALUE
233# endif
234#endif
235
236#endif /* RUBY_ATOMIC_H */
atomic.h