From f005c7e8727ba908e7ff0d220831ac9a212685c3 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Sun, 9 Aug 2020 11:33:07 +0200 Subject: [PATCH] MODULE: Can now create the repo module The error was that passing to threads the pointer to a variable that will be copied then destroyed before the end of the thread is a really bad idea --- src/module/module_repo.c | 22 ++++++++++++---------- src/module/worker.c | 27 +++++++++------------------ 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/module/module_repo.c b/src/module/module_repo.c index c301be37..4b402a5c 100644 --- a/src/module/module_repo.c +++ b/src/module/module_repo.c @@ -424,16 +424,6 @@ module_repo_new(struct module_repo_internal *repo_, struct queue *queue, volatil .base_url = safe_zero_malloc(LKT_LINE_MAX * sizeof(char)), }; - int workers_count; - if (!database_config_get_int(db, "repo", "workers_count", &workers_count)) - workers_count = 5; - if (worker_pool_new(&repo.workers, - 10 /* Initial number of elements in the call queue */, - workers_count /* Number of worker threads */)) { - LOG_ERROR("REPO", "Out of memory"); - return false; - } - /* Copies */ if (!database_config_get_text(db, "database", "kara_dir", repo.kara_dir, PATH_MAX) || !database_config_get_text(db, "repo", "name", repo.name, LKT_LINE_MAX) || @@ -443,6 +433,18 @@ module_repo_new(struct module_repo_internal *repo_, struct queue *queue, volatil !database_config_get_text(db, "repo", "json", repo.get_all_json, LKT_LINE_MAX) ) return false; memcpy(repo_, &repo, sizeof(struct module_repo_internal)); + + /* Init the worker only now ! */ + int workers_count; + if (!database_config_get_int(db, "repo", "workers_count", &workers_count)) + workers_count = 5; + if (worker_pool_new(&repo_->workers, + 10 /* Initial number of elements in the call queue */, + workers_count /* Number of worker threads */)) { + LOG_ERROR("REPO", "Out of memory"); + return false; + } + return true; } diff --git a/src/module/worker.c b/src/module/worker.c index 220de13d..08748ec7 100644 --- a/src/module/worker.c +++ b/src/module/worker.c @@ -14,40 +14,34 @@ static void * __worker_thread(void *__pool) { - struct worker_pool *pool = (struct worker_pool *) __pool; + volatile struct worker_pool *pool = (volatile struct worker_pool *) __pool; volatile void *volatile arg; worker_function func; for (;;) { - errno = 0; - if (pthread_mutex_lock(&pool->lock)) { - LOG_ERROR("WORKER", "Failed to lock mutex: %s", strerror(errno)); - abort(); - } + assert(!pthread_mutex_lock((pthread_mutex_t *) &pool->lock)); if (pool->len) { --(pool->len); ++(pool->thread_working); func = pool->functions[pool->len]; arg = pool->args[pool->len]; } else if (pool->exit) { - pthread_mutex_unlock(&pool->lock); + assert(!pthread_mutex_unlock((pthread_mutex_t *) &pool->lock)); LOG_INFO("WORKER", "Exiting"); break; } else { - func = NULL; - pthread_mutex_unlock(&pool->lock); + assert(!pthread_mutex_unlock((pthread_mutex_t *) &pool->lock)); sched_yield(); continue; } - pthread_mutex_unlock(&pool->lock); + assert(!pthread_mutex_unlock((pthread_mutex_t *) &pool->lock)); func((void *) arg); - while (pthread_mutex_trylock(&pool->lock)) - sched_yield(); + assert(!pthread_mutex_lock((pthread_mutex_t *) &pool->lock)); --(pool->thread_working); - pthread_mutex_unlock(&pool->lock); + assert(!pthread_mutex_unlock((pthread_mutex_t *) &pool->lock)); } - return NULL; + pthread_exit(NULL); } int @@ -71,10 +65,7 @@ worker_pool_new(struct worker_pool *ret, size_t init_size, size_t thread_count) return 1; } *ret = __ret; - if (pthread_mutex_init(&ret->lock, NULL)) { - LOG_ERROR("WORKER", "Failed to init mutex: %s", strerror(errno)); - abort(); - } + assert(!pthread_mutex_init(&ret->lock, NULL)); size_t i; for (i = 0; i < ret->thread_size; ++i) assert(!pthread_create(&ret->threads[i], NULL, __worker_thread, ret)); -- GitLab