Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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_or_fetch(&(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__(oldval) 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 ATOMIC_PTR_EXCHANGE(var, val) __atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST)
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_or_and_fetch(&(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 ATOMIC_PTR_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
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, val) InterlockedCompareExchange64(&(var), (oldval), (val))
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) atomic_size_exchange(&(var), (val))
137 static inline size_t
138 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 #ifndef ATOMIC_PTR_EXCHANGE
160 # if SIZEOF_VOIDP == SIZEOF_SIZE_T
161 # define ATOMIC_PTR_EXCHANGE(var, val) (void *)ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
162 # endif
163 #endif
164 #ifndef ATOMIC_PTR_CAS
165 # if SIZEOF_VOIDP == SIZEOF_SIZE_T
166 # define ATOMIC_PTR_CAS(var, oldval, val) (void *)ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
167 # endif
168 #endif
169
170 #endif /* RUBY_ATOMIC_H */
atomic_size_exchange
static size_t atomic_size_exchange(size_t *ptr, size_t val)
Definition: ruby_atomic.h:138
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:53
val
#define val
rb_atomic_t
int rb_atomic_t
Definition: ruby_atomic.h:120
cmp
static ID cmp
Definition: compar.c:16
ruby_atomic_exchange
rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val)
Definition: signal.c:45