Date: Tue, 22 Jan 2019 19:22:53 -0500 From: Rich Felker <dalias@...c.org> To: musl@...ts.openwall.com Subject: Re: MSB in rwlock? 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
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.