Change futex_wait_with_timeout to return a boolean rather than an enum

This commit is contained in:
gingerBill
2021-10-11 13:23:11 +01:00
parent a1e8769cff
commit 240b6aab13
5 changed files with 22 additions and 26 deletions

View File

@@ -23,26 +23,26 @@ EINTR :: -4
EFAULT :: -14
ETIMEDOUT :: -60
_futex_wait :: proc(f: ^Futex, expected: u32) -> Futex_Error {
_futex_wait :: proc(f: ^Futex, expected: u32) -> bool {
return _futex_wait_with_timeout(f, expected, 0)
}
_futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Duration) -> Futex_Error {
_futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Duration) -> bool {
timeout_ns := u64(duration)
s := __ulock_wait2(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, f, u64(expected), timeout_ns, 0)
if s >= 0 {
return nil
return true
}
switch s {
case EINTR, EFAULT:
return nil
return true
case ETIMEDOUT:
return .Timed_Out
return false
case:
panic("futex_wait failure")
}
return nil
return true
}

View File

@@ -39,22 +39,22 @@ internal_futex :: proc(f: ^Futex, op: c.int, val: u32, timeout: rawptr) -> int {
}
_futex_wait :: proc(f: ^Futex, expected: u32) -> Futex_Error {
_futex_wait :: proc(f: ^Futex, expected: u32) -> bool {
err := internal_futex(f, FUTEX_WAIT_PRIVATE | FUTEX_WAIT, expected, nil)
switch err {
case ESUCCESS, EINTR, EAGAIN, EINVAL:
// okay
case ETIMEDOUT:
return .Timed_Out
return false
case EFAULT:
fallthrough
case:
panic("futex_wait failure")
}
return nil
return true
}
_futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Duration) -> Futex_Error {
_futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Duration) -> bool {
timespec_t :: struct {
tv_sec: c.long,
tv_nsec: c.long,
@@ -74,13 +74,13 @@ _futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Durati
case ESUCCESS, EINTR, EAGAIN, EINVAL:
// okay
case ETIMEDOUT:
return .Timed_Out
return false
case EFAULT:
fallthrough
case:
panic("futex_wait_with_timeout failure")
}
return nil
return true
}

View File

@@ -20,13 +20,13 @@ foreign Synchronization {
_futex_wait :: proc(f: ^Futex, expect: u32) -> Futex_Error {
_futex_wait :: proc(f: ^Futex, expect: u32) -> bool {
expect := expect
ok := RtlWaitOnAddress(f, &expect, size_of(expect), nil)
return nil if ok else .Timed_Out
return bool(ok)
}
_futex_wait_with_timeout :: proc(f: ^Futex, expect: u32, duration: time.Duration) -> Futex_Error {
_futex_wait_with_timeout :: proc(f: ^Futex, expect: u32, duration: time.Duration) -> bool {
expect := expect
timeout: i64
@@ -38,7 +38,7 @@ _futex_wait_with_timeout :: proc(f: ^Futex, expect: u32, duration: time.Duration
}
\
ok := RtlWaitOnAddress(f, &expect, size_of(expect), timeout_ptr)
return nil if ok else .Timed_Out
return bool(ok)
}
_futex_wake_single :: proc(f: ^Futex) {

View File

@@ -202,25 +202,21 @@ sema_post :: proc(s: ^Sema, count := 1) {
// An Futex must not be copied after first use
Futex :: distinct u32
Futex_Error :: enum {
None,
Timed_Out,
}
futex_wait :: proc(f: ^Futex, expected: u32) {
if u32(atomic_load(f)) != expected {
return
}
assert(_futex_wait(f, expected) != nil, "futex_wait failure")
assert(_futex_wait(f, expected), "futex_wait failure")
}
futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Duration) -> Futex_Error {
// returns true if the wait happened within the duration, false if it exceeded the time duration
futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Duration) -> bool {
if u32(atomic_load(f)) != expected {
return nil
return true
}
if duration == 0 {
return .Timed_Out
return false
}
return _futex_wait_with_timeout(f, expected, duration)

View File

@@ -307,7 +307,7 @@ queue_item_wait_with_timeout :: proc(item: ^Queue_Item, duration: time.Duration)
if remaining < 0 {
return false
}
if futex_wait_with_timeout(&item.futex, 0, remaining) == .Timed_Out {
if !futex_wait_with_timeout(&item.futex, 0, remaining) {
return false
}
cpu_relax()