Browse Source

Start threads with support for tor's subsystem management

The 'start_tor_thread' function is a new drop-in replacement for the
'spawn_func' function, but uses the subsystem manager to properly
initialize and free threadlocals, and calls the 'spawn_exit' function
as required by comments in 'compat_pthreads.c'.
Steven Engler 4 years ago
parent
commit
4c518522df
4 changed files with 56 additions and 2 deletions
  1. 41 0
      src/app/main/tor_threads.c
  2. 11 0
      src/app/main/tor_threads.h
  3. 2 0
      src/core/include.am
  4. 2 2
      src/core/mainloop/cpuworker.c

+ 41 - 0
src/app/main/tor_threads.c

@@ -0,0 +1,41 @@
+/* Copyright (c) 2007-2019, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "app/main/tor_threads.h"
+
+#include "lib/thread/threads.h"
+#include "app/main/subsysmgr.h"
+
+/** Wraps a void (*)(void*) function and its argument so we can
+ * invoke them with proper subsystem thread management.
+ */
+typedef struct tor_thread_helper_info_t {
+  void (*func)(void *);
+  void *data;
+} tor_thread_helper_info_t;
+
+static void tor_thread_helper(void *_info) ATTR_NORETURN;
+
+static void
+tor_thread_helper(void *_info)
+{
+  tor_thread_helper_info_t *info = _info;
+  void (*func)(void*) = info->func;
+  void *data = info->data;
+  tor_free(_info);
+
+  subsystems_thread_init();
+  func(data);
+  subsystems_thread_cleanup();
+  spawn_exit();
+}
+
+int
+start_tor_thread(void (*func)(void *), void *data)
+{
+  tor_thread_helper_info_t *d = tor_malloc(sizeof(tor_thread_helper_info_t));
+  d->func = func;
+  d->data = data;
+
+  return spawn_func(tor_thread_helper, d);
+}

+ 11 - 0
src/app/main/tor_threads.h

@@ -0,0 +1,11 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2019, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_APP_THREADS_H
+#define TOR_APP_THREADS_H
+
+int start_tor_thread(void (*func)(void *), void *data);
+
+#endif /* !defined(TOR_APP_THREADS_H) */

+ 2 - 0
src/core/include.am

@@ -14,6 +14,7 @@ LIBTOR_APP_A_SOURCES = 				\
 	src/app/main/shutdown.c			\
 	src/app/main/subsystem_list.c		\
 	src/app/main/subsysmgr.c		\
+	src/app/main/tor_threads.c		\
 	src/core/crypto/hs_ntor.c		\
 	src/core/crypto/onion_crypto.c		\
 	src/core/crypto/onion_fast.c		\
@@ -220,6 +221,7 @@ noinst_HEADERS +=					\
 	src/app/main/ntmain.h				\
 	src/app/main/shutdown.h 			\
 	src/app/main/subsysmgr.h			\
+	src/app/main/tor_threads.h			\
 	src/core/crypto/hs_ntor.h			\
 	src/core/crypto/onion_crypto.h	        	\
 	src/core/crypto/onion_fast.h			\

+ 2 - 2
src/core/mainloop/cpuworker.c

@@ -32,7 +32,7 @@
 #include "feature/relay/router.h"
 #include "lib/evloop/workqueue.h"
 #include "core/crypto/onion_crypto.h"
-#include "lib/thread/threads.h"
+#include "app/main/tor_threads.h"
 
 #include "core/or/or_circuit_st.h"
 
@@ -99,7 +99,7 @@ cpu_init(void)
                                 worker_state_new,
                                 worker_state_free_void,
                                 NULL,
-                                spawn_func);
+                                start_tor_thread);
 
     int r = threadpool_register_reply_event(threadpool, NULL);