diff mbox series

[2/4] Use builtin atomics for compare_and_exchange_val/bool

Message ID AM5PR0801MB16689CF6EAA9379BC90419EA838F9@AM5PR0801MB1668.eurprd08.prod.outlook.com
State New
Headers show
Series [1/4] Switch to builtin atomics | expand

Commit Message

Wilco Dijkstra July 19, 2022, 2:12 p.m. UTC
Define atomic_compare_and_exchange_val/bool_* in terms of standard atomics.
Later cleanups will replace existing uses given the interface is very confusing.
A minor change is needed in nscd-client.h which has a volatile parameter.

---
diff mbox series

Patch

diff --git a/include/atomic.h b/include/atomic.h
index bf6417621a34458a1d7585ee6a7a24cea6670a07..e59e210e83fa301f48c4cdb3676d37e4980cc4a2 100644
--- a/include/atomic.h
+++ b/include/atomic.h
@@ -78,37 +78,30 @@ 
 
 /* Atomically store NEWVAL in *MEM if *MEM is equal to OLDVAL.
    Return the old *MEM value.  */
-#if !defined atomic_compare_and_exchange_val_acq \
-    && defined __arch_compare_and_exchange_val_32_acq
+#undef atomic_compare_and_exchange_val_acq
 # define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
-  __atomic_val_bysize (__arch_compare_and_exchange_val,acq,		      \
-		       mem, newval, oldval)
-#endif
-
-
-#ifndef atomic_compare_and_exchange_val_rel
-# define atomic_compare_and_exchange_val_rel(mem, newval, oldval)	      \
-  atomic_compare_and_exchange_val_acq (mem, newval, oldval)
-#endif
+  ({									      \
+     __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;							      \
+  })
 
 /* Atomically store NEWVAL in *MEM if *MEM is equal to OLDVAL.
    Return zero if *MEM was changed or non-zero if no exchange happened.  */
-#ifndef atomic_compare_and_exchange_bool_acq
-# ifdef __arch_compare_and_exchange_bool_32_acq
-#  define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
-  __atomic_bool_bysize (__arch_compare_and_exchange_bool,acq,		      \
-		        mem, newval, oldval)
-# else
-#  define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
-  ({ /* Cannot use __oldval here, because macros later in this file might     \
-	call this macro with __oldval argument.	 */			      \
-     __typeof (oldval) __atg3_old = (oldval);				      \
-     atomic_compare_and_exchange_val_acq (mem, newval, __atg3_old)	      \
-       != __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);      \
   })
-# endif
-#endif
 
 
 /* Store NEWVALUE in *MEM and return the old value.  */
@@ -333,6 +326,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); })
diff --git a/nscd/nscd-client.h b/nscd/nscd-client.h
index ca9e6def1a88ff14c5e8b39f0e236aa8a30f95ae..89bac1899f1173c63699013f27e58571d9df9994 100644
--- a/nscd/nscd-client.h
+++ b/nscd/nscd-client.h
@@ -367,8 +367,9 @@  struct locked_map_ptr
 /* Try acquiring lock for mapptr, returns true if it succeeds, false
    if not.  */
 static inline bool
-__nscd_acquire_maplock (volatile struct locked_map_ptr *mapptr)
+__nscd_acquire_maplock (volatile struct locked_map_ptr *mapptr_in)
 {
+  struct locked_map_ptr *mapptr = (struct locked_map_ptr *) mapptr_in;
   int cnt = 0;
   while (__builtin_expect (atomic_compare_and_exchange_val_acq (&mapptr->lock,
 								1, 0) != 0, 0))