diff options
| author | Feoramund <161657516+Feoramund@users.noreply.github.com> | 2024-06-26 01:14:25 -0400 |
|---|---|---|
| committer | Feoramund <161657516+Feoramund@users.noreply.github.com> | 2024-06-26 10:02:07 -0400 |
| commit | 10ce76fcc2ce45a28858ca1562e2b802a781ee58 (patch) | |
| tree | c48c15fcb0c5bd4ae502c76f2f0dce4e962da471 /core/sync | |
| parent | c61f5bbfe70d02cc505fac2cd59b6bc56fd39600 (diff) | |
Fix FreeBSD futex implementation
- Add missing size of timeout struct to `_umtx_op`.
- Use `WAIT_UINT` not `WAIT`, as the expected value is an unsigned integer.
- Use new native calls instead of libc.
Diffstat (limited to 'core/sync')
| -rw-r--r-- | core/sync/futex_freebsd.odin | 41 |
1 files changed, 17 insertions, 24 deletions
diff --git a/core/sync/futex_freebsd.odin b/core/sync/futex_freebsd.odin index 60b1d6e0d..ac6e2400a 100644 --- a/core/sync/futex_freebsd.odin +++ b/core/sync/futex_freebsd.odin @@ -3,35 +3,27 @@ package sync import "core:c" +import "core:sys/freebsd" import "core:time" -UMTX_OP_WAIT :: 2 -UMTX_OP_WAKE :: 3 - -ETIMEDOUT :: 60 - -foreign import libc "system:c" - -foreign libc { - _umtx_op :: proc "c" (obj: rawptr, op: c.int, val: c.ulong, uaddr: rawptr, uaddr2: rawptr) -> c.int --- - __error :: proc "c" () -> ^c.int --- -} - _futex_wait :: proc "contextless" (f: ^Futex, expected: u32) -> bool { - timeout := [2]i64{14400, 0} // 4 hours + timeout := freebsd.timespec {14400, 0} // 4 hours + timeout_size := cast(rawptr)cast(uintptr)size_of(timeout) + for { - res := _umtx_op(f, UMTX_OP_WAIT, c.ulong(expected), nil, &timeout) + errno := freebsd._umtx_op(f, .WAIT_UINT, cast(c.ulong)expected, timeout_size, &timeout) - if res != -1 { + if errno == nil { return true } - if __error()^ == ETIMEDOUT { + if errno == .ETIMEDOUT { continue } _panic("_futex_wait failure") } + unreachable() } @@ -40,14 +32,15 @@ _futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expected: u32, durati return false } - timeout := [2]i64{i64(duration/1e9), i64(duration%1e9)} + timeout := freebsd.timespec {cast(freebsd.time_t)duration / 1e9, cast(c.long)duration % 1e9} + timeout_size := cast(rawptr)cast(uintptr)size_of(timeout) - res := _umtx_op(f, UMTX_OP_WAIT, c.ulong(expected), nil, &timeout) - if res != -1 { + errno := freebsd._umtx_op(f, .WAIT_UINT, cast(c.ulong)expected, timeout_size, &timeout) + if errno == nil { return true } - if __error()^ == ETIMEDOUT { + if errno == .ETIMEDOUT { return false } @@ -55,17 +48,17 @@ _futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expected: u32, durati } _futex_signal :: proc "contextless" (f: ^Futex) { - res := _umtx_op(f, UMTX_OP_WAKE, 1, nil, nil) + errno := freebsd._umtx_op(f, .WAKE, 1, nil, nil) - if res == -1 { + if errno != nil { _panic("_futex_signal failure") } } _futex_broadcast :: proc "contextless" (f: ^Futex) { - res := _umtx_op(f, UMTX_OP_WAKE, c.ulong(max(i32)), nil, nil) + errno := freebsd._umtx_op(f, .WAKE, cast(c.ulong)max(i32), nil, nil) - if res == -1 { + if errno != nil { _panic("_futex_broadcast failure") } } |