public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Use C11 atomics instead atomic_add(_zero)
@ 2022-09-08 14:04 Wilco Dijkstra
  2022-09-08 19:49 ` DJ Delorie
  0 siblings, 1 reply; 2+ messages in thread
From: Wilco Dijkstra @ 2022-09-08 14:04 UTC (permalink / raw)
  To: 'GNU C Library'; +Cc: Adhemerval Zanella

Replace atomic_add and atomic_add_zero with atomic_fetch_add_relaxed.

Passes regress on AArch64.

---

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 6402cf94ea8ae724d86d2a04fc7d42e3af174564..adafef9d5a7b3c323bd6307c8d15f7f1921f0192 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3034,7 +3034,7 @@ munmap_chunk (mchunkptr p)
     malloc_printerr ("munmap_chunk(): invalid pointer");
 
   atomic_decrement (&mp_.n_mmaps);
-  atomic_add (&mp_.mmapped_mem, -total_size);
+  atomic_fetch_add_relaxed (&mp_.mmapped_mem, -total_size);
 
   /* If munmap failed the process virtual memory address space is in a
      bad shape.  Just leave the block hanging around, the process will
diff --git a/sysdeps/unix/sysv/linux/check_pf.c b/sysdeps/unix/sysv/linux/check_pf.c
index fe73fe3ba84db02b1d7188de8f29d55019a69ff3..4d486ca9b5026c7a7950bd7c7155212966df3c44 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);
 


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Use C11 atomics instead atomic_add(_zero)
  2022-09-08 14:04 [PATCH] Use C11 atomics instead atomic_add(_zero) Wilco Dijkstra
@ 2022-09-08 19:49 ` DJ Delorie
  0 siblings, 0 replies; 2+ messages in thread
From: DJ Delorie @ 2022-09-08 19:49 UTC (permalink / raw)
  To: Wilco Dijkstra; +Cc: libc-alpha

Wilco Dijkstra via Libc-alpha <libc-alpha@sourceware.org> writes:
> -  atomic_add (&mp_.mmapped_mem, -total_size);
> +  atomic_fetch_add_relaxed (&mp_.mmapped_mem, -total_size);

Ok.

> -      atomic_add (&noai6ai_cached.usecnt, 2);
> +      atomic_fetch_add_relaxed (&noai6ai_cached.usecnt, 2);

Ok.

> -	  && atomic_add_zero (&olddata->usecnt, -1))
> +	  && atomic_fetch_add_relaxed (&olddata->usecnt, -1) == 1)

atomic_add_zero includes the comparison, so Ok.

> -      if (atomic_add_zero (&data->usecnt, -1))
> +      if (atomic_fetch_add_relaxed (&data->usecnt, -1) == 1)

Ok.

LGTM
Reviewed-by: DJ Delorie <dj@redhat.com>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-08 19:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-08 14:04 [PATCH] Use C11 atomics instead atomic_add(_zero) Wilco Dijkstra
2022-09-08 19:49 ` DJ Delorie

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).