remove pthread from sys/unix and use sys/posix where used

This commit is contained in:
Laytan
2024-10-28 19:20:43 +01:00
parent e064f8c6be
commit afed3ce6b5
10 changed files with 38 additions and 749 deletions

View File

@@ -1,96 +0,0 @@
#+build darwin
package unix
import "core:c"
// NOTE(tetra): No 32-bit Macs.
// Source: _pthread_types.h on my Mac.
PTHREAD_SIZE :: 8176
PTHREAD_ATTR_SIZE :: 56
PTHREAD_MUTEXATTR_SIZE :: 8
PTHREAD_MUTEX_SIZE :: 56
PTHREAD_CONDATTR_SIZE :: 8
PTHREAD_COND_SIZE :: 40
PTHREAD_ONCE_SIZE :: 8
PTHREAD_RWLOCK_SIZE :: 192
PTHREAD_RWLOCKATTR_SIZE :: 16
pthread_t :: distinct u64
pthread_attr_t :: struct {
sig: c.long,
_: [PTHREAD_ATTR_SIZE] c.char,
}
pthread_cond_t :: struct {
sig: c.long,
_: [PTHREAD_COND_SIZE] c.char,
}
pthread_condattr_t :: struct {
sig: c.long,
_: [PTHREAD_CONDATTR_SIZE] c.char,
}
pthread_mutex_t :: struct {
sig: c.long,
_: [PTHREAD_MUTEX_SIZE] c.char,
}
pthread_mutexattr_t :: struct {
sig: c.long,
_: [PTHREAD_MUTEXATTR_SIZE] c.char,
}
pthread_once_t :: struct {
sig: c.long,
_: [PTHREAD_ONCE_SIZE] c.char,
}
pthread_rwlock_t :: struct {
sig: c.long,
_: [PTHREAD_RWLOCK_SIZE] c.char,
}
pthread_rwlockattr_t :: struct {
sig: c.long,
_: [PTHREAD_RWLOCKATTR_SIZE] c.char,
}
SCHED_OTHER :: 1 // Avoid if you are writing portable software.
SCHED_FIFO :: 4
SCHED_RR :: 2 // Round robin.
SCHED_PARAM_SIZE :: 4
sched_param :: struct {
sched_priority: c.int,
_: [SCHED_PARAM_SIZE] c.char,
}
// Source: https://github.com/apple/darwin-libpthread/blob/03c4628c8940cca6fd6a82957f683af804f62e7f/pthread/pthread.h#L138
PTHREAD_CREATE_JOINABLE :: 1
PTHREAD_CREATE_DETACHED :: 2
PTHREAD_INHERIT_SCHED :: 1
PTHREAD_EXPLICIT_SCHED :: 2
PTHREAD_PROCESS_SHARED :: 1
PTHREAD_PROCESS_PRIVATE :: 2
PTHREAD_MUTEX_NORMAL :: 0
PTHREAD_MUTEX_RECURSIVE :: 1
PTHREAD_MUTEX_ERRORCHECK :: 2
PTHREAD_CANCEL_ENABLE :: 0
PTHREAD_CANCEL_DISABLE :: 1
PTHREAD_CANCEL_DEFERRED :: 0
PTHREAD_CANCEL_ASYNCHRONOUS :: 1
foreign import pthread "system:System.framework"
@(default_calling_convention="c")
foreign pthread {
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
}

View File

