[PATCH] virthreadpool: create threads from the newly expanded workers

Wei Gong posted 1 patch 1 month, 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20240314032302.2458-1-gongwei833x@gmail.com
There is a newer version of this series
src/util/virthreadpool.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] virthreadpool: create threads from the newly expanded workers
Posted by Wei Gong 1 month, 2 weeks ago
when the thread pool is dynamically expanded, threads should
not be created from the existing workers; they should be created
from the newly expanded workers

Signed-off-by: Wei Gong <gongwei833x@gmail.com>
---
 src/util/virthreadpool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c
index d45fa92061..6140b7a0a3 100644
--- a/src/util/virthreadpool.c
+++ b/src/util/virthreadpool.c
@@ -199,7 +199,7 @@ virThreadPoolExpand(virThreadPool *pool, size_t gain, bool priority)
         else
             name = g_strdup(pool->jobName);
 
-        if (virThreadCreateFull(&(*workers)[i],
+        if (virThreadCreateFull(&(*workers)[*curWorkers - gain + i],
                                 false,
                                 virThreadPoolWorker,
                                 name,
-- 
2.32.1 (Apple Git-133)
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org
Re: [PATCH] virthreadpool: create threads from the newly expanded workers
Posted by Michal Prívozník 1 month, 1 week ago
On 3/14/24 04:23, Wei Gong wrote:
> when the thread pool is dynamically expanded, threads should
> not be created from the existing workers; they should be created
> from the newly expanded workers
> 
> Signed-off-by: Wei Gong <gongwei833x@gmail.com>
> ---
>  src/util/virthreadpool.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c
> index d45fa92061..6140b7a0a3 100644
> --- a/src/util/virthreadpool.c
> +++ b/src/util/virthreadpool.c
> @@ -199,7 +199,7 @@ virThreadPoolExpand(virThreadPool *pool, size_t gain, bool priority)
>          else
>              name = g_strdup(pool->jobName);
>  
> -        if (virThreadCreateFull(&(*workers)[i],
> +        if (virThreadCreateFull(&(*workers)[*curWorkers - gain + i],
>                                  false,
>                                  virThreadPoolWorker,
>                                  name,

I was wondering how joining threads could have worked, but we are
creating individual threads with a detached state (i.e. non-joinable),
effectively papering over this issue.

While your code works, I think we can make it more readable. For
instance, consider the following:

diff --git i/src/util/virthreadpool.c w/src/util/virthreadpool.c
index d45fa92061..e00accd9eb 100644
--- i/src/util/virthreadpool.c
+++ w/src/util/virthreadpool.c
@@ -184,7 +184,7 @@ virThreadPoolExpand(virThreadPool *pool, size_t gain, bool priority)
     size_t i = 0;
     struct virThreadPoolWorkerData *data = NULL;
 
-    VIR_EXPAND_N(*workers, *curWorkers, gain);
+    VIR_REALLOC_N(*workers, *curWorkers + gain);
 
     for (i = 0; i < gain; i++) {
         g_autofree char *name = NULL;
@@ -199,7 +199,7 @@ virThreadPoolExpand(virThreadPool *pool, size_t gain, bool priority)
         else
             name = g_strdup(pool->jobName);
 
-        if (virThreadCreateFull(&(*workers)[i],
+        if (virThreadCreateFull(&(*workers)[*curWorkers],
                                 false,
                                 virThreadPoolWorker,
                                 name,
@@ -207,15 +207,13 @@ virThreadPoolExpand(virThreadPool *pool, size_t gain, bool priority)
                                 data) < 0) {
             VIR_FREE(data);
             virReportSystemError(errno, "%s", _("Failed to create thread"));
-            goto error;
+            return -1;
         }
+
+        (*curWorkers)++;
     }
 
     return 0;
-
- error:
-    *curWorkers -= gain - i;
-    return -1;
 }
 
 virThreadPool *


Feel free to send it as v2.

Michal
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org