summaryrefslogtreecommitdiff
path: root/ruby_atomic.h
diff options
context:
space:
mode:
Diffstat (limited to 'ruby_atomic.h')
-rw-r--r--ruby_atomic.h46
1 files changed, 40 insertions, 6 deletions
diff --git a/ruby_atomic.h b/ruby_atomic.h
index 085c307ac6..cbcfe682ce 100644
--- a/ruby_atomic.h
+++ b/ruby_atomic.h
@@ -2,6 +2,11 @@
#define INTERNAL_ATOMIC_H
#include "ruby/atomic.h"
+#ifdef HAVE_STDATOMIC_H
+# include <stdatomic.h>
+#endif
+
+#define RUBY_ATOMIC_VALUE_LOAD(x) rbimpl_atomic_value_load(&(x), RBIMPL_ATOMIC_SEQ_CST)
/* shim macros only */
#define ATOMIC_ADD(var, val) RUBY_ATOMIC_ADD(var, val)
@@ -25,15 +30,44 @@
#define ATOMIC_VALUE_CAS(var, oldval, val) RUBY_ATOMIC_VALUE_CAS(var, oldval, val)
#define ATOMIC_VALUE_EXCHANGE(var, val) RUBY_ATOMIC_VALUE_EXCHANGE(var, val)
-static inline rb_atomic_t
-rbimpl_atomic_load_relaxed(rb_atomic_t *ptr)
+#define ATOMIC_LOAD_RELAXED(var) rbimpl_atomic_load(&(var), RBIMPL_ATOMIC_RELAXED)
+
+typedef RBIMPL_ALIGNAS(8) uint64_t rbimpl_atomic_uint64_t;
+
+static inline uint64_t
+rbimpl_atomic_u64_load_relaxed(const volatile rbimpl_atomic_uint64_t *value)
{
-#if defined(HAVE_GCC_ATOMIC_BUILTINS)
- return __atomic_load_n(ptr, __ATOMIC_RELAXED);
+#if defined(HAVE_GCC_ATOMIC_BUILTINS_64)
+ return __atomic_load_n(value, __ATOMIC_RELAXED);
+#elif defined(_WIN32)
+ uint64_t val = *value;
+ return InterlockedCompareExchange64(RBIMPL_CAST((uint64_t *)value), val, val);
+#elif defined(__sun) && defined(HAVE_ATOMIC_H) && (defined(_LP64) || defined(_I32LPx))
+ uint64_t val = *value;
+ return atomic_cas_64(value, val, val);
#else
- return *ptr;
+ // TODO: stdatomic
+
+ return *value;
+#endif
+}
+#define ATOMIC_U64_LOAD_RELAXED(var) rbimpl_atomic_u64_load_relaxed(&(var))
+
+static inline void
+rbimpl_atomic_u64_set_relaxed(volatile rbimpl_atomic_uint64_t *address, uint64_t value)
+{
+#if defined(HAVE_GCC_ATOMIC_BUILTINS_64)
+ __atomic_store_n(address, value, __ATOMIC_RELAXED);
+#elif defined(_WIN32)
+ InterlockedExchange64(address, value);
+#elif defined(__sun) && defined(HAVE_ATOMIC_H) && (defined(_LP64) || defined(_I32LPx))
+ atomic_swap_64(address, value);
+#else
+ // TODO: stdatomic
+
+ *address = value;
#endif
}
-#define ATOMIC_LOAD_RELAXED(var) rbimpl_atomic_load_relaxed(&(var))
+#define ATOMIC_U64_SET_RELAXED(var, val) rbimpl_atomic_u64_set_relaxed(&(var), val)
#endif