mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 22:58:46 +00:00
Revert *nix thread stuff to old sync
(I was just testing)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package thread
|
package thread
|
||||||
|
|
||||||
import "intrinsics"
|
import "intrinsics"
|
||||||
import sync "core:sync/sync2"
|
import "core:sync"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
|
||||||
Task_Status :: enum i32 {
|
Task_Status :: enum i32 {
|
||||||
@@ -26,7 +26,7 @@ INVALID_TASK_ID :: Task_Id(-1);
|
|||||||
Pool :: struct {
|
Pool :: struct {
|
||||||
allocator: mem.Allocator,
|
allocator: mem.Allocator,
|
||||||
mutex: sync.Mutex,
|
mutex: sync.Mutex,
|
||||||
sem_available: sync.Sema,
|
sem_available: sync.Semaphore,
|
||||||
processing_task_count: int, // atomic
|
processing_task_count: int, // atomic
|
||||||
is_running: bool,
|
is_running: bool,
|
||||||
|
|
||||||
@@ -40,14 +40,14 @@ pool_init :: proc(pool: ^Pool, thread_count: int, allocator := context.allocator
|
|||||||
pool := (^Pool)(t.data);
|
pool := (^Pool)(t.data);
|
||||||
|
|
||||||
for pool.is_running {
|
for pool.is_running {
|
||||||
sync.sema_wait(&pool.sem_available);
|
sync.semaphore_wait_for(&pool.sem_available);
|
||||||
|
|
||||||
if task, ok := pool_try_and_pop_task(pool); ok {
|
if task, ok := pool_try_and_pop_task(pool); ok {
|
||||||
pool_do_work(pool, &task);
|
pool_do_work(pool, &task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sync.sema_post(&pool.sem_available);
|
sync.semaphore_post(&pool.sem_available, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -56,6 +56,8 @@ pool_init :: proc(pool: ^Pool, thread_count: int, allocator := context.allocator
|
|||||||
pool.tasks = make([dynamic]Task);
|
pool.tasks = make([dynamic]Task);
|
||||||
pool.threads = make([]^Thread, thread_count);
|
pool.threads = make([]^Thread, thread_count);
|
||||||
|
|
||||||
|
sync.mutex_init(&pool.mutex);
|
||||||
|
sync.semaphore_init(&pool.sem_available);
|
||||||
pool.is_running = true;
|
pool.is_running = true;
|
||||||
|
|
||||||
for _, i in pool.threads {
|
for _, i in pool.threads {
|
||||||
@@ -74,6 +76,9 @@ pool_destroy :: proc(pool: ^Pool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete(pool.threads, pool.allocator);
|
delete(pool.threads, pool.allocator);
|
||||||
|
|
||||||
|
sync.mutex_destroy(&pool.mutex);
|
||||||
|
sync.semaphore_destroy(&pool.sem_available);
|
||||||
}
|
}
|
||||||
|
|
||||||
pool_start :: proc(pool: ^Pool) {
|
pool_start :: proc(pool: ^Pool) {
|
||||||
@@ -85,7 +90,7 @@ pool_start :: proc(pool: ^Pool) {
|
|||||||
pool_join :: proc(pool: ^Pool) {
|
pool_join :: proc(pool: ^Pool) {
|
||||||
pool.is_running = false;
|
pool.is_running = false;
|
||||||
|
|
||||||
sync.sema_post(&pool.sem_available, len(pool.threads));
|
sync.semaphore_post(&pool.sem_available, len(pool.threads));
|
||||||
|
|
||||||
yield();
|
yield();
|
||||||
|
|
||||||
@@ -104,7 +109,7 @@ pool_add_task :: proc(pool: ^Pool, procedure: Task_Proc, data: rawptr, user_inde
|
|||||||
task.user_index = user_index;
|
task.user_index = user_index;
|
||||||
|
|
||||||
append(&pool.tasks, task);
|
append(&pool.tasks, task);
|
||||||
sync.sema_post(&pool.sem_available);
|
sync.semaphore_post(&pool.sem_available, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pool_try_and_pop_task :: proc(pool: ^Pool) -> (task: Task, got_task: bool = false) {
|
pool_try_and_pop_task :: proc(pool: ^Pool) -> (task: Task, got_task: bool = false) {
|
||||||
@@ -135,7 +140,7 @@ pool_wait_and_process :: proc(pool: ^Pool) {
|
|||||||
// Safety kick
|
// Safety kick
|
||||||
if len(pool.tasks) != 0 && intrinsics.atomic_load(&pool.processing_task_count) == 0 {
|
if len(pool.tasks) != 0 && intrinsics.atomic_load(&pool.processing_task_count) == 0 {
|
||||||
sync.mutex_lock(&pool.mutex);
|
sync.mutex_lock(&pool.mutex);
|
||||||
sync.sema_post(&pool.sem_available, len(pool.tasks));
|
sync.semaphore_post(&pool.sem_available, len(pool.tasks));
|
||||||
sync.mutex_unlock(&pool.mutex);
|
sync.mutex_unlock(&pool.mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package thread
|
|||||||
|
|
||||||
import "core:runtime"
|
import "core:runtime"
|
||||||
import "core:intrinsics"
|
import "core:intrinsics"
|
||||||
import sync "core:sync/sync2"
|
import "core:sync"
|
||||||
import "core:sys/unix"
|
import "core:sys/unix"
|
||||||
|
|
||||||
// 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.
|
||||||
@@ -20,7 +20,7 @@ Thread_Os_Specific :: struct #align 16 {
|
|||||||
// in a suspended state, we have it wait on this gate, which we
|
// in a suspended state, we have it wait on this gate, which we
|
||||||
// signal to start it.
|
// signal to start it.
|
||||||
// destroyed after thread is started.
|
// destroyed after thread is started.
|
||||||
start_gate: sync.Cond,
|
start_gate: sync.Condition,
|
||||||
start_mutex: sync.Mutex,
|
start_mutex: sync.Mutex,
|
||||||
|
|
||||||
// if true, the thread has been started and the start_gate has been destroyed.
|
// if true, the thread has been started and the start_gate has been destroyed.
|
||||||
@@ -41,7 +41,9 @@ _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^
|
|||||||
context = runtime.default_context();
|
context = runtime.default_context();
|
||||||
|
|
||||||
t := (^Thread)(t);
|
t := (^Thread)(t);
|
||||||
sync.cond_wait(&t.start_gate, &t.start_mutex);
|
sync.condition_wait_for(&t.start_gate);
|
||||||
|
sync.condition_destroy(&t.start_gate);
|
||||||
|
sync.mutex_destroy(&t.start_mutex);
|
||||||
t.start_gate = {};
|
t.start_gate = {};
|
||||||
t.start_mutex = {};
|
t.start_mutex = {};
|
||||||
|
|
||||||
@@ -102,6 +104,9 @@ _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^
|
|||||||
}
|
}
|
||||||
thread.procedure = procedure;
|
thread.procedure = procedure;
|
||||||
|
|
||||||
|
sync.mutex_init(&thread.start_mutex);
|
||||||
|
sync.condition_init(&thread.start_gate, &thread.start_mutex);
|
||||||
|
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +114,7 @@ _start :: proc(t: ^Thread) {
|
|||||||
if intrinsics.atomic_xchg(&t.started, true) {
|
if intrinsics.atomic_xchg(&t.started, true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sync.cond_signal(&t.start_gate);
|
sync.condition_signal(&t.start_gate);
|
||||||
}
|
}
|
||||||
|
|
||||||
_is_done :: proc(t: ^Thread) -> bool {
|
_is_done :: proc(t: ^Thread) -> bool {
|
||||||
@@ -162,6 +167,8 @@ _join_multiple :: proc(threads: ..^Thread) {
|
|||||||
|
|
||||||
_destroy :: proc(t: ^Thread) {
|
_destroy :: proc(t: ^Thread) {
|
||||||
_join(t);
|
_join(t);
|
||||||
|
sync.condition_destroy(&t.start_gate);
|
||||||
|
sync.mutex_destroy(&t.start_mutex);
|
||||||
t.unix_thread = {};
|
t.unix_thread = {};
|
||||||
free(t, t.creation_allocator);
|
free(t, t.creation_allocator);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user