mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Switched to native futex on NetBSD
This commit is contained in:
+49
-140
@@ -1,165 +1,74 @@
|
|||||||
//+private
|
//+private
|
||||||
package sync
|
package sync
|
||||||
|
|
||||||
import "core:c"
|
import "base:intrinsics"
|
||||||
import "core:time"
|
import "core:time"
|
||||||
|
import "core:c"
|
||||||
import "core:sys/unix"
|
import "core:sys/unix"
|
||||||
|
|
||||||
@(private="file")
|
foreign import libc "system:c"
|
||||||
Wait_Node :: struct {
|
|
||||||
thread: unix.pthread_t,
|
FUTEX_PRIVATE_FLAG :: 128
|
||||||
futex: ^Futex,
|
|
||||||
prev, next: ^Wait_Node,
|
FUTEX_WAIT_PRIVATE :: 0 | FUTEX_PRIVATE_FLAG
|
||||||
|
FUTEX_WAKE_PRIVATE :: 1 | FUTEX_PRIVATE_FLAG
|
||||||
|
|
||||||
|
EINTR :: 4 /* Interrupted system call */
|
||||||
|
EAGAIN :: 35 /* Resource temporarily unavailable */
|
||||||
|
ETIMEDOUT :: 60 /* Operation timed out */
|
||||||
|
|
||||||
|
Time_Spec :: struct {
|
||||||
|
time_sec: uint,
|
||||||
|
time_nsec: uint,
|
||||||
}
|
}
|
||||||
@(private="file")
|
|
||||||
atomic_flag :: distinct bool
|
get_last_error :: proc "contextless" () -> int {
|
||||||
@(private="file")
|
foreign libc {
|
||||||
Wait_Queue :: struct {
|
__errno :: proc() -> ^c.int ---
|
||||||
lock: atomic_flag,
|
|
||||||
list: Wait_Node,
|
|
||||||
}
|
|
||||||
@(private="file")
|
|
||||||
waitq_lock :: proc "contextless" (waitq: ^Wait_Queue) {
|
|
||||||
for cast(bool)atomic_exchange_explicit(&waitq.lock, atomic_flag(true), .Acquire) {
|
|
||||||
cpu_relax() // spin...
|
|
||||||
}
|
}
|
||||||
}
|
return int(__errno()^)
|
||||||
@(private="file")
|
|
||||||
waitq_unlock :: proc "contextless" (waitq: ^Wait_Queue) {
|
|
||||||
atomic_store_explicit(&waitq.lock, atomic_flag(false), .Release)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: This approach may scale badly in the future,
|
_futex_wait :: proc "contextless" (futex: ^Futex, expected: u32) -> bool {
|
||||||
// possible solution - hash map (leads to deadlocks now).
|
if cast(int) intrinsics.syscall(unix.SYS___futex, uintptr(futex), FUTEX_WAIT_PRIVATE, uintptr(expected), 0) == -1 {
|
||||||
@(private="file")
|
switch get_last_error() {
|
||||||
g_waitq: Wait_Queue
|
case EINTR, EAGAIN:
|
||||||
|
return true
|
||||||
@(init, private="file")
|
case:
|
||||||
g_waitq_init :: proc() {
|
_panic("futex_wait failure")
|
||||||
g_waitq = {
|
}
|
||||||
list = {
|
|
||||||
prev = &g_waitq.list,
|
|
||||||
next = &g_waitq.list,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private="file")
|
_futex_wait_with_timeout :: proc "contextless" (futex: ^Futex, expected: u32, duration: time.Duration) -> bool {
|
||||||
get_waitq :: #force_inline proc "contextless" (f: ^Futex) -> ^Wait_Queue {
|
|
||||||
_ = f
|
|
||||||
return &g_waitq
|
|
||||||
}
|
|
||||||
|
|
||||||
_futex_wait :: proc "contextless" (f: ^Futex, expect: u32) -> (ok: bool) {
|
|
||||||
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: unix.sigset_t
|
|
||||||
unix.sigemptyset(&mask)
|
|
||||||
unix.sigaddset(&mask, unix.SIGCONT)
|
|
||||||
unix.pthread_sigmask(unix.SIG_BLOCK, &mask, &old_mask)
|
|
||||||
|
|
||||||
if u32(atomic_load_explicit(f, .Acquire)) == expect {
|
|
||||||
waitq_unlock(waitq)
|
|
||||||
defer waitq_lock(waitq)
|
|
||||||
|
|
||||||
sig: c.int
|
|
||||||
unix.sigwait(&mask, &sig)
|
|
||||||
errno := unix.errno()
|
|
||||||
ok = errno == unix.ERROR_NONE
|
|
||||||
}
|
|
||||||
|
|
||||||
waiter.prev.next = waiter.next
|
|
||||||
waiter.next.prev = waiter.prev
|
|
||||||
|
|
||||||
unix.pthread_sigmask(unix.SIG_SETMASK, &old_mask, nil)
|
|
||||||
|
|
||||||
// FIXME: Add error handling!
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expect: u32, duration: time.Duration) -> (ok: bool) {
|
|
||||||
if duration <= 0 {
|
if duration <= 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
waitq := get_waitq(f)
|
if cast(int) intrinsics.syscall(unix.SYS___futex, uintptr(futex), FUTEX_WAIT_PRIVATE, uintptr(expected), cast(uintptr) &Time_Spec{
|
||||||
waitq_lock(waitq)
|
time_sec = cast(uint)(duration / 1e9),
|
||||||
defer waitq_unlock(waitq)
|
time_nsec = cast(uint)(duration % 1e9),
|
||||||
|
}) == -1 {
|
||||||
head := &waitq.list
|
switch get_last_error() {
|
||||||
waiter := Wait_Node{
|
case EINTR, EAGAIN:
|
||||||
thread = unix.pthread_self(),
|
return true
|
||||||
futex = f,
|
case ETIMEDOUT:
|
||||||
prev = head,
|
return false
|
||||||
next = head.next,
|
case:
|
||||||
}
|
_panic("futex_wait_with_timeout failure")
|
||||||
|
|
||||||
waiter.prev.next = &waiter
|
|
||||||
waiter.next.prev = &waiter
|
|
||||||
|
|
||||||
old_mask, mask: unix.sigset_t
|
|
||||||
unix.sigemptyset(&mask)
|
|
||||||
unix.sigaddset(&mask, unix.SIGCONT)
|
|
||||||
unix.pthread_sigmask(unix.SIG_BLOCK, &mask, &old_mask)
|
|
||||||
|
|
||||||
if u32(atomic_load_explicit(f, .Acquire)) == expect {
|
|
||||||
waitq_unlock(waitq)
|
|
||||||
defer waitq_lock(waitq)
|
|
||||||
|
|
||||||
info: unix.siginfo_t
|
|
||||||
ts := unix.timespec{
|
|
||||||
tv_sec = i64(duration / 1e9),
|
|
||||||
tv_nsec = i64(duration % 1e9),
|
|
||||||
}
|
}
|
||||||
unix.sigtimedwait(&mask, &info, &ts)
|
|
||||||
errno := unix.errno()
|
|
||||||
ok = errno == unix.EAGAIN || errno == unix.ERROR_NONE
|
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
waiter.prev.next = waiter.next
|
|
||||||
waiter.next.prev = waiter.prev
|
|
||||||
|
|
||||||
unix.pthread_sigmask(unix.SIG_SETMASK, &old_mask, nil)
|
|
||||||
|
|
||||||
// FIXME: Add error handling!
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_futex_signal :: proc "contextless" (f: ^Futex) {
|
_futex_signal :: proc "contextless" (futex: ^Futex) {
|
||||||
waitq := get_waitq(f)
|
if cast(int) intrinsics.syscall(unix.SYS___futex, uintptr(futex), FUTEX_WAKE_PRIVATE, 1) == -1 {
|
||||||
waitq_lock(waitq)
|
_panic("futex_wake_single failure")
|
||||||
defer waitq_unlock(waitq)
|
|
||||||
|
|
||||||
head := &waitq.list
|
|
||||||
for waiter := head.next; waiter != head; waiter = waiter.next {
|
|
||||||
if waiter.futex == f {
|
|
||||||
unix.pthread_kill(waiter.thread, unix.SIGCONT)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_futex_broadcast :: proc "contextless" (f: ^Futex) {
|
_futex_broadcast :: proc "contextless" (futex: ^Futex) {
|
||||||
waitq := get_waitq(f)
|
if cast(int) intrinsics.syscall(unix.SYS___futex, uintptr(futex), FUTEX_WAKE_PRIVATE, uintptr(max(i32))) == -1 {
|
||||||
waitq_lock(waitq)
|
_panic("_futex_wake_all failure")
|
||||||
defer waitq_unlock(waitq)
|
|
||||||
|
|
||||||
head := &waitq.list
|
|
||||||
for waiter := head.next; waiter != head; waiter = waiter.next {
|
|
||||||
if waiter.futex == f {
|
|
||||||
unix.pthread_kill(waiter.thread, unix.SIGCONT)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
package unix
|
|
||||||
|
|
||||||
import "core:c"
|
|
||||||
|
|
||||||
foreign import libc "system:c"
|
|
||||||
|
|
||||||
ERROR_NONE :: 0
|
|
||||||
EAGAIN :: 35
|
|
||||||
|
|
||||||
SIGCONT :: 19
|
|
||||||
|
|
||||||
SIG_BLOCK :: 1
|
|
||||||
SIG_UNBLOCK :: 2
|
|
||||||
SIG_SETMASK :: 3
|
|
||||||
|
|
||||||
siginfo_t :: struct { _: [128]u8 }
|
|
||||||
sigset_t :: struct { _: [4]u32 }
|
|
||||||
|
|
||||||
foreign libc {
|
|
||||||
@(link_name="__sigemptyset14") sigemptyset :: proc(set: ^sigset_t) -> c.int ---
|
|
||||||
@(link_name="__sigaddset14") sigaddset :: proc(set: ^sigset_t, _signal: c.int) -> c.int ---
|
|
||||||
@(link_name="__sigtimedwait50") sigtimedwait :: proc(set: ^sigset_t, info: ^siginfo_t, timeout: ^timespec) -> c.int ---
|
|
||||||
@(link_name="sigwait") sigwait :: proc(set: ^sigset_t, _signal: ^c.int) -> c.int ---
|
|
||||||
|
|
||||||
@(private="file", link_name="__errno") get_error_location :: proc() -> ^c.int ---
|
|
||||||
}
|
|
||||||
|
|
||||||
errno :: #force_inline proc "contextless" () -> int {
|
|
||||||
return int(get_error_location()^)
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package unix
|
||||||
|
|
||||||
|
SYS___futex : uintptr : 166
|
||||||
+9
-4
@@ -634,9 +634,15 @@ gb_internal void thread_set_name(Thread *t, char const *name) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(GB_SYSTEM_LINUX)
|
#if defined(GB_SYSTEM_LINUX) || defined(GB_SYSTEM_NETBSD)
|
||||||
#include <linux/futex.h>
|
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
|
#ifdef GB_SYSTEM_LINUX
|
||||||
|
#include <linux/futex.h>
|
||||||
|
#else
|
||||||
|
#include <sys/futex.h>
|
||||||
|
#define SYS_futex SYS___futex
|
||||||
|
#endif
|
||||||
|
|
||||||
gb_internal void futex_signal(Futex *addr) {
|
gb_internal void futex_signal(Futex *addr) {
|
||||||
int ret = syscall(SYS_futex, addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1, NULL, NULL, 0);
|
int ret = syscall(SYS_futex, addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1, NULL, NULL, 0);
|
||||||
@@ -903,11 +909,10 @@ gb_internal void futex_wait(Futex *f, Footex val) {
|
|||||||
} while (f->load() == val);
|
} while (f->load() == val);
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(GB_SYSTEM_HAIKU) || defined(GB_SYSTEM_NETBSD)
|
#elif defined(GB_SYSTEM_HAIKU)
|
||||||
|
|
||||||
// Futex implementation taken from https://tavianator.com/2023/futex.html
|
// Futex implementation taken from https://tavianator.com/2023/futex.html
|
||||||
|
|
||||||
#include <signal.h>
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user