[PATCH v1] Revert "futex.2: Make the example use C11 atomics rather than GCC builtins"
Alejandro Colomar
alx@kernel.org
Sat Mar 1 15:39:48 GMT 2025
This reverts commit 915c4ba36f9f71db88e7e7913b845d046996f485.
There were several conflicts during the revert, and I decided to keep
some of the changes from that commit, but the main part, which is using
C11 atomics, is gone.
It transformed a correct program into one that had important bugs, such
as passing incorrectly qualified variables. Since nobody here
(including gcc-help@ and glibc-help@) seems to understand C11 atomics
enough to fix the code, let's revert to a known-good state.
Fixes: 915c4ba36f9f (2018-11-17; "futex.2: Make the example use C11 atomics rather than GCC builtins")
Reported-by: nrk <nrk@disroot.org>
Reported-by: rahl <rahl@everwhe.re>
Reported-by: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: GCC <gcc-help@gcc.gnu.org>
Cc: glibc <libc-help@sourceware.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
man/man2/futex.2 | 20 +++++++-------------
1 file changed, 7 insertions(+), 13 deletions(-)
diff --git a/man/man2/futex.2 b/man/man2/futex.2
index 70c3956ab..00c7febd0 100644
--- a/man/man2/futex.2
+++ b/man/man2/futex.2
@@ -1796,7 +1796,6 @@ .SS Program source
#include <err.h>
#include <errno.h>
#include <linux/futex.h>
-#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -1822,13 +1821,12 @@ .SS Program source
static void
fwait(uint32_t *futexp)
{
- long s;
- const uint32_t one = 1;
+ long s;
\&
- /* atomic_compare_exchange_strong(ptr, oldval, newval)
- atomically performs the equivalent of:
+ /* __sync_bool_compare_and_swap(ptr, oldval, newval) is a gcc
+ built-in function. It atomically performs the equivalent of:
\&
- if (*ptr == *oldval)
+ if (*ptr == oldval)
*ptr = newval;
\&
It returns true if the test yielded true and *ptr was updated. */
@@ -1836,7 +1834,7 @@ .SS Program source
while (1) {
\&
/* Is the futex available? */
- if (atomic_compare_exchange_strong(futexp, &one, 0))
+ if (__sync_bool_compare_and_swap(futexp, 1, 0))
break; /* Yes */
\&
/* Futex is not available; wait. */
@@ -1854,13 +1852,9 @@ .SS Program source
static void
fpost(uint32_t *futexp)
{
- long s;
- const uint32_t zero = 0;
+ long s;
\&
- /* atomic_compare_exchange_strong() was described
- in comments above. */
-\&
- if (atomic_compare_exchange_strong(futexp, &zero, 1)) {
+ if (__sync_bool_compare_and_swap(futexp, 0, 1)) {
s = futex(futexp, FUTEX_WAKE, 1, NULL, NULL, 0);
if (s == \-1)
err(EXIT_FAILURE, "futex\-FUTEX_WAKE");
Range-diff against v0:
-: --------- > 1: 160a90299 Revert "futex.2: Make the example use C11 atomics rather than GCC builtins"
base-commit: 46b7bcaae3cbd18b3022caa3ddd52e7be66bb21a
--
2.47.2
More information about the Libc-help
mailing list