mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Default to pthreads in sync2 for *nix
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
//+private
|
//+private
|
||||||
package sync2
|
package sync2
|
||||||
|
|
||||||
when !#config(ODIN_SYNC_USE_PTHREADS, false) {
|
when !#config(ODIN_SYNC_USE_PTHREADS, true) {
|
||||||
|
|
||||||
import "core:time"
|
import "core:time"
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
//+private
|
//+private
|
||||||
package sync2
|
package sync2
|
||||||
|
|
||||||
when #config(ODIN_SYNC_USE_PTHREADS, false) {
|
when #config(ODIN_SYNC_USE_PTHREADS, true) {
|
||||||
|
|
||||||
import "core:time"
|
import "core:time"
|
||||||
import "core:sys/unix"
|
import "core:sys/unix"
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package thread
|
package thread
|
||||||
|
|
||||||
import "core:runtime"
|
import "core:runtime"
|
||||||
import "core:sync"
|
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
import "intrinsics"
|
import "intrinsics"
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package thread
|
package thread
|
||||||
|
|
||||||
import "intrinsics"
|
import "intrinsics"
|
||||||
import "core:sync"
|
import sync "core:sync/sync2"
|
||||||
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.Semaphore,
|
sem_available: sync.Sema,
|
||||||
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.semaphore_wait_for(&pool.sem_available);
|
sync.sema_wait(&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.semaphore_post(&pool.sem_available, 1);
|
sync.sema_post(&pool.sem_available);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -56,8 +56,6 @@ 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 {
|
||||||
@@ -76,9 +74,6 @@ 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) {
|
||||||
@@ -90,7 +85,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.semaphore_post(&pool.sem_available, len(pool.threads));
|
sync.sema_post(&pool.sem_available, len(pool.threads));
|
||||||
|
|
||||||
yield();
|
yield();
|
||||||
|
|
||||||
@@ -109,7 +104,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.semaphore_post(&pool.sem_available, 1);
|
sync.sema_post(&pool.sem_available);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
@@ -140,7 +135,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.semaphore_post(&pool.sem_available, len(pool.tasks));
|
sync.sema_post(&pool.sem_available, len(pool.tasks));
|
||||||
sync.mutex_unlock(&pool.mutex);
|
sync.mutex_unlock(&pool.mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
package thread
|
package thread
|
||||||
|
|
||||||
import "core:runtime"
|
import "core:runtime"
|
||||||
import "core:sync"
|
import sync "core:sync/sync2"
|
||||||
import win32 "core:sys/windows"
|
import win32 "core:sys/windows"
|
||||||
|
|
||||||
Thread_Os_Specific :: struct {
|
Thread_Os_Specific :: struct {
|
||||||
@@ -38,7 +38,7 @@ _create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sync.atomic_store(&t.done, true, .Sequentially_Consistent);
|
sync.atomic_store(&t.done, true);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ _is_done :: proc(using thread: ^Thread) -> bool {
|
|||||||
// NOTE(tetra, 2019-10-31): Apparently using wait_for_single_object and
|
// NOTE(tetra, 2019-10-31): Apparently using wait_for_single_object and
|
||||||
// checking if it didn't time out immediately, is not good enough,
|
// checking if it didn't time out immediately, is not good enough,
|
||||||
// so we do it this way instead.
|
// so we do it this way instead.
|
||||||
return sync.atomic_load(&done, .Sequentially_Consistent);
|
return sync.atomic_load(&done);
|
||||||
}
|
}
|
||||||
|
|
||||||
_join :: proc(using thread: ^Thread) {
|
_join :: proc(using thread: ^Thread) {
|
||||||
|
|||||||
Reference in New Issue
Block a user