@@ -1,122 +0,0 @@
#+build freebsd
package unix
import "core:c"
pthread_t :: distinct u64
// pthread_t :: struct #align(16) { x: u64 }
PTHREAD_COND_T_SIZE :: 8
PTHREAD_MUTEXATTR_T_SIZE :: 8
PTHREAD_CONDATTR_T_SIZE :: 8
PTHREAD_RWLOCKATTR_T_SIZE :: 8
PTHREAD_BARRIERATTR_T_SIZE :: 8
// WARNING: The sizes of these things are different yet again
// on non-X86!
when size_of(int) == 8 {
PTHREAD_ATTR_T_SIZE :: 8
PTHREAD_MUTEX_T_SIZE :: 8
PTHREAD_RWLOCK_T_SIZE :: 8
PTHREAD_BARRIER_T_SIZE :: 8
} else when size_of(int) == 4 { // TODO
PTHREAD_ATTR_T_SIZE :: 32
PTHREAD_MUTEX_T_SIZE :: 32
PTHREAD_RWLOCK_T_SIZE :: 44
PTHREAD_BARRIER_T_SIZE :: 20
}
pthread_cond_t :: struct #align(16) {
_: [PTHREAD_COND_T_SIZE] c.char,
}
pthread_mutex_t :: struct #align(16) {
_: [PTHREAD_MUTEX_T_SIZE] c.char,
}
pthread_rwlock_t :: struct #align(16) {
_: [PTHREAD_RWLOCK_T_SIZE] c.char,
}
pthread_barrier_t :: struct #align(16) {
_: [PTHREAD_BARRIER_T_SIZE] c.char,
}
pthread_attr_t :: struct #align(16) {
_: [PTHREAD_ATTR_T_SIZE] c.char,
}
pthread_condattr_t :: struct #align(16) {
_: [PTHREAD_CONDATTR_T_SIZE] c.char,
}
pthread_mutexattr_t :: struct #align(16) {
_: [PTHREAD_MUTEXATTR_T_SIZE] c.char,
}
pthread_rwlockattr_t :: struct #align(16) {
_: [PTHREAD_RWLOCKATTR_T_SIZE] c.char,
}
pthread_barrierattr_t :: struct #align(16) {
_: [PTHREAD_BARRIERATTR_T_SIZE] c.char,
}
PTHREAD_MUTEX_ERRORCHECK :: 1
PTHREAD_MUTEX_RECURSIVE :: 2
PTHREAD_MUTEX_NORMAL :: 3
PTHREAD_CREATE_JOINABLE :: 0
PTHREAD_CREATE_DETACHED :: 1
PTHREAD_INHERIT_SCHED :: 4
PTHREAD_EXPLICIT_SCHED :: 0
PTHREAD_PROCESS_PRIVATE :: 0
PTHREAD_PROCESS_SHARED :: 1
SCHED_FIFO :: 1
SCHED_OTHER :: 2
SCHED_RR :: 3 // Round robin.
sched_param :: struct {
sched_priority: c.int,
}
_usem :: struct {
_has_waiters: u32,
_count: u32,
_flags: u32,
}
_usem2 :: struct {
_count: u32,
_flags: u32,
}
sem_t :: struct {
_magic: u32,
_kern: _usem2,
_padding: u32,
}
PTHREAD_CANCEL_ENABLE :: 0
PTHREAD_CANCEL_DISABLE :: 1
PTHREAD_CANCEL_DEFERRED :: 0
PTHREAD_CANCEL_ASYNCHRONOUS :: 2
foreign import "system:pthread"
@(default_calling_convention="c")
foreign pthread {
// create named semaphore.
// used in process-shared semaphores.
sem_open :: proc(name: cstring, flags: c.int) -> ^sem_t ---
sem_init :: proc(sem: ^sem_t, pshared: c.int, initial_value: c.uint) -> c.int ---
sem_destroy :: proc(sem: ^sem_t) -> c.int ---
sem_post :: proc(sem: ^sem_t) -> c.int ---
sem_wait :: proc(sem: ^sem_t) -> c.int ---
sem_trywait :: proc(sem: ^sem_t) -> c.int ---
// sem_timedwait :: proc(sem: ^sem_t, timeout: time.TimeSpec) -> c.int ---
// NOTE: unclear whether pthread_yield is well-supported on Linux systems,
// see https://linux.die.net/man/3/pthread_yield
pthread_yield :: proc() ---
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
}

View File

