|
@@ -4,14 +4,14 @@ package thread
|
|
|
|
|
|
import "base:runtime"
|
|
import "base:runtime"
|
|
import "core:sync"
|
|
import "core:sync"
|
|
-import "core:sys/unix"
|
|
|
|
|
|
+import "core:sys/posix"
|
|
|
|
|
|
_IS_SUPPORTED :: true
|
|
_IS_SUPPORTED :: true
|
|
|
|
|
|
// NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
|
|
// NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
|
|
// Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
|
|
// Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
|
|
Thread_Os_Specific :: struct #align(16) {
|
|
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,
|
|
start_ok: sync.Sema,
|
|
}
|
|
}
|
|
//
|
|
//
|
|
@@ -23,7 +23,7 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
|
|
t := (^Thread)(t)
|
|
t := (^Thread)(t)
|
|
|
|
|
|
// We need to give the thread a moment to start up before we enable cancellation.
|
|
// 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()
|
|
t.id = sync.current_thread_id()
|
|
|
|
|
|
@@ -37,8 +37,8 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
|
|
|
|
|
|
// Enable thread's cancelability.
|
|
// Enable thread's cancelability.
|
|
if can_set_thread_cancel_state {
|
|
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 })
|
|
sync.atomic_or(&t.flags, { .Done })
|
|
|
|
|
|
if .Self_Cleanup in sync.atomic_load(&t.flags) {
|
|
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 = {}
|
|
t.unix_thread = {}
|
|
// NOTE(ftphikari): It doesn't matter which context 'free' received, right?
|
|
// 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
|
|
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.
|
|
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.
|
|
// 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 {
|
|
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)
|
|
thread := new(Thread)
|
|
@@ -93,26 +93,26 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
|
|
thread.creation_allocator = context.allocator
|
|
thread.creation_allocator = context.allocator
|
|
|
|
|
|
// Set thread priority.
|
|
// Set thread priority.
|
|
- policy: i32
|
|
|
|
|
|
+ policy: posix.Sched_Policy
|
|
when ODIN_OS != .Haiku && ODIN_OS != .NetBSD {
|
|
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, ¶ms)
|
|
|
|
- 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, ¶ms)
|
|
|
|
+ assert(res == nil)
|
|
|
|
+ low := posix.sched_get_priority_min(policy)
|
|
|
|
+ high := posix.sched_get_priority_max(policy)
|
|
switch priority {
|
|
switch priority {
|
|
case .Normal: // Okay
|
|
case .Normal: // Okay
|
|
case .Low: params.sched_priority = low + 1
|
|
case .Low: params.sched_priority = low + 1
|
|
case .High: params.sched_priority = high
|
|
case .High: params.sched_priority = high
|
|
}
|
|
}
|
|
- res = unix.pthread_attr_setschedparam(&attrs, ¶ms)
|
|
|
|
- assert(res == 0)
|
|
|
|
|
|
+ res = posix.pthread_attr_setschedparam(&attrs, ¶ms)
|
|
|
|
+ assert(res == nil)
|
|
|
|
|
|
thread.procedure = procedure
|
|
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)
|
|
free(thread, thread.creation_allocator)
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
@@ -130,7 +130,7 @@ _is_done :: proc(t: ^Thread) -> bool {
|
|
}
|
|
}
|
|
|
|
|
|
_join :: proc(t: ^Thread) {
|
|
_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
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
@@ -144,7 +144,7 @@ _join :: proc(t: ^Thread) {
|
|
if .Started not_in sync.atomic_load(&t.flags) {
|
|
if .Started not_in sync.atomic_load(&t.flags) {
|
|
_start(t)
|
|
_start(t)
|
|
}
|
|
}
|
|
- unix.pthread_join(t.unix_thread, nil)
|
|
|
|
|
|
+ posix.pthread_join(t.unix_thread, nil)
|
|
}
|
|
}
|
|
|
|
|
|
_join_multiple :: proc(threads: ..^Thread) {
|
|
_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
|
|
// This is in contrast to behavior I have seen on Linux where the thread is
|
|
// just terminated.
|
|
// just terminated.
|
|
- unix.pthread_cancel(t.unix_thread)
|
|
|
|
|
|
+ posix.pthread_cancel(t.unix_thread)
|
|
}
|
|
}
|
|
|
|
|
|
_yield :: proc() {
|
|
_yield :: proc() {
|
|
- unix.sched_yield()
|
|
|
|
|
|
+ posix.sched_yield()
|
|
}
|
|
}
|