freebsd_amd64 support

This commit is contained in:
Sébastien Marie
2022-03-13 11:42:42 +00:00
parent f907516cbd
commit ca67cf032c
9 changed files with 515 additions and 97 deletions

View File

@@ -0,0 +1,75 @@
//+private
//+build freebsd
package sync2
import "core:c"
import "core:os"
import "core:time"
UMTX_OP_WAIT :: 2
UMTX_OP_WAKE :: 3
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 ---
}
_futex_wait :: proc(f: ^Futex, expected: u32) -> bool {
timeout := os.Unix_File_Time{
seconds = 5,
nanoseconds = 0,
}
for {
res := _umtx_op(f, UMTX_OP_WAIT, c.ulong(expected), nil, &timeout)
if res != -1 {
return true
}
if os.Errno(os.get_last_error()) == os.ETIMEDOUT {
continue
}
panic("_futex_wait failure")
}
unreachable()
}
_futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Duration) -> bool {
if duration <= 0 {
return false
}
res := _umtx_op(f, UMTX_OP_WAIT, c.ulong(expected), nil, &os.Unix_File_Time{
seconds = (os.time_t)(duration/1e9),
nanoseconds = (c.long)(duration%1e9),
})
if res != -1 {
return true
}
if os.Errno(os.get_last_error()) == os.ETIMEDOUT {
return false
}
panic("_futex_wait_with_timeout failure")
}
_futex_signal :: proc(f: ^Futex) {
res := _umtx_op(f, UMTX_OP_WAKE, 1, nil, nil)
if res == -1 {
panic("_futex_signal failure")
}
}
_futex_broadcast :: proc(f: ^Futex) {
res := _umtx_op(f, UMTX_OP_WAKE, c.ulong(max(i32)), nil, nil)
if res == -1 {
panic("_futex_broadcast failure")
}
}

View File

@@ -0,0 +1,9 @@
//+build freebsd
//+private
package sync2
import "core:os"
_current_thread_id :: proc "contextless" () -> int {
return os.current_thread_id()
}

View File

@@ -5,8 +5,8 @@ import "core:intrinsics"
current_thread_id :: proc "contextless" () -> int {
SYS_GETTID :: 186;
return int(intrinsics.syscall(SYS_GETTID));
SYS_GETTID :: 186
return int(intrinsics.syscall(SYS_GETTID))
}
@@ -19,22 +19,22 @@ Semaphore :: struct #align 16 {
}
semaphore_init :: proc(s: ^Semaphore, initial_count := 0) {
assert(unix.sem_init(&s.handle, 0, u32(initial_count)) == 0);
assert(unix.sem_init(&s.handle, 0, u32(initial_count)) == 0)
}
semaphore_destroy :: proc(s: ^Semaphore) {
assert(unix.sem_destroy(&s.handle) == 0);
s.handle = {};
assert(unix.sem_destroy(&s.handle) == 0)
s.handle = {}
}
semaphore_post :: proc(s: ^Semaphore, count := 1) {
// NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop.
for in 0..<count {
assert(unix.sem_post(&s.handle) == 0);
assert(unix.sem_post(&s.handle) == 0)
}
}
semaphore_wait_for :: proc(s: ^Semaphore) {
assert(unix.sem_wait(&s.handle) == 0);
assert(unix.sem_wait(&s.handle) == 0)
}