small fixes

This commit is contained in:
avanspector
2024-02-27 01:59:17 +01:00
parent 8d4bb35bcc
commit 38c69b9691
4 changed files with 78 additions and 28 deletions
+55 -15
View File
@@ -2,7 +2,6 @@
package sync
import "core:c"
import "core:c/libc"
import "core:runtime"
import "core:sys/haiku"
import "core:sys/unix"
@@ -15,23 +14,21 @@ Wait_Node :: struct {
prev, next: ^Wait_Node,
}
@(private="file")
atomic_flag :: distinct bool
@(private="file")
Wait_Queue :: struct {
lock: libc.atomic_flag,
lock: atomic_flag,
list: Wait_Node,
}
@(private="file")
waitq_lock :: proc "contextless" (waitq: ^Wait_Queue) {
// FIXME: Get rid of context here.
context = runtime.default_context()
for libc.atomic_flag_test_and_set_explicit(&waitq.lock, .acquire) {
; // spin...
for cast(bool)atomic_exchange_explicit(&waitq.lock, atomic_flag(true), .Acquire) {
cpu_relax() // spin...
}
}
@(private="file")
waitq_unlock :: proc "contextless" (waitq: ^Wait_Queue) {
// FIXME: Get rid of context here.
context = runtime.default_context()
libc.atomic_flag_clear_explicit(&waitq.lock, .release)
atomic_store_explicit(&waitq.lock, atomic_flag(false), .Release)
}
// FIXME: This approach may scale badly in the future,
@@ -55,7 +52,7 @@ get_waitq :: #force_inline proc "contextless" (f: ^Futex) -> ^Wait_Queue {
return &g_waitq
}
_futex_wait :: proc "contextless" (f: ^Futex, expect: u32) -> bool {
_futex_wait :: proc "contextless" (f: ^Futex, expect: u32) -> (ok: bool) {
waitq := get_waitq(f)
waitq_lock(waitq)
defer waitq_unlock(waitq)
@@ -82,6 +79,8 @@ _futex_wait :: proc "contextless" (f: ^Futex, expect: u32) -> bool {
sig: c.int
haiku.sigwait(&mask, &sig)
errno := haiku.errno()
ok = errno == .OK
}
waiter.prev.next = waiter.next
@@ -90,13 +89,54 @@ _futex_wait :: proc "contextless" (f: ^Futex, expect: u32) -> bool {
unix.pthread_sigmask(haiku.SIG_SETMASK, &old_mask, nil)
// FIXME: Add error handling!
return true
return
}
_futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expect: u32, duration: time.Duration) -> bool {
// FIXME: Add timeout!
_ = duration
return _futex_wait(f, expect)
_futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expect: u32, duration: time.Duration) -> (ok: bool) {
if duration <= 0 {
return false
}
waitq := get_waitq(f)
waitq_lock(waitq)
defer waitq_unlock(waitq)
head := &waitq.list
waiter := Wait_Node{
thread = unix.pthread_self(),
futex = f,
prev = head,
next = head.next,
}
waiter.prev.next = &waiter
waiter.next.prev = &waiter
old_mask, mask: haiku.sigset_t
haiku.sigemptyset(&mask)
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)
info: haiku.siginfo_t
ts := unix.timespec{
tv_sec = i64(duration / 1e9),
tv_nsec = i64(duration % 1e9),
}
haiku.sigtimedwait(&mask, &info, &ts)
errno := haiku.errno()
ok = errno == .EAGAIN || errno == .OK
}
waiter.prev.next = waiter.next
waiter.next.prev = waiter.prev
unix.pthread_sigmask(haiku.SIG_SETMASK, &old_mask, nil)
// FIXME: Add error handling!
return
}
_futex_signal :: proc "contextless" (f: ^Futex) {