libstd/sync
Atomic operations and futexes for code shared between threads, plus 32- and 64-bit peek/poke on raw addresses. The futex is the futex system call on Linux and WaitOnAddress/WakeByAddress* (KernelBase.dll, Windows 8 and later) on Windows.
#include <libstd/sync>
Includes libstd/macro. Same functions and constant names on Linux and Windows: the system-specific part is in wrapper/linux/syssync and wrapper/windows/syssync (see Linux and Windows). Links the shared library kernelbase.dll (FFI).
Constants
| Name | Linux | Windows | Meaning |
|---|---|---|---|
WAKE_UP_ALL | 2147483647 | 2147483647 |
Functions
Reads a 32-bit integer at ptr.
Writes a 32-bit integer at ptr.
Reads a 64-bit integer (a pointer, a HANDLE) at ptr.
Writes a 64-bit integer at ptr.
Atomically adds delta to the 32-bit value at ptr (lock xadd); returns the previous value.
Atomic compare-and-swap: if *ptr == expected stores desired. Returns the value *ptr had before, in both cases.
Sleeps until the 32-bit value at addr is no longer expected and someone calls sys_futex_wake; a value already different returns immediately. Re-check the condition in a loop after it returns.
Wakes threads sleeping in sys_futex_wait on addr: one if num_waiters is 1, all with WAKE_UP_ALL. Returns how many were woken on Linux, always 0 on Windows (which does not say).