Re: [PATCH 03/10] util: add stop/drain functions to thread pool

2020-07-21 Thread Daniel P . Berrangé
On Tue, Jul 14, 2020 at 12:32:54PM +0300, Nikolay Shirokovskiy wrote:
> Stop just send signal for threads to exit when they finish with
> current task. Drain waits when all threads will finish.
> 
> Signed-off-by: Nikolay Shirokovskiy 
> ---
>  src/libvirt_private.syms |  2 ++
>  src/util/virthreadpool.c | 43 ++-
>  src/util/virthreadpool.h |  3 +++
>  3 files changed, 43 insertions(+), 5 deletions(-)

Reviewed-by: Daniel P. Berrangé 


Regards,
Daniel
-- 
|: https://berrange.com  -o-https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o-https://fstop138.berrange.com :|
|: https://entangle-photo.org-o-https://www.instagram.com/dberrange :|



[PATCH 03/10] util: add stop/drain functions to thread pool

2020-07-14 Thread Nikolay Shirokovskiy
Stop just send signal for threads to exit when they finish with
current task. Drain waits when all threads will finish.

Signed-off-by: Nikolay Shirokovskiy 
---
 src/libvirt_private.syms |  2 ++
 src/util/virthreadpool.c | 43 ++-
 src/util/virthreadpool.h |  3 +++
 3 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 73b72c9..f64b1de 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3326,6 +3326,7 @@ virThreadJobSetWorker;
 
 
 # util/virthreadpool.h
+virThreadPoolDrain;
 virThreadPoolFree;
 virThreadPoolGetCurrentWorkers;
 virThreadPoolGetFreeWorkers;
@@ -3336,6 +3337,7 @@ virThreadPoolGetPriorityWorkers;
 virThreadPoolNewFull;
 virThreadPoolSendJob;
 virThreadPoolSetParameters;
+virThreadPoolStop;
 
 
 # util/virtime.h
diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c
index 10a44de..ca44f55 100644
--- a/src/util/virthreadpool.c
+++ b/src/util/virthreadpool.c
@@ -268,19 +268,27 @@ virThreadPoolNewFull(size_t minWorkers,
 
 }
 
-void virThreadPoolFree(virThreadPoolPtr pool)
-{
-virThreadPoolJobPtr job;
 
-if (!pool)
+static void
+virThreadPoolStopLocked(virThreadPoolPtr pool)
+{
+if (pool->quit)
 return;
 
-virMutexLock(>mutex);
 pool->quit = true;
 if (pool->nWorkers > 0)
 virCondBroadcast(>cond);
 if (pool->nPrioWorkers > 0)
 virCondBroadcast(>prioCond);
+}
+
+
+static void
+virThreadPoolDrainLocked(virThreadPoolPtr pool)
+{
+virThreadPoolJobPtr job;
+
+virThreadPoolStopLocked(pool);
 
 while (pool->nWorkers > 0 || pool->nPrioWorkers > 0)
 ignore_value(virCondWait(>quit_cond, >mutex));
@@ -289,6 +297,15 @@ void virThreadPoolFree(virThreadPoolPtr pool)
 pool->jobList.head = pool->jobList.head->next;
 VIR_FREE(job);
 }
+}
+
+void virThreadPoolFree(virThreadPoolPtr pool)
+{
+if (!pool)
+return;
+
+virMutexLock(>mutex);
+virThreadPoolDrainLocked(pool);
 
 VIR_FREE(pool->workers);
 virMutexUnlock(>mutex);
@@ -475,3 +492,19 @@ virThreadPoolSetParameters(virThreadPoolPtr pool,
 virMutexUnlock(>mutex);
 return -1;
 }
+
+void
+virThreadPoolStop(virThreadPoolPtr pool)
+{
+virMutexLock(>mutex);
+virThreadPoolStopLocked(pool);
+virMutexUnlock(>mutex);
+}
+
+void
+virThreadPoolDrain(virThreadPoolPtr pool)
+{
+virMutexLock(>mutex);
+virThreadPoolDrainLocked(pool);
+virMutexUnlock(>mutex);
+}
diff --git a/src/util/virthreadpool.h b/src/util/virthreadpool.h
index c97d9b3..dd1aaf3 100644
--- a/src/util/virthreadpool.h
+++ b/src/util/virthreadpool.h
@@ -56,3 +56,6 @@ int virThreadPoolSetParameters(virThreadPoolPtr pool,
long long int minWorkers,
long long int maxWorkers,
long long int prioWorkers);
+
+void virThreadPoolStop(virThreadPoolPtr pool);
+void virThreadPoolDrain(virThreadPoolPtr pool);
-- 
1.8.3.1