diff options
author | Jiri Denemark <jdenemar@redhat.com> | 2015-05-05 22:07:12 +0200 |
---|---|---|
committer | Jiri Denemark <jdenemar@redhat.com> | 2015-05-21 14:35:02 +0200 |
commit | 6cc5c33eb5daa90ee63c79fa8b21f6fa334e9d7b (patch) | |
tree | ab25597edeb01913f3b35ea449ed4573564f22d2 /src/util/virthreadpool.c | |
parent | 82cffb58a188d12d9fb50c5bafc1a2b323948733 (diff) | |
download | libvirt-6cc5c33eb5daa90ee63c79fa8b21f6fa334e9d7b.tar.gz |
threadpool: Switch to detached threads
Using joinable threads does not help anything, but it can lead to memory
leaks.
When a worker thread exits, it decreases nWorkers or nPrioWorkers and
once both nWorkers and nPrioWorkers are zero (i.e., the last worker is
gone), quit_cond is signaled. When freeing the pool we first tell all
threads to die and then we are waiting for both nWorkers and
nPrioWorkers to become zero. At this point we already know all threads
are gone. So the only reason for calling virThreadJoin of all workers is
to free the memory allocated for joinable threads. If we avoid
allocating this memory, we don't need to take care of freeing it.
Moreover, any memory associated with a worker thread which died before
we asked it to die (e.g., because virCondWait failed in the thread)
would be lost anyway since virThreadPoolFree calls virThreadJoin only
for threads which were running at the time virThreadPoolFree was called.
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Diffstat (limited to 'src/util/virthreadpool.c')
-rw-r--r-- | src/util/virthreadpool.c | 17 |
1 files changed, 3 insertions, 14 deletions
diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c index ed224867c4..f640448bb3 100644 --- a/src/util/virthreadpool.c +++ b/src/util/virthreadpool.c @@ -201,7 +201,7 @@ virThreadPoolNewFull(size_t minWorkers, data->cond = &pool->cond; if (virThreadCreateFull(&pool->workers[i], - true, + false, virThreadPoolWorker, pool->jobFuncName, true, @@ -225,7 +225,7 @@ virThreadPoolNewFull(size_t minWorkers, data->priority = true; if (virThreadCreateFull(&pool->prioWorkers[i], - true, + false, virThreadPoolWorker, pool->jobFuncName, true, @@ -249,16 +249,11 @@ void virThreadPoolFree(virThreadPoolPtr pool) { virThreadPoolJobPtr job; bool priority = false; - size_t i; - size_t nWorkers; - size_t nPrioWorkers; if (!pool) return; virMutexLock(&pool->mutex); - nWorkers = pool->nWorkers; - nPrioWorkers = pool->nPrioWorkers; pool->quit = true; if (pool->nWorkers > 0) virCondBroadcast(&pool->cond); @@ -275,12 +270,6 @@ void virThreadPoolFree(virThreadPoolPtr pool) VIR_FREE(job); } - for (i = 0; i < nWorkers; i++) - virThreadJoin(&pool->workers[i]); - - for (i = 0; i < nPrioWorkers; i++) - virThreadJoin(&pool->prioWorkers[i]); - VIR_FREE(pool->workers); virMutexUnlock(&pool->mutex); virMutexDestroy(&pool->mutex); @@ -338,7 +327,7 @@ int virThreadPoolSendJob(virThreadPoolPtr pool, data->cond = &pool->cond; if (virThreadCreateFull(&pool->workers[pool->nWorkers - 1], - true, + false, virThreadPoolWorker, pool->jobFuncName, true, |