@@ -1,71 +0,0 @@
package unix
import "core:c"
pthread_t :: distinct rawptr
pthread_attr_t :: distinct rawptr
pthread_mutex_t :: distinct rawptr
pthread_mutexattr_t :: distinct rawptr
pthread_cond_t :: distinct rawptr
pthread_condattr_t :: distinct rawptr
pthread_rwlock_t :: distinct rawptr
pthread_rwlockattr_t :: distinct rawptr
pthread_barrier_t :: distinct rawptr
pthread_barrierattr_t :: distinct rawptr
pthread_spinlock_t :: distinct rawptr
pthread_key_t :: distinct c.int
pthread_once_t :: struct {
state: c.int,
mutex: pthread_mutex_t,
}
PTHREAD_MUTEX_DEFAULT :: 0
PTHREAD_MUTEX_NORMAL :: 1
PTHREAD_MUTEX_ERRORCHECK :: 2
PTHREAD_MUTEX_RECURSIVE :: 3
PTHREAD_DETACHED :: 0x1
PTHREAD_SCOPE_SYSTEM :: 0x2
PTHREAD_INHERIT_SCHED :: 0x4
PTHREAD_NOFLOAT :: 0x8
PTHREAD_CREATE_DETACHED :: PTHREAD_DETACHED
PTHREAD_CREATE_JOINABLE :: 0
PTHREAD_SCOPE_PROCESS :: 0
PTHREAD_EXPLICIT_SCHED :: 0
SCHED_FIFO :: 1
SCHED_RR :: 2
SCHED_SPORADIC :: 3
SCHED_OTHER :: 4
sched_param :: struct {
sched_priority: c.int,
}
sem_t :: distinct rawptr
PTHREAD_CANCEL_ENABLE :: 0
PTHREAD_CANCEL_DISABLE :: 1
PTHREAD_CANCEL_DEFERRED :: 0
PTHREAD_CANCEL_ASYNCHRONOUS :: 2
foreign import libc "system:c"
@(default_calling_convention="c")
foreign libc {
sem_open :: proc(name: cstring, flags: c.int) -> ^sem_t ---
sem_init :: proc(sem: ^sem_t, pshared: c.int, initial_value: c.uint) -> c.int ---
sem_destroy :: proc(sem: ^sem_t) -> c.int ---
sem_post :: proc(sem: ^sem_t) -> c.int ---
sem_wait :: proc(sem: ^sem_t) -> c.int ---
sem_trywait :: proc(sem: ^sem_t) -> c.int ---
pthread_yield :: proc() ---
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
}

View File

