]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
atomic: Use builtin atomics with USE_ATOMIC_COMPILER_BUILTINS
authorWilco Dijkstra <wilco.dijkstra@arm.com>
Mon, 8 Sep 2025 17:46:29 +0000 (17:46 +0000)
committerWilco Dijkstra <wilco.dijkstra@arm.com>
Tue, 9 Sep 2025 13:53:54 +0000 (13:53 +0000)
Use builtin atomics for atomic_compare_and_exchange_* and
atomic_exchange_and_add if USE_ATOMIC_COMPILER_BUILTINS is enabled.
This allows removing target atomic-machine.h headers.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
include/atomic.h

index 16b36d80f186cab8e2bede3a7d7e79907d3344f1..98ba808ee9e3bdd30a81d62bacfdeb8333c061d9 100644 (file)
     __atg2_result;                                                           \
   })
 
+#if USE_ATOMIC_COMPILER_BUILTINS
+
+# undef atomic_compare_and_exchange_val_acq
+# define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
+  ({                                                                         \
+     __typeof (*(mem)) __atg3_old = (oldval);                                \
+     atomic_compare_exchange_acquire (mem, (void*)&__atg3_old, newval);              \
+     __atg3_old;                                                             \
+  })
+
+# undef atomic_compare_and_exchange_val_rel
+# define atomic_compare_and_exchange_val_rel(mem, newval, oldval)            \
+  ({                                                                         \
+     __typeof (*(mem)) __atg3_old = (oldval);                                \
+     atomic_compare_exchange_release (mem, (void*)&__atg3_old, newval);              \
+     __atg3_old;                                                             \
+  })
+
+# undef atomic_compare_and_exchange_bool_acq
+# define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
+  ({                                                                         \
+     __typeof (*(mem)) __atg3_old = (oldval);                                \
+     !atomic_compare_exchange_acquire (mem, (void*)&__atg3_old, newval);      \
+  })
+
+# undef atomic_exchange_and_add
+# define atomic_exchange_and_add(mem, val) atomic_fetch_add_relaxed(mem,val)
+
+#endif
+
 
 /* Atomically store NEWVAL in *MEM if *MEM is equal to OLDVAL.
    Return the old *MEM value.  */
@@ -603,6 +633,19 @@ void __atomic_link_error (void);
   __atomic_compare_exchange_n ((mem), (expected), (desired), 1,                      \
     __ATOMIC_RELEASE, __ATOMIC_RELAXED); })
 
+# define atomic_compare_exchange_relaxed(mem, expected, desired) \
+  ({ __atomic_check_size((mem));                                             \
+  __atomic_compare_exchange_n ((mem), (expected), (desired), 0,                      \
+    __ATOMIC_RELAXED, __ATOMIC_RELAXED); })
+# define atomic_compare_exchange_acquire(mem, expected, desired) \
+  ({ __atomic_check_size((mem));                                             \
+  __atomic_compare_exchange_n ((mem), (expected), (desired), 0,                      \
+    __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); })
+# define atomic_compare_exchange_release(mem, expected, desired) \
+  ({ __atomic_check_size((mem));                                             \
+  __atomic_compare_exchange_n ((mem), (expected), (desired), 0,                      \
+    __ATOMIC_RELEASE, __ATOMIC_RELAXED); })
+
 # define atomic_exchange_relaxed(mem, desired) \
   ({ __atomic_check_size((mem));                                             \
   __atomic_exchange_n ((mem), (desired), __ATOMIC_RELAXED); })