about summary refs log tree commit diff
path: root/nptl
diff options
context:
space:
mode:
authorWilco Dijkstra <wdijkstr@arm.com>2022-09-22 15:40:37 +0100
committerWilco Dijkstra <wdijkstr@arm.com>2022-09-23 15:59:56 +0100
commit4a07fbb689eeec30e7d71a0d144c26e0d1e424ac (patch)
tree85d53e165fb64b144ce9ac3018cd90afdd70bf4f /nptl
parentd1babeb32de5dae8893c640bd925357b218d846c (diff)
downloadglibc-4a07fbb689eeec30e7d71a0d144c26e0d1e424ac.tar.gz
glibc-4a07fbb689eeec30e7d71a0d144c26e0d1e424ac.tar.xz
glibc-4a07fbb689eeec30e7d71a0d144c26e0d1e424ac.zip
Use C11 atomics instead of atomic_decrement_and_test
Replace atomic_decrement_and_test with atomic_fetch_add_relaxed.
These are simple counters which do not protect any shared data from
concurrent accesses. Also remove the unused file cond-perf.c.

Passes regress on AArch64.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
Diffstat (limited to 'nptl')
-rw-r--r--nptl/cond-perf.c103
-rw-r--r--nptl/pthread_create.c2
2 files changed, 1 insertions, 104 deletions
diff --git a/nptl/cond-perf.c b/nptl/cond-perf.c
deleted file mode 100644
index 9c9488e274..0000000000
--- a/nptl/cond-perf.c
+++ /dev/null
@@ -1,103 +0,0 @@
-#include <pthread.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <atomic.h>
-
-static pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
-static pthread_mutex_t mut1 = PTHREAD_MUTEX_INITIALIZER;
-
-static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
-static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
-
-static bool last_round;
-static int ntogo;
-static bool alldone;
-
-
-static void *
-cons (void *arg)
-{
-  pthread_mutex_lock (&mut1);
-
-  do
-    {
-      if (atomic_decrement_and_test (&ntogo))
-	{
-	  pthread_mutex_lock (&mut2);
-	  alldone = true;
-	  pthread_cond_signal (&cond2);
-	  pthread_mutex_unlock (&mut2);
-	}
-
-      pthread_cond_wait (&cond1, &mut1);
-    }
-  while (! last_round);
-
-  pthread_mutex_unlock (&mut1);
-
-  return NULL;
-}
-
-
-int
-main (int argc, char *argv[])
-{
-  int opt;
-  int err;
-  int nthreads = 10;
-  int nrounds = 100;
-  bool keeplock = false;
-
-  while ((opt = getopt (argc, argv, "n:r:k")) != -1)
-    switch (opt)
-      {
-      case 'n':
-	nthreads = atol (optarg);
-	break;
-      case 'r':
-	nrounds = atol (optarg);
-	break;
-      case 'k':
-	keeplock = true;
-	break;
-      }
-
-  ntogo = nthreads;
-
-  pthread_t th[nthreads];
-  int i;
-  for (i = 0; __builtin_expect (i < nthreads, 1); ++i)
-    if (__glibc_unlikely ((err = pthread_create (&th[i], NULL, cons, (void *) (long) i)) != 0))
-      printf ("pthread_create: %s\n", strerror (err));
-
-  for (i = 0; __builtin_expect (i < nrounds, 1); ++i)
-    {
-      pthread_mutex_lock (&mut2);
-      while (! alldone)
-	pthread_cond_wait (&cond2, &mut2);
-      pthread_mutex_unlock (&mut2);
-
-      pthread_mutex_lock (&mut1);
-      if (! keeplock)
-	pthread_mutex_unlock (&mut1);
-
-      ntogo = nthreads;
-      alldone = false;
-      if (i + 1 >= nrounds)
-	last_round = true;
-
-      pthread_cond_broadcast (&cond1);
-
-      if (keeplock)
-	pthread_mutex_unlock (&mut1);
-    }
-
-  for (i = 0; i < nthreads; ++i)
-    if ((err = pthread_join (th[i], NULL)) != 0)
-      printf ("pthread_create: %s\n", strerror (err));
-
-  return 0;
-}
diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
index 0df67484e2..54afee5976 100644
--- a/nptl/pthread_create.c
+++ b/nptl/pthread_create.c
@@ -489,7 +489,7 @@ start_thread (void *arg)
      the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE.  */
   atomic_fetch_or_relaxed (&pd->cancelhandling, EXITING_BITMASK);
 
-  if (__glibc_unlikely (atomic_decrement_and_test (&__nptl_nthreads)))
+  if (__glibc_unlikely (atomic_fetch_add_relaxed (&__nptl_nthreads, -1) == 1))
     /* This was the last thread.  */
     exit (0);