@@ -1,124 +0,0 @@
#+build linux
package unix
import "core:c"
// TODO(tetra): For robustness, I'd like to mark this with align 16.
// I cannot currently do this.
// And at the time of writing there is a bug with putting it
// as the only field in a struct.
pthread_t :: distinct u64
// pthread_t :: struct #align(16) { x: u64 };
// NOTE(tetra): Got all the size constants from pthreadtypes-arch.h on my
// Linux machine.
PTHREAD_COND_T_SIZE :: 48
PTHREAD_MUTEXATTR_T_SIZE :: 4
PTHREAD_CONDATTR_T_SIZE :: 4
PTHREAD_RWLOCKATTR_T_SIZE :: 8
PTHREAD_BARRIERATTR_T_SIZE :: 4
// WARNING: The sizes of these things are different yet again
// on non-X86!
when size_of(int) == 8 {
PTHREAD_ATTR_T_SIZE :: 56
PTHREAD_MUTEX_T_SIZE :: 40
PTHREAD_RWLOCK_T_SIZE :: 56
PTHREAD_BARRIER_T_SIZE :: 32
} else when size_of(int) == 4 {
PTHREAD_ATTR_T_SIZE :: 32
PTHREAD_MUTEX_T_SIZE :: 32
PTHREAD_RWLOCK_T_SIZE :: 44
PTHREAD_BARRIER_T_SIZE :: 20
}
pthread_cond_t :: struct #align(16) {
_: [PTHREAD_COND_T_SIZE] c.char,
}
pthread_mutex_t :: struct #align(16) {
_: [PTHREAD_MUTEX_T_SIZE] c.char,
}
pthread_rwlock_t :: struct #align(16) {
_: [PTHREAD_RWLOCK_T_SIZE] c.char,
}
pthread_barrier_t :: struct #align(16) {
_: [PTHREAD_BARRIER_T_SIZE] c.char,
}
pthread_attr_t :: struct #align(16) {
_: [PTHREAD_ATTR_T_SIZE] c.char,
}
pthread_condattr_t :: struct #align(16) {
_: [PTHREAD_CONDATTR_T_SIZE] c.char,
}
pthread_mutexattr_t :: struct #align(16) {
_: [PTHREAD_MUTEXATTR_T_SIZE] c.char,
}
pthread_rwlockattr_t :: struct #align(16) {
_: [PTHREAD_RWLOCKATTR_T_SIZE] c.char,
}
pthread_barrierattr_t :: struct #align(16) {
_: [PTHREAD_BARRIERATTR_T_SIZE] c.char,
}
PTHREAD_MUTEX_NORMAL :: 0
PTHREAD_MUTEX_RECURSIVE :: 1
PTHREAD_MUTEX_ERRORCHECK :: 2
// TODO(tetra, 2019-11-01): Maybe make `enum c.int`s for these?
PTHREAD_CREATE_JOINABLE :: 0
PTHREAD_CREATE_DETACHED :: 1
PTHREAD_INHERIT_SCHED :: 0
PTHREAD_EXPLICIT_SCHED :: 1
PTHREAD_PROCESS_PRIVATE :: 0
PTHREAD_PROCESS_SHARED :: 1
SCHED_OTHER :: 0
SCHED_FIFO :: 1
SCHED_RR :: 2 // Round robin.
sched_param :: struct {
sched_priority: c.int,
}
sem_t :: struct #align(16) {
_: [SEM_T_SIZE] c.char,
}
when size_of(int) == 8 {
SEM_T_SIZE :: 32
} else when size_of(int) == 4 {
SEM_T_SIZE :: 16
}
PTHREAD_CANCEL_ENABLE :: 0
PTHREAD_CANCEL_DISABLE :: 1
PTHREAD_CANCEL_DEFERRED :: 0
PTHREAD_CANCEL_ASYNCHRONOUS :: 1
foreign import "system:pthread"
@(default_calling_convention="c")
foreign pthread {
// create named semaphore.
// used in process-shared semaphores.
sem_open :: proc(name: cstring, flags: c.int) -> ^sem_t ---
sem_init :: proc(sem: ^sem_t, pshared: c.int, initial_value: c.uint) -> c.int ---
sem_destroy :: proc(sem: ^sem_t) -> c.int ---
sem_post :: proc(sem: ^sem_t) -> c.int ---
sem_wait :: proc(sem: ^sem_t) -> c.int ---
sem_trywait :: proc(sem: ^sem_t) -> c.int ---
// sem_timedwait :: proc(sem: ^sem_t, timeout: time.TimeSpec) -> c.int ---;
// NOTE: unclear whether pthread_yield is well-supported on Linux systems,
// see https://linux.die.net/man/3/pthread_yield
pthread_yield :: proc() -> c.int ---
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
}

View File

