[sheepdog] [PATCH v3 1/5] work: remove short thread

MORITA Kazutaka morita.kazutaka at gmail.com
Sun Mar 3 14:08:02 CET 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

This is a preparation for a dynamic worker thread pool.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 sheep/work.c |  101 +++++++++-------------------------------------------------
 1 file changed, 15 insertions(+), 86 deletions(-)

diff --git a/sheep/work.c b/sheep/work.c
index 3855e3f..81e7e54 100644
--- a/sheep/work.c
+++ b/sheep/work.c
@@ -25,7 +25,6 @@
 #include <sys/types.h>
 #include <sys/eventfd.h>
 #include <linux/types.h>
-#include <urcu/uatomic.h>
 
 #include "list.h"
 #include "util.h"
@@ -42,75 +41,15 @@ enum wq_state {
 	WQ_DEAD = (1U << 1),
 };
 
-/*
- * Short thread is created on demand and destroyed after serving the work for
- * gateway or io requests, aiming to solve two problems:
- *
- *  1. timeout of IO requests from guests. With on-demand short threads, we
- *     guarantee that there is always one thread available to execute the
- *     request as soon as possible.
- *  2. sheep halt for corner case that all gateway and io threads are executing
- *     local requests that ask for creation of another thread to execute the
- *     requests and sleep-wait for responses.
- */
-struct short_work {
-	struct work *work;
-	struct worker_info *wi;
-};
-
-static void *run_short_thread(void *arg)
-{
-	struct short_work *sw = arg;
-	eventfd_t value = 1;
-	static unsigned long idx;
-	int err;
-
-	/* Tell runtime to release resources after termination */
-	err = pthread_detach(pthread_self());
-	if (err)
-		panic("%s", strerror(err));
-
-	set_thread_name(sw->wi->name, uatomic_add_return(&idx, 1));
-
-	sw->work->fn(sw->work);
-
-	pthread_mutex_lock(&sw->wi->finished_lock);
-	list_add_tail(&sw->work->w_list, &sw->wi->finished_list);
-	pthread_mutex_unlock(&sw->wi->finished_lock);
-
-	eventfd_write(efd, value);
-	free(sw);
-	pthread_exit(NULL);
-}
-
-static inline void create_short_thread(struct worker_info *wi,
-				       struct work *work)
-{
-	pthread_t thread;
-	struct short_work *sw = xmalloc(sizeof *sw);
-	int err;
-
-	sw->work = work;
-	sw->wi = wi;
-
-	err = pthread_create(&thread, NULL, run_short_thread, sw);
-	if (err)
-		panic("%s", strerror(err));
-	short_thread_begin();
-}
-
 void queue_work(struct work_queue *q, struct work *work)
 {
 	struct worker_info *wi = container_of(q, struct worker_info, q);
 
-	if (wi->ordered) {
-		pthread_mutex_lock(&wi->pending_lock);
-		list_add_tail(&work->w_list, &wi->q.pending_list);
-		pthread_mutex_unlock(&wi->pending_lock);
+	pthread_mutex_lock(&wi->pending_lock);
+	list_add_tail(&work->w_list, &wi->q.pending_list);
+	pthread_mutex_unlock(&wi->pending_lock);
 
-		pthread_cond_signal(&wi->pending_cond);
-	} else
-		create_short_thread(wi, work);
+	pthread_cond_signal(&wi->pending_cond);
 }
 
 static void bs_thread_request_done(int fd, int events, void *data)
@@ -135,8 +74,6 @@ static void bs_thread_request_done(int fd, int events, void *data)
 			list_del(&work->w_list);
 
 			work->done(work);
-			if (!wi->ordered)
-				short_thread_end();
 		}
 	}
 }
@@ -213,30 +150,22 @@ struct work_queue *init_work_queue(const char *name, bool ordered)
 	wi->name = name;
 	wi->ordered = ordered;
 
+	INIT_LIST_HEAD(&wi->q.pending_list);
 	INIT_LIST_HEAD(&wi->finished_list);
 
-	pthread_mutex_init(&wi->finished_lock, NULL);
-
-	if (ordered) {
-		INIT_LIST_HEAD(&wi->q.pending_list);
-
-		pthread_cond_init(&wi->pending_cond, NULL);
-		pthread_mutex_init(&wi->pending_lock, NULL);
-		pthread_mutex_init(&wi->startup_lock, NULL);
+	pthread_cond_init(&wi->pending_cond, NULL);
 
-		pthread_mutex_lock(&wi->startup_lock);
-
-		ret = pthread_create(&wi->worker_thread, NULL, worker_routine,
-				     wi);
-		if (ret) {
-			sd_eprintf("failed to create worker thread: %s",
-				   strerror(ret));
-			goto destroy_threads;
-		}
+	pthread_mutex_init(&wi->finished_lock, NULL);
+	pthread_mutex_init(&wi->pending_lock, NULL);
+	pthread_mutex_init(&wi->startup_lock, NULL);
 
-		pthread_mutex_unlock(&wi->startup_lock);
-		total_ordered_workers++;
+	pthread_mutex_lock(&wi->startup_lock);
+	ret = pthread_create(&wi->worker_thread, NULL, worker_routine, wi);
+	if (ret) {
+		sd_eprintf("failed to create worker thread: %s", strerror(ret));
+		goto destroy_threads;
 	}
+	pthread_mutex_unlock(&wi->startup_lock);
 
 	list_add(&wi->worker_info_siblings, &worker_info_list);
 
-- 
1.7.9.5




More information about the sheepdog mailing list