diff mbox series

[5/7] Remove atomic_add/add_negative/add_zero

Message ID AM5PR0801MB1668536023BDB49449F88DF583809@AM5PR0801MB1668.eurprd08.prod.outlook.com
State New
Headers show
Series [1/7] Use atomic_exchange_release/acquire | expand

Commit Message

Wilco Dijkstra July 6, 2022, 3:17 p.m. UTC
Remove the 3 uses of atomic_add, atomic_add_zero and atomic_add_negative with atomic_fetch_add_relaxed.

---
diff mbox series

Patch

diff --git a/include/atomic.h b/include/atomic.h
index 492485597a12950d946691cb98b1b3ecbed7d592..50778792e444238dddceb95386afd3d70450b2a1 100644
--- a/include/atomic.h
+++ b/include/atomic.h
@@ -224,20 +224,6 @@ 
 #endif
 
 
-#ifndef atomic_add_negative
-# define atomic_add_negative(mem, value)				      \
-  ({ __typeof (value) __atg12_value = (value);				      \
-     atomic_exchange_and_add (mem, __atg12_value) < -__atg12_value; })
-#endif
-
-
-#ifndef atomic_add_zero
-# define atomic_add_zero(mem, value)					      \
-  ({ __typeof (value) __atg13_value = (value);				      \
-     atomic_exchange_and_add (mem, __atg13_value) == -__atg13_value; })
-#endif
-
-
 /* Atomically *mem &= mask and return the old value of *mem.  */
 #ifndef atomic_and_val
 # define atomic_and_val(mem, mask) \
diff --git a/misc/tst-atomic.c b/misc/tst-atomic.c
index 841ef2e2c6be2da8d1547cfb8bfcc03015154e58..67073c23c34126b87a6049b8d9ecd6cf2d9d23bc 100644
--- a/misc/tst-atomic.c
+++ b/misc/tst-atomic.c
@@ -120,14 +120,6 @@  do_test (void)
       ret = 1;
     }
 
-  mem = -21;
-  atomic_add (&mem, 22);
-  if (mem != 1)
-    {
-      puts ("atomic_add test failed");
-      ret = 1;
-    }
-
   mem = 17;
   atomic_decrement (&mem);
   if (mem != 16)
@@ -190,102 +182,6 @@  do_test (void)
       ret = 1;
     }
 
-  mem = -12;
-  if (! atomic_add_negative (&mem, 10)
-      || mem != -2)
-    {
-      puts ("atomic_add_negative test 1 failed");
-      ret = 1;
-    }
-
-  mem = 0;
-  if (atomic_add_negative (&mem, 100)
-      || mem != 100)
-    {
-      puts ("atomic_add_negative test 2 failed");
-      ret = 1;
-    }
-
-  mem = 15;
-  if (atomic_add_negative (&mem, -10)
-      || mem != 5)
-    {
-      puts ("atomic_add_negative test 3 failed");
-      ret = 1;
-    }
-
-  mem = -12;
-  if (atomic_add_negative (&mem, 14)
-      || mem != 2)
-    {
-      puts ("atomic_add_negative test 4 failed");
-      ret = 1;
-    }
-
-  mem = 0;
-  if (! atomic_add_negative (&mem, -1)
-      || mem != -1)
-    {
-      puts ("atomic_add_negative test 5 failed");
-      ret = 1;
-    }
-
-  mem = -31;
-  if (atomic_add_negative (&mem, 31)
-      || mem != 0)
-    {
-      puts ("atomic_add_negative test 6 failed");
-      ret = 1;
-    }
-
-  mem = -34;
-  if (atomic_add_zero (&mem, 31)
-      || mem != -3)
-    {
-      puts ("atomic_add_zero test 1 failed");
-      ret = 1;
-    }
-
-  mem = -36;
-  if (! atomic_add_zero (&mem, 36)
-      || mem != 0)
-    {
-      puts ("atomic_add_zero test 2 failed");
-      ret = 1;
-    }
-
-  mem = 113;
-  if (atomic_add_zero (&mem, -13)
-      || mem != 100)
-    {
-      puts ("atomic_add_zero test 3 failed");
-      ret = 1;
-    }
-
-  mem = -18;
-  if (atomic_add_zero (&mem, 20)
-      || mem != 2)
-    {
-      puts ("atomic_add_zero test 4 failed");
-      ret = 1;
-    }
-
-  mem = 10;
-  if (atomic_add_zero (&mem, -20)
-      || mem != -10)
-    {
-      puts ("atomic_add_zero test 5 failed");
-      ret = 1;
-    }
-
-  mem = 10;
-  if (! atomic_add_zero (&mem, -10)
-      || mem != 0)
-    {
-      puts ("atomic_add_zero test 6 failed");
-      ret = 1;
-    }
-
   /* Tests for C11-like atomics.  */
   mem = 11;
   if (atomic_load_relaxed (&mem) != 11 || atomic_load_acquire (&mem) != 11)
