//- futex #if PLATFORM_LINUX internal void futex_wait(u32 *address, u32 expected, s64 timeout_ns) { struct timespec timeout = {}; struct timespec *timeout_ptr = NULL; if(timeout_ns >= 0) { timeout.tv_sec = timeout_ns / 1000000000ll; timeout.tv_nsec = timeout_ns % 1000000000ll; timeout_ptr = &timeout; } // NOTE: EAGAIN (value already changed), EINTR and ETIMEDOUT are all fine, the caller re-checks syscall(SYS_futex, address, FUTEX_WAIT_PRIVATE, expected, timeout_ptr, NULL, 0); } internal void futex_wake_all(u32 *address) { syscall(SYS_futex, address, FUTEX_WAKE_PRIVATE, INT32_MAX, NULL, NULL, 0); } #elif PLATFORM_APPLE internal void futex_wait(u32 *address, u32 expected, s64 timeout_ns) { // NOTE: timeout in microseconds, 0 = forever. spurious returns are fine, the caller re-checks u32 timeout_us = 0; if(timeout_ns >= 0) timeout_us = (u32)Max(Min((timeout_ns + 999) / 1000, (s64)UINT32_MAX), (s64)1); __ulock_wait(DARWIN_UL_COMPARE_AND_WAIT | DARWIN_ULF_NO_ERRNO, address, expected, timeout_us); } internal void futex_wake_all(u32 *address) { __ulock_wake(DARWIN_UL_COMPARE_AND_WAIT | DARWIN_ULF_WAKE_ALL | DARWIN_ULF_NO_ERRNO, address, 0); } #elif PLATFORM_WINDOWS internal void futex_wait(u32 *address, u32 expected, s64 timeout_ns) { // NOTE: like the futex, spurious and early returns are fine, the caller re-checks DWORD timeout_ms = (timeout_ns < 0) ? INFINITE : (DWORD)((timeout_ns + 999999ll) / 1000000ll); WaitOnAddress((volatile VOID *)address, &expected, sizeof(expected), timeout_ms); } internal void futex_wake_all(u32 *address) { WakeByAddressAll((PVOID)address); } #endif internal u32 signal_snapshot(Signal *signal) { return AtomicLoad(&signal->sequence); } internal void signal_notify(Signal *signal) { AtomicAdd(&signal->sequence, 1); futex_wake_all(&signal->sequence); } internal void signal_wait(Signal *signal, u32 snapshot, s64 timeout_ns) { futex_wait(&signal->sequence, snapshot, timeout_ns); } //- spin lock internal void spin_lock(SpinLock *lock) { for(;;) { if(!AtomicExchange(&lock->locked, 1)) { return; } // NOTE: spin on a plain load so we don't hammer the line with exclusive requests while(AtomicLoadRelaxed(&lock->locked)) { CpuRelax(); } } } internal void spin_unlock(SpinLock *lock) { AtomicStore(&lock->locked, 0u); } //- main thread wakeup global_variable int global_wake_fd = -1; #if PLATFORM_LINUX internal void wake_main_init(void) { global_wake_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); AssertLog(global_wake_fd != -1, "failed to create eventfd"); } internal void wake_main(void) { u64 one = 1; ssize_t written = write(global_wake_fd, &one, sizeof(one)); Unused(written); } internal b32 wake_main_drain(void) { u64 value = 0; ssize_t got = read(global_wake_fd, &value, sizeof(value)); return got == (ssize_t)sizeof(value) && value > 0; } #elif PLATFORM_APPLE // NOTE: no eventfd: a non blocking pipe, the read end is what the pump watches global_variable int global_wake_write_fd = -1; internal void wake_main_init(void) { int fds[2]; AssertLog(pipe(fds) == 0, "failed to create the wake pipe"); for(u32 index = 0; index < 2; ++index) { fcntl(fds[index], F_SETFL, fcntl(fds[index], F_GETFL) | O_NONBLOCK); fcntl(fds[index], F_SETFD, FD_CLOEXEC); } global_wake_fd = fds[0]; global_wake_write_fd = fds[1]; } internal void wake_main(void) { u8 one = 1; ssize_t written = write(global_wake_write_fd, &one, 1); Unused(written); } internal b32 wake_main_drain(void) { u8 buffer[64]; b32 woke = false; while(read(global_wake_fd, buffer, sizeof(buffer)) > 0) woke = true; return woke; } #elif PLATFORM_WINDOWS internal HANDLE wake_main_handle(void) { return (global_wake_fd == -1) ? NULL : (HANDLE)(smm)global_wake_fd; } internal void wake_main_init(void) { // NOTE: manual reset: the pump's wait must not consume it, wake_main_drain resets it HANDLE event = CreateEventW(NULL, TRUE, FALSE, NULL); AssertLog(event != NULL, "failed to create the wake event"); global_wake_fd = (int)(smm)event; } internal void wake_main(void) { HANDLE event = wake_main_handle(); if(event) SetEvent(event); } internal b32 wake_main_drain(void) { HANDLE event = wake_main_handle(); if(!event || WaitForSingleObject(event, 0) != WAIT_OBJECT_0) return false; ResetEvent(event); return true; } #endif internal int wake_main_fd(void) { return global_wake_fd; } //- time #if PLATFORM_LINUX || PLATFORM_APPLE internal u64 time_now_ns(void) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); return (u64)now.tv_sec * 1000000000ull + (u64)now.tv_nsec; } internal void sleep_ns(u64 ns) { struct timespec duration = {(time_t)(ns / 1000000000ull), (long)(ns % 1000000000ull)}; nanosleep(&duration, NULL); } #elif PLATFORM_WINDOWS internal u64 time_now_ns(void) { local_persist u64 frequency = 0; if(!frequency) { LARGE_INTEGER f; QueryPerformanceFrequency(&f); frequency = (u64)f.QuadPart; } LARGE_INTEGER counter; QueryPerformanceCounter(&counter); u64 ticks = (u64)counter.QuadPart; // NOTE: split to avoid overflowing ticks * 1e9 return (ticks / frequency) * 1000000000ull + ((ticks % frequency) * 1000000000ull) / frequency; } internal void sleep_ns(u64 ns) { // NOTE: high resolution waitable timer (windows 10 1803+), plain Sleep as the fallback HANDLE timer = CreateWaitableTimerExW(NULL, NULL, 0x00000002 /* CREATE_WAITABLE_TIMER_HIGH_RESOLUTION */, TIMER_ALL_ACCESS); if(timer) { LARGE_INTEGER due; due.QuadPart = -(LONGLONG)(ns / 100ull); if(SetWaitableTimer(timer, &due, 0, NULL, NULL, FALSE)) { WaitForSingleObject(timer, INFINITE); } CloseHandle(timer); return; } Sleep((DWORD)((ns + 999999ull) / 1000000ull)); } #endif //- aligned arena pushes // NOTE: arena_alloc only guarantees 16 byte alignment, hot simd arrays want a full cache line internal void *push_cache_aligned_raw(Arena *arena, u64 size) { u8 *raw = (u8 *)arena_alloc(arena, size + CACHE_LINE_SIZE, 0); return (void *)Align(raw, CACHE_LINE_SIZE); } internal void *push_cache_aligned(Arena *arena, u64 size) { u8 *raw = (u8 *)arena_alloc(arena, size + CACHE_LINE_SIZE, 1); return (void *)Align(raw, CACHE_LINE_SIZE); }