[libvirt] [PATCH] virThreadPoolFree: Set n(Prio)Workers after the pool is locked

In 78839da I am trying to join the worker threads. However, I can't sipmly reuse pool->nWorkers (same applies for pool->nPrioWorkers), because of the following flow that is currently implemented: 1) the main thread executing virThreadPoolFree sets pool->quit = true, wakes up all the workers and wait on pool->quit_cond. 2) A worker is woken up and see quit request. It immediately jumps of the while() loop and decrements pool->nWorkers (or pool->nPrioWorkers in case of priority worker). The last thread signalizes pool->quit_cond. 3) Main thread is woken up, with both pool->nWorkers and pool->nPrioWorkers being zero. So there's a need to copy the original value of worker thread counts into local variables. However, these need to set *after* the check for pool being NULL (dereferencing a NULL is no no). And for safety they can be set right after the pool is locked. Reported-by: John Ferlan <jferlan@redhat.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virthreadpool.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c index 99f36ec..aa442d1 100644 --- a/src/util/virthreadpool.c +++ b/src/util/virthreadpool.c @@ -242,13 +242,15 @@ void virThreadPoolFree(virThreadPoolPtr pool) virThreadPoolJobPtr job; bool priority = false; size_t i; - size_t nWorkers = pool->nWorkers; - size_t nPrioWorkers = pool->nPrioWorkers; + 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); -- 1.8.4.4

On Thu, Dec 05, 2013 at 02:54:41PM +0100, Michal Privoznik wrote:
In 78839da I am trying to join the worker threads. However, I can't sipmly reuse pool->nWorkers (same applies for pool->nPrioWorkers), because of the following flow that is currently implemented:
1) the main thread executing virThreadPoolFree sets pool->quit = true, wakes up all the workers and wait on pool->quit_cond.
2) A worker is woken up and see quit request. It immediately jumps of the while() loop and decrements pool->nWorkers (or pool->nPrioWorkers in case of priority worker). The last thread signalizes pool->quit_cond.
3) Main thread is woken up, with both pool->nWorkers and pool->nPrioWorkers being zero.
So there's a need to copy the original value of worker thread counts into local variables. However, these need to set *after* the check for pool being NULL (dereferencing a NULL is no no). And for safety they can be set right after the pool is locked.
Reported-by: John Ferlan <jferlan@redhat.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virthreadpool.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c index 99f36ec..aa442d1 100644 --- a/src/util/virthreadpool.c +++ b/src/util/virthreadpool.c @@ -242,13 +242,15 @@ void virThreadPoolFree(virThreadPoolPtr pool) virThreadPoolJobPtr job; bool priority = false; size_t i; - size_t nWorkers = pool->nWorkers; - size_t nPrioWorkers = pool->nPrioWorkers; + 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);
ACK Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (2)
-
Daniel P. Berrange
-
Michal Privoznik