pthreadpool: use strict sync processing only with max_threads=0
authorStefan Metzmacher <metze@samba.org>
Thu, 21 Jun 2018 22:29:53 +0000 (00:29 +0200)
committerStefan Metzmacher <metze@samba.org>
Thu, 12 Jul 2018 12:25:18 +0000 (14:25 +0200)
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 <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
lib/pthreadpool/pthreadpool.c

index 0f695bc..31ff02d 100644 (file)
@@ -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;
 }