2011-06-11 18:27:08 +04:00
|
|
|
#ifndef RUBY_ATOMIC_H
|
|
|
|
#define RUBY_ATOMIC_H
|
|
|
|
|
2012-07-25 12:17:08 +04:00
|
|
|
#if 0
|
|
|
|
#elif defined HAVE_GCC_ATOMIC_BUILTINS
|
|
|
|
/* @shyouhei hack to support atomic operations in case of gcc. Gcc
|
|
|
|
* has its own pseudo-insns to support them. See info, or
|
|
|
|
* http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html */
|
|
|
|
|
|
|
|
typedef unsigned int rb_atomic_t; /* Anything OK */
|
|
|
|
# define ATOMIC_SET(var, val) (void)__sync_lock_test_and_set(&(var), (val))
|
|
|
|
# define ATOMIC_INC(var) __sync_fetch_and_add(&(var), 1)
|
|
|
|
# define ATOMIC_DEC(var) __sync_fetch_and_sub(&(var), 1)
|
|
|
|
# define ATOMIC_OR(var, val) __sync_or_and_fetch(&(var), (val))
|
|
|
|
# define ATOMIC_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
|
2012-11-26 14:57:39 +04:00
|
|
|
# define ATOMIC_CAS(var, oldval, newval) __sync_val_compare_and_swap(&(var), (oldval), (newval))
|
2012-07-25 12:17:08 +04:00
|
|
|
|
|
|
|
# define ATOMIC_SIZE_ADD(var, val) __sync_fetch_and_add(&(var), (val))
|
|
|
|
# define ATOMIC_SIZE_SUB(var, val) __sync_fetch_and_sub(&(var), (val))
|
|
|
|
# define ATOMIC_SIZE_INC(var) __sync_fetch_and_add(&(var), 1)
|
|
|
|
# define ATOMIC_SIZE_DEC(var) __sync_fetch_and_sub(&(var), 1)
|
|
|
|
# define ATOMIC_SIZE_EXCHANGE(var, val) __sync_lock_test_and_set(&(var), (val))
|
|
|
|
|
|
|
|
#elif defined _WIN32
|
2011-07-09 11:56:04 +04:00
|
|
|
#if defined _MSC_VER && _MSC_VER > 1200
|
2011-06-11 18:27:08 +04:00
|
|
|
#pragma intrinsic(_InterlockedOr)
|
2011-06-12 04:32:32 +04:00
|
|
|
#endif
|
2011-06-11 18:27:08 +04:00
|
|
|
typedef LONG rb_atomic_t;
|
|
|
|
|
|
|
|
# define ATOMIC_SET(var, val) InterlockedExchange(&(var), (val))
|
|
|
|
# define ATOMIC_INC(var) InterlockedIncrement(&(var))
|
|
|
|
# define ATOMIC_DEC(var) InterlockedDecrement(&(var))
|
2011-06-12 04:32:32 +04:00
|
|
|
#if defined __GNUC__
|
|
|
|
# define ATOMIC_OR(var, val) __asm__("lock\n\t" "orl\t%1, %0" : "=m"(var) : "Ir"(val))
|
2011-07-09 11:56:04 +04:00
|
|
|
#elif defined _MSC_VER && _MSC_VER <= 1200
|
|
|
|
# define ATOMIC_OR(var, val) rb_w32_atomic_or(&(var), (val))
|
|
|
|
static inline void
|
|
|
|
rb_w32_atomic_or(volatile rb_atomic_t *var, rb_atomic_t val)
|
|
|
|
{
|
|
|
|
#ifdef _M_IX86
|
|
|
|
__asm mov eax, var;
|
|
|
|
__asm mov ecx, val;
|
|
|
|
__asm lock or [eax], ecx;
|
|
|
|
#else
|
|
|
|
#error unsupported architecture
|
|
|
|
#endif
|
|
|
|
}
|
2011-06-12 04:32:32 +04:00
|
|
|
#else
|
2011-06-11 18:27:08 +04:00
|
|
|
# define ATOMIC_OR(var, val) _InterlockedOr(&(var), (val))
|
2011-06-12 04:32:32 +04:00
|
|
|
#endif
|
2011-06-11 18:27:08 +04:00
|
|
|
# define ATOMIC_EXCHANGE(var, val) InterlockedExchange(&(var), (val))
|
2012-11-26 14:57:39 +04:00
|
|
|
# define ATOMIC_CAS(var, oldval, newval) InterlockedCompareExchange(&(var), (newval), (oldval))
|
2013-07-03 18:47:42 +04:00
|
|
|
# if defined _MSC_VER && _MSC_VER <= 1200
|
|
|
|
static inline rb_atomic_t
|
|
|
|
rb_w32_atomic_cas(volatile rb_atomic_t *var, rb_atomic_t oldval, rb_atomic_t newval)
|
|
|
|
{
|
2013-07-03 19:13:06 +04:00
|
|
|
return (rb_atomic_t)InterlockedCompareExchange((PVOID *)var, (PVOID)newval, (PVOID)oldval);
|
2013-07-03 18:47:42 +04:00
|
|
|
}
|
|
|
|
# undef ATOMIC_CAS
|
2013-07-03 19:13:06 +04:00
|
|
|
# define ATOMIC_CAS(var, oldval, newval) rb_w32_atomic_cas(&(var), (oldval), (newval))
|
2013-07-03 18:47:42 +04:00
|
|
|
# endif
|
2012-07-04 17:02:03 +04:00
|
|
|
# ifdef _M_AMD64
|
|
|
|
# define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd64(&(var), (val))
|
|
|
|
# define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd64(&(var), -(val))
|
|
|
|
# define ATOMIC_SIZE_INC(var) InterlockedIncrement64(&(var))
|
|
|
|
# define ATOMIC_SIZE_DEC(var) InterlockedDecrement64(&(var))
|
|
|
|
# define ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange64(&(var), (val))
|
|
|
|
# else
|
2012-07-09 08:35:50 +04:00
|
|
|
# define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd((LONG *)&(var), (val))
|
|
|
|
# define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd((LONG *)&(var), -(val))
|
|
|
|
# define ATOMIC_SIZE_INC(var) InterlockedIncrement((LONG *)&(var))
|
|
|
|
# define ATOMIC_SIZE_DEC(var) InterlockedDecrement((LONG *)&(var))
|
|
|
|
# define ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange((LONG *)&(var), (val))
|
2012-07-04 17:02:03 +04:00
|
|
|
# endif
|
|
|
|
|
2013-04-06 19:00:44 +04:00
|
|
|
#elif defined(__sun) && defined(HAVE_ATOMIC_H)
|
2011-10-13 14:39:58 +04:00
|
|
|
#include <atomic.h>
|
|
|
|
typedef unsigned int rb_atomic_t;
|
|
|
|
|
|
|
|
# define ATOMIC_SET(var, val) (void)atomic_swap_uint(&(var), (val))
|
|
|
|
# define ATOMIC_INC(var) atomic_inc_uint(&(var))
|
|
|
|
# define ATOMIC_DEC(var) atomic_dec_uint(&(var))
|
|
|
|
# define ATOMIC_OR(var, val) atomic_or_uint(&(var), (val))
|
|
|
|
# define ATOMIC_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
|
2012-11-27 14:21:59 +04:00
|
|
|
# define ATOMIC_CAS(var, oldval, newval) atomic_cas_uint(&(var), (oldval), (newval))
|
2011-10-13 14:39:58 +04:00
|
|
|
|
2012-07-04 17:02:03 +04:00
|
|
|
# if SIZEOF_SIZE_T == SIZEOF_LONG
|
|
|
|
# define ATOMIC_SIZE_ADD(var, val) atomic_add_long(&(var), (val))
|
|
|
|
# define ATOMIC_SIZE_SUB(var, val) atomic_add_long(&(var), -(val))
|
|
|
|
# define ATOMIC_SIZE_INC(var) atomic_inc_ulong(&(var))
|
|
|
|
# define ATOMIC_SIZE_DEC(var) atomic_dec_ulong(&(var))
|
|
|
|
# define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_ulong(&(var), (val))
|
|
|
|
# else
|
|
|
|
# define ATOMIC_SIZE_ADD(var, val) atomic_add_int(&(var), (val))
|
|
|
|
# define ATOMIC_SIZE_SUB(var, val) atomic_add_int(&(var), -(val))
|
|
|
|
# define ATOMIC_SIZE_INC(var) atomic_inc_uint(&(var))
|
|
|
|
# define ATOMIC_SIZE_DEC(var) atomic_dec_uint(&(var))
|
|
|
|
# define ATOMIC_SIZE_EXCHANGE(var, val) atomic_swap_uint(&(var), (val))
|
|
|
|
# endif
|
|
|
|
|
2011-06-11 18:27:08 +04:00
|
|
|
#else
|
|
|
|
typedef int rb_atomic_t;
|
2012-11-27 14:21:59 +04:00
|
|
|
#define NEED_RUBY_ATOMIC_OPS
|
2011-06-11 18:27:08 +04:00
|
|
|
extern rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val);
|
2012-11-27 14:21:59 +04:00
|
|
|
extern rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr,
|
|
|
|
rb_atomic_t cmp,
|
|
|
|
rb_atomic_t newval);
|
2011-06-11 18:27:08 +04:00
|
|
|
|
2011-10-13 13:19:10 +04:00
|
|
|
# define ATOMIC_SET(var, val) (void)((var) = (val))
|
2011-10-14 00:58:08 +04:00
|
|
|
# define ATOMIC_INC(var) ((var)++)
|
|
|
|
# define ATOMIC_DEC(var) ((var)--)
|
2011-06-11 18:27:08 +04:00
|
|
|
# define ATOMIC_OR(var, val) ((var) |= (val))
|
|
|
|
# define ATOMIC_EXCHANGE(var, val) ruby_atomic_exchange(&(var), (val))
|
2012-11-27 14:21:59 +04:00
|
|
|
# define ATOMIC_CAS(var, oldval, newval) ruby_atomic_compare_and_swap(&(var), (oldval), (newval))
|
2012-07-04 17:02:03 +04:00
|
|
|
|
|
|
|
# define ATOMIC_SIZE_ADD(var, val) (void)((var) += (val))
|
|
|
|
# define ATOMIC_SIZE_SUB(var, val) (void)((var) -= (val))
|
|
|
|
# define ATOMIC_SIZE_INC(var) ((var)++)
|
|
|
|
# define ATOMIC_SIZE_DEC(var) ((var)--)
|
|
|
|
# define ATOMIC_SIZE_EXCHANGE(var, val) atomic_size_exchange(&(var), (val))
|
|
|
|
static inline size_t
|
|
|
|
atomic_size_exchange(size_t *ptr, size_t val)
|
|
|
|
{
|
|
|
|
size_t old = *ptr;
|
|
|
|
*ptr = val;
|
|
|
|
return old;
|
|
|
|
}
|
2011-06-11 18:27:08 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* RUBY_ATOMIC_H */
|