Ruby: ruby_atomic.h Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

ruby_atomic.h
1#ifndef INTERNAL_ATOMIC_H
2#define INTERNAL_ATOMIC_H
3
4#include "ruby/atomic.h"
5#ifdef HAVE_STDATOMIC_H
6# include <stdatomic.h>
7#endif
8
9#define RUBY_ATOMIC_VALUE_LOAD(x) rbimpl_atomic_value_load(&(x), RBIMPL_ATOMIC_SEQ_CST)
10
11/* shim macros only */
12#define ATOMIC_ADD(var, val) RUBY_ATOMIC_ADD(var, val)
13#define ATOMIC_CAS(var, oldval, newval) RUBY_ATOMIC_CAS(var, oldval, newval)
14#define ATOMIC_DEC(var) RUBY_ATOMIC_DEC(var)
15#define ATOMIC_EXCHANGE(var, val) RUBY_ATOMIC_EXCHANGE(var, val)
16#define ATOMIC_FETCH_ADD(var, val) RUBY_ATOMIC_FETCH_ADD(var, val)
17#define ATOMIC_FETCH_SUB(var, val) RUBY_ATOMIC_FETCH_SUB(var, val)
18#define ATOMIC_INC(var) RUBY_ATOMIC_INC(var)
19#define ATOMIC_OR(var, val) RUBY_ATOMIC_OR(var, val)
20#define ATOMIC_PTR_CAS(var, oldval, newval) RUBY_ATOMIC_PTR_CAS(var, oldval, newval)
21#define ATOMIC_PTR_EXCHANGE(var, val) RUBY_ATOMIC_PTR_EXCHANGE(var, val)
22#define ATOMIC_SET(var, val) RUBY_ATOMIC_SET(var, val)
23#define ATOMIC_SIZE_ADD(var, val) RUBY_ATOMIC_SIZE_ADD(var, val)
24#define ATOMIC_SIZE_CAS(var, oldval, newval) RUBY_ATOMIC_SIZE_CAS(var, oldval, newval)
25#define ATOMIC_SIZE_DEC(var) RUBY_ATOMIC_SIZE_DEC(var)
26#define ATOMIC_SIZE_EXCHANGE(var, val) RUBY_ATOMIC_SIZE_EXCHANGE(var, val)
27#define ATOMIC_SIZE_INC(var) RUBY_ATOMIC_SIZE_INC(var)
28#define ATOMIC_SIZE_SUB(var, val) RUBY_ATOMIC_SIZE_SUB(var, val)
29#define ATOMIC_SUB(var, val) RUBY_ATOMIC_SUB(var, val)
30#define ATOMIC_VALUE_CAS(var, oldval, val) RUBY_ATOMIC_VALUE_CAS(var, oldval, val)
31#define ATOMIC_VALUE_EXCHANGE(var, val) RUBY_ATOMIC_VALUE_EXCHANGE(var, val)
32
33#define ATOMIC_LOAD_RELAXED(var) rbimpl_atomic_load(&(var), RBIMPL_ATOMIC_RELAXED)
34
35typedef RBIMPL_ALIGNAS(8) uint64_t rbimpl_atomic_uint64_t;
36
37static inline uint64_t
38rbimpl_atomic_u64_load_relaxed(const volatile rbimpl_atomic_uint64_t *value)
39{
40#if defined(HAVE_GCC_ATOMIC_BUILTINS_64)
41 return __atomic_load_n(value, __ATOMIC_RELAXED);
42#elif defined(_WIN32)
43 uint64_t val = *value;
44 return InterlockedCompareExchange64(RBIMPL_CAST((uint64_t *)value), val, val);
45#elif defined(__sun) && defined(HAVE_ATOMIC_H) && (defined(_LP64) || defined(_I32LPx))
46 uint64_t val = *value;
47 return atomic_cas_64(value, val, val);
48#else
49 // TODO: stdatomic
50
51 return *value;
52#endif
53}
54#define ATOMIC_U64_LOAD_RELAXED(var) rbimpl_atomic_u64_load_relaxed(&(var))
55
56static inline void
57rbimpl_atomic_u64_set_relaxed(volatile rbimpl_atomic_uint64_t *address, uint64_t value)
58{
59#if defined(HAVE_GCC_ATOMIC_BUILTINS_64)
60 __atomic_store_n(address, value, __ATOMIC_RELAXED);
61#elif defined(_WIN32)
62 InterlockedExchange64(address, value);
63#elif defined(__sun) && defined(HAVE_ATOMIC_H) && (defined(_LP64) || defined(_I32LPx))
64 atomic_swap_64(address, value);
65#else
66 // TODO: stdatomic
67
68 *address = value;
69#endif
70}
71#define ATOMIC_U64_SET_RELAXED(var, val) rbimpl_atomic_u64_set_relaxed(&(var), val)
72
73#endif
atomic.h
Atomic operations.
RBIMPL_ALIGNAS
#define RBIMPL_ALIGNAS
Wraps (or simulates) alignas.
Definition stdalign.h:57
inline
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:91