mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
[thread] Refactor handling of 'init_context' + add doc comments for it
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
// +private
|
||||
package thread
|
||||
|
||||
import "core:runtime"
|
||||
import "core:intrinsics"
|
||||
import "core:sync"
|
||||
import "core:sys/unix"
|
||||
@@ -27,7 +26,7 @@ Thread_Os_Specific :: struct #align 16 {
|
||||
// Creates a thread which will run the given procedure.
|
||||
// It then waits for `start` to be called.
|
||||
//
|
||||
_create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
|
||||
_create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
|
||||
__linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
|
||||
t := (^Thread)(t)
|
||||
|
||||
@@ -36,8 +35,6 @@ _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^
|
||||
can_set_thread_cancel_state := unix.pthread_setcancelstate(unix.PTHREAD_CANCEL_DISABLE, nil) == 0
|
||||
}
|
||||
|
||||
context = runtime.default_context()
|
||||
|
||||
sync.lock(&t.mutex)
|
||||
|
||||
t.id = sync.current_thread_id()
|
||||
@@ -46,9 +43,6 @@ _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^
|
||||
sync.wait(&t.cond, &t.mutex)
|
||||
}
|
||||
|
||||
init_context := t.init_context
|
||||
context = init_context.? or_else runtime.default_context()
|
||||
|
||||
when ODIN_OS != .Darwin {
|
||||
// Enable thread's cancelability.
|
||||
if can_set_thread_cancel_state {
|
||||
@@ -57,16 +51,22 @@ _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^
|
||||
}
|
||||
}
|
||||
|
||||
t.procedure(t)
|
||||
{
|
||||
init_context := t.init_context
|
||||
|
||||
// NOTE(tetra, 2023-05-31): Must do this AFTER thread.start() is called, so that the user can set the init_context, etc!
|
||||
// Here on Unix, we start the OS thread in a running state, and so we manually have it wait on a condition
|
||||
// variable above. We must perform that waiting BEFORE we select the context!
|
||||
context = _select_context_for_thread(init_context)
|
||||
defer _maybe_destroy_default_temp_allocator(init_context)
|
||||
|
||||
t.procedure(t)
|
||||
}
|
||||
|
||||
intrinsics.atomic_store(&t.flags, t.flags + { .Done })
|
||||
|
||||
sync.unlock(&t.mutex)
|
||||
|
||||
if init_context == nil && context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
|
||||
runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user