Fix early join after start.

This commit is contained in:
Jeroen van Rijn
2025-06-21 11:47:00 +02:00
parent 8dc374a6ae
commit 1903d7211e
5 changed files with 75 additions and 75 deletions
+5 -6
View File
@@ -29,14 +29,10 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
t.id = sync.current_thread_id()
if .Started not_in sync.atomic_load(&t.flags) {
for (.Started not_in sync.atomic_load(&t.flags)) {
sync.wait(&t.start_ok)
}
if .Joined in sync.atomic_load(&t.flags) {
return nil
}
// Enable thread's cancelability.
// NOTE(laytan): Darwin does not correctly/fully support all of this, not doing this does
// actually make pthread_cancel work in the capacity of my tests, while executing this would
@@ -148,10 +144,13 @@ _join :: proc(t: ^Thread) {
// Prevent non-started threads from blocking main thread with initial wait
// condition.
if .Started not_in sync.atomic_load(&t.flags) {
for (.Started not_in sync.atomic_load(&t.flags)) {
_start(t)
}
posix.pthread_join(t.unix_thread, nil)
t.flags += {.Joined}
}
_join_multiple :: proc(threads: ..^Thread) {
+8 -7
View File
@@ -13,6 +13,7 @@ Thread_Os_Specific :: struct {
win32_thread: win32.HANDLE,
win32_thread_id: win32.DWORD,
mutex: sync.Mutex,
start_ok: sync.Sema,
}
_thread_priority_map := [Thread_Priority]i32{
@@ -27,8 +28,8 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
__windows_thread_entry_proc :: proc "system" (t_: rawptr) -> win32.DWORD {
t := (^Thread)(t_)
if .Joined in sync.atomic_load(&t.flags) {
return 0
for (.Started not_in sync.atomic_load(&t.flags)) {
sync.wait(&t.start_ok)
}
{
@@ -102,16 +103,15 @@ _join :: proc(t: ^Thread) {
return
}
t.flags += {.Joined}
if .Started not_in t.flags {
t.flags += {.Started}
win32.ResumeThread(t.win32_thread)
for (.Started not_in sync.atomic_load(&t.flags)) {
_start(t)
}
win32.WaitForSingleObject(t.win32_thread, win32.INFINITE)
win32.CloseHandle(t.win32_thread)
t.win32_thread = win32.INVALID_HANDLE
t.flags += {.Joined}
}
_join_multiple :: proc(threads: ..^Thread) {
@@ -135,6 +135,7 @@ _join_multiple :: proc(threads: ..^Thread) {
for t in threads {
win32.CloseHandle(t.win32_thread)
t.win32_thread = win32.INVALID_HANDLE
t.flags += {.Joined}
}
}