From 3ebf5dcc0ec443cd822e1ddf8c80de7e606ca2f4 Mon Sep 17 00:00:00 2001 From: avanspector Date: Mon, 26 Feb 2024 07:59:53 +0100 Subject: [PATCH] fix haiku --- core/sync/futex_haiku.odin | 29 +++++++++++++++-------------- core/sync/primitives_haiku.odin | 8 ++++++++ core/sys/haiku/os.odin | 2 +- core/sys/unix/pthread_unix.odin | 2 ++ 4 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 core/sync/primitives_haiku.odin diff --git a/core/sync/futex_haiku.odin b/core/sync/futex_haiku.odin index b82f7ee58..9e04cf420 100644 --- a/core/sync/futex_haiku.odin +++ b/core/sync/futex_haiku.odin @@ -5,12 +5,13 @@ import "core:c" import "core:c/libc" import "core:sys/haiku" import "core:sys/unix" +import "core:time" @(private="file") Wait_Node :: struct { thread: unix.pthread_t, futex: ^Futex, - prev, next: Wait_Node, + prev, next: ^Wait_Node, } @(private="file") Wait_Queue :: struct { @@ -18,14 +19,14 @@ Wait_Queue :: struct { list: Wait_Node, } @(private="file") -waitq_lock :: proc(waitq: ^Wait_Queue) { - for libc.atomic_flag_test_and_set_explicit(&waitq.lock, .Acquire) { +waitq_lock :: proc "contextless" (waitq: ^Wait_Queue) { + for libc.atomic_flag_test_and_set_explicit(&waitq.lock, .acquire) { ; // spin... } } @(private="file") -waitq_unlock :: proc(waitq: ^Wait_Queue) { - libc.atomic_flag_clear(&waitq.lock, .Release) +waitq_unlock :: proc "contextless" (waitq: ^Wait_Queue) { + libc.atomic_flag_clear_explicit(&waitq.lock, .release) } // FIXME: This approach may scale badly in the future, @@ -45,8 +46,8 @@ get_waitq :: #force_inline proc "contextless" (f: ^Futex) -> ^Wait_Queue { _futex_wait :: proc "contextless" (f: ^Futex, expect: u32) -> bool { waitq := get_waitq(f) - waitq_lock(&waitq) - defer waitq_unlock(&waitq) + waitq_lock(waitq) + defer waitq_unlock(waitq) head := &waitq.list waiter := Wait_Node{ @@ -64,9 +65,9 @@ _futex_wait :: proc "contextless" (f: ^Futex, expect: u32) -> bool { haiku.sigaddset(&mask, haiku.SIGCONT) unix.pthread_sigmask(haiku.SIG_BLOCK, &mask, &old_mask) - if u32(atomic_load_explicit(f, .Acquire)) == expect { - waitq_unlock(&waitq) - defer waitq_lock(&waitq) + if u32(atomic_load_explicit(f, .acquire)) == expect { + waitq_unlock(waitq) + defer waitq_lock(waitq) sig: c.int haiku.sigwait(&mask, &sig) @@ -89,8 +90,8 @@ _futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expect: u32, duration _futex_signal :: proc "contextless" (f: ^Futex) { waitq := get_waitq(f) - waitq_lock(&waitq) - defer waitq_unlock(&waitq) + waitq_lock(waitq) + defer waitq_unlock(waitq) head := &waitq.list for waiter := head.next; waiter != head; waiter = waiter.next { @@ -103,8 +104,8 @@ _futex_signal :: proc "contextless" (f: ^Futex) { _futex_broadcast :: proc "contextless" (f: ^Futex) { waitq := get_waitq(f) - waitq_lock(&waitq) - defer waitq_unlock(&waitq) + waitq_lock(waitq) + defer waitq_unlock(waitq) head := &waitq.list for waiter := head.next; waiter != head; waiter = waiter.next { diff --git a/core/sync/primitives_haiku.odin b/core/sync/primitives_haiku.odin new file mode 100644 index 000000000..4b8f6b02d --- /dev/null +++ b/core/sync/primitives_haiku.odin @@ -0,0 +1,8 @@ +//+private +package sync + +import "core:sys/haiku" + +_current_thread_id :: proc "contextless" () -> int { + return int(haiku.find_thread(nil)) +} diff --git a/core/sys/haiku/os.odin b/core/sys/haiku/os.odin index 6aa156ce5..b77d50f80 100644 --- a/core/sys/haiku/os.odin +++ b/core/sys/haiku/os.odin @@ -139,7 +139,7 @@ foreign libroot { */ disable_debugger :: proc(state: c.int) -> c.int --- - find_thread(name: cstring) -> thread_id --- + find_thread :: proc(name: cstring) -> thread_id --- } // Signal.h diff --git a/core/sys/unix/pthread_unix.odin b/core/sys/unix/pthread_unix.odin index 5ac4782f3..2759d5aae 100644 --- a/core/sys/unix/pthread_unix.odin +++ b/core/sys/unix/pthread_unix.odin @@ -16,6 +16,8 @@ foreign pthread { // retval is a pointer to a location to put the return value of the thread proc. pthread_join :: proc(t: pthread_t, retval: ^rawptr) -> c.int --- + pthread_kill :: proc(t: pthread_t, sig: c.int) -> c.int --- + pthread_self :: proc() -> pthread_t --- pthread_equal :: proc(a, b: pthread_t) -> b32 ---