diff --git a/src/module/module_repo.c b/src/module/module_repo.c index c301be3711aba08f87ee38c5bce8c1dc712fd900..4b402a5c6554a44bbf3003d01351eeb6cd488a11 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 220de13d84c1598ec444973ce56172010442b995..08748ec7406f04de243c4aa84d260e484bb87d05 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));