diff mbox series

[6/7] Remove atomic_increment/increment_and_test/increment_val

Message ID AM5PR0801MB1668FA65C1D6620B9679BFA383809@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:18 p.m. UTC
Replace atomic_increment, atomic_increment_val and atomic_increment_and_test with
atomic_fetch_add_relaxed for counters. One case in sem_post.c uses release semantics.

---
diff mbox series

Patch

diff --git a/htl/pt-create.c b/htl/pt-create.c
index ce52ed9f52210a4e4c7a049ebee817ec9ccfeeb1..14f02cd2b8a19e8581a170dfba2b948ef8304203 100644
--- a/htl/pt-create.c
+++ b/htl/pt-create.c
@@ -228,7 +228,7 @@  __pthread_create_internal (struct __pthread **thread,
      the number of threads from within the new thread isn't an option
      since this thread might return and call `pthread_exit' before the
      new thread runs.  */
-  atomic_increment (&__pthread_total);
+  atomic_fetch_add_relaxed (&__pthread_total, 1);
 
   /* Store a pointer to this thread in the thread ID lookup table.  We
      could use __thread_setid, however, we only lock for reading as no
diff --git a/include/atomic.h b/include/atomic.h
index fd1a1eb0f87d288251d176d69246cff2d3422af7..492485597a12950d946691cb98b1b3ecbed7d592 100644
--- a/include/atomic.h
+++ b/include/atomic.h
@@ -187,23 +187,6 @@ 
 #endif
 
 
-#ifndef atomic_increment
-# define atomic_increment(mem) atomic_add ((mem), 1)
-#endif
-
-
-#ifndef atomic_increment_val
-# define atomic_increment_val(mem) (atomic_exchange_and_add ((mem), 1) + 1)
-#endif
-
-
-/* Add one to *MEM and return true iff it's now zero.  */
-#ifndef atomic_increment_and_test
-# define atomic_increment_and_test(mem) \
-  (atomic_exchange_and_add ((mem), 1) + 1 == 0)
-#endif
-
-
 #ifndef atomic_decrement
 # define atomic_decrement(mem) atomic_add ((mem), -1)
 #endif
diff --git a/manual/ipc.texi b/manual/ipc.texi
index 081b98fe29e0b3b5b7f4f916ad2085d170bd3825..f7cbdc3e09b0b4aea9a96ddcaf571c474024cc32 100644
--- a/manual/ipc.texi
+++ b/manual/ipc.texi
@@ -85,7 +85,7 @@  by @theglibc{}.
 
 @deftypefun int sem_wait (sem_t *@var{sem});
 @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@acucorrupt{}}}
-@c atomic_increment (nwaiters) acucorrupt
+@c atomic_fetch_add_relaxed (nwaiters) acucorrupt
 @c
 @c Given the use atomic operations this function seems
 @c to be AS-safe.  It is AC-unsafe because there is still
diff --git a/manual/llio.texi b/manual/llio.texi
index 92bfd93e067ce2a782369084ed7dae099300f418..4e6e3fb672bb8ecbb1bb52faf3d70d9b7b33973f 100644
--- a/manual/llio.texi
+++ b/manual/llio.texi
@@ -2528,7 +2528,7 @@  aiocb64}, since the LFS transparently replaces the old interface.
 @c      _dl_allocate_tls_init ok
 @c       GET_DTV ok
 @c     mmap ok
-@c     atomic_increment_val ok
+@c     atomic_fetch_add_relaxed ok
 @c     munmap ok
 @c     change_stack_perm ok
 @c      mprotect ok
@@ -2567,7 +2567,7 @@  aiocb64}, since the LFS transparently replaces the old interface.
 @c     do_clone @asulock @ascuheap @aculock @acsmem
 @c      PREPARE_CREATE ok
 @c      lll_lock (pd->lock) @asulock @aculock
-@c      atomic_increment ok
+@c      atomic_fetch_add_relaxed ok
 @c      clone ok
 @c      atomic_decrement ok
 @c      atomic_exchange_acquire ok
diff --git a/misc/tst-atomic.c b/misc/tst-atomic.c
index 765b036873e181d6c49299c0ad08d5dbfcc05387..841ef2e2c6be2da8d1547cfb8bfcc03015154e58 100644
--- a/misc/tst-atomic.c
+++ b/misc/tst-atomic.c
@@ -128,45 +128,6 @@  do_test (void)
       ret = 1;
     }
 
-  mem = -1;
-  atomic_increment (&mem);
-  if (mem != 0)
-    {
-      puts ("atomic_increment test failed");
-      ret = 1;
-    }
-
-  mem = 2;
-  if (atomic_increment_val (&mem) != 3)
-    {
-      puts ("atomic_increment_val test failed");
-      ret = 1;
-    }
-
-  mem = 0;
-  if (atomic_increment_and_test (&mem)
-      || mem != 1)
-    {
-      puts ("atomic_increment_and_test test 1 failed");
-      ret = 1;
-    }
-
-  mem = 35;
-  if (atomic_increment_and_test (&mem)
-      || mem != 36)
-    {
-      puts ("atomic_increment_and_test test 2 failed");
-      ret = 1;
-    }
-
-  mem = -1;
-  if (! atomic_increment_and_test (&mem)
-      || mem != 0)
-    {
-      puts ("atomic_increment_and_test test 3 failed");
-      ret = 1;
-    }
-
   mem = 17;
   atomic_decrement (&mem);
   if (mem != 16)
diff --git a/nptl/nptl_setxid.c b/nptl/nptl_setxid.c
index aa863c7ea8122ea01d1aa4cffe101bbb7c11270c..3b7e2d434abe8a15145349d1a08a4e706061c74d 100644
--- a/nptl/nptl_setxid.c
+++ b/nptl/nptl_setxid.c
@@ -163,7 +163,7 @@  setxid_signal_thread (struct xid_command *cmdp, struct pthread *t)
   /* If this failed, it must have had not started yet or else exited.  */
   if (!INTERNAL_SYSCALL_ERROR_P (val))
     {
-      atomic_increment (&cmdp->cntr);
+      atomic_fetch_add_relaxed (&cmdp->cntr, 1);
       return 1;
     }
   else
diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
index 1e6b385787aab5fa2f452de0cd2eae2b5f0fba83..6542c3183eda33b145aa80adcb620c7a19450aa2 100644
--- a/nptl/pthread_create.c
+++ b/nptl/pthread_create.c
@@ -759,7 +759,7 @@  __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr,
      we momentarily store a false value; this doesn't matter because there
      is no kosher thing a signal handler interrupting us right here can do
      that cares whether the thread count is correct.  */
-  atomic_increment (&__nptl_nthreads);
+  atomic_fetch_add_relaxed (&__nptl_nthreads, 1);
 
   /* Our local value of stopped_start and thread_ran can be accessed at
      any time. The PD->stopped_start may only be accessed if we have
diff --git a/nptl/sem_post.c b/nptl/sem_post.c
index 9e5741753a741034db96cfff8b8978908fafc1f6..7ec21e92eb4c71d7f17764e96bc7603837f7522d 100644
--- a/nptl/sem_post.c
+++ b/nptl/sem_post.c
@@ -91,7 +91,7 @@  __old_sem_post (sem_t *sem)
   /* We must need to synchronize with consumers of this token, so the atomic
      increment must have release MO semantics.  */
   atomic_write_barrier ();
-  (void) atomic_increment_val (futex);
+  atomic_fetch_add_release (futex, 1);
   /* We always have to assume it is a shared semaphore.  */
   futex_wake (futex, 1, LLL_SHARED);
   return 0;
diff --git a/nscd/cache.c b/nscd/cache.c
index b66c35334a79a315ac9fd533df09ec097221d8e0..21af9a0f9550dfc7c1121fb5014cd80acdfcf696 100644
--- a/nscd/cache.c
+++ b/nscd/cache.c
@@ -192,7 +192,7 @@  cache_add (int type, const void *key, size_t len, struct datahead *packet,
 
   /* We depend on this value being correct and at least as high as the
      real number of entries.  */
-  atomic_increment (&table->head->nentries);
+  atomic_fetch_add_relaxed (&table->head->nentries, 1);
 
   /* It does not matter that we are not loading the just increment
      value, this is just for statistics.  */
diff --git a/nscd/nscd_helper.c b/nscd/nscd_helper.c
index 8e66fa2548e87c9b24cade41339c83f4cef7f2cf..d3e05e272a854e13c0e4d18594ea57336b8db2bf 100644
--- a/nscd/nscd_helper.c
+++ b/nscd/nscd_helper.c
@@ -425,7 +425,7 @@  __nscd_get_map_ref (request_type type, const char *name,
 				0))
 	    cur = NO_MAPPING;
 	  else
-	    atomic_increment (&cur->counter);
+	    atomic_fetch_add_relaxed (&cur->counter, 1);
 	}
     }
 
diff --git a/sysdeps/m68k/m680x0/m68020/atomic-machine.h b/sysdeps/m68k/m680x0/m68020/atomic-machine.h
index 72a3b81642c1a23207054092d16bb856556cd897..6e454c5ceddd4e82b88b71203a94549bbb20ed20 100644
--- a/sysdeps/m68k/m680x0/m68020/atomic-machine.h
+++ b/sysdeps/m68k/m680x0/m68020/atomic-machine.h
@@ -157,37 +157,6 @@ 
 	      }								      \
 	    })
 
-#define atomic_increment_and_test(mem) \
-  ({ char __result;							      \
-     if (sizeof (*(mem)) == 1)						      \
-       __asm __volatile ("addq%.b %#1,%1; seq %0"			      \
-			 : "=dm" (__result), "+m" (*(mem)));		      \
-     else if (sizeof (*(mem)) == 2)					      \
-       __asm __volatile ("addq%.w %#1,%1; seq %0"			      \
-			 : "=dm" (__result), "+m" (*(mem)));		      \
-     else if (sizeof (*(mem)) == 4)					      \
-       __asm __volatile ("addq%.l %#1,%1; seq %0"			      \
-			 : "=dm" (__result), "+m" (*(mem)));		      \
-     else								      \
-       {								      \
-	 __typeof (mem) __memp = (mem);					      \
-	 __typeof (*(mem)) __oldval = *__memp;				      \
-	 __typeof (*(mem)) __temp;					      \
-	 __asm __volatile ("1: move%.l %1,%2;"				      \
-			   "   move%.l %R1,%R2;"			      \
-			   "   addq%.l %#1,%R2;"			      \
-			   "   addx%.l %5,%2;"				      \
-			   "   seq %0;"					      \
-			   "   cas2%.l %1:%R1,%2:%R2,(%3):(%4);"	      \
-			   "   jbne 1b"					      \
-			   : "=&dm" (__result), "=d" (__oldval),	      \
-			     "=&d" (__temp)				      \
-			   : "r" (__memp), "r" ((char *) __memp + 4),	      \
-			     "d" (0), "1" (__oldval)			      \
-			   : "memory");					      \
-       }								      \
-     __result; })
-
 #define atomic_decrement_and_test(mem) \
   ({ char __result;							      \
      if (sizeof (*(mem)) == 1)						      \
diff --git a/sysdeps/microblaze/atomic-machine.h b/sysdeps/microblaze/atomic-machine.h
index 871750e3cb4f193f610c4ccd804c8595eabbdc3a..4f94632c5184d3154b7b3b12cd9369d25008c2d8 100644
--- a/sysdeps/microblaze/atomic-machine.h
+++ b/sysdeps/microblaze/atomic-machine.h
@@ -173,45 +173,6 @@ 
     __result;                                                                  \
   })
 
-#define __arch_atomic_increment_val_32(mem)                                    \
-  ({                                                                           \
-    __typeof (*(mem)) __val;                                                   \
-    int test;                                                                  \
-    __asm __volatile (                                                         \
-                "   addc    r0, r0, r0;"                                       \
-                "1: lwx     %0, %3, r0;"                                       \
-                "   addic   %1, r0, 0;"                                        \
-                "   bnei    %1, 1b;"                                           \
-                "   addi    %0, %0, 1;"                                        \
-                "   swx     %0, %3, r0;"                                       \
-                "   addic   %1, r0, 0;"                                        \
-                "   bnei    %1, 1b;"                                           \
-                    : "=&r" (__val),                                           \
-                    "=&r" (test),                                              \
-                    "=m" (*mem)                                                \
-                    : "r" (mem),                                               \
-                    "m" (*mem)                                                 \
-                    : "cc", "memory");                                         \
-    __val;                                                                     \
-  })
-
-#define __arch_atomic_increment_val_64(mem)                                    \
-  (abort (), (__typeof (*mem)) 0)
-
-#define atomic_increment_val(mem)                                              \
-  ({                                                                           \
-    __typeof (*(mem)) __result;                                                \
-    if (sizeof (*(mem)) == 4)                                                  \
-      __result = __arch_atomic_increment_val_32 (mem);                         \
-    else if (sizeof (*(mem)) == 8)                                             \
-      __result = __arch_atomic_increment_val_64 (mem);                         \
-    else                                                                       \
-       abort ();                                                               \
-    __result;                                                                  \
-  })
-
-#define atomic_increment(mem) ({ atomic_increment_val (mem); (void) 0; })
-
 #define __arch_atomic_decrement_val_32(mem)                                    \
   ({                                                                           \
     __typeof (*(mem)) __val;                                                   \
diff --git a/sysdeps/powerpc/atomic-machine.h b/sysdeps/powerpc/atomic-machine.h
index 853bd8a75eaa429feb4a707069a8f5859943d103..10d94d6cbf0497f8d2b0f431efd9f79fb9b127e1 100644
--- a/sysdeps/powerpc/atomic-machine.h
+++ b/sysdeps/powerpc/atomic-machine.h
@@ -151,19 +151,6 @@ 
     __val;								      \
   })
 
-#define __arch_atomic_increment_val_32(mem) \
-  ({									      \
-    __typeof (*(mem)) __val;						      \
-    __asm __volatile ("1:	lwarx	%0,0,%2\n"			      \
-		      "		addi	%0,%0,1\n"			      \
-		      "		stwcx.	%0,0,%2\n"			      \
-		      "		bne-	1b"				      \
-		      : "=&b" (__val), "=m" (*mem)			      \
-		      : "b" (mem), "m" (*mem)				      \
-		      : "cr0", "memory");				      \
-    __val;								      \
-  })
-
 #define __arch_atomic_decrement_val_32(mem) \
   ({									      \
     __typeof (*(mem)) __val;						      \
@@ -274,20 +261,6 @@ 
     __result;								      \
   })
 
-#define atomic_increment_val(mem) \
-  ({									      \
-    __typeof (*(mem)) __result;						      \
-    if (sizeof (*(mem)) == 4)						      \
-      __result = __arch_atomic_increment_val_32 (mem);			      \
-    else if (sizeof (*(mem)) == 8)					      \
-      __result = __arch_atomic_increment_val_64 (mem);			      \
-    else 								      \
-       abort ();							      \
-    __result;								      \
-  })
-
-#define atomic_increment(mem) ({ atomic_increment_val (mem); (void) 0; })
-
 #define atomic_decrement_val(mem) \
   ({									      \
     __typeof (*(mem)) __result;						      \
diff --git a/sysdeps/powerpc/powerpc32/atomic-machine.h b/sysdeps/powerpc/powerpc32/atomic-machine.h
index 33ff2aa071b93c7497e6c8ded8e0fbc33ce41722..5a82e75399615c40d02aca420116c6ac76e9d627 100644
--- a/sysdeps/powerpc/powerpc32/atomic-machine.h
+++ b/sysdeps/powerpc/powerpc32/atomic-machine.h
@@ -85,9 +85,6 @@ 
 #define __arch_atomic_exchange_and_add_64_rel(mem, value) \
     ({ abort (); (*mem) = (value); })
 
-#define __arch_atomic_increment_val_64(mem) \
-    ({ abort (); (*mem)++; })
-
 #define __arch_atomic_decrement_val_64(mem) \
     ({ abort (); (*mem)--; })
 
diff --git a/sysdeps/powerpc/powerpc64/atomic-machine.h b/sysdeps/powerpc/powerpc64/atomic-machine.h
index cbaf01162a277519bdcf730c33d4e5526c3dcb39..7ac9ef6ab4c32550ba9de54873ace239757b6a77 100644
--- a/sysdeps/powerpc/powerpc64/atomic-machine.h
+++ b/sysdeps/powerpc/powerpc64/atomic-machine.h
@@ -181,19 +181,6 @@ 
       __val;								      \
     })
 
-#define __arch_atomic_increment_val_64(mem) \
-    ({									      \
-      __typeof (*(mem)) __val;						      \
-      __asm __volatile ("1:	ldarx	%0,0,%2\n"			      \
-			"	addi	%0,%0,1\n"			      \
-			"	stdcx.	%0,0,%2\n"			      \
-			"	bne-	1b"				      \
-			: "=&b" (__val), "=m" (*mem)			      \
-			: "b" (mem), "m" (*mem)				      \
-			: "cr0", "memory");				      \
-      __val;								      \
-    })
-
 #define __arch_atomic_decrement_val_64(mem) \
     ({									      \
       __typeof (*(mem)) __val;						      \
diff --git a/sysdeps/unix/sysv/linux/check_pf.c b/sysdeps/unix/sysv/linux/check_pf.c
index fe73fe3ba84db02b1d7188de8f29d55019a69ff3..fdb4d71cb5890272cec88d3af73aef463ff11c8b 100644
--- a/sysdeps/unix/sysv/linux/check_pf.c
+++ b/sysdeps/unix/sysv/linux/check_pf.c
@@ -72,8 +72,8 @@  static uint32_t nl_timestamp;
 uint32_t
 __bump_nl_timestamp (void)
 {
-  if (atomic_increment_val (&nl_timestamp) == 0)
-    atomic_increment (&nl_timestamp);
+  if (atomic_fetch_add_relaxed (&nl_timestamp, 1) + 1 == 0)
+    atomic_fetch_add_relaxed (&nl_timestamp, 1);
 
   return nl_timestamp;
 }
@@ -309,7 +309,7 @@  __check_pf (bool *seen_ipv4, bool *seen_ipv6,
   if (cache_valid_p ())
     {
       data = cache;
-      atomic_increment (&cache->usecnt);
+      atomic_fetch_add_relaxed (&cache->usecnt, 1);
     }
   else
     {
diff --git a/sysdeps/unix/sysv/linux/sh/atomic-machine.h b/sysdeps/unix/sysv/linux/sh/atomic-machine.h
index 3bf84d06bc875fca75b38b5ab01fca6683d390f7..2f45171240ce8d1970b101134a5c838b8a621cb9 100644
--- a/sysdeps/unix/sysv/linux/sh/atomic-machine.h
+++ b/sysdeps/unix/sysv/linux/sh/atomic-machine.h
@@ -299,5 +299,4 @@ 
        abort (); \
      __result; })
 
-#define atomic_increment_and_test(mem) atomic_add_zero((mem), 1)
 #define atomic_decrement_and_test(mem) atomic_add_zero((mem), -1)
diff --git a/sysdeps/x86/atomic-machine.h b/sysdeps/x86/atomic-machine.h
index 2abab58628e88bf2f5fc980d2de6b0dd556f6cd4..97d9d4a194a9d7deddbaa1781a1f9484a3e9232d 100644
--- a/sysdeps/x86/atomic-machine.h
+++ b/sysdeps/x86/atomic-machine.h
@@ -124,9 +124,7 @@ 
 
 #define __arch_add_body(lock, pfx, apfx, mem, value) \
   do {									      \
-    if (__builtin_constant_p (value) && (value) == 1)			      \
-      pfx##_increment (mem);						      \
-    else if (__builtin_constant_p (value) && (value) == -1)		      \
+    if (__builtin_constant_p (value) && (value) == -1)		      \
       pfx##_decrement (mem);						      \
     else if (sizeof (*mem) == 1)					      \
       __asm __volatile (lock "addb %b1, %0"				      \
@@ -200,53 +198,6 @@ 
      __result; })
 
 
-#define __arch_increment_body(lock, pfx, mem) \
-  do {									      \
-    if (sizeof (*mem) == 1)						      \
-      __asm __volatile (lock "incb %b0"					      \
-			: "=m" (*mem)					      \
-			: "m" (*mem));					      \
-    else if (sizeof (*mem) == 2)					      \
-      __asm __volatile (lock "incw %w0"					      \
-			: "=m" (*mem)					      \
-			: "m" (*mem));					      \
-    else if (sizeof (*mem) == 4)					      \
-      __asm __volatile (lock "incl %0"					      \
-			: "=m" (*mem)					      \
-			: "m" (*mem));					      \
-    else if (__HAVE_64B_ATOMICS)					      \
-      __asm __volatile (lock "incq %q0"					      \
-			: "=m" (*mem)					      \
-			: "m" (*mem));					      \
-    else								      \
-      do_add_val_64_acq (pfx, mem, 1);					      \
-  } while (0)
-
-#define atomic_increment(mem) __arch_increment_body (LOCK_PREFIX, __arch, mem)
-
-#define atomic_increment_and_test(mem) \
-  ({ unsigned char __result;						      \
-     if (sizeof (*mem) == 1)						      \
-       __asm __volatile (LOCK_PREFIX "incb %b0; sete %b1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "m" (*mem));					      \
-     else if (sizeof (*mem) == 2)					      \
-       __asm __volatile (LOCK_PREFIX "incw %w0; sete %w1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "m" (*mem));					      \
-     else if (sizeof (*mem) == 4)					      \
-       __asm __volatile (LOCK_PREFIX "incl %0; sete %1"			      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "m" (*mem));					      \
-     else if (__HAVE_64B_ATOMICS)					      \
-       __asm __volatile (LOCK_PREFIX "incq %q0; sete %1"		      \
-			 : "=m" (*mem), "=qm" (__result)		      \
-			 : "m" (*mem));					      \
-     else								      \
-       __atomic_link_error ();					      \
-     __result; })
-
-
 #define __arch_decrement_body(lock, pfx, mem) \
   do {									      \
     if (sizeof (*mem) == 1)						      \