Message ID | AS4PR08MB79012E086E6E43404D3CFFB683409@AS4PR08MB7901.eurprd08.prod.outlook.com |
---|---|
State | New |
Headers | show |
Series | Use C11 atomics instead of atomic_decrement(_val) | expand |
atomic_decrement returns the NEW value atomic_fetch_add_relaxed returns the OLD value Wilco Dijkstra via Libc-alpha <libc-alpha@sourceware.org> writes: > - atomic_decrement (&__pthread_total); > + atomic_fetch_add_relaxed (&__pthread_total, -1); Ok. > - atomic_decrement (&mp_.n_mmaps); > + atomic_fetch_add_relaxed (&mp_.n_mmaps, -1); Ok. > +++ 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 Ok. > +++ 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_fetch_add_relaxed ok > @c clone ok > -@c atomic_decrement ok > +@c atomic_fetch_add_relaxed ok Ok. > - if (atomic_decrement_val (&xidcmd->cntr) == 0) > + if (atomic_fetch_add_relaxed (&xidcmd->cntr, -1) == 1) Ok. See note at beginning for why ;-) > /* Oops, we lied for a second. */ > - atomic_decrement (&__nptl_nthreads); > + atomic_fetch_add_relaxed (&__nptl_nthreads, -1); Ok. > - if (atomic_decrement_val (&map->counter) == 0) > + if (atomic_fetch_add_relaxed (&map->counter, -1) == 1) Ok. > - if (atomic_decrement_val (&mapped->counter) == 0) > + if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1) Ok. > - if (atomic_decrement_val (&mapped->counter) == 0) > + if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1) Ok. > - if (atomic_decrement_val (&mapped->counter) == 0) > + if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1) Ok. > - if (atomic_decrement_val (&mapped->counter) == 0) > + if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1) Ok. > - if (atomic_decrement_val (&mapped->counter) == 0) > + if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1) Ok. > - if (oldval != NULL && atomic_decrement_val (&oldval->counter) == 0) > + if (oldval != NULL && atomic_fetch_add_relaxed (&oldval->counter, -1) == 1) Ok. > - if (atomic_decrement_val (&mapped->counter) == 0) > + if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1) Ok. > - if (atomic_decrement_val (&mapped->counter) == 0) > + if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1) Ok. > - if (atomic_decrement_val (&mapped->counter) == 0) > + if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1) Ok. LGTM. Reviewed-by: DJ Delorie <dj@redhat.com>
Hi DJ, Thanks for the review. > atomic_decrement returns the NEW value > atomic_fetch_add_relaxed returns the OLD value Indeed, this series of patches replaces the many different variations of atomic add with a single one that directly maps to the GCC builtin (and the underlying atomic operation). GCC also has __atomic_add_fetch which returns the new value, so once all this code has been cleaned up we could add support for that. Cheers, Wilco
diff --git a/htl/pt-create.c b/htl/pt-create.c index 14f02cd2b8a19e8581a170dfba2b948ef8304203..5d37edbbffb16b7cbb6db133016653227fc45f47 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 adafef9d5a7b3c323bd6307c8d15f7f1921f0192..4bd95d81ae51c8ad33d8e30c83f681688c41723d 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 f7cbdc3e09b0b4aea9a96ddcaf571c474024cc32..be74664af9c146dd3024b080e90b7ca9834b14ef 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 eba9a0e6593f17116b41ff302cfca05846727155..1b801ee817db2935d8866894be23ffa516690ca3 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_fetch_add_relaxed ok @c clone ok -@c atomic_decrement ok +@c atomic_fetch_add_relaxed ok @c atomic_exchange_acquire ok @c lll_futex_wake ok @c deallocate_stack dup diff --git a/nptl/nptl_setxid.c b/nptl/nptl_setxid.c index 3b7e2d434abe8a15145349d1a08a4e706061c74d..301809d200d1ac6c89fbf100e553c5713f326ed7 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 ada3fdd4d440c3fdcf4567734a452c8ba4d0fce1..32ae2f4b2fcb81d057c6cea68182af20f8a4e1f9 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 fee2a15dcc323258cecd45c5fb8e472da9161792..ca9e6def1a88ff14c5e8b39f0e236aa8a30f95ae 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 a99a4d8142b89521f3ca1759f42d5d88e1103f61..8e4650ebaedc6d79bd5ccda6721551b0227dbbb5 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 3636c031ec9403c7b447ebc52eecb2c60d74740f..bde3b588a01274730e1b1d817dfff62fd4f60c15 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 9becb620335d14d7becebe6c29ad96cc4e7463ee..31d13580a11e64792d6f02ecd7437884d4436eb3 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 20986f44332fca42d2f43794b4c365f6b3968cae..82fdd17d8c39287a9d450ff25802116028dc9475 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 42b875d024c674a6c46dd28be8308a8d5d650ca6..de843b33631e2c57ac4e0192c6f1027c586cf329 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 d3e05e272a854e13c0e4d18594ea57336b8db2bf..fc41bfdb6eebb880d6132ea5cf409ca657570f82 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 dfce76a06090d5bd2869f8a239bdef51866bd854..47b6deb0699eaa984691388d146ae5e01ddd83ab 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 7e51dd3d941e383ab97bc0f70dd4dda65c340e0b..11b7f3214c2fb08f2e08df86f0d984e7bfc9bda2 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; }