From d0e8944c565467b894c7b0cc16b840a47f11d666 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 2 Jun 2023 21:57:49 +0200 Subject: [PATCH] Updated task pool. --- src/compiler/compiler.c | 3 +-- src/utils/lib.h | 5 +---- src/utils/taskqueue.c | 42 +++++++++++------------------------------ src/version.h | 2 +- 4 files changed, 14 insertions(+), 38 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 7405a9dac..e8caec440 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -432,8 +432,7 @@ void compiler_compile(void) } else if (task_count > 1) { - TaskQueueRef queue = taskqueue_create(active_target.build_threads > task_count ? task_count : active_target.build_threads, tasks); - taskqueue_wait_for_completion(queue); + taskqueue_run(active_target.build_threads > task_count ? task_count : active_target.build_threads, tasks); } if (active_target.print_output) diff --git a/src/utils/lib.h b/src/utils/lib.h index 0a645c245..37d99a8c4 100644 --- a/src/utils/lib.h +++ b/src/utils/lib.h @@ -57,8 +57,6 @@ typedef struct Task_ void *arg; } Task; -typedef void *TaskQueueRef; - uint16_t *win_utf8to16(const char *name); char *win_utf16to8(const uint16_t *name); // Use as if it was mkdir(..., 0755) == 0 @@ -98,8 +96,7 @@ char *calloc_string(size_t len); void free_arena(void); void print_arena_status(void); void run_arena_allocator_tests(void); -TaskQueueRef taskqueue_create(int threads, Task **task_list); -void taskqueue_wait_for_completion(TaskQueueRef queue); +void taskqueue_run(int threads, Task **task_list); int cpus(void); const char *date_get(void); const char *time_get(void); diff --git a/src/utils/taskqueue.c b/src/utils/taskqueue.c index 24aa32d2a..e2e1ce75e 100644 --- a/src/utils/taskqueue.c +++ b/src/utils/taskqueue.c @@ -9,8 +9,6 @@ typedef struct TaskQueue_ { - pthread_t *threads; - int thread_count; pthread_mutex_t lock; Task **queue; } TaskQueue; @@ -35,48 +33,30 @@ SHUTDOWN: return NULL; } -TaskQueueRef taskqueue_create(int threads, Task **task_list) +void taskqueue_run(int threads, Task **task_list) { assert(threads > 0); - TaskQueue *queue = CALLOCS(TaskQueue); - queue->threads = MALLOC(sizeof(pthread_t) * (unsigned)threads); - queue->thread_count = threads; - queue->queue = task_list; - if (pthread_mutex_init(&queue->lock, NULL)) error_exit("Failed to set up mutex"); + pthread_t *pthreads = malloc(sizeof(pthread_t) * (unsigned)threads); + TaskQueue queue = { .queue = task_list }; + if (pthread_mutex_init(&queue.lock, NULL)) error_exit("Failed to set up mutex"); for (int i = 0; i < threads; i++) { - if (pthread_create(queue->threads + i, NULL, taskqueue_thread, queue)) error_exit("Fail to set up thread pool"); + if (pthread_create(&pthreads[i], NULL, taskqueue_thread, &queue)) error_exit("Fail to set up thread pool"); } - return queue; -} - -void taskqueue_wait_for_completion(TaskQueueRef queue_ref) -{ - assert(queue_ref); - TaskQueue *queue = queue_ref; - for (int i = 0; i < queue->thread_count; i++) + for (int i = 0; i < threads; i++) { - if (pthread_join(queue->threads[i], NULL) != 0) error_exit("Failed to join thread."); + if (pthread_join(pthreads[i], NULL) != 0) error_exit("Failed to join thread."); } - pthread_mutex_destroy(&queue->lock); + free(pthreads); + pthread_mutex_destroy(&queue.lock); } - #else -void taskqueue_add(TaskQueueRef queue_ref, Task *task) -{ -} -TaskQueueRef taskqueue_create(int threads, Task **tasks) +void taskqueue_run(int threads, Task **task_list) { - return tasks; -} - -void taskqueue_wait_for_completion(TaskQueueRef queue) -{ - Task **tasks = queue; - FOREACH_BEGIN(Task *task, tasks) + FOREACH_BEGIN(Task *task, task_list) task->task(task->arg); FOREACH_END(); } diff --git a/src/version.h b/src/version.h index dae9cfa8a..8cce8d362 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.4.520" \ No newline at end of file +#define COMPILER_VERSION "0.4.521" \ No newline at end of file