From 0ff9c1ab8852bec846822ee2af55ebcb7e5f5967 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Dec 01 2020 18:40:30 +0000 Subject: sanlock: Shrink thread pool when there is no work When the thread pool has no work to do, and we have enough free workers, other worker threads will terminate. Without this change, when using large number of worker threads, the thread pool grows to max_worker_threads workers, and never shrink down. With this change, the pool quickly shrinks down, but we always have enough free worker threads for serving new requests. Signed-off-by: Nir Soffer --- diff --git a/src/main.c b/src/main.c index 63d8cff..622dc8e 100644 --- a/src/main.c +++ b/src/main.c @@ -932,6 +932,9 @@ static void *thread_pool_worker(void *data) while (1) { while (!pool.quit && list_empty(&pool.work_data)) { + if (pool.free_workers >= DEFAULT_MIN_WORKER_THREADS) + goto out; + pool.free_workers++; pthread_cond_wait(&pool.cond, &pool.mutex); pool.free_workers--; @@ -952,6 +955,7 @@ static void *thread_pool_worker(void *data) break; } +out: pool.num_workers--; if (!pool.num_workers) pthread_cond_signal(&pool.quit_wait);