@@ -1,102 +0,0 @@
package unix
import "core:c"
pthread_t :: distinct rawptr
SEM_T_SIZE :: 8
PTHREAD_CONDATTR_T_SIZE :: 16
PTHREAD_MUTEXATTR_T_SIZE :: 16
PTHREAD_RWLOCKATTR_T_SIZE :: 16
PTHREAD_BARRIERATTR_T_SIZE :: 16
PTHREAD_COND_T_SIZE :: 40
PTHREAD_MUTEX_T_SIZE :: 48
PTHREAD_RWLOCK_T_SIZE :: 64
PTHREAD_BARRIER_T_SIZE :: 48
PTHREAD_ATTR_T_SIZE :: 16
pthread_cond_t :: struct #align(8) {
_: [PTHREAD_COND_T_SIZE] c.char,
}
pthread_mutex_t :: struct #align(8) {
_: [PTHREAD_MUTEX_T_SIZE] c.char,
}
pthread_rwlock_t :: struct #align(8) {
_: [PTHREAD_RWLOCK_T_SIZE] c.char,
}
pthread_barrier_t :: struct #align(8) {
_: [PTHREAD_BARRIER_T_SIZE] c.char,
}
pthread_attr_t :: struct #align(8) {
_: [PTHREAD_ATTR_T_SIZE] c.char,
}
pthread_condattr_t :: struct #align(8) {
_: [PTHREAD_CONDATTR_T_SIZE] c.char,
}
pthread_mutexattr_t :: struct #align(8) {
_: [PTHREAD_MUTEXATTR_T_SIZE] c.char,
}
pthread_rwlockattr_t :: struct #align(8) {
_: [PTHREAD_RWLOCKATTR_T_SIZE] c.char,
}
pthread_barrierattr_t :: struct #align(8) {
_: [PTHREAD_BARRIERATTR_T_SIZE] c.char,
}
PTHREAD_MUTEX_NORMAL :: 0
PTHREAD_MUTEX_ERRORCHECK :: 1
PTHREAD_MUTEX_RECURSIVE :: 2
PTHREAD_CREATE_JOINABLE :: 0
PTHREAD_CREATE_DETACHED :: 1
PTHREAD_INHERIT_SCHED :: 0
PTHREAD_EXPLICIT_SCHED :: 1
PTHREAD_PROCESS_PRIVATE :: 0
PTHREAD_PROCESS_SHARED :: 1
SCHED_NONE :: -1
SCHED_OTHER :: 0
SCHED_FIFO :: 1
SCHED_RR :: 3
sched_param :: struct {
sched_priority: c.int,
}
sem_t :: struct #align(16) {
_: [SEM_T_SIZE] c.char,
}
PTHREAD_CANCEL_ENABLE :: 0
PTHREAD_CANCEL_DISABLE :: 1
PTHREAD_CANCEL_DEFERRED :: 0
PTHREAD_CANCEL_ASYNCHRONOUS :: 1
foreign import "system:pthread"
@(default_calling_convention="c")
foreign pthread {
sem_open :: proc(name: cstring, flags: c.int) -> ^sem_t ---
sem_init :: proc(sem: ^sem_t, pshared: c.int, initial_value: c.uint) -> c.int ---
sem_destroy :: proc(sem: ^sem_t) -> c.int ---
sem_post :: proc(sem: ^sem_t) -> c.int ---
sem_wait :: proc(sem: ^sem_t) -> c.int ---
sem_trywait :: proc(sem: ^sem_t) -> c.int ---
pthread_yield :: proc() ---
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
}

View File

@@ -1,74 +0,0 @@
#+build openbsd
package unix
import "core:c"
pthread_t :: distinct rawptr
pthread_attr_t :: distinct rawptr
pthread_mutex_t :: distinct rawptr
pthread_mutexattr_t :: distinct rawptr
pthread_cond_t :: distinct rawptr
pthread_condattr_t :: distinct rawptr
pthread_rwlock_t :: distinct rawptr
pthread_rwlockattr_t :: distinct rawptr
pthread_barrier_t :: distinct rawptr
pthread_barrierattr_t :: distinct rawptr
pthread_spinlock_t :: distinct rawptr
pthread_key_t :: distinct c.int
pthread_once_t :: struct {
state: c.int,
mutex: pthread_mutex_t,
}
PTHREAD_MUTEX_ERRORCHECK :: 1
PTHREAD_MUTEX_RECURSIVE :: 2
PTHREAD_MUTEX_NORMAL :: 3
PTHREAD_MUTEX_STRICT_NP :: 4
PTHREAD_DETACHED :: 0x1
PTHREAD_SCOPE_SYSTEM :: 0x2
PTHREAD_INHERIT_SCHED :: 0x4
PTHREAD_NOFLOAT :: 0x8
PTHREAD_CREATE_DETACHED :: PTHREAD_DETACHED
PTHREAD_CREATE_JOINABLE :: 0
PTHREAD_SCOPE_PROCESS :: 0
PTHREAD_EXPLICIT_SCHED :: 0
SCHED_FIFO :: 1
SCHED_OTHER :: 2
SCHED_RR :: 3
sched_param :: struct {
sched_priority: c.int,
}
sem_t :: distinct rawptr
PTHREAD_CANCEL_ENABLE :: 0
PTHREAD_CANCEL_DISABLE :: 1
PTHREAD_CANCEL_DEFERRED :: 0
PTHREAD_CANCEL_ASYNCHRONOUS :: 2
foreign import libc "system:c"
@(default_calling_convention="c")
foreign libc {
sem_open :: proc(name: cstring, flags: c.int) -> ^sem_t ---
sem_init :: proc(sem: ^sem_t, pshared: c.int, initial_value: c.uint) -> c.int ---
sem_destroy :: proc(sem: ^sem_t) -> c.int ---
sem_post :: proc(sem: ^sem_t) -> c.int ---
sem_wait :: proc(sem: ^sem_t) -> c.int ---
sem_trywait :: proc(sem: ^sem_t) -> c.int ---
//sem_timedwait :: proc(sem: ^sem_t, timeout: time.TimeSpec) -> c.int ---
// NOTE: unclear whether pthread_yield is well-supported on Linux systems,
// see https://linux.die.net/man/3/pthread_yield
pthread_yield :: proc() ---
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
}

