2011-06-11 18:27:08 +04:00
|
|
|
#ifndef RUBY_ATOMIC_H
|
|
|
|
#define RUBY_ATOMIC_H
|
|
|
|
|
2020-04-26 13:34:07 +03:00
|
|
|
/*
|
|
|
|
* - ATOMIC_CAS, ATOMIC_EXCHANGE, ATOMIC_FETCH_*:
|
|
|
|
* return the old * value.
|
|
|
|
* - ATOMIC_ADD, ATOMIC_SUB, ATOMIC_INC, ATOMIC_DEC, ATOMIC_OR, ATOMIC_SET:
|
|
|
|
* may be void.
|
|
|
|
*/
|
2012-07-25 12:17:08 +04:00
|
|
|
#if 0
|
|
|
|
#elif defined HAVE_GCC_ATOMIC_BUILTINS
|
2013-11-26 06:45:53 +04:00
|
|
|
typedef unsigned int rb_atomic_t;
|
2020-04-26 13:34:07 +03:00
|
|
|
# define ATOMIC_FETCH_ADD(var, val) __atomic_fetch_add(&(var), (val), __ATOMIC_SEQ_CST)
|
|
|
|
# define ATOMIC_FETCH_SUB(var, val) __atomic_fetch_sub(&(var), (val), __ATOMIC_SEQ_CST)
|
2014-10-19 21:34:02 +04:00
|
|
|
# define ATOMIC_OR(var, val) __atomic_fetch_or(&(var), (val), __ATOMIC_SEQ_CST)
|
2013-11-26 06:45:53 +04:00
|
|
|
# define ATOMIC_EXCHANGE(var, val) __atomic_exchange_n(&(var), (val), __ATOMIC_SEQ_CST)
|
2018-01-02 09:41:53 +03:00
|
|
|
# define ATOMIC_CAS(var, oldval, newval) RB_GNUC_EXTENSION_BLOCK( \
|
|
|
|
__typeof__(var) oldvaldup = (oldval); /* oldval should not be modified */ \
|
2013-11-27 18:57:33 +04:00
|
|
|
__atomic_compare_exchange_n(&(var), &oldvaldup, (newval), 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
|
2018-01-02 09:41:53 +03:00
|
|
|
oldvaldup )
|
2013-11-26 06:45:53 +04:00
|
|
|
|
2015-08-06 04:44:03 +03:00
|
|
|
# define RUBY_ATOMIC_GENERIC_MACRO 1
|
2013-12-05 05:48:10 +04:00
|
|
|
|
2013-11-26 06:45:53 +04:00
|
|
|
#elif defined HAVE_GCC_SYNC_BUILTINS
|
2012-07-25 12:17:08 +04:00
|
|
|
/* @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 */
|
2020-04-26 13:34:07 +03:00
|
|
|
# define ATOMIC_FETCH_ADD(var, val) __sync_fetch_and_add(&(var), (val))
|
|
|
|
# define ATOMIC_FETCH_SUB(var, var) __sync_fetch_and_sub(&(var), (val))
|
2014-10-19 21:34:02 +04:00
|
|
|
# define ATOMIC_OR(var, val) __sync_fetch_and_or(&(var), (val))
|
2012-07-25 12:17:08 +04:00
|
|
|
# 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
|
|
|
|
2015-08-06 04:44:03 +03:00
|
|
|
# define RUBY_ATOMIC_GENERIC_MACRO 1
|
2013-12-05 05:48:10 +04:00
|
|
|
|
2012-07-25 12:17:08 +04:00
|
|
|
#elif defined _WIN32
|
2019-12-02 08:58:43 +03:00
|
|
|
#if MSC_VERSION_SINCE(1300)
|
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))
|
2020-04-26 13:34:07 +03:00
|
|
|
# define ATOMIC_FETCH_ADD(var, val) InterlockedExchangeAdd(&(var), (val))
|
|
|
|
# define ATOMIC_FETCH_SUB(var, val) InterlockedExchangeAdd(&(var), -(LONG)(val))
|
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))
|
2019-12-02 08:58:43 +03:00
|
|
|
#elif MSC_VERSION_BEFORE(1300)
|
2011-07-09 11:56:04 +04:00
|
|
|
# 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))
|
2019-12-02 08:58:43 +03:00
|
|
|
# if MSC_VERSION_BEFORE(1300)
|
2013-07-03 18:47:42 +04:00
|
|
|
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
|
2013-10-18 18:23:50 +04:00
|
|
|
# define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd64((LONG_LONG *)&(var), (val))
|
|
|
|
# define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd64((LONG_LONG *)&(var), -(LONG)(val))
|
2012-07-04 17:02:03 +04:00
|
|
|
# define ATOMIC_SIZE_INC(var) InterlockedIncrement64(&(var))
|
|
|
|
# define ATOMIC_SIZE_DEC(var) InterlockedDecrement64(&(var))
|
|
|
|
# define ATOMIC_SIZE_EXCHANGE(var, val) InterlockedExchange64(&(var), (val))
|
2015-12-05 03:25:43 +03:00
|
|
|
# define ATOMIC_SIZE_CAS(var, oldval, newval) InterlockedCompareExchange64(&(var), (newval), (oldval))
|
2012-07-04 17:02:03 +04:00
|
|
|
# else
|
2012-07-09 08:35:50 +04:00
|
|
|
# define ATOMIC_SIZE_ADD(var, val) InterlockedExchangeAdd((LONG *)&(var), (val))
|
2013-10-18 18:23:50 +04:00
|
|
|
# define ATOMIC_SIZE_SUB(var, val) InterlockedExchangeAdd((LONG *)&(var), -(LONG)(val))
|
2012-07-09 08:35:50 +04:00
|
|
|
# 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
|
|
|
|
|
2018-08-01 06:40:37 +03:00
|
|
|
# ifdef InterlockedExchangePointer
|
|
|
|
# define ATOMIC_PTR_EXCHANGE(var, val) InterlockedExchangePointer((PVOID volatile *)&(var), (PVOID)(val))
|
|
|
|
# endif /* See below for definitions of other situations */
|
|
|
|
|
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_INC(var) atomic_inc_uint(&(var))
|
|
|
|
# define ATOMIC_DEC(var) atomic_dec_uint(&(var))
|
2020-04-26 13:34:07 +03:00
|
|
|
# define ATOMIC_FETCH_ADD(var, val) rb_atomic_fetch_add(&(var), (val))
|
|
|
|
# define ATOMIC_FETCH_SUB(var, val) rb_atomic_fetch_sub(&(var), (val))
|
|
|
|
# define ATOMIC_ADD(var, val) atomic_add_uint(&(var), (val))
|
|
|
|
# define ATOMIC_SUB(var, val) atomic_sub_uint(&(var), (val))
|
2011-10-13 14:39:58 +04:00
|
|
|
# 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
|
|
|
|
2020-04-26 13:34:07 +03:00
|
|
|
static inline rb_atomic_t
|
|
|
|
rb_atomic_fetch_add(volatile rb_atomic_t *var, rb_atomic_t val)
|
|
|
|
{
|
|
|
|
return atomic_add_int_nv(var, val) - val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline rb_atomic_t
|
|
|
|
rb_atomic_fetch_sub(volatile rb_atomic_t *var, rb_atomic_t val)
|
|
|
|
{
|
2020-04-26 16:39:36 +03:00
|
|
|
return atomic_add_int_nv(var, (rb_atomic_t)(-(int)val)) + val;
|
2020-04-26 13:34:07 +03:00
|
|
|
}
|
|
|
|
|
2020-01-31 09:07:40 +03:00
|
|
|
# if defined(_LP64) || defined(_I32LPx)
|
2012-07-04 17:02:03 +04:00
|
|
|
# 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))
|
2013-11-14 15:40:15 +04:00
|
|
|
# define ATOMIC_SIZE_CAS(var, oldval, val) atomic_cas_ulong(&(var), (oldval), (val))
|
2012-07-04 17:02:03 +04:00
|
|
|
# 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
|
2020-04-20 04:44:52 +03:00
|
|
|
# error No atomic operation found
|
2011-06-11 18:27:08 +04:00
|
|
|
#endif
|
|
|
|
|
2020-04-26 13:34:07 +03:00
|
|
|
#ifndef ATOMIC_SET
|
|
|
|
# define ATOMIC_SET(var, val) (void)ATOMIC_EXCHANGE(var, val)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ATOMIC_ADD
|
|
|
|
# define ATOMIC_ADD(var, val) (void)ATOMIC_FETCH_ADD(var, val)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ATOMIC_SUB
|
|
|
|
# define ATOMIC_SUB(var, val) (void)ATOMIC_FETCH_SUB(var, val)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ATOMIC_INC
|
|
|
|
# define ATOMIC_INC(var) ATOMIC_ADD(var, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ATOMIC_DEC
|
|
|
|
# define ATOMIC_DEC(var) ATOMIC_SUB(var, 1)
|
|
|
|
#endif
|
|
|
|
|
2013-11-27 19:13:08 +04:00
|
|
|
#ifndef ATOMIC_SIZE_INC
|
|
|
|
# define ATOMIC_SIZE_INC(var) ATOMIC_INC(var)
|
|
|
|
#endif
|
2018-08-01 05:55:11 +03:00
|
|
|
|
2013-11-27 19:13:08 +04:00
|
|
|
#ifndef ATOMIC_SIZE_DEC
|
|
|
|
# define ATOMIC_SIZE_DEC(var) ATOMIC_DEC(var)
|
|
|
|
#endif
|
2018-08-01 05:55:11 +03:00
|
|
|
|
2013-11-27 19:13:08 +04:00
|
|
|
#ifndef ATOMIC_SIZE_EXCHANGE
|
|
|
|
# define ATOMIC_SIZE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
|
|
|
|
#endif
|
2018-08-01 05:55:11 +03:00
|
|
|
|
2013-10-29 10:46:33 +04:00
|
|
|
#ifndef ATOMIC_SIZE_CAS
|
|
|
|
# define ATOMIC_SIZE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
|
|
|
|
#endif
|
|
|
|
|
2020-04-26 13:34:07 +03:00
|
|
|
#ifndef ATOMIC_SIZE_ADD
|
|
|
|
# define ATOMIC_SIZE_ADD(var, val) ATOMIC_ADD(var, val)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ATOMIC_SIZE_SUB
|
|
|
|
# define ATOMIC_SIZE_SUB(var, val) ATOMIC_SUB(var, val)
|
|
|
|
#endif
|
|
|
|
|
2015-08-06 04:44:03 +03:00
|
|
|
#if RUBY_ATOMIC_GENERIC_MACRO
|
|
|
|
# ifndef ATOMIC_PTR_EXCHANGE
|
|
|
|
# define ATOMIC_PTR_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
|
|
|
|
# endif
|
2018-08-01 05:55:11 +03:00
|
|
|
|
2015-08-06 04:44:03 +03:00
|
|
|
# ifndef ATOMIC_PTR_CAS
|
|
|
|
# define ATOMIC_PTR_CAS(var, oldval, newval) ATOMIC_CAS(var, oldval, newval)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef ATOMIC_VALUE_EXCHANGE
|
|
|
|
# define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_EXCHANGE(var, val)
|
|
|
|
# endif
|
2018-08-01 05:55:11 +03:00
|
|
|
|
2015-08-06 04:44:03 +03:00
|
|
|
# ifndef ATOMIC_VALUE_CAS
|
2015-09-07 09:55:01 +03:00
|
|
|
# define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_CAS(var, oldval, val)
|
2015-08-06 04:44:03 +03:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2013-12-05 05:48:10 +04:00
|
|
|
#ifndef ATOMIC_PTR_EXCHANGE
|
|
|
|
# if SIZEOF_VOIDP == SIZEOF_SIZE_T
|
|
|
|
# define ATOMIC_PTR_EXCHANGE(var, val) (void *)ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
|
2015-08-06 04:44:03 +03:00
|
|
|
# else
|
2020-04-20 15:13:08 +03:00
|
|
|
# error No atomic exchange for void*
|
2013-12-05 05:48:10 +04:00
|
|
|
# endif
|
|
|
|
#endif
|
2018-08-01 05:55:11 +03:00
|
|
|
|
2014-01-12 11:41:10 +04:00
|
|
|
#ifndef ATOMIC_PTR_CAS
|
|
|
|
# if SIZEOF_VOIDP == SIZEOF_SIZE_T
|
|
|
|
# define ATOMIC_PTR_CAS(var, oldval, val) (void *)ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
|
2015-08-06 04:44:03 +03:00
|
|
|
# else
|
2020-04-20 15:13:08 +03:00
|
|
|
# error No atomic compare-and-set for void*
|
2015-08-06 04:44:03 +03:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ATOMIC_VALUE_EXCHANGE
|
|
|
|
# if SIZEOF_VALUE == SIZEOF_SIZE_T
|
|
|
|
# define ATOMIC_VALUE_EXCHANGE(var, val) ATOMIC_SIZE_EXCHANGE(*(size_t *)&(var), (size_t)(val))
|
|
|
|
# else
|
2020-04-20 15:13:08 +03:00
|
|
|
# error No atomic exchange for VALUE
|
2015-08-06 04:44:03 +03:00
|
|
|
# endif
|
|
|
|
#endif
|
2018-08-01 05:55:11 +03:00
|
|
|
|
2015-08-06 04:44:03 +03:00
|
|
|
#ifndef ATOMIC_VALUE_CAS
|
|
|
|
# if SIZEOF_VALUE == SIZEOF_SIZE_T
|
|
|
|
# define ATOMIC_VALUE_CAS(var, oldval, val) ATOMIC_SIZE_CAS(*(size_t *)&(var), (size_t)(oldval), (size_t)(val))
|
|
|
|
# else
|
2020-04-20 15:13:08 +03:00
|
|
|
# error No atomic compare-and-set for VALUE
|
2014-01-12 11:41:10 +04:00
|
|
|
# endif
|
|
|
|
#endif
|
2013-12-05 05:48:10 +04:00
|
|
|
|
2011-06-11 18:27:08 +04:00
|
|
|
#endif /* RUBY_ATOMIC_H */
|