Which OS / platforms implement wait morphing optimization?
Asked Answered
N

1

11

Which major OS / platforms implement wait morphing?

This question came up when I noticed that there's no clearcut best practice about whether one should signal a condition variable with mutex locked or not. A typical recommendation is to signal while holding the lock unless profiling shows a substantial performance improvement overhead from unlocking (by removing an extra context switch).

IIUC, the only disadvantage of holding the lock while signalling is the extra two context switches; the advantages are the lower risk of a bug, and easier to achieve real-time predictability.

So, it seems that if wait morphing is implemented, the practice of holding the lock while signalling is strictly better.

Nursery answered 18/7, 2017 at 10:10 Comment(4)
There is uncertain evidence at news.ycombinator.com/item?id=11893756 that Linux implements it. The pthreads spec supports wait morphing, and the link claims that Linux futexes, which are behind condition variables, also support it.Poppy
From that comment "You are thinking of FUTEX_*_REQUEUE (see the man page for details). It will move (some of) the waiters from the condvar futex to the mutex futex. IIRC, the optimization is called wait morphing. IIRC it is so hard to get it right in practice (the number of races and corner cases is staggering) that recent libc versions might have stopped doing it. I might be misremembering though."Bixby
Also I tried to check if macOS does this (their libpthreads implementation is open source) but it's not really well commented and it's hard to follow. The only relevant line may be under "No need to signal if the CV is already balanced." but it's not clear what "balanced means here"; my gut guess is that this is unrelated and macOS's libpthreads does not implement wait morphing (but I'd love to be corrected)Bixby
If you need guaranteed wait morphing, I believe absl::mutex (which reimplements CV on top of lower-level wait queue abstraction) does do this.Bixby
P
6

It's not supported on Linux. Mark Mossberg investigated it here, and it still holds true as of glibc master today (June 9, 2022).

Poppy answered 9/6, 2022 at 20:10 Comment(1)
Mark says: " fun fact: There's a widely assumed optimization with glibc condition variables ("wait morphing"/requeue) that was silently removed in 2016. Few seem to be aware of this, including the creator of libc++. It's beyond my ability to dig into why and what the implications really are, "Nursery

© 2022 - 2024 — McMap. All rights reserved.