Browse Source

Add thread init functionality to the subsystem manager

Subsystems can now set an optional 'thread_init' function so that they
can initialize threadlocals in a single location.
Steven Engler 4 years ago
parent
commit
ad15c31896
3 changed files with 28 additions and 0 deletions
  1. 21 0
      src/app/main/subsysmgr.c
  2. 1 0
      src/app/main/subsysmgr.h
  3. 6 0
      src/lib/subsys/subsys.h

+ 21 - 0
src/app/main/subsysmgr.c

@@ -230,6 +230,27 @@ subsystems_postfork(void)
   }
 }
 
+/**
+ * Run thread-init code on all subsystems that declare any
+ **/
+void
+subsystems_thread_init(void)
+{
+  check_and_setup();
+
+  for (unsigned i = 0; i < n_tor_subsystems; ++i) {
+    const subsys_fns_t *sys = tor_subsystems[i];
+    if (!sys->supported)
+      continue;
+    if (! sys_initialized[i])
+      continue;
+    if (sys->thread_init) {
+      log_debug(LD_GENERAL, "Thread init: %s", sys->name);
+      sys->thread_init();
+    }
+  }
+}
+
 /**
  * Run thread-cleanup code on all subsystems that declare any
  **/

+ 1 - 0
src/app/main/subsysmgr.h

@@ -24,6 +24,7 @@ void subsystems_shutdown_downto(int level);
 
 void subsystems_prefork(void);
 void subsystems_postfork(void);
+void subsystems_thread_init(void);
 void subsystems_thread_cleanup(void);
 
 #endif /* !defined(TOR_SUBSYSMGR_T) */

+ 6 - 0
src/lib/subsys/subsys.h

@@ -70,6 +70,12 @@ typedef struct subsys_fns_t {
    */
   void (*postfork)(void);
 
+  /**
+   * Initialize any thread-local resources held by this subsystem. Called
+   * after the subsystem's global components are initialized.
+   */
+  void (*thread_init)(void);
+
   /**
    * Free any thread-local resources held by this subsystem. Called before
    * the thread exits.