util_os.c (564B)
1 /* See LICENSE for license details. */ 2 3 // NOTE(rnp): functions which require platform layer support but 4 // otherwise share implementation 5 6 function b32 7 take_lock(i32 *lock, i32 timeout_ms) 8 { 9 b32 result = 0; 10 for (;;) { 11 i32 current = 0; 12 if (atomic_cas_u32(lock, ¤t, 1)) 13 result = 1; 14 if (result || !timeout_ms || (!os_wait_on_address(lock, current, (u32)timeout_ms) && timeout_ms != -1)) 15 break; 16 } 17 return result; 18 } 19 20 function void 21 release_lock(i32 *lock) 22 { 23 assert(atomic_load_u32(lock)); 24 atomic_store_u32(lock, 0); 25 os_wake_all_waiters(lock); 26 }