diff --git a/sysdeps/unix/sysv/linux/check_pf.c b/sysdeps/unix/sysv/linux/check_pf.c
index fdb4d71cb5890272cec88d3af73aef463ff11c8b..0b77a2d897027edc36e956301f018c2ab0121444 100644
--- a/sysdeps/unix/sysv/linux/check_pf.c
+++ b/sysdeps/unix/sysv/linux/check_pf.c
@@ -278,7 +278,7 @@  make_request (int fd, pid_t pid)
     {
       free (result);
 
-      atomic_add (&noai6ai_cached.usecnt, 2);
+      atomic_fetch_add_relaxed (&noai6ai_cached.usecnt, 2);
       noai6ai_cached.seen_ipv4 = seen_ipv4;
       noai6ai_cached.seen_ipv6 = seen_ipv6;
       result = &noai6ai_cached;
@@ -349,7 +349,7 @@  __check_pf (bool *seen_ipv4, bool *seen_ipv6,
       *in6ai = data->in6ai;
 
       if (olddata != NULL && olddata->usecnt > 0
-	  && atomic_add_zero (&olddata->usecnt, -1))
+	  && atomic_fetch_add_relaxed (&olddata->usecnt, -1) == 1)
 	free (olddata);
 
       return;
@@ -377,7 +377,7 @@  __free_in6ai (struct in6addrinfo *ai)
 	(struct cached_data *) ((char *) ai
 				- offsetof (struct cached_data, in6ai));
 
-      if (atomic_add_zero (&data->usecnt, -1))
+      if (atomic_fetch_add_relaxed (&data->usecnt, -1) == 1)
 	{
 	  __libc_lock_lock (lock);
 
diff --git a/sysdeps/unix/sysv/linux/sh/atomic-machine.h b/sysdeps/unix/sysv/linux/sh/atomic-machine.h
index 2f45171240ce8d1970b101134a5c838b8a621cb9..6d4f8bdf1a9a96153c86c7a7fc9a796bc4feac7b 100644
--- a/sysdeps/unix/sysv/linux/sh/atomic-machine.h
+++ b/sysdeps/unix/sysv/linux/sh/atomic-machine.h
@@ -201,55 +201,6 @@ 
 	      } \
 	    })
 
