[libvirt] [PATCH v2 3/9] util: Introduce virThreadPoolQuitRequested

John Ferlan posted 9 patches 22 weeks ago

[libvirt] [PATCH v2 3/9] util: Introduce virThreadPoolQuitRequested

Posted by John Ferlan 22 weeks ago
Create a helper API to set the quit flag and wake up the worker
threads when a quit has been requested such as via a signal handler.

Signed-off-by: John Ferlan <jferlan@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virthreadpool.c | 43 ++++++++++++++++++++++++++++++++++------
 src/util/virthreadpool.h |  2 ++
 3 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 3e304907b9..af76c29928 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3001,6 +3001,7 @@ virThreadPoolGetMaxWorkers;
 virThreadPoolGetMinWorkers;
 virThreadPoolGetPriorityWorkers;
 virThreadPoolNewFull;
+virThreadPoolQuitRequested;
 virThreadPoolSendJob;
 virThreadPoolSetParameters;
 
diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c
index 10f2bd2c3a..137c5d1746 100644
--- a/src/util/virthreadpool.c
+++ b/src/util/virthreadpool.c
@@ -30,9 +30,12 @@
 #include "viralloc.h"
 #include "virthread.h"
 #include "virerror.h"
+#include "virlog.h"
 
 #define VIR_FROM_THIS VIR_FROM_NONE
 
+VIR_LOG_INIT("util.threadpool");
+
 typedef struct _virThreadPoolJob virThreadPoolJob;
 typedef virThreadPoolJob *virThreadPoolJobPtr;
 
@@ -269,6 +272,18 @@ virThreadPoolNewFull(size_t minWorkers,
 
 }
 
+
+static void
+virThreadPoolSetQuit(virThreadPoolPtr pool)
+{
+    pool->quit = true;
+    if (pool->nWorkers > 0)
+        virCondBroadcast(&pool->cond);
+    if (pool->nPrioWorkers > 0)
+        virCondBroadcast(&pool->prioCond);
+}
+
+
 void virThreadPoolFree(virThreadPoolPtr pool)
 {
     virThreadPoolJobPtr job;
@@ -278,13 +293,9 @@ void virThreadPoolFree(virThreadPoolPtr pool)
         return;
 
     virMutexLock(&pool->mutex);
-    pool->quit = true;
-    if (pool->nWorkers > 0)
-        virCondBroadcast(&pool->cond);
-    if (pool->nPrioWorkers > 0) {
+    if (pool->nPrioWorkers > 0)
         priority = true;
-        virCondBroadcast(&pool->prioCond);
-    }
+    virThreadPoolSetQuit(pool);
 
     while (pool->nWorkers > 0 || pool->nPrioWorkers > 0)
         ignore_value(virCondWait(&pool->quit_cond, &pool->mutex));
@@ -307,6 +318,26 @@ void virThreadPoolFree(virThreadPoolPtr pool)
 }
 
 
+/*
+ * virThreadPoolQuitRequested:
+ * @pool: Pointer to thread pool
+ *
+ * When libvirtd quit is requested via the daemonShutdownHandler let's
+ * set the quit flag for current workers and wake them up.
+ */
+void
+virThreadPoolQuitRequested(virThreadPoolPtr pool)
+{
+    virMutexLock(&pool->mutex);
+
+    VIR_DEBUG("nWorkers=%zd, nPrioWorkers=%zd jobQueueDepth=%zd",
+              pool->nWorkers, pool->nPrioWorkers, pool->jobQueueDepth);
+
+    virThreadPoolSetQuit(pool);
+    virMutexUnlock(&pool->mutex);
+}
+
+
 size_t virThreadPoolGetMinWorkers(virThreadPoolPtr pool)
 {
     size_t ret;
diff --git a/src/util/virthreadpool.h b/src/util/virthreadpool.h
index e1f362f5bb..f2c8b2ae61 100644
--- a/src/util/virthreadpool.h
+++ b/src/util/virthreadpool.h
@@ -52,6 +52,8 @@ size_t virThreadPoolGetJobQueueDepth(virThreadPoolPtr pool);
 
 void virThreadPoolFree(virThreadPoolPtr pool);
 
+void virThreadPoolQuitRequested(virThreadPoolPtr pool);
+
 int virThreadPoolSendJob(virThreadPoolPtr pool,
                          unsigned int priority,
                          void *jobdata) ATTRIBUTE_NONNULL(1)
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list