From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: MSB in rwlock?
Date: Tue, 22 Jan 2019 19:22:53 -0500 [thread overview]
Message-ID: <20190123002253.GT23599@brightrain.aerifal.cx> (raw)
In-Reply-To: <20190122202847.GB23924@voyager>
On Tue, Jan 22, 2019 at 09:28:47PM +0100, Markus Wichmann wrote:
> Hi all,
>
> would someone kindly explain what the highest bit in the rwlock value is
> doing? As far as I can see, it is set whenever one of the locking
> functions enters a contended case, and cleared when the unlock function
> sets the lock to 0 (so when the last reader or the writer unlocks the
> rwlock). But at any other point the bit is masked out, AFAICS. So what
> is it doing? Or is it just a vestige that can be done away with?
In most of the locking primitives, the high bit of the lock word is
used as a maybe-has-waiters flag. It looks like it's redundant with
the waiters count field, but it's not.
If we used *only* this bit, then threads obtaining the lock after
contending for it would have to set it to true (since they couldn't
know if they were the last waiter), thereby performing a
potentially-useless futex wake when they subsequently unlock it.
If on the other hand we used only the waiters count, there would be a
race condition at unlock time: between the time the waiter count was
read and the time the lock word was unlocked, a new waiter could
arrive, and go to sleep on the futex without getting woken up. (The
unlocking thread cannot check the waiter count *after* unlocking the
lock word, since the lock object may no longer exist at that point.)
Rich
prev parent reply other threads:[~2019-01-23 0:22 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-22 20:28 Markus Wichmann
2019-01-23 0:22 ` Rich Felker [this message]
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=20190123002253.GT23599@brightrain.aerifal.cx \
--to=dalias@libc.org \
--cc=musl@lists.openwall.com \
/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.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
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 NNTP newsgroup(s).