Skip to contentSkip to conversation

erofs-utils: lib: get rid of pthread_cancel() for workqueue

PATCH In review Gao Xiang 2 participants 4 messages Dec 11, 2024 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: lib: get rid of pthread_cancel() for workqueue

Bionic (Android's libc) does not have pthread_cancel, call erofs_destroy_workqueue() when initialization fails.

Cc Kelvin Zhang <[email protected]>
Cc Yifan Zhao <[email protected]>

---
https://lore.kernel.org/r/[email protected]
https://lore.kernel.org/r/[email protected]

 lib/workqueue.c | 63 ++++++++++++++++++++++++-------------------------
 1 file changed, 31 insertions(+), 32 deletions(-)
lib/workqueue.c +31 -32
index 47cec9b..840c204 100644
@@ -15,9 +15,9 @@ static void *worker_thread(void *arg)
 	while (true) {
 		pthread_mutex_lock(&wq->lock);
 
-		while (wq->job_count == 0 && !wq->shutdown)
+		while (!wq->job_count && !wq->shutdown)
 			pthread_cond_wait(&wq->cond_empty, &wq->lock);
-		if (wq->job_count == 0 && wq->shutdown) {
+		if (!wq->job_count && wq->shutdown) {
 			pthread_mutex_unlock(&wq->lock);
 			break;
 		}
@@ -40,6 +40,29 @@ static void *worker_thread(void *arg)
 	return NULL;
 }
 
+int erofs_destroy_workqueue(struct erofs_workqueue *wq)
+{
+	if (!wq)
+		return -EINVAL;
+
+	pthread_mutex_lock(&wq->lock);
+	wq->shutdown = true;
+	pthread_cond_broadcast(&wq->cond_empty);
+	pthread_mutex_unlock(&wq->lock);
+
+	while (wq->nworker) {
+		int ret = -pthread_join(wq->workers[wq->nworker - 1], NULL);
+
+		if (ret)
+			return ret;
+		--wq->nworker;
+	}
+	free(wq->workers);
+	pthread_mutex_destroy(&wq->lock);
+	pthread_cond_destroy(&wq->cond_empty);
+	pthread_cond_destroy(&wq->cond_full);
+	return 0;
+}
 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)
@@ -51,7 +74,6 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 		return -EINVAL;
 
 	wq->head = wq->tail = NULL;
-	wq->nworker = nworker;
 	wq->max_jobs = max_jobs;
 	wq->job_count = 0;
 	wq->shutdown = false;
@@ -67,14 +89,13 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 
 	for (i = 0; i < nworker; i++) {
 		ret = pthread_create(&wq->workers[i], NULL, worker_thread, wq);
-		if (ret) {
-			while (i)
-				pthread_cancel(wq->workers[--i]);
-			free(wq->workers);
-			return ret;
-		}
+		if (ret)
+			break;
 	}
-	return 0;
+	wq->nworker = i;
+	if (ret)
+		erofs_destroy_workqueue(wq);
+	return ret;
 }
 
 int erofs_queue_work(struct erofs_workqueue *wq, struct erofs_work *work)
@@ -99,25 +120,3 @@ int erofs_queue_work(struct erofs_workqueue *wq, struct erofs_work *work)
 	pthread_mutex_unlock(&wq->lock);
 	return 0;
 }
-
-int erofs_destroy_workqueue(struct erofs_workqueue *wq)
-{
-	unsigned int i;
-
-	if (!wq)
-		return -EINVAL;
-
-	pthread_mutex_lock(&wq->lock);
-	wq->shutdown = true;
-	pthread_cond_broadcast(&wq->cond_empty);
-	pthread_mutex_unlock(&wq->lock);
-
-	for (i = 0; i < wq->nworker; i++)
-		pthread_join(wq->workers[i], NULL);
-
-	free(wq->workers);
-	pthread_mutex_destroy(&wq->lock);
-	pthread_cond_destroy(&wq->cond_empty);
-	pthread_cond_destroy(&wq->cond_full);
-	return 0;
-}
Signature · 1 line
2.43.5
#1 lore raw

LGTM.

Thanks,

Yifan Zhao

Quoted 114 lines · Gao Xiang

