[sheepdog] [PATCH v2 06/10] collie: add a function to wait for all the running works

MORITA Kazutaka morita.kazutaka at gmail.com
Mon May 13 17:11:14 CEST 2013


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

This is in preparation to use work queue in collie.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/Makefile.am |    2 +-
 collie/collie.h    |    3 +++
 collie/common.c    |    8 ++++++++
 include/work.h     |    1 +
 lib/work.c         |   12 ++++++++++++
 5 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/collie/Makefile.am b/collie/Makefile.am
index 386aa49..3fba2dd 100644
--- a/collie/Makefile.am
+++ b/collie/Makefile.am
@@ -30,7 +30,7 @@ collie_SOURCES          += debug.c
 override CFLAGS         := $(subst -pg -gstabs,,$(CFLAGS))
 endif
 
-collie_LDADD	  	= ../lib/libsheepdog.a
+collie_LDADD	  	= ../lib/libsheepdog.a -lpthread
 collie_DEPENDENCIES	= ../lib/libsheepdog.a
 
 noinst_HEADERS		= treeview.h collie.h
diff --git a/collie/collie.h b/collie/collie.h
index 1f5de61..ec8d667 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -21,6 +21,8 @@
 #include "sheep.h"
 #include "exits.h"
 #include "option.h"
+#include "work.h"
+#include "event.h"
 
 #define SUBCMD_FLAG_NEED_NODELIST (1 << 0)
 #define SUBCMD_FLAG_NEED_ARG (1 << 1)
@@ -73,6 +75,7 @@ int send_light_req(struct sd_req *hdr, const char *host, int port);
 int do_generic_subcommand(struct subcommand *sub, int argc, char **argv);
 int update_node_list(int max_nodes);
 void confirm(const char *message);
+void work_queue_wait(struct work_queue *q);
 
 extern struct command vdi_command;
 extern struct command node_command;
diff --git a/collie/common.c b/collie/common.c
index 60c6701..90a12de 100644
--- a/collie/common.c
+++ b/collie/common.c
@@ -246,3 +246,11 @@ void confirm(const char *message)
 	if (ret == NULL || strncasecmp(input, "yes", 3) != 0)
 		exit(EXIT_SUCCESS);
 }
+
+void work_queue_wait(struct work_queue *q)
+{
+	assert(is_main_thread());
+
+	while (!work_queue_empty(q))
+		event_loop(-1);
+}
diff --git a/include/work.h b/include/work.h
index dc95791..3349ee2 100644
--- a/include/work.h
+++ b/include/work.h
@@ -60,5 +60,6 @@ int init_work_queue(size_t (*get_nr_nodes)(void), void (*create_cb)(pthread_t),
 struct work_queue *create_work_queue(const char *name, enum wq_thread_control);
 struct work_queue *create_ordered_work_queue(const char *name);
 void queue_work(struct work_queue *q, struct work *work);
+bool work_queue_empty(struct work_queue *q);
 
 #endif
diff --git a/lib/work.c b/lib/work.c
index f457cd7..2bfd742 100644
--- a/lib/work.c
+++ b/lib/work.c
@@ -342,3 +342,15 @@ struct work_queue *create_ordered_work_queue(const char *name)
 {
 	return create_work_queue(name, WQ_ORDERED);
 }
+
+bool work_queue_empty(struct work_queue *q)
+{
+	struct worker_info *wi = container_of(q, struct worker_info, q);
+	size_t nr_works;
+
+	pthread_mutex_lock(&wi->pending_lock);
+	nr_works = wi->nr_running + wi->nr_pending;
+	pthread_mutex_unlock(&wi->pending_lock);
+
+	return nr_works == 0;
+}
-- 
1.7.9.5




More information about the sheepdog mailing list