5 #elif defined HAVE_GCC_ATOMIC_BUILTINS 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); \ 14 __atomic_compare_exchange_n(&(var), &oldvaldup, (newval), 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \ 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) 20 # define RUBY_ATOMIC_GENERIC_MACRO 1 22 #elif defined HAVE_GCC_SYNC_BUILTINS 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)) 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)) 38 # define RUBY_ATOMIC_GENERIC_MACRO 1 41 #if defined _MSC_VER && _MSC_VER > 1200 42 #pragma intrinsic(_InterlockedOr) 46 # define ATOMIC_SET(var, val) InterlockedExchange(&(var), (val)) 47 # define ATOMIC_INC(var) InterlockedIncrement(&(var)) 48 # define ATOMIC_DEC(var) InterlockedDecrement(&(var)) 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)) 59 __asm lock or [eax], ecx;
61 #error unsupported architecture 65 # define ATOMIC_OR(var, val) _InterlockedOr(&(var), (val)) 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 73 return (
rb_atomic_t)InterlockedCompareExchange((PVOID *)var, (PVOID)newval, (PVOID)oldval);
76 # define ATOMIC_CAS(var, oldval, newval) rb_w32_atomic_cas(&(var), (oldval), (newval)) 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)) 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)) 93 #elif defined(__sun) && defined(HAVE_ATOMIC_H) 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)) 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)) 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)) 121 #define NEED_RUBY_ATOMIC_OPS 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)) 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)) 138 ruby_atomic_size_exchange(
size_t *ptr,
size_t val)
146 #ifndef ATOMIC_SIZE_INC 147 # define ATOMIC_SIZE_INC(var) ATOMIC_INC(var) 149 #ifndef ATOMIC_SIZE_DEC 150 # define ATOMIC_SIZE_DEC(var) ATOMIC_DEC(var) 152 #ifndef ATOMIC_SIZE_EXCHANGE 153 # define ATOMIC_SIZE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val) 155 #ifndef ATOMIC_SIZE_CAS 156 # define ATOMIC_SIZE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val) 159 #if RUBY_ATOMIC_GENERIC_MACRO 160 # ifndef ATOMIC_PTR_EXCHANGE 161 # define ATOMIC_PTR_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val) 163 # ifndef ATOMIC_PTR_CAS 164 # define ATOMIC_PTR_CAS(var, oldval, newval) ATOMIC_CAS(var, oldval, newval) 167 # ifndef ATOMIC_VALUE_EXCHANGE 168 # define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val) 170 # ifndef ATOMIC_VALUE_CAS 171 # define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val) 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)) 179 # define ATOMIC_PTR_EXCHANGE(var, val) ruby_atomic_ptr_exchange((const void **)&(var), (val)) 181 ruby_atomic_ptr_exchange(
const void **ptr,
const void *
val)
183 const void *
const old = *ptr;
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)) 193 # define ATOMIC_PTR_CAS(var, oldval, val) ruby_atomic_ptr_cas(&(var), (oldval), (val)) 195 ruby_atomic_ptr_cas(
const void **ptr,
const void *oldval,
const void *
val)
197 const void *
const old = *ptr;
198 if (old == oldval) *ptr =
val;
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)) 208 # define ATOMIC_VALUE_EXCHANGE(var, val) ruby_atomic_value_exchange(&(var), (val)) 212 const VALUE old = *ptr;
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)) 222 # define ATOMIC_VALUE_CAS(var, oldval, val) ruby_atomic_value_cas(&(var), (oldval), (val)) 226 const VALUE old = *ptr;
227 if (old == oldval) *ptr =
val;
rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr, rb_atomic_t cmp, rb_atomic_t newval)
rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val)