-#define atomic_add_negative(mem, value) \
-  ({ unsigned char __result; \
-     __typeof (*(mem)) __tmp, __value = (value); \
-     if (sizeof (*(mem)) == 1) \
-       __asm __volatile ("\
-	  mova 1f,r0\n\
-	  mov r15,r1\n\
-	  .align 2\n\
-	  mov #(0f-1f),r15\n\
-       0: mov.b @%2,r2\n\
-	  add %1,r2\n\
-	  mov.b r2,@%2\n\
-       1: mov r1,r15\n\
-	  shal r2\n\
-	  movt %0"\
-	: "=r" (__result), "=&r" (__tmp) : "u" (mem), "1" (__value) \
-	: "r0", "r1", "r2", "t", "memory"); \
-     else if (sizeof (*(mem)) == 2) \
-       __asm __volatile ("\
-	  mova 1f,r0\n\
-	  mov r15,r1\n\
-	  .align 2\n\
-	  mov #(0f-1f),r15\n\
-       0: mov.w @%2,r2\n\
-	  add %1,r2\n\
-	  mov.w r2,@%2\n\
-       1: mov r1,r15\n\
-	  shal r2\n\
-	  movt %0"\
-	: "=r" (__result), "=&r" (__tmp) : "u" (mem), "1" (__value) \
-	: "r0", "r1", "r2", "t", "memory"); \
-     else if (sizeof (*(mem)) == 4) \
-       __asm __volatile ("\
-	  mova 1f,r0\n\
-	  mov r15,r1\n\
-	  .align 2\n\
-	  mov #(0f-1f),r15\n\
-       0: mov.l @%2,r2\n\
-	  add %1,r2\n\
-	  mov.l r2,@%2\n\
-       1: mov r1,r15\n\
-	  shal r2\n\
-	  movt %0"\
-	: "=r" (__result), "=&r" (__tmp) : "u" (mem), "1" (__value) \
-	: "r0", "r1", "r2", "t", "memory"); \
-     else \
-       abort (); \
-     __result; })
-
 #define atomic_add_zero(mem, value) \
   ({ unsigned char __result; \
      __typeof (*(mem)) __tmp, __value = (value); \
diff --git a/sysdeps/x86/atomic-machine.h b/sysdeps/x86/atomic-machine.h
index 97d9d4a194a9d7deddbaa1781a1f9484a3e9232d..2f180abe9e682f9e64b471d091e455e192b156ba 100644
--- a/sysdeps/x86/atomic-machine.h
+++ b/sysdeps/x86/atomic-machine.h
@@ -122,82 +122,6 @@ 
 #define atomic_exchange_and_add(mem, value) \
   __sync_fetch_and_add (mem, value)
 
-#define __arch_add_body(lock, pfx, apfx, mem, value) \
-  do {									      \
-    if (__builtin_constant_p (value) && (value) == -1)		      \
-      pfx##_decrement (mem);						      \
-    else if (sizeof (*mem) == 1)					      \
-      __asm __volatile (lock "addb %b1, %0"				      \
-			: "=m" (*mem)					      \
-			: IBR_CONSTRAINT (value), "m" (*mem));		      \
-    else if (sizeof (*mem) == 2)					      \
-      __asm __volatile (lock "addw %w1, %0"				      \
-			: "=m" (*mem)					      \
-			: "ir" (value), "m" (*mem));			      \
-    else if (sizeof (*mem) == 4)					      \
-      __asm __volatile (lock "addl %1, %0"				      \
-			: "=m" (*mem)					      \
-			: "ir" (value), "m" (*mem));			      \
-    else if (__HAVE_64B_ATOMICS)					      \
-      __asm __volatile (lock "addq %q1, %0"				      \
-			: "=m" (*mem)					      \
-			: "ir" ((int64_t) cast_to_integer (value)),	      \
-			  "m" (*mem));					      \
-    else								      \
-      do_add_val_64_acq (apfx, (mem), (value));				      \
-  } while (0)
-
-# define atomic_add(mem, value) \
-  __arch_add_body (LOCK_PREFIX, atomic, __arch, mem, value)
-
-#define atomic_add_negative(mem, value) \
-  ({ unsigned char __result;						      \
-     if (sizeof (*mem) == 1)						      \
-       __asm __volatile (LOCK_PREFIX "addb %b2, %0; sets %1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : IBR_CONSTRAINT (value), "m" (*mem));		      \
-     else if (sizeof (*mem) == 2)					      \
-       __asm __volatile (LOCK_PREFIX "addw %w2, %0; sets %1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "ir" (value), "m" (*mem));			      \
-     else if (sizeof (*mem) == 4)					      \
-       __asm __volatile (LOCK_PREFIX "addl %2, %0; sets %1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "ir" (value), "m" (*mem));			      \
-     else if (__HAVE_64B_ATOMICS)					      \
-       __asm __volatile (LOCK_PREFIX "addq %q2, %0; sets %1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "ir" ((int64_t) cast_to_integer (value)),	      \
-			   "m" (*mem));					      \
-     else								      \
-       __atomic_link_error ();						      \
-     __result; })
-
-
-#define atomic_add_zero(mem, value) \
-  ({ unsigned char __result;						      \
-     if (sizeof (*mem) == 1)						      \
-       __asm __volatile (LOCK_PREFIX "addb %b2, %0; setz %1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : IBR_CONSTRAINT (value), "m" (*mem));		      \
-     else if (sizeof (*mem) == 2)					      \
-       __asm __volatile (LOCK_PREFIX "addw %w2, %0; setz %1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "ir" (value), "m" (*mem));			      \
-     else if (sizeof (*mem) == 4)					      \
-       __asm __volatile (LOCK_PREFIX "addl %2, %0; setz %1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "ir" (value), "m" (*mem));			      \
-     else if (__HAVE_64B_ATOMICS)					      \
-       __asm __volatile (LOCK_PREFIX "addq %q2, %0; setz %1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "ir" ((int64_t) cast_to_integer (value)),	      \
-			   "m" (*mem));					      \
-     else								      \
-       __atomic_link_error ();					      \
-     __result; })
-
-
 #define __arch_decrement_body(lock, pfx, mem) \
   do {									      \
     if (sizeof (*mem) == 1)						      \