mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Change futex_wait_with_timeout to return a boolean rather than an enum
This commit is contained in:
@@ -23,26 +23,26 @@ EINTR :: -4
|
|||||||
EFAULT :: -14
|
EFAULT :: -14
|
||||||
ETIMEDOUT :: -60
|
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)
|
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)
|
timeout_ns := u64(duration)
|
||||||
|
|
||||||
s := __ulock_wait2(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, f, u64(expected), timeout_ns, 0)
|
s := __ulock_wait2(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, f, u64(expected), timeout_ns, 0)
|
||||||
if s >= 0 {
|
if s >= 0 {
|
||||||
return nil
|
return true
|
||||||
}
|
}
|
||||||
switch s {
|
switch s {
|
||||||
case EINTR, EFAULT:
|
case EINTR, EFAULT:
|
||||||
return nil
|
return true
|
||||||
case ETIMEDOUT:
|
case ETIMEDOUT:
|
||||||
return .Timed_Out
|
return false
|
||||||
case:
|
case:
|
||||||
panic("futex_wait failure")
|
panic("futex_wait failure")
|
||||||
}
|
}
|
||||||
return nil
|
return true
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
err := internal_futex(f, FUTEX_WAIT_PRIVATE | FUTEX_WAIT, expected, nil)
|
||||||
switch err {
|
switch err {
|
||||||
case ESUCCESS, EINTR, EAGAIN, EINVAL:
|
case ESUCCESS, EINTR, EAGAIN, EINVAL:
|
||||||
// okay
|
// okay
|
||||||
case ETIMEDOUT:
|
case ETIMEDOUT:
|
||||||
return .Timed_Out
|
return false
|
||||||
case EFAULT:
|
case EFAULT:
|
||||||
fallthrough
|
fallthrough
|
||||||
case:
|
case:
|
||||||
panic("futex_wait failure")
|
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 {
|
timespec_t :: struct {
|
||||||
tv_sec: c.long,
|
tv_sec: c.long,
|
||||||
tv_nsec: 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:
|
case ESUCCESS, EINTR, EAGAIN, EINVAL:
|
||||||
// okay
|
// okay
|
||||||
case ETIMEDOUT:
|
case ETIMEDOUT:
|
||||||
return .Timed_Out
|
return false
|
||||||
case EFAULT:
|
case EFAULT:
|
||||||
fallthrough
|
fallthrough
|
||||||
case:
|
case:
|
||||||
panic("futex_wait_with_timeout failure")
|
panic("futex_wait_with_timeout failure")
|
||||||
}
|
}
|
||||||
return nil
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
expect := expect
|
||||||
ok := RtlWaitOnAddress(f, &expect, size_of(expect), nil)
|
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
|
expect := expect
|
||||||
|
|
||||||
timeout: i64
|
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)
|
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) {
|
_futex_wake_single :: proc(f: ^Futex) {
|
||||||
|
|||||||
@@ -202,25 +202,21 @@ sema_post :: proc(s: ^Sema, count := 1) {
|
|||||||
// An Futex must not be copied after first use
|
// An Futex must not be copied after first use
|
||||||
Futex :: distinct u32
|
Futex :: distinct u32
|
||||||
|
|
||||||
Futex_Error :: enum {
|
|
||||||
None,
|
|
||||||
Timed_Out,
|
|
||||||
}
|
|
||||||
|
|
||||||
futex_wait :: proc(f: ^Futex, expected: u32) {
|
futex_wait :: proc(f: ^Futex, expected: u32) {
|
||||||
if u32(atomic_load(f)) != expected {
|
if u32(atomic_load(f)) != expected {
|
||||||
return
|
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 {
|
if u32(atomic_load(f)) != expected {
|
||||||
return nil
|
return true
|
||||||
}
|
}
|
||||||
if duration == 0 {
|
if duration == 0 {
|
||||||
return .Timed_Out
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return _futex_wait_with_timeout(f, expected, duration)
|
return _futex_wait_with_timeout(f, expected, duration)
|
||||||
|
|||||||
@@ -307,7 +307,7 @@ queue_item_wait_with_timeout :: proc(item: ^Queue_Item, duration: time.Duration)
|
|||||||
if remaining < 0 {
|
if remaining < 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if futex_wait_with_timeout(&item.futex, 0, remaining) == .Timed_Out {
|
if !futex_wait_with_timeout(&item.futex, 0, remaining) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
cpu_relax()
|
cpu_relax()
|
||||||
|
|||||||
Reference in New Issue
Block a user