![]() |
Gaia-ECS v0.9.3
A simple and powerful entity component system
|
An implementation of a simple futex (fast userspace mutex). Only wait and wake are implemented. More...
#include <futex.h>
Public Types | |
| enum class | Result { Change , WakeUp } |
Static Public Member Functions | |
| static Result | wait (const std::atomic_uint32_t *pFutexValue, uint32_t expected, uint32_t waitMask) |
| Suspends the caller on the futex while its value remains expected. | |
| static uint32_t | wake (const std::atomic_uint32_t *pFutexValue, uint32_t wakeCount, uint32_t wakeMask=detail::WaitMaskAny) |
| Wakes up to wakeCount waiters whose waitMask matches wakeMask. | |
An implementation of a simple futex (fast userspace mutex). Only wait and wake are implemented.
The main advantage of futex is performance. It avoids kernel involvement in uncontended cases. When there’s no contention, futexes allow threads to lock and unlock in userspace without entering the kernel, making operations significantly faster and reducing context-switch overhead. Only when there is contention does a futex use the kernel to put threads to sleep and wake them up, resulting in a hybrid model that is more efficient than mutexes, which always require kernel calls.
TODO: Consider using WaitOnAddress for Windows, futex call for Linux etc. The current solution is platform-agnostic but platform-specific solutions might be more performant.
|
strong |
|
inlinestatic |
Suspends the caller on the futex while its value remains expected.
| pFutexValue | Target futex |
| expected | Expected futex value |
| waitMask | Mask of waiters to wait for |
|
inlinestatic |
Wakes up to wakeCount waiters whose waitMask matches wakeMask.
| pFutexValue | Target futex |
| wakeCount | How many waiters are supposed to make up |
| wakeMask | Mask of callers to wake |