|
|
bca718 |
commit ff8714269c9312d9164456279a56b6f6c47e2771
|
|
|
bca718 |
Author: Torvald Riegel <triegel@redhat.com>
|
|
|
bca718 |
Date: Sun Sep 14 20:04:54 2014 +0200
|
|
|
bca718 |
|
|
|
bca718 |
Add atomic operations similar to those provided by C11.
|
|
|
bca718 |
|
|
|
bca718 |
Index: glibc-2.17-c758a686/include/atomic.h
|
|
|
bca718 |
===================================================================
|
|
|
bca718 |
--- glibc-2.17-c758a686.orig/include/atomic.h
|
|
|
bca718 |
+++ glibc-2.17-c758a686/include/atomic.h
|
|
|
bca718 |
@@ -542,6 +542,218 @@
|
|
|
bca718 |
({ __typeof (x) __x; __asm ("" : "=r" (__x) : "0" (x)); __x; })
|
|
|
bca718 |
#endif
|
|
|
bca718 |
|
|
|
bca718 |
+/* This is equal to 1 iff the architecture supports 64b atomic operations. */
|
|
|
bca718 |
+#ifndef __HAVE_64B_ATOMICS
|
|
|
bca718 |
+#error Unable to determine if 64-bit atomics are present.
|
|
|
bca718 |
+#endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* The following functions are a subset of the atomic operations provided by
|
|
|
bca718 |
+ C11. Usually, a function named atomic_OP_MO(args) is equivalent to C11's
|
|
|
bca718 |
+ atomic_OP_explicit(args, memory_order_MO); exceptions noted below. */
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* Each arch can request to use compiler built-ins for C11 atomics. If it
|
|
|
bca718 |
+ does, all atomics will be based on these. */
|
|
|
bca718 |
+#if USE_ATOMIC_COMPILER_BUILTINS
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* We require 32b atomic operations; some archs also support 64b atomic
|
|
|
bca718 |
+ operations. */
|
|
|
bca718 |
+void __atomic_link_error (void);
|
|
|
bca718 |
+# if __HAVE_64B_ATOMICS == 1
|
|
|
bca718 |
+# define __atomic_check_size(mem) \
|
|
|
bca718 |
+ if ((sizeof (*mem) != 4) && (sizeof (*mem) != 8)) \
|
|
|
bca718 |
+ __atomic_link_error ();
|
|
|
bca718 |
+# else
|
|
|
bca718 |
+# define __atomic_check_size(mem) \
|
|
|
bca718 |
+ if (sizeof (*mem) != 4) \
|
|
|
bca718 |
+ __atomic_link_error ();
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+# define atomic_thread_fence_acquire() \
|
|
|
bca718 |
+ __atomic_thread_fence (__ATOMIC_ACQUIRE)
|
|
|
bca718 |
+# define atomic_thread_fence_release() \
|
|
|
bca718 |
+ __atomic_thread_fence (__ATOMIC_RELEASE)
|
|
|
bca718 |
+# define atomic_thread_fence_seq_cst() \
|
|
|
bca718 |
+ __atomic_thread_fence (__ATOMIC_SEQ_CST)
|
|
|
bca718 |
+
|
|
|
bca718 |
+# define atomic_load_relaxed(mem) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); __atomic_load_n ((mem), __ATOMIC_RELAXED); })
|
|
|
bca718 |
+# define atomic_load_acquire(mem) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); __atomic_load_n ((mem), __ATOMIC_ACQUIRE); })
|
|
|
bca718 |
+
|
|
|
bca718 |
+# define atomic_store_relaxed(mem, val) \
|
|
|
bca718 |
+ do { \
|
|
|
bca718 |
+ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_store_n ((mem), (val), __ATOMIC_RELAXED); \
|
|
|
bca718 |
+ } while (0)
|
|
|
bca718 |
+# define atomic_store_release(mem, val) \
|
|
|
bca718 |
+ do { \
|
|
|
bca718 |
+ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_store_n ((mem), (val), __ATOMIC_RELEASE); \
|
|
|
bca718 |
+ } while (0)
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* On failure, this CAS has memory_order_relaxed semantics. */
|
|
|
bca718 |
+# define atomic_compare_exchange_weak_relaxed(mem, expected, desired) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_compare_exchange_n ((mem), (expected), (desired), 1, \
|
|
|
bca718 |
+ __ATOMIC_RELAXED, __ATOMIC_RELAXED); })
|
|
|
bca718 |
+# define atomic_compare_exchange_weak_acquire(mem, expected, desired) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_compare_exchange_n ((mem), (expected), (desired), 1, \
|
|
|
bca718 |
+ __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); })
|
|
|
bca718 |
+# define atomic_compare_exchange_weak_release(mem, expected, desired) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_compare_exchange_n ((mem), (expected), (desired), 1, \
|
|
|
bca718 |
+ __ATOMIC_RELEASE, __ATOMIC_RELAXED); })
|
|
|
bca718 |
+
|
|
|
bca718 |
+# define atomic_exchange_acquire(mem, desired) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_exchange_n ((mem), (desired), __ATOMIC_ACQUIRE); })
|
|
|
bca718 |
+# define atomic_exchange_release(mem, desired) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_exchange_n ((mem), (desired), __ATOMIC_RELEASE); })
|
|
|
bca718 |
+
|
|
|
bca718 |
+# define atomic_fetch_add_relaxed(mem, operand) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_fetch_add ((mem), (operand), __ATOMIC_RELAXED); })
|
|
|
bca718 |
+# define atomic_fetch_add_acquire(mem, operand) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_fetch_add ((mem), (operand), __ATOMIC_ACQUIRE); })
|
|
|
bca718 |
+# define atomic_fetch_add_release(mem, operand) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_fetch_add ((mem), (operand), __ATOMIC_RELEASE); })
|
|
|
bca718 |
+# define atomic_fetch_add_acq_rel(mem, operand) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_fetch_add ((mem), (operand), __ATOMIC_ACQ_REL); })
|
|
|
bca718 |
+
|
|
|
bca718 |
+# define atomic_fetch_and_acquire(mem, operand) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_fetch_and ((mem), (operand), __ATOMIC_ACQUIRE); })
|
|
|
bca718 |
+
|
|
|
bca718 |
+# define atomic_fetch_or_relaxed(mem, operand) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_fetch_or ((mem), (operand), __ATOMIC_RELAXED); })
|
|
|
bca718 |
+# define atomic_fetch_or_acquire(mem, operand) \
|
|
|
bca718 |
+ ({ __atomic_check_size((mem)); \
|
|
|
bca718 |
+ __atomic_fetch_or ((mem), (operand), __ATOMIC_ACQUIRE); })
|
|
|
bca718 |
+
|
|
|
bca718 |
+#else /* !USE_ATOMIC_COMPILER_BUILTINS */
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* By default, we assume that read, write, and full barriers are equivalent
|
|
|
bca718 |
+ to acquire, release, and seq_cst barriers. Archs for which this does not
|
|
|
bca718 |
+ hold have to provide custom definitions of the fences. */
|
|
|
bca718 |
+# ifndef atomic_thread_fence_acquire
|
|
|
bca718 |
+# define atomic_thread_fence_acquire() atomic_read_barrier ()
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_thread_fence_release
|
|
|
bca718 |
+# define atomic_thread_fence_release() atomic_write_barrier ()
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_thread_fence_seq_cst
|
|
|
bca718 |
+# define atomic_thread_fence_seq_cst() atomic_full_barrier ()
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+# ifndef atomic_load_relaxed
|
|
|
bca718 |
+# define atomic_load_relaxed(mem) \
|
|
|
bca718 |
+ ({ __typeof (*(mem)) __atg100_val; \
|
|
|
bca718 |
+ __asm ("" : "=r" (__atg100_val) : "0" (*(mem))); \
|
|
|
bca718 |
+ __atg100_val; })
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_load_acquire
|
|
|
bca718 |
+# define atomic_load_acquire(mem) \
|
|
|
bca718 |
+ ({ __typeof (*(mem)) __atg101_val = atomic_load_relaxed (mem); \
|
|
|
bca718 |
+ atomic_thread_fence_acquire (); \
|
|
|
bca718 |
+ __atg101_val; })
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+# ifndef atomic_store_relaxed
|
|
|
bca718 |
+/* XXX Use inline asm here? */
|
|
|
bca718 |
+# define atomic_store_relaxed(mem, val) do { *(mem) = (val); } while (0)
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_store_release
|
|
|
bca718 |
+# define atomic_store_release(mem, val) \
|
|
|
bca718 |
+ do { \
|
|
|
bca718 |
+ atomic_thread_fence_release (); \
|
|
|
bca718 |
+ atomic_store_relaxed ((mem), (val)); \
|
|
|
bca718 |
+ } while (0)
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* On failure, this CAS has memory_order_relaxed semantics. */
|
|
|
bca718 |
+/* XXX This potentially has one branch more than necessary, but archs
|
|
|
bca718 |
+ currently do not define a CAS that returns both the previous value and
|
|
|
bca718 |
+ the success flag. */
|
|
|
bca718 |
+# ifndef atomic_compare_exchange_weak_acquire
|
|
|
bca718 |
+# define atomic_compare_exchange_weak_acquire(mem, expected, desired) \
|
|
|
bca718 |
+ ({ typeof (*(expected)) __atg102_expected = *(expected); \
|
|
|
bca718 |
+ *(expected) = \
|
|
|
bca718 |
+ atomic_compare_and_exchange_val_acq ((mem), (desired), *(expected)); \
|
|
|
bca718 |
+ *(expected) == __atg102_expected; })
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_compare_exchange_weak_relaxed
|
|
|
bca718 |
+/* XXX Fall back to CAS with acquire MO because archs do not define a weaker
|
|
|
bca718 |
+ CAS. */
|
|
|
bca718 |
+# define atomic_compare_exchange_weak_relaxed(mem, expected, desired) \
|
|
|
bca718 |
+ atomic_compare_exchange_weak_acquire ((mem), (expected), (desired))
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_compare_exchange_weak_release
|
|
|
bca718 |
+# define atomic_compare_exchange_weak_release(mem, expected, desired) \
|
|
|
bca718 |
+ ({ typeof (*(expected)) __atg103_expected = *(expected); \
|
|
|
bca718 |
+ *(expected) = \
|
|
|
bca718 |
+ atomic_compare_and_exchange_val_rel ((mem), (desired), *(expected)); \
|
|
|
bca718 |
+ *(expected) == __atg103_expected; })
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+# ifndef atomic_exchange_acquire
|
|
|
bca718 |
+# define atomic_exchange_acquire(mem, val) \
|
|
|
bca718 |
+ atomic_exchange_acq ((mem), (val))
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_exchange_release
|
|
|
bca718 |
+# define atomic_exchange_release(mem, val) \
|
|
|
bca718 |
+ atomic_exchange_rel ((mem), (val))
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+# ifndef atomic_fetch_add_acquire
|
|
|
bca718 |
+# define atomic_fetch_add_acquire(mem, operand) \
|
|
|
bca718 |
+ atomic_exchange_and_add_acq ((mem), (operand))
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_fetch_add_relaxed
|
|
|
bca718 |
+/* XXX Fall back to acquire MO because the MO semantics of
|
|
|
bca718 |
+ atomic_exchange_and_add are not documented; the generic version falls back
|
|
|
bca718 |
+ to atomic_exchange_and_add_acq if atomic_exchange_and_add is not defined,
|
|
|
bca718 |
+ and vice versa. */
|
|
|
bca718 |
+# define atomic_fetch_add_relaxed(mem, operand) \
|
|
|
bca718 |
+ atomic_fetch_add_acquire ((mem), (operand))
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_fetch_add_release
|
|
|
bca718 |
+# define atomic_fetch_add_release(mem, operand) \
|
|
|
bca718 |
+ atomic_exchange_and_add_rel ((mem), (operand))
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+# ifndef atomic_fetch_add_acq_rel
|
|
|
bca718 |
+# define atomic_fetch_add_acq_rel(mem, operand) \
|
|
|
bca718 |
+ ({ atomic_thread_fence_release (); \
|
|
|
bca718 |
+ atomic_exchange_and_add_acq ((mem), (operand)); })
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* XXX The default for atomic_and_val has acquire semantics, but this is not
|
|
|
bca718 |
+ documented. */
|
|
|
bca718 |
+# ifndef atomic_fetch_and_acquire
|
|
|
bca718 |
+# define atomic_fetch_and_acquire(mem, operand) \
|
|
|
bca718 |
+ atomic_and_val ((mem), (operand))
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* XXX The default for atomic_or_val has acquire semantics, but this is not
|
|
|
bca718 |
+ documented. */
|
|
|
bca718 |
+# ifndef atomic_fetch_or_acquire
|
|
|
bca718 |
+# define atomic_fetch_or_acquire(mem, operand) \
|
|
|
bca718 |
+ atomic_or_val ((mem), (operand))
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+/* XXX Fall back to acquire MO because archs do not define a weaker
|
|
|
bca718 |
+ atomic_or_val. */
|
|
|
bca718 |
+# ifndef atomic_fetch_or_relaxed
|
|
|
bca718 |
+# define atomic_fetch_or_relaxed(mem, operand) \
|
|
|
bca718 |
+ atomic_fetch_or_acquire ((mem), (operand))
|
|
|
bca718 |
+# endif
|
|
|
bca718 |
+
|
|
|
bca718 |
+#endif /* !USE_ATOMIC_COMPILER_BUILTINS */
|
|
|
bca718 |
+
|
|
|
bca718 |
|
|
|
bca718 |
#ifndef atomic_delay
|
|
|
bca718 |
# define atomic_delay() do { /* nothing */ } while (0)
|