Bionic (Android's libc) does not have pthread_cancel, call erofs_destroy_workqueue() when initialization fails.

Cc Kelvin Zhang <[email protected]>
Cc Yifan Zhao <[email protected]>

---
https://lore.kernel.org/r/[email protected]
https://lore.kernel.org/r/[email protected]

  lib/workqueue.c | 63 ++++++++++++++++++++++++-------------------------
  1 file changed, 31 insertions(+), 32 deletions(-)
lib/workqueue.c +31 -32
index 47cec9b..840c204 100644
@@ -15,9 +15,9 @@ static void *worker_thread(void *arg)
  	while (true) {
  		pthread_mutex_lock(&wq->lock);
 
-		while (wq->job_count == 0 && !wq->shutdown)
+		while (!wq->job_count && !wq->shutdown)
  			pthread_cond_wait(&wq->cond_empty, &wq->lock);
-		if (wq->job_count == 0 && wq->shutdown) {
+		if (!wq->job_count && wq->shutdown) {
  			pthread_mutex_unlock(&wq->lock);
  			break;
  		}
@@ -40,6 +40,29 @@ static void *worker_thread(void *arg)
  	return NULL;
  }
 
+int erofs_destroy_workqueue(struct erofs_workqueue *wq)
+{
+	if (!wq)
+		return -EINVAL;
+
+	pthread_mutex_lock(&wq->lock);
+	wq->shutdown = true;
+	pthread_cond_broadcast(&wq->cond_empty);
+	pthread_mutex_unlock(&wq->lock);
+
+	while (wq->nworker) {
+		int ret = -pthread_join(wq->workers[wq->nworker - 1], NULL);
+
+		if (ret)
+			return ret;
+		--wq->nworker;
+	}
+	free(wq->workers);
+	pthread_mutex_destroy(&wq->lock);
+	pthread_cond_destroy(&wq->cond_empty);
+	pthread_cond_destroy(&wq->cond_full);
+	return 0;
+}
  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)
@@ -51,7 +74,6 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
  		return -EINVAL;
 
  	wq->head = wq->tail = NULL;
-	wq->nworker = nworker;
  	wq->max_jobs = max_jobs;
  	wq->job_count = 0;
  	wq->shutdown = false;
@@ -67,14 +89,13 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 
  	for (i = 0; i < nworker; i++) {
  		ret = pthread_create(&wq->workers[i], NULL, worker_thread, wq);
-		if (ret) {
-			while (i)
-				pthread_cancel(wq->workers[--i]);
-			free(wq->workers);
-			return ret;
-		}
+		if (ret)
+			break;
  	}
-	return 0;
+	wq->nworker = i;
+	if (ret)
+		erofs_destroy_workqueue(wq);
+	return ret;
  }
 
  int erofs_queue_work(struct erofs_workqueue *wq, struct erofs_work *work)
@@ -99,25 +120,3 @@ int erofs_queue_work(struct erofs_workqueue *wq, struct erofs_work *work)
  	pthread_mutex_unlock(&wq->lock);
  	return 0;
  }
-
-int erofs_destroy_workqueue(struct erofs_workqueue *wq)
-{
-	unsigned int i;
-
-	if (!wq)
-		return -EINVAL;
-
-	pthread_mutex_lock(&wq->lock);
-	wq->shutdown = true;
-	pthread_cond_broadcast(&wq->cond_empty);
-	pthread_mutex_unlock(&wq->lock);
-
-	for (i = 0; i < wq->nworker; i++)
-		pthread_join(wq->workers[i], NULL);
-
-	free(wq->workers);
-	pthread_mutex_destroy(&wq->lock);
-	pthread_cond_destroy(&wq->cond_empty);
-	pthread_cond_destroy(&wq->cond_full);
-	return 0;
-}
author
#2 lore raw
Quoted 1 line · Yifan Zhao

LGTM.

Thanks, I will add as a r-v-b tag then.

Thanks,
Gao Xiang

Quoted 116 lines

Thanks,

Yifan Zhao

Quoted 109 lines · Gao Xiang

