|
|
|
|
|
by viega
299 days ago
|
|
You actually issue the `futex` system call to get yourself on the wait queue tied to the memory address. It separates out the waiting from the locking. And that can absolutely save a bunch of system calls, especially vs. polling mixed with `sleep()` or similar. |
|
It does not, in fact the two are fundamentally inseparable and the state of the memory address must be treated atomically with the waiting state. The magic of futex is that you can use a hardware atomic operation (c.f. lock cmpxchg on x86) to get the lock in the common/uncontended case, but if you have to wait you need to tell the kernel both that you need to wait and the address on which you're waiting, so it can use the same hardware interlocks along with its own state locking to put you to sleep race-free.