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