From 4fc9b743c1154f1099281109556fd1915baefc6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arttu=20Yl=C3=A4-Outinen?= Date: Thu, 13 Jul 2017 14:44:59 +0300 Subject: [PATCH] Drop an unnecessary pthread_cond_broadcast Drop pthread_cond_broadcast on threadqueue->cond in function kvz_threadqueue_waitfor. The broadcast caused threads to be woken up more often than necessary. --- src/threadqueue.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/threadqueue.c b/src/threadqueue.c index 63406651..881c4a5a 100644 --- a/src/threadqueue.c +++ b/src/threadqueue.c @@ -443,25 +443,18 @@ int kvz_threadqueue_finalize(threadqueue_queue_t * const threadqueue) return 1; } -int kvz_threadqueue_waitfor(threadqueue_queue_t * const threadqueue, threadqueue_job_t * const job) { - int job_done = 0; - - //NULL job is clearly OK :-) +int kvz_threadqueue_waitfor(threadqueue_queue_t * const threadqueue, threadqueue_job_t * const job) +{ + // NULL job is clearly OK. if (!job) return 1; - - //Lock the queue + + PTHREAD_LOCK(&job->lock); + while (job->state != THREADQUEUE_JOB_STATE_DONE) { + PTHREAD_COND_WAIT(&threadqueue->cb_cond, &job->lock); + } + PTHREAD_UNLOCK(&job->lock); + PTHREAD_LOCK(&threadqueue->lock); - do { - - PTHREAD_LOCK(&job->lock); - job_done = (job->state == THREADQUEUE_JOB_STATE_DONE); - PTHREAD_UNLOCK(&job->lock); - - if (!job_done) { - PTHREAD_COND_BROADCAST(&(threadqueue->cond)); - PTHREAD_COND_WAIT(&threadqueue->cb_cond, &threadqueue->lock); - } - } while (!job_done); // Free jobs submitted before this job. int i = 0; @@ -478,7 +471,7 @@ int kvz_threadqueue_waitfor(threadqueue_queue_t * const threadqueue, threadqueue } PTHREAD_UNLOCK(&threadqueue->lock); - + return 1; }