Updated task pool.

This commit is contained in:
Christoffer Lerno
2023-06-02 21:57:49 +02:00
parent 3e54d13b62
commit d0e8944c56
4 changed files with 14 additions and 38 deletions

View File

@@ -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)

View File

@@ -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);

View File

@@ -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();
}

View File

@@ -1 +1 @@
#define COMPILER_VERSION "0.4.520"
#define COMPILER_VERSION "0.4.521"