Thread (22 messages) 22 messages, 6 authors, 2016-06-15
STALE3669d

[PATCH 2/3] run-command: factor out running_main_thread function

From: Jeff King <hidden>
Date: 2016-06-15 22:56:51
Subsystem: the rest · Maintainer: Linus Torvalds

When the async subsystem is used, we may spawn off
sub-threads (if the platform supports it), and consider the
original thread to be the "main" thread of execution. We use
this information in a custom die_routine to decide whether
to call pthread_exit or a regular full-process exit.

Let's pull this decision out into its own function so that
other parts of the system can use it to make decisions.

Signed-off-by: Jeff King <redacted>
---
 run-command.c | 15 ++++++++++++++-
 run-command.h | 10 ++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/run-command.c b/run-command.c
index 765c2ce..69ca052 100644
--- a/run-command.c
+++ b/run-command.c
@@ -603,7 +603,7 @@ static NORETURN void die_async(const char *err, va_list params)
 {
 	vreportf("fatal: ", err, params);
 
-	if (!pthread_equal(main_thread, pthread_self())) {
+	if (!running_main_thread()) {
 		struct async *async = pthread_getspecific(async_key);
 		if (async->proc_in >= 0)
 			close(async->proc_in);
@@ -614,6 +614,19 @@ static NORETURN void die_async(const char *err, va_list params)
 
 	exit(128);
 }
+
+int running_main_thread(void)
+{
+	return pthread_equal(main_thread, pthread_self());
+}
+
+#else /* NO_PTHREADS defined */
+
+int running_main_thread(void)
+{
+	return 1;
+}
+
 #endif
 
 int start_async(struct async *async)
diff --git a/run-command.h b/run-command.h
index 221ce33..981dd10 100644
--- a/run-command.h
+++ b/run-command.h
@@ -92,4 +92,14 @@ int finish_async(struct async *async);
 int start_async(struct async *async);
 int finish_async(struct async *async);
 
+/*
+ * If the platform supports threads, returns 1 if we are running the "main"
+ * thread that spawned other async threads, and zero if we are executing one
+ * of the async threads.
+ *
+ * If the platform does not support threads, returns 1 (we are always in the
+ * main thread then).
+ */
+int running_main_thread(void);
+
 #endif
-- 
1.8.2.8.g44e4c28
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help