mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Convert POSIX Thread to use semaphore instead
One less value to store, and it should be less of a hack too. Semaphores will not wait around if they have the go-ahead; they depend on an internal value being non-zero, instead of whatever was loaded when they started waiting, which is the case with a `Cond`.
This commit is contained in:
@@ -5,7 +5,6 @@ package thread
|
|||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
import "core:sync"
|
import "core:sync"
|
||||||
import "core:sys/unix"
|
import "core:sys/unix"
|
||||||
import "core:time"
|
|
||||||
|
|
||||||
_IS_SUPPORTED :: true
|
_IS_SUPPORTED :: true
|
||||||
|
|
||||||
@@ -13,8 +12,7 @@ _IS_SUPPORTED :: true
|
|||||||
// 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: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
|
||||||
cond: sync.Cond,
|
start_ok: sync.Sema,
|
||||||
mutex: sync.Mutex,
|
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
// Creates a thread which will run the given procedure.
|
// Creates a thread which will run the given procedure.
|
||||||
@@ -27,14 +25,10 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
|
|||||||
// 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 := unix.pthread_setcancelstate(unix.PTHREAD_CANCEL_ENABLE, nil) == 0
|
||||||
|
|
||||||
sync.lock(&t.mutex)
|
|
||||||
|
|
||||||
t.id = sync.current_thread_id()
|
t.id = sync.current_thread_id()
|
||||||
|
|
||||||
for (.Started not_in sync.atomic_load(&t.flags)) {
|
if .Started not_in sync.atomic_load(&t.flags) {
|
||||||
// HACK: use a timeout so in the event that the condition is signalled at THIS comment's exact point
|
sync.wait(&t.start_ok)
|
||||||
// (after checking flags, before starting the wait) it gets itself out of that deadlock after a ms.
|
|
||||||
sync.wait_with_timeout(&t.cond, &t.mutex, time.Millisecond)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if .Joined in sync.atomic_load(&t.flags) {
|
if .Joined in sync.atomic_load(&t.flags) {
|
||||||
@@ -64,8 +58,6 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
|
|||||||
|
|
||||||
sync.atomic_or(&t.flags, { .Done })
|
sync.atomic_or(&t.flags, { .Done })
|
||||||
|
|
||||||
sync.unlock(&t.mutex)
|
|
||||||
|
|
||||||
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)
|
res := unix.pthread_detach(t.unix_thread)
|
||||||
assert_contextless(res == 0)
|
assert_contextless(res == 0)
|
||||||
@@ -130,7 +122,7 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
|
|||||||
|
|
||||||
_start :: proc(t: ^Thread) {
|
_start :: proc(t: ^Thread) {
|
||||||
sync.atomic_or(&t.flags, { .Started })
|
sync.atomic_or(&t.flags, { .Started })
|
||||||
sync.signal(&t.cond)
|
sync.post(&t.start_ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
_is_done :: proc(t: ^Thread) -> bool {
|
_is_done :: proc(t: ^Thread) -> bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user