aboutsummaryrefslogtreecommitdiff
path: root/core/sync
diff options
context:
space:
mode:
authorSébastien Marie <semarie@online.fr>2022-03-13 11:42:42 +0000
committerSébastien Marie <semarie@online.fr>2022-03-13 11:42:42 +0000
commitca67cf032c5a8e0c0fe711fe668cf4d77b080a62 (patch)
tree84bda0a4fd28eb6ef515247089b5c311d6148180 /core/sync
parentf907516cbd0078b69996929d02742d0c1a48c226 (diff)
freebsd_amd64 support
Diffstat (limited to 'core/sync')
-rw-r--r--core/sync/sync2/futex_freebsd.odin75
-rw-r--r--core/sync/sync2/primitives_freebsd.odin9
-rw-r--r--core/sync/sync_freebsd.odin14
3 files changed, 91 insertions, 7 deletions
diff --git a/core/sync/sync2/futex_freebsd.odin b/core/sync/sync2/futex_freebsd.odin
new file mode 100644
index 000000000..2cbdb4aaa
--- /dev/null
+++ b/core/sync/sync2/futex_freebsd.odin
@@ -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")
+ }
+}
diff --git a/core/sync/sync2/primitives_freebsd.odin b/core/sync/sync2/primitives_freebsd.odin
new file mode 100644
index 000000000..2a25a18a4
--- /dev/null
+++ b/core/sync/sync2/primitives_freebsd.odin
@@ -0,0 +1,9 @@
+//+build freebsd
+//+private
+package sync2
+
+import "core:os"
+
+_current_thread_id :: proc "contextless" () -> int {
+ return os.current_thread_id()
+}
diff --git a/core/sync/sync_freebsd.odin b/core/sync/sync_freebsd.odin
index 240308b7d..20e6bfceb 100644
--- a/core/sync/sync_freebsd.odin
+++ b/core/sync/sync_freebsd.odin
@@ -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)
}