[PATCH] erofs-utils: mkfs: use pthread_kill instead of pthread_cancel

Yifan Zhao zhaoyifan at sjtu.edu.cn
Thu Oct 3 03:49:12 AEST 2024


Bionic (Android's libc) does not have pthread_cancel. Let's use
pthread_kill instead to gracefully terminate the worker threads.

Signed-off-by: Yifan Zhao <zhaoyifan at sjtu.edu.cn>
---
 lib/workqueue.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/workqueue.c b/lib/workqueue.c
index 47cec9b..f6d1a7f 100644
--- a/lib/workqueue.c
+++ b/lib/workqueue.c
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
 #include <pthread.h>
+#include <signal.h>
 #include <stdlib.h>
 #include "erofs/workqueue.h"
 
@@ -40,6 +41,11 @@ static void *worker_thread(void *arg)
 	return NULL;
 }
 
+void worker_exit_handler(int sig)
+{
+	pthread_exit(NULL);
+}
+
 int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 			  unsigned int max_jobs, erofs_wq_func_t on_start,
 			  erofs_wq_func_t on_exit)
@@ -50,6 +56,8 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 	if (!wq || nworker <= 0 || max_jobs <= 0)
 		return -EINVAL;
 
+	signal(SIGUSR1, worker_exit_handler);
+
 	wq->head = wq->tail = NULL;
 	wq->nworker = nworker;
 	wq->max_jobs = max_jobs;
@@ -69,7 +77,7 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 		ret = pthread_create(&wq->workers[i], NULL, worker_thread, wq);
 		if (ret) {
 			while (i)
-				pthread_cancel(wq->workers[--i]);
+				pthread_kill(wq->workers[--i], SIGUSR1);
 			free(wq->workers);
 			return ret;
 		}
-- 
2.46.2



More information about the Linux-erofs mailing list