public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Wilco Dijkstra <wilco@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] Use C11 atomics instead of atomic_decrement(_val)
Date: Fri,  9 Sep 2022 14:04:47 +0000 (GMT)	[thread overview]
Message-ID: <20220909140447.500BF385742E@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=a364a3a7090b82ddd30e9209df2af56e781d51e4

commit a364a3a7090b82ddd30e9209df2af56e781d51e4
Author: Wilco Dijkstra <wdijkstr@arm.com>
Date:   Fri Sep 9 14:22:26 2022 +0100

    Use C11 atomics instead of atomic_decrement(_val)
    
    Replace atomic_decrement and atomic_decrement_val with
    atomic_fetch_add_relaxed.
    
    Reviewed-by: DJ Delorie <dj@redhat.com>

Diff:
---
 htl/pt-create.c        | 2 +-
 malloc/malloc.c        | 2 +-
 manual/ipc.texi        | 2 +-
 manual/llio.texi       | 2 +-
 nptl/nptl_setxid.c     | 2 +-
 nptl/pthread_create.c  | 2 +-
 nscd/nscd-client.h     | 2 +-
 nscd/nscd_getai.c      | 2 +-
 nscd/nscd_getgr_r.c    | 2 +-
 nscd/nscd_gethst_r.c   | 2 +-
 nscd/nscd_getpw_r.c    | 2 +-
 nscd/nscd_getserv_r.c  | 2 +-
 nscd/nscd_helper.c     | 2 +-
 nscd/nscd_initgroups.c | 2 +-
 nscd/nscd_netgroup.c   | 4 ++--
 15 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/htl/pt-create.c b/htl/pt-create.c
index ce52ed9f52..36f138b498 100644
--- a/htl/pt-create.c
+++ b/htl/pt-create.c
@@ -262,7 +262,7 @@ failed_starting:
     }
 
   __pthread_setid (pthread->thread, NULL);
-  atomic_decrement (&__pthread_total);
+  atomic_fetch_add_relaxed (&__pthread_total, -1);
 failed_sigstate:
   __pthread_sigstate_destroy (pthread);
 failed_setup:
diff --git a/malloc/malloc.c b/malloc/malloc.c
index aea4993f1b..bfe1955737 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3033,7 +3033,7 @@ munmap_chunk (mchunkptr p)
       || __glibc_unlikely (!powerof2 (mem & (pagesize - 1))))
     malloc_printerr ("munmap_chunk(): invalid pointer");
 
-  atomic_decrement (&mp_.n_mmaps);
+  atomic_fetch_add_relaxed (&mp_.n_mmaps, -1);
   atomic_fetch_add_relaxed (&mp_.mmapped_mem, -total_size);
 
   /* If munmap failed the process virtual memory address space is in a
diff --git a/manual/ipc.texi b/manual/ipc.texi
index 081b98fe29..46c049c3da 100644
--- a/manual/ipc.texi
+++ b/manual/ipc.texi
@@ -89,7 +89,7 @@ by @theglibc{}.
 @c
 @c Given the use atomic operations this function seems
 @c to be AS-safe.  It is AC-unsafe because there is still
-@c a window between atomic_decrement and the pthread_push
+@c a window between atomic_fetch_add_relaxed and the pthread_push
 @c of the handler that undoes that operation.  A cancellation
 @c at that point would fail to remove the process from the
 @c waiters count.
diff --git a/manual/llio.texi b/manual/llio.texi
index e55b02d8bd..0dfcdad434 100644
--- a/manual/llio.texi
+++ b/manual/llio.texi
@@ -2569,7 +2569,7 @@ aiocb64}, since the LFS transparently replaces the old interface.
 @c      lll_lock (pd->lock) @asulock @aculock
 @c      atomic_increment ok
 @c      clone ok
-@c      atomic_decrement ok
+@c      atomic_fetch_add_relaxed ok
 @c      atomic_exchange_acq ok
 @c      lll_futex_wake ok
 @c      deallocate_stack dup
diff --git a/nptl/nptl_setxid.c b/nptl/nptl_setxid.c
index aa863c7ea8..e709822b9b 100644
--- a/nptl/nptl_setxid.c
+++ b/nptl/nptl_setxid.c
@@ -88,7 +88,7 @@ __nptl_setxid_sighandler (int sig, siginfo_t *si, void *ctx)
   self->setxid_futex = 1;
   futex_wake (&self->setxid_futex, 1, FUTEX_PRIVATE);
 
-  if (atomic_decrement_val (&xidcmd->cntr) == 0)
+  if (atomic_fetch_add_relaxed (&xidcmd->cntr, -1) == 1)
     futex_wake ((unsigned int *) &xidcmd->cntr, 1, FUTEX_PRIVATE);
 }
 libc_hidden_def (__nptl_setxid_sighandler)
diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
index 308db65cd4..e7e4ede6b2 100644
--- a/nptl/pthread_create.c
+++ b/nptl/pthread_create.c
@@ -861,7 +861,7 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr,
 	 NOTES above).  */
 
       /* Oops, we lied for a second.  */
-      atomic_decrement (&__nptl_nthreads);
+      atomic_fetch_add_relaxed (&__nptl_nthreads, -1);
 
       /* Free the resources.  */
       __nptl_deallocate_stack (pd);
diff --git a/nscd/nscd-client.h b/nscd/nscd-client.h
index fee2a15dcc..ca9e6def1a 100644
--- a/nscd/nscd-client.h
+++ b/nscd/nscd-client.h
@@ -421,7 +421,7 @@ __nscd_drop_map_ref (struct mapped_database *map, int *gc_cycle)
 	  return -1;
 	}
 