View File

@@ -1,127 +0,0 @@
#+build linux, darwin, freebsd, openbsd, netbsd, haiku
package unix
foreign import "system:pthread"
import "core:c"
timespec :: struct {
tv_sec: i64,
tv_nsec: i64,
}
//
// On success, these functions return 0.
//
@(default_calling_convention="c")
foreign pthread {
pthread_create :: proc(t: ^pthread_t, attrs: ^pthread_attr_t, routine: proc(data: rawptr) -> rawptr, arg: rawptr) -> c.int ---
// 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 ---
pthread_detach :: proc(t: pthread_t) -> c.int ---
sched_get_priority_min :: proc(policy: c.int) -> c.int ---
sched_get_priority_max :: proc(policy: c.int) -> c.int ---
// NOTE: POSIX says this can fail with OOM.
pthread_attr_init :: proc(attrs: ^pthread_attr_t) -> c.int ---
pthread_attr_destroy :: proc(attrs: ^pthread_attr_t) -> c.int ---
pthread_attr_getschedparam :: proc(attrs: ^pthread_attr_t, param: ^sched_param) -> c.int ---
pthread_attr_setschedparam :: proc(attrs: ^pthread_attr_t, param: ^sched_param) -> c.int ---
// states: PTHREAD_CREATE_DETACHED, PTHREAD_CREATE_JOINABLE
pthread_attr_setdetachstate :: proc(attrs: ^pthread_attr_t, detach_state: c.int) -> c.int ---
// NOTE(tetra, 2019-11-06): WARNING: Different systems have different alignment requirements.
// For maximum usefulness, use the OS's page size.
// ALSO VERY MAJOR WARNING: `stack_ptr` must be the LAST byte of the stack on systems
// where the stack grows downwards, which is the common case, so far as I know.
// On systems where it grows upwards, give the FIRST byte instead.
// ALSO SLIGHTLY LESS MAJOR WARNING: Using this procedure DISABLES automatically-provided
// guard pages. If you are using this procedure, YOU must set them up manually.
// If you forget to do this, you WILL get stack corruption bugs if you do not EXTREMELY
// know what you are doing!
pthread_attr_setstack :: proc(attrs: ^pthread_attr_t, stack_ptr: rawptr, stack_size: u64) -> c.int ---
pthread_attr_getstack :: proc(attrs: ^pthread_attr_t, stack_ptr: ^rawptr, stack_size: ^u64) -> c.int ---
pthread_sigmask :: proc(how: c.int, set: rawptr, oldset: rawptr) -> c.int ---
sched_yield :: proc() -> c.int ---
}
// NOTE: Unimplemented in Haiku.
when ODIN_OS != .Haiku {
foreign pthread {
// scheds: PTHREAD_INHERIT_SCHED, PTHREAD_EXPLICIT_SCHED
pthread_attr_setinheritsched :: proc(attrs: ^pthread_attr_t, sched: c.int) -> c.int ---
pthread_attr_getschedpolicy :: proc(t: ^pthread_attr_t, policy: ^c.int) -> c.int ---
pthread_attr_setschedpolicy :: proc(t: ^pthread_attr_t, policy: c.int) -> c.int ---
}
}
@(default_calling_convention="c")
foreign pthread {
// NOTE: POSIX says this can fail with OOM.
pthread_cond_init :: proc(cond: ^pthread_cond_t, attrs: ^pthread_condattr_t) -> c.int ---
pthread_cond_destroy :: proc(cond: ^pthread_cond_t) -> c.int ---
pthread_cond_signal :: proc(cond: ^pthread_cond_t) -> c.int ---
// same as signal, but wakes up _all_ threads that are waiting
pthread_cond_broadcast :: proc(cond: ^pthread_cond_t) -> c.int ---
// assumes the mutex is pre-locked
pthread_cond_wait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t) -> c.int ---
pthread_cond_timedwait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t, timeout: ^timespec) -> c.int ---
pthread_condattr_init :: proc(attrs: ^pthread_condattr_t) -> c.int ---
pthread_condattr_destroy :: proc(attrs: ^pthread_condattr_t) -> c.int ---
// p-shared = "process-shared" - i.e: is this condition shared among multiple processes?
// values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
pthread_condattr_setpshared :: proc(attrs: ^pthread_condattr_t, value: c.int) -> c.int ---
pthread_condattr_getpshared :: proc(attrs: ^pthread_condattr_t, result: ^c.int) -> c.int ---
}
@(default_calling_convention="c")
foreign pthread {
// NOTE: POSIX says this can fail with OOM.
pthread_mutex_init :: proc(mutex: ^pthread_mutex_t, attrs: ^pthread_mutexattr_t) -> c.int ---
pthread_mutex_destroy :: proc(mutex: ^pthread_mutex_t) -> c.int ---
pthread_mutex_trylock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
pthread_mutex_lock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
pthread_mutex_timedlock :: proc(mutex: ^pthread_mutex_t, timeout: ^timespec) -> c.int ---
pthread_mutex_unlock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
pthread_mutexattr_init :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---
pthread_mutexattr_destroy :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---
pthread_mutexattr_settype :: proc(attrs: ^pthread_mutexattr_t, type: c.int) -> c.int ---
// p-shared = "process-shared" - i.e: is this mutex shared among multiple processes?
// values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
pthread_mutexattr_setpshared :: proc(attrs: ^pthread_mutexattr_t, value: c.int) -> c.int ---
pthread_mutexattr_getpshared :: proc(attrs: ^pthread_mutexattr_t, result: ^c.int) -> c.int ---
pthread_testcancel :: proc () ---
}

