From e45d33e92e47995faa7c523cf82dfb65efb190f8 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Fri, 22 Jun 2018 00:29:53 +0200 Subject: pthreadpool: use strict sync processing only with max_threads=0 Otherwise it's an error if not at least one thread is possible. This gives a much saner behaviour and doesn't end up with unexpected sync processing. Signed-off-by: Stefan Metzmacher Reviewed-by: Ralph Boehme --- lib/pthreadpool/pthreadpool.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'lib/pthreadpool') diff --git a/lib/pthreadpool/pthreadpool.c b/lib/pthreadpool/pthreadpool.c index 0f695bcd769..31ff02dd583 100644 --- a/lib/pthreadpool/pthreadpool.c +++ b/lib/pthreadpool/pthreadpool.c @@ -77,6 +77,7 @@ struct pthreadpool { /* * maximum number of threads + * 0 means no real thread, only strict sync processing. */ unsigned max_threads; @@ -649,6 +650,19 @@ int pthreadpool_add_job(struct pthreadpool *pool, int job_id, return EINVAL; } + if (pool->max_threads == 0) { + unlock_res = pthread_mutex_unlock(&pool->mutex); + assert(unlock_res == 0); + + /* + * If no thread are allowed we do strict sync processing. + */ + fn(private_data); + res = pool->signal_fn(job_id, fn, private_data, + pool->signal_fn_private_data); + return res; + } + /* * Add job to the end of the queue */ @@ -671,8 +685,7 @@ int pthreadpool_add_job(struct pthreadpool *pool, int job_id, return res; } - if ((pool->max_threads != 0) && - (pool->num_threads >= pool->max_threads)) { + if (pool->num_threads >= pool->max_threads) { /* * No more new threads, we just queue the request */ @@ -707,8 +720,5 @@ int pthreadpool_add_job(struct pthreadpool *pool, int job_id, unlock_res = pthread_mutex_unlock(&pool->mutex); assert(unlock_res == 0); - fn(private_data); - res = pool->signal_fn(job_id, fn, private_data, - pool->signal_fn_private_data); return res; } -- cgit v1.2.1