From: malteskarupke@fastmail.fm
To: libc-alpha@sourceware.org
Cc: Malte Skarupke <malteskarupke@fastmail.fm>
Subject: [PATCH 2/2] nptl: Simplifying condvar-internal mutex
Date: Sat, 15 Oct 2022 15:53:05 -0400 [thread overview]
Message-ID: <20221015195305.1322087-2-malteskarupke@fastmail.fm> (raw)
In-Reply-To: <20221015195305.1322087-1-malteskarupke@fastmail.fm>
From: Malte Skarupke <malteskarupke@fastmail.fm>
The condvar-internal mutex no longer shares its 32 bit futex with other
information. There is no reason to keep the complexity, so simplify.
---
nptl/pthread_cond_common.c | 24 ++++++++++--------------
1 file changed, 10 insertions(+), 14 deletions(-)
diff --git a/nptl/pthread_cond_common.c b/nptl/pthread_cond_common.c
index b9043a218b..efc7999cfe 100644
--- a/nptl/pthread_cond_common.c
+++ b/nptl/pthread_cond_common.c
@@ -32,11 +32,11 @@
static void __attribute__ ((unused))
__condvar_acquire_lock (pthread_cond_t *cond, int private)
{
- unsigned int s = atomic_load_relaxed (&cond->__data.__lock);
- while ((s & 3) == 0)
+ unsigned int *lock = &cond->__data.__lock;
+ unsigned int s = atomic_load_relaxed (lock);
+ while (s == 0)
{
- if (atomic_compare_exchange_weak_acquire (&cond->__data.__lock,
- &s, s | 1))
+ if (atomic_compare_exchange_weak_acquire (lock, &s, 1))
return;
/* TODO Spinning and back-off. */
}
@@ -45,21 +45,19 @@ __condvar_acquire_lock (pthread_cond_t *cond, int private)
from not acquired. */
while (1)
{
- while ((s & 3) != 2)
+ while (s != 2)
{
- if (atomic_compare_exchange_weak_acquire
- (&cond->__data.__lock, &s, (s & ~(unsigned int) 3) | 2))
+ if (atomic_compare_exchange_weak_acquire (lock, &s, 2))
{
- if ((s & 3) == 0)
+ if (s == 0)
return;
break;
}
/* TODO Back off. */
}
- futex_wait_simple (&cond->__data.__lock,
- (s & ~(unsigned int) 3) | 2, private);
+ futex_wait_simple (lock, 2, private);
/* Reload so we see a recent value. */
- s = atomic_load_relaxed (&cond->__data.__lock);
+ s = atomic_load_relaxed (lock);
}
}
@@ -67,9 +65,7 @@ __condvar_acquire_lock (pthread_cond_t *cond, int private)
static void __attribute__ ((unused))
__condvar_release_lock (pthread_cond_t *cond, int private)
{
- if ((atomic_fetch_and_release (&cond->__data.__lock,
- ~(unsigned int) 3) & 3)
- == 2)
+ if (atomic_exchange_release (&cond->__data.__lock, 0) == 2)
futex_wake (&cond->__data.__lock, 1, private);
}
--
2.25.1
next prev parent reply other threads:[~2022-10-15 19:53 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-15 19:53 [PATCH 1/2] nptl: Simplify condition variables to fix pthread_cond_signal (BZ 25847) malteskarupke
2022-10-15 19:53 ` malteskarupke [this message]
2022-10-24 3:50 ` Carlos O'Donell
2022-10-24 11:17 ` Malte Skarupke
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=20221015195305.1322087-2-malteskarupke@fastmail.fm \
--to=malteskarupke@fastmail.fm \
--cc=libc-alpha@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).