Bionic (Android's libc) does not have pthread_cancel, call erofs_destroy_workqueue() when initialization fails.

Cc Kelvin Zhang <[email protected]>
Cc Yifan Zhao <[email protected]>

---
https://lore.kernel.org/r/[email protected]
https://lore.kernel.org/r/[email protected]

  lib/workqueue.c | 63 ++++++++++++++++++++++++-------------------------

  1 file changed, 31 insertions(+), 32 deletions(-)
lib/workqueue.c +31 -32
index 47cec9b..840c204 100644
@@ -15,9 +15,9 @@ static void *worker_thread(void *arg)
       while (true) {
           pthread_mutex_lock(&wq->lock);
-        while (wq->job_count == 0 && !wq->shutdown)
+        while (!wq->job_count && !wq->shutdown)
               pthread_cond_wait(&wq->cond_empty, &wq->lock);
-        if (wq->job_count == 0 && wq->shutdown) {
+        if (!wq->job_count && wq->shutdown) {
               pthread_mutex_unlock(&wq->lock);
               break;
           }
@@ -40,6 +40,29 @@ static void *worker_thread(void *arg)
       return NULL;
   }
+int erofs_destroy_workqueue(struct erofs_workqueue *wq)
+{
+    if (!wq)
+        return -EINVAL;
+
+    pthread_mutex_lock(&wq->lock);
+    wq->shutdown = true;
+    pthread_cond_broadcast(&wq->cond_empty);
+    pthread_mutex_unlock(&wq->lock);
+
+    while (wq->nworker) {
+        int ret = -pthread_join(wq->workers[wq->nworker - 1], NULL);
+
+        if (ret)
+            return ret;
+        --wq->nworker;
+    }
+    free(wq->workers);
+    pthread_mutex_destroy(&wq->lock);
+    pthread_cond_destroy(&wq->cond_empty);
+    pthread_cond_destroy(&wq->cond_full);
+    return 0;
+}
   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)
@@ -51,7 +74,6 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
           return -EINVAL;
       wq->head = wq->tail = NULL;
-    wq->nworker = nworker;
       wq->max_jobs = max_jobs;
       wq->job_count = 0;
       wq->shutdown = false;
@@ -67,14 +89,13 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
       for (i = 0; i < nworker; i++) {
           ret = pthread_create(&wq->workers[i], NULL, worker_thread, wq);
-        if (ret) {
-            while (i)
-                pthread_cancel(wq->workers[--i]);
-            free(wq->workers);
-            return ret;
-        }
+        if (ret)
+            break;
       }
-    return 0;
+    wq->nworker = i;
+    if (ret)
+        erofs_destroy_workqueue(wq);
+    return ret;
   }
   int erofs_queue_work(struct erofs_workqueue *wq, struct erofs_work *work)
@@ -99,25 +120,3 @@ int erofs_queue_work(struct erofs_workqueue *wq, struct erofs_work *work)
       pthread_mutex_unlock(&wq->lock);
       return 0;
   }
-
-int erofs_destroy_workqueue(struct erofs_workqueue *wq)
-{
-    unsigned int i;
-
-    if (!wq)
-        return -EINVAL;
-
-    pthread_mutex_lock(&wq->lock);
-    wq->shutdown = true;
-    pthread_cond_broadcast(&wq->cond_empty);
-    pthread_mutex_unlock(&wq->lock);
-
-    for (i = 0; i < wq->nworker; i++)
-        pthread_join(wq->workers[i], NULL);
-
-    free(wq->workers);
-    pthread_mutex_destroy(&wq->lock);
-    pthread_cond_destroy(&wq->cond_empty);
-    pthread_cond_destroy(&wq->cond_full);
-    return 0;
-}
author
#3 lore raw
In reply to Gao Xiang

Applied with the following diff:

lib/compress.c +3 -1
index ce1056b..8446fe4 100644
@@ -1790,7 +1790,9 @@ int z_erofs_compress_init(struct erofs_sb_info *sbi, struct erofs_buffer_head *s
 					    cfg.c_mt_workers << 2,
 					    z_erofs_mt_wq_tls_alloc,
 					    z_erofs_mt_wq_tls_free);
-		z_erofs_mt_enabled = !ret;
+		if (ret)
+			return ret;
+		z_erofs_mt_enabled = true;
 	}
 	pthread_mutex_init(&g_ictx.mutex, NULL);
 	pthread_cond_init(&g_ictx.cond, NULL);
lib/workqueue.c +1 -1
index 840c204..2d4aa68 100644
@@ -88,7 +88,7 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 		return -ENOMEM;
 
 	for (i = 0; i < nworker; i++) {
-		ret = pthread_create(&wq->workers[i], NULL, worker_thread, wq);
+		ret = -pthread_create(&wq->workers[i], NULL, worker_thread, wq);
 		if (ret)
 			break;
 	}