diff options
author | Mikhail Burakov <mburakov@mailbox.org> | 2022-12-25 13:50:41 +0100 |
---|---|---|
committer | Mikhail Burakov <mburakov@mailbox.org> | 2022-12-25 13:50:41 +0100 |
commit | d58fcf64d9e7a3f309a706ce6a81eb9a41eb5348 (patch) | |
tree | 56ce15a6d0a006d8c8d5e01144216d8d2d46e5f0 /thread_pool.c | |
parent | 3f1e645ee1410d90b493b2e0034e25d68bd0d3eb (diff) |
Cosmetic rework of toolbox
Diffstat (limited to 'thread_pool.c')
-rw-r--r-- | thread_pool.c | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/thread_pool.c b/thread_pool.c index fa44ef1..0e04439 100644 --- a/thread_pool.c +++ b/thread_pool.c @@ -18,45 +18,46 @@ #include "thread_pool.h" #include <stdatomic.h> +#include <stdbool.h> #include <stddef.h> #include <stdlib.h> #include <threads.h> -struct ThreadPool_Task { +struct ThreadPoolTask { void (*fun)(void*); void* user; }; -static _Bool FetchTask(struct ThreadPool* thread_pool, - struct ThreadPool_Task* task) { +static bool FetchTask(struct ThreadPool* thread_pool, + struct ThreadPoolTask* task) { for (size_t i = 0; i < thread_pool->tasks_count; i++) { if (thread_pool->tasks[i].fun) { *task = thread_pool->tasks[i]; thread_pool->tasks[i].fun = NULL; - return 1; + return true; } } - return 0; + return false; } -static _Bool StoreTask(struct ThreadPool* thread_pool, - const struct ThreadPool_Task* task) { +static bool StoreTask(struct ThreadPool* thread_pool, + const struct ThreadPoolTask* task) { for (size_t i = 0; i < thread_pool->tasks_count; i++) { if (!thread_pool->tasks[i].fun) { thread_pool->tasks[i] = *task; - return 1; + return true; } } size_t tasks_count = thread_pool->tasks_count + 1; - struct ThreadPool_Task* tasks = - realloc(thread_pool->tasks, tasks_count * sizeof(struct ThreadPool_Task)); - if (!tasks) return 0; + struct ThreadPoolTask* tasks = + realloc(thread_pool->tasks, tasks_count * sizeof(struct ThreadPoolTask)); + if (!tasks) return false; tasks[thread_pool->tasks_count] = *task; thread_pool->tasks = tasks; thread_pool->tasks_count = tasks_count; - return 1; + return true; } static int ThreadProc(void* arg) { @@ -73,7 +74,7 @@ static int ThreadProc(void* arg) { mtx_unlock(&thread_pool->tasks_mutex); return 0; } - struct ThreadPool_Task task; + struct ThreadPoolTask task; if (FetchTask(thread_pool, &task)) { mtx_unlock(&thread_pool->tasks_mutex); task.fun(task.user); @@ -88,7 +89,7 @@ static int ThreadProc(void* arg) { int ThreadPool_Create(struct ThreadPool* thread_pool, size_t threads_count) { atomic_init(&thread_pool->running, 1); thread_pool->threads = malloc(threads_count * sizeof(thrd_t)); - if (!thread_pool->threads) return -1; + if (!thread_pool->threads) return false; thread_pool->threads_count = 0; if (cnd_init(&thread_pool->tasks_cond) != thrd_success) goto rollback_threads; @@ -103,7 +104,7 @@ int ThreadPool_Create(struct ThreadPool* thread_pool, size_t threads_count) { if (thrd_create(thread, ThreadProc, thread_pool) != thrd_success) goto rollback_running; } - return 0; + return true; rollback_running: atomic_store_explicit(&thread_pool->running, 0, memory_order_relaxed); @@ -115,20 +116,20 @@ rollback_tasks_cond: cnd_destroy(&thread_pool->tasks_cond); rollback_threads: free(thread_pool->threads); - return -1; + return false; } -int ThreadPool_Schedule(struct ThreadPool* thread_pool, void (*fun)(void*), +bool ThreadPoolSchedule(struct ThreadPool* thread_pool, void (*fun)(void*), void* user) { - if (mtx_lock(&thread_pool->tasks_mutex) != thrd_success) return -1; - struct ThreadPool_Task task = {.fun = fun, .user = user}; - _Bool result = StoreTask(thread_pool, &task); + if (mtx_lock(&thread_pool->tasks_mutex) != thrd_success) return false; + struct ThreadPoolTask task = {.fun = fun, .user = user}; + bool result = StoreTask(thread_pool, &task); if (result) cnd_broadcast(&thread_pool->tasks_cond); mtx_unlock(&thread_pool->tasks_mutex); - return result ? 0 : -1; + return result; } -void ThreadPool_Destroy(struct ThreadPool* thread_pool) { +void ThreadPoolDestroy(struct ThreadPool* thread_pool) { atomic_store_explicit(&thread_pool->running, 0, memory_order_relaxed); cnd_broadcast(&thread_pool->tasks_cond); while (thread_pool->threads_count-- > 0) @@ -136,4 +137,5 @@ void ThreadPool_Destroy(struct ThreadPool* thread_pool) { mtx_destroy(&thread_pool->tasks_mutex); cnd_destroy(&thread_pool->tasks_cond); free(thread_pool->threads); + free(thread_pool->tasks); } |