aboutsummaryrefslogtreecommitdiff
path: root/core/thread/thread_pool.odin
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-04-13 19:09:04 +0100
committergingerBill <bill@gingerbill.org>2021-04-13 19:09:04 +0100
commitebed29fc09a2e9e7cf1efe929729bdacfd5660bd (patch)
tree1314a045d7f1b973b1b718dbc777ad6a8e40f49d /core/thread/thread_pool.odin
parentbee8beb2c92dfde1e751027922727923e32c0ef7 (diff)
Revert *nix thread stuff to old sync
(I was just testing)
Diffstat (limited to 'core/thread/thread_pool.odin')
-rw-r--r--core/thread/thread_pool.odin19
1 files changed, 12 insertions, 7 deletions
diff --git a/core/thread/thread_pool.odin b/core/thread/thread_pool.odin
index 700eaf703..64cd8ea38 100644
--- a/core/thread/thread_pool.odin
+++ b/core/thread/thread_pool.odin
@@ -1,7 +1,7 @@
package thread
import "intrinsics"
-import sync "core:sync/sync2"
+import "core:sync"
import "core:mem"
Task_Status :: enum i32 {
@@ -26,7 +26,7 @@ INVALID_TASK_ID :: Task_Id(-1);
Pool :: struct {
allocator: mem.Allocator,
mutex: sync.Mutex,
- sem_available: sync.Sema,
+ sem_available: sync.Semaphore,
processing_task_count: int, // atomic
is_running: bool,
@@ -40,14 +40,14 @@ pool_init :: proc(pool: ^Pool, thread_count: int, allocator := context.allocator
pool := (^Pool)(t.data);
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 {
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.threads = make([]^Thread, thread_count);
+ sync.mutex_init(&pool.mutex);
+ sync.semaphore_init(&pool.sem_available);
pool.is_running = true;
for _, i in pool.threads {
@@ -74,6 +76,9 @@ pool_destroy :: proc(pool: ^Pool) {
}
delete(pool.threads, pool.allocator);
+
+ sync.mutex_destroy(&pool.mutex);
+ sync.semaphore_destroy(&pool.sem_available);
}
pool_start :: proc(pool: ^Pool) {
@@ -85,7 +90,7 @@ pool_start :: proc(pool: ^Pool) {
pool_join :: proc(pool: ^Pool) {
pool.is_running = false;
- sync.sema_post(&pool.sem_available, len(pool.threads));
+ sync.semaphore_post(&pool.sem_available, len(pool.threads));
yield();
@@ -104,7 +109,7 @@ pool_add_task :: proc(pool: ^Pool, procedure: Task_Proc, data: rawptr, user_inde
task.user_index = user_index;
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) {
@@ -135,7 +140,7 @@ pool_wait_and_process :: proc(pool: ^Pool) {
// Safety kick
if len(pool.tasks) != 0 && intrinsics.atomic_load(&pool.processing_task_count) == 0 {
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);
}