wip: fix existing todos

Co-authored-by: Github CoPilot
This commit is contained in:
2026-06-16 22:48:04 +02:00
parent a5618d3c5f
commit 80857e0f0a
10 changed files with 212 additions and 65 deletions
+21 -14
View File
@@ -41,28 +41,33 @@ static void threadpool_work_destroy(threadpool_work* work) {
free(work);
}
static threadpool_work* threadpool_work_pop(threadpool* pool) {
threadpool_work* work;
static optional_threadpool_work threadpool_work_pop(threadpool* pool) {
optional_threadpool_work result;
result.value = NULL;
result.has_value = false;
if (pool == NULL)
return NULL;
return result;
work = pool->work_first;
threadpool_work* work = pool->work_first;
if (work == NULL)
return NULL; // TODO: This should propbably be using optionals
return result;
if (work->next == NULL) {
pool->work_first = NULL;
pool->work_last = NULL;
return work;
result.value = work;
result.has_value = true;
return result;
}
pool->work_first = work->next;
return work;
result.value = work;
result.has_value = true;
return result;
}
static void* threadpool_worker(void* arg) {
log_trace("threadpool worker spawned");
threadpool* pool = arg;
threadpool_work* work;
while(true) {
// Wait for work
pthread_mutex_lock(&(pool->work_mutex));
@@ -70,18 +75,20 @@ static void* threadpool_worker(void* arg) {
pthread_cond_wait(&(pool->work_cond), &(pool->work_mutex));
if (pool->stop)
break;
work = threadpool_work_pop(pool);
pool->working_count++;
optional_threadpool_work work = threadpool_work_pop(pool);
if(work.has_value)
pool->working_count++;
pthread_mutex_unlock(&(pool->work_mutex));
// Do the work
if (work != NULL) {
work->func(work->arg);
threadpool_work_destroy(work);
if (work.has_value) {
work.value->func(work.value->arg);
threadpool_work_destroy(work.value);
}
pthread_mutex_lock(&(pool->work_mutex));
pool->working_count--;
if(work.has_value)
pool->working_count--;
if (!pool->stop && pool->working_count == 0 && pool->work_first == NULL)
pthread_cond_signal(&(pool->working_cond));
pthread_mutex_unlock(&(pool->work_mutex));