5
core/sys/unix/unix.odin Normal file
View File

@@ -0,0 +1,5 @@
package unix
import "core:sys/posix"
timespec :: posix.timespec

View File

@@ -15,7 +15,7 @@ import "core:c/libc"
import "core:encoding/ansi"
import "core:sync"
import "core:os"
@require import "core:sys/unix"
@require import "core:sys/posix"
@(private="file") stop_runner_flag: libc.sig_atomic_t
@@ -114,8 +114,8 @@ This is a dire bug and should be reported to the Odin developers.
// properly set to PTHREAD_CANCEL_ASYNCHRONOUS.
//
// The runner would stall after returning from `pthread_cancel`.
unix.pthread_testcancel()
posix.pthread_testcancel()
}
}
}

View File

@@ -4,14 +4,14 @@ package thread
import "base:runtime"
import "core:sync"
import "core:sys/unix"
import "core:sys/posix"
_IS_SUPPORTED :: true
// NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
// Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
Thread_Os_Specific :: struct #align(16) {
unix_thread: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
unix_thread: posix.pthread_t, // NOTE: very large on Darwin, small on Linux.
start_ok: sync.Sema,
}
//
@@ -23,7 +23,7 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
t := (^Thread)(t)
// We need to give the thread a moment to start up before we enable cancellation.
can_set_thread_cancel_state := unix.pthread_setcancelstate(unix.PTHREAD_CANCEL_ENABLE, nil) == 0
can_set_thread_cancel_state := posix.pthread_setcancelstate(.ENABLE, nil) == nil
t.id = sync.current_thread_id()
@@ -37,8 +37,8 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
// Enable thread's cancelability.
if can_set_thread_cancel_state {
unix.pthread_setcanceltype (unix.PTHREAD_CANCEL_ASYNCHRONOUS, nil)
unix.pthread_setcancelstate(unix.PTHREAD_CANCEL_ENABLE, nil)
posix.pthread_setcanceltype (.ASYNCHRONOUS, nil)
posix.pthread_setcancelstate(.ENABLE, nil)
}
{
@@ -59,8 +59,8 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
sync.atomic_or(&t.flags, { .Done })
if .Self_Cleanup in sync.atomic_load(&t.flags) {
res := unix.pthread_detach(t.unix_thread)
assert_contextless(res == 0)
res := posix.pthread_detach(t.unix_thread)
assert_contextless(res == nil)
t.unix_thread = {}
// NOTE(ftphikari): It doesn't matter which context 'free' received, right?
@@ -71,19 +71,19 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
return nil
}
attrs: unix.pthread_attr_t
if unix.pthread_attr_init(&attrs) != 0 {
attrs: posix.pthread_attr_t
if posix.pthread_attr_init(&attrs) != nil {
return nil // NOTE(tetra, 2019-11-01): POSIX OOM.
}
defer unix.pthread_attr_destroy(&attrs)
defer posix.pthread_attr_destroy(&attrs)
// NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
res: i32
res = unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE)
assert(res == 0)
res: posix.Errno
res = posix.pthread_attr_setdetachstate(&attrs, .CREATE_JOINABLE)
assert(res == nil)
when ODIN_OS != .Haiku && ODIN_OS != .NetBSD {
res = unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED)
assert(res == 0)
res = posix.pthread_attr_setinheritsched(&attrs, .EXPLICIT_SCHED)
assert(res == nil)
}
thread := new(Thread)
@@ -93,26 +93,26 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
thread.creation_allocator = context.allocator
// Set thread priority.
policy: i32
policy: posix.Sched_Policy
when ODIN_OS != .Haiku && ODIN_OS != .NetBSD {
res = unix.pthread_attr_getschedpolicy(&attrs, &policy)
assert(res == 0)
res = posix.pthread_attr_getschedpolicy(&attrs, &policy)
assert(res == nil)
}
params: unix.sched_param
res = unix.pthread_attr_getschedparam(&attrs, &params)
assert(res == 0)
low := unix.sched_get_priority_min(policy)
high := unix.sched_get_priority_max(policy)
params: posix.sched_param
res = posix.pthread_attr_getschedparam(&attrs, &params)
assert(res == nil)
low := posix.sched_get_priority_min(policy)
high := posix.sched_get_priority_max(policy)
switch priority {
case .Normal: // Okay
case .Low: params.sched_priority = low + 1
case .High: params.sched_priority = high
}
res = unix.pthread_attr_setschedparam(&attrs, &params)
assert(res == 0)
res = posix.pthread_attr_setschedparam(&attrs, &params)
assert(res == nil)
thread.procedure = procedure
if unix.pthread_create(&thread.unix_thread, &attrs, __unix_thread_entry_proc, thread) != 0 {
if posix.pthread_create(&thread.unix_thread, &attrs, __unix_thread_entry_proc, thread) != nil {
free(thread, thread.creation_allocator)
return nil
}
@@ -130,7 +130,7 @@ _is_done :: proc(t: ^Thread) -> bool {
}
_join :: proc(t: ^Thread) {
if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
if posix.pthread_equal(posix.pthread_self(), t.unix_thread) {
return
}
@@ -144,7 +144,7 @@ _join :: proc(t: ^Thread) {
if .Started not_in sync.atomic_load(&t.flags) {
_start(t)
}
unix.pthread_join(t.unix_thread, nil)
posix.pthread_join(t.unix_thread, nil)
}
_join_multiple :: proc(threads: ..^Thread) {
@@ -170,9 +170,9 @@ _terminate :: proc(t: ^Thread, exit_code: int) {
//
// This is in contrast to behavior I have seen on Linux where the thread is
// just terminated.
unix.pthread_cancel(t.unix_thread)
posix.pthread_cancel(t.unix_thread)
}
_yield :: proc() {
unix.sched_yield()
posix.sched_yield()
}