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

NameLinuxWindowsMeaning
WAKE_UP_ALL21474836472147483647

Functions

def peek_int(ptr: str) -> int

Reads a 32-bit integer at ptr.

def poke_int(ptr: str, value: int) -> None

Writes a 32-bit integer at ptr.

def peek_long(ptr: str) -> long

Reads a 64-bit integer (a pointer, a HANDLE) at ptr.

def poke_long(ptr: str, value: long) -> None

Writes a 64-bit integer at ptr.

def atomic_add32(ptr: str, delta: int) -> int

Atomically adds delta to the 32-bit value at ptr (lock xadd); returns the previous value.

def atomic_cas32(ptr: str, expected: int, desired: int) -> int

Atomic compare-and-swap: if *ptr == expected stores desired. Returns the value *ptr had before, in both cases.

def sys_futex_wait(addr: str, expected: int) -> int

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.

def sys_futex_wake(addr: str, num_waiters: int) -> int

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).