Add cond_wait_with_timeout

This commit is contained in:
gingerBill
2021-10-11 12:55:25 +01:00
parent 49c761dc6d
commit 73cba2cf13
6 changed files with 60 additions and 13 deletions
+18 -9
View File
@@ -5,30 +5,39 @@ package sync2
import "core:time"
foreign import Synchronization "system:Synchronization.lib"
foreign import NtDll "system:NtDll.lib"
@(default_calling_convention="c")
@(default_calling_convention="stdcall")
foreign NtDll {
RtlWaitOnAddress :: proc(Address: rawptr, CompareAddress: rawptr, AddressSize: uint, Timeout: ^i64) -> b32 ---
}
@(default_calling_convention="stdcall")
foreign Synchronization {
WaitOnAddress :: proc(Address: rawptr, CompareAddress: rawptr, AddressSize: uint, dwMilliseconds: u32) -> b32 ---
WakeByAddressSingle :: proc(Address: rawptr) ---
WakeByAddressAll :: proc(Address: rawptr) ---
}
_futex_wait :: proc(f: ^Futex, expect: u32) -> Futex_Error {
expect := expect
ms :: ~u32(0) // infinite
ok := WaitOnAddress(f, &expect, size_of(expect), ms)
ok := RtlWaitOnAddress(f, &expect, size_of(expect), nil)
return nil if ok else .Timed_Out
}
_futex_wait_with_timeout :: proc(f: ^Futex, expect: u32, duration: time.Duration) -> Futex_Error {
expect := expect
ms: u32 = 0
if duration >= 0 {
ms = u32(u64(duration)/1e6)
timeout: i64
timeout_ptr: ^i64
if duration > 0 {
// In 100 ns units
timeout = i64(timeout)/100
timeout_ptr = &timeout
}
ok := WaitOnAddress(f, &expect, size_of(expect), ms)
\
ok := RtlWaitOnAddress(f, &expect, size_of(expect), timeout_ptr)
return nil if ok else .Timed_Out
}