-      if (atomic_decrement_val (&map->counter) == 0)
+      if (atomic_fetch_add_relaxed (&map->counter, -1) == 1)
 	__nscd_unmap (map);
     }
 
diff --git a/nscd/nscd_getai.c b/nscd/nscd_getai.c
index a99a4d8142..8e4650ebae 100644
--- a/nscd/nscd_getai.c
+++ b/nscd/nscd_getai.c
@@ -198,7 +198,7 @@ __nscd_getai (const char *key, struct nscd_ai_result **result, int *h_errnop)
       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
 	{
 	  /* nscd is just running gc now.  Disable using the mapping.  */
-	  if (atomic_decrement_val (&mapped->counter) == 0)
+	  if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
 	    __nscd_unmap (mapped);
 	  mapped = NO_MAPPING;
 	}
diff --git a/nscd/nscd_getgr_r.c b/nscd/nscd_getgr_r.c
index 3636c031ec..bde3b588a0 100644
--- a/nscd/nscd_getgr_r.c
+++ b/nscd/nscd_getgr_r.c
@@ -312,7 +312,7 @@ nscd_getgr_r (const char *key, size_t keylen, request_type type,
       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
 	{
 	  /* nscd is just running gc now.  Disable using the mapping.  */
-	  if (atomic_decrement_val (&mapped->counter) == 0)
+	  if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
 	    __nscd_unmap (mapped);
 	  mapped = NO_MAPPING;
 	}
diff --git a/nscd/nscd_gethst_r.c b/nscd/nscd_gethst_r.c
index 9becb62033..31d13580a1 100644
--- a/nscd/nscd_gethst_r.c
+++ b/nscd/nscd_gethst_r.c
@@ -440,7 +440,7 @@ nscd_gethst_r (const char *key, size_t keylen, request_type type,
       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
 	{
 	  /* nscd is just running gc now.  Disable using the mapping.  */
-	  if (atomic_decrement_val (&mapped->counter) == 0)
+	  if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
 	    __nscd_unmap (mapped);
 	  mapped = NO_MAPPING;
 	}
diff --git a/nscd/nscd_getpw_r.c b/nscd/nscd_getpw_r.c
index 20986f4433..82fdd17d8c 100644
--- a/nscd/nscd_getpw_r.c
+++ b/nscd/nscd_getpw_r.c
@@ -225,7 +225,7 @@ nscd_getpw_r (const char *key, size_t keylen, request_type type,
       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
 	{
 	  /* nscd is just running gc now.  Disable using the mapping.  */
-	  if (atomic_decrement_val (&mapped->counter) == 0)
+	  if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
 	    __nscd_unmap (mapped);
 	  mapped = NO_MAPPING;
 	}
diff --git a/nscd/nscd_getserv_r.c b/nscd/nscd_getserv_r.c
index 42b875d024..de843b3363 100644
--- a/nscd/nscd_getserv_r.c
+++ b/nscd/nscd_getserv_r.c
@@ -365,7 +365,7 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
 	{
 	  /* nscd is just running gc now.  Disable using the mapping.  */
-	  if (atomic_decrement_val (&mapped->counter) == 0)
+	  if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
 	    __nscd_unmap (mapped);
 	  mapped = NO_MAPPING;
 	}
diff --git a/nscd/nscd_helper.c b/nscd/nscd_helper.c
index 8e66fa2548..0651817a99 100644
--- a/nscd/nscd_helper.c
+++ b/nscd/nscd_helper.c
@@ -390,7 +390,7 @@ __nscd_get_mapping (request_type type, const char *key,
   struct mapped_database *oldval = *mappedp;
   *mappedp = result;
 
-  if (oldval != NULL && atomic_decrement_val (&oldval->counter) == 0)
+  if (oldval != NULL && atomic_fetch_add_relaxed (&oldval->counter, -1) == 1)
     __nscd_unmap (oldval);
 
   return result;
diff --git a/nscd/nscd_initgroups.c b/nscd/nscd_initgroups.c
index dfce76a060..47b6deb069 100644
--- a/nscd/nscd_initgroups.c
+++ b/nscd/nscd_initgroups.c
@@ -166,7 +166,7 @@ __nscd_getgrouplist (const char *user, gid_t group, long int *size,
       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
 	{
 	  /* nscd is just running gc now.  Disable using the mapping.  */
-	  if (atomic_decrement_val (&mapped->counter) == 0)
+	  if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
 	    __nscd_unmap (mapped);
 	  mapped = NO_MAPPING;
 	}
diff --git a/nscd/nscd_netgroup.c b/nscd/nscd_netgroup.c
index 7e51dd3d94..11b7f3214c 100644
--- a/nscd/nscd_netgroup.c
+++ b/nscd/nscd_netgroup.c
@@ -148,7 +148,7 @@ __nscd_setnetgrent (const char *group, struct __netgrent *datap)
       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
 	{
 	  /* nscd is just running gc now.  Disable using the mapping.  */
-	  if (atomic_decrement_val (&mapped->counter) == 0)
+	  if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
 	    __nscd_unmap (mapped);
 	  mapped = NO_MAPPING;
 	}
@@ -272,7 +272,7 @@ __nscd_innetgr (const char *netgroup, const char *host, const char *user,
       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
 	{
 	  /* nscd is just running gc now.  Disable using the mapping.  */
-	  if (atomic_decrement_val (&mapped->counter) == 0)
+	  if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
 	    __nscd_unmap (mapped);
 	  mapped = NO_MAPPING;
 	}

                 reply	other threads:[~2022-09-09 14:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220909140447.500BF385742E@sourceware.org \
    --to=wilco@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).