diff --git a/src/threadpool.c b/src/threadpool.c index dd0abd9a..15d71994 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -44,6 +44,7 @@ static void uv__req_init(uv_loop_t* loop, static uv_once_t once = UV_ONCE_INIT; static uv_cond_t cond; static uv_mutex_t mutex; +static unsigned int idle_threads; static unsigned int nthreads; static uv_thread_t* threads; static uv_thread_t default_threads[4]; @@ -69,8 +70,11 @@ static void worker(void* arg) { for (;;) { uv_mutex_lock(&mutex); - while (QUEUE_EMPTY(&wq)) + while (QUEUE_EMPTY(&wq)) { + idle_threads += 1; uv_cond_wait(&cond, &mutex); + idle_threads -= 1; + } q = QUEUE_HEAD(&wq); @@ -101,11 +105,9 @@ static void worker(void* arg) { static void post(QUEUE* q) { - int empty_queue; uv_mutex_lock(&mutex); - empty_queue = QUEUE_EMPTY(&wq); QUEUE_INSERT_TAIL(&wq, q); - if (empty_queue) + if (idle_threads > 0) uv_cond_signal(&cond); uv_mutex_unlock(&mutex); }