4 ms·
You can implement a 1-bit mutex, by keeping the contended state in a side structure. The side structure need only scale with about the number of contending thre
by throwawaylinux 4y ago
You can implement a 1-bit mutex, by keeping the contended state in a side structure. The side structure need only scale with about the number of contending threads, or approximately the number of total threads, rather than the number of locks.
- gpderetta 4y agoIt is complicated. The side structure for a futex is kernel side, so you want to fast path the userspace to avoid the syscal. On the other hand atomic::wait doesn't necessarily map directly to a futex_wait (nor it can in all cases, futexes are always exactly 32 bits). IIRC, at least on libstdc++, atomic::{wait,notify_one} are user-space fast pathed so they already keep some side structure, so you could get away with calling notify_one unconditionally and use only one bit. I think it is a mistake on libstdc++ part though. I really would like atomic to be a thin wrapper around futex and, as you can't rely on the fast path being available everywhere you need either platform specific code or suboptimal duplicate code. In fact libstdc++ does more as it adds spin-waiting and more optimizations which are not necessarily wanted.
- throwawaylinux 4y agoI'm talking about in general you can implement a 1-bit mutex. Regardless of how you sleep and wake, you make a side structure which contains the contended state of the mutex. In the kernel of course you could just reuse your wait queue structure for that, which is exactly what bit mutexes do in Linux, but that's an implementation detail to my point.