[tor-commits] [tor/master] Refactor initialization logic for control-event-queue

nickm at torproject.org nickm at torproject.org
Tue Aug 18 13:10:55 UTC 2015


commit 81f3572467583c54eb2a22c3af93f7c819796563
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Aug 12 09:35:26 2015 -0400

    Refactor initialization logic for control-event-queue
    
    This puts the init logic in a separate function, which we will need
    once we have locking.
---
 src/or/config.c           |    3 +++
 src/or/control.c          |   38 +++++++++++++++++++++++---------------
 src/or/control.h          |    2 ++
 src/test/testing_common.c |    2 ++
 4 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/or/config.c b/src/or/config.c
index 011a36d..9573b61 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1106,6 +1106,9 @@ options_act_reversible(const or_options_t *old_options, char **msg)
       init_libevent(options);
       libevent_initialized = 1;
 
+      /* This has to come up after libevent is initialized. */
+      control_initialize_event_queue();
+
       /*
        * Initialize the scheduler - this has to come after
        * options_init_from_torrc() sets up libevent - why yes, that seems
diff --git a/src/or/control.c b/src/or/control.c
index 859c660..4b8e9c1 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -605,6 +605,24 @@ static smartlist_t *queued_control_events = NULL;
  * queued_control_events. */
 static struct event *flush_queued_events_event = NULL;
 
+void
+control_initialize_event_queue(void)
+{
+  if (queued_control_events == NULL) {
+    queued_control_events = smartlist_new();
+  }
+
+  if (flush_queued_events_event == NULL) {
+    struct event_base *b = tor_libevent_get_base();
+    if (b) {
+      flush_queued_events_event = tor_event_new(b,
+                                              -1, 0, flush_queued_events_cb,
+                                              NULL);
+      tor_assert(flush_queued_events_event);
+    }
+  }
+}
+
 /** Helper: inserts an event on the list of events queued to be sent to
  * one or more controllers, and schedules the events to be flushed if needed.
  *
@@ -619,10 +637,6 @@ static struct event *flush_queued_events_event = NULL;
 MOCK_IMPL(STATIC void,
 queue_control_event_string,(uint16_t event, char *msg))
 {
-  if (PREDICT_UNLIKELY(queued_control_events == NULL)) {
-    queued_control_events = smartlist_new();
-  }
-
   /* This is redundant with checks done elsewhere, but it's a last-ditch
    * attempt to avoid queueing something we shouldn't have to queue. */
   if (PREDICT_UNLIKELY( ! EVENT_IS_INTERESTING(event) )) {
@@ -634,27 +648,21 @@ queue_control_event_string,(uint16_t event, char *msg))
     return;
   }
 
-  /* No queueing an event while queueing an event */
-  ++block_event_queue;
-
   queued_event_t *ev = tor_malloc(sizeof(*ev));
   ev->event = event;
   ev->msg = msg;
 
+  /* No queueing an event while queueing an event */
+  ++block_event_queue;
+
+  tor_assert(queued_control_events);
   smartlist_add(queued_control_events, ev);
 
   /* We just put the first event on the queue; mark the queue to be
    * flushed.
    */
   if (smartlist_len(queued_control_events) == 1) {
-    if (PREDICT_UNLIKELY(flush_queued_events_event == NULL)) {
-      struct event_base *b = tor_libevent_get_base();
-      tor_assert(b);
-      flush_queued_events_event = tor_event_new(b,
-                                                -1, 0, flush_queued_events_cb,
-                                                NULL);
-      tor_assert(flush_queued_events_event);
-    }
+    tor_assert(flush_queued_events_event);
     event_active(flush_queued_events_event, EV_READ, 1);
   }
 
diff --git a/src/or/control.h b/src/or/control.h
index 8aa4780..574dd85 100644
--- a/src/or/control.h
+++ b/src/or/control.h
@@ -12,6 +12,8 @@
 #ifndef TOR_CONTROL_H
 #define TOR_CONTROL_H
 
+void control_initialize_event_queue(void);
+
 void control_update_global_event_mask(void);
 void control_adjust_event_log_severity(void);
 
diff --git a/src/test/testing_common.c b/src/test/testing_common.c
index 7f387c0..441024b 100644
--- a/src/test/testing_common.c
+++ b/src/test/testing_common.c
@@ -14,6 +14,7 @@ const char tor_git_revision[] = "";
 
 #include "orconfig.h"
 #include "or.h"
+#include "control.h"
 #include "config.h"
 #include "rephist.h"
 #include "backtrace.h"
@@ -237,6 +238,7 @@ main(int c, const char **v)
   update_approx_time(time(NULL));
   options = options_new();
   tor_threads_init();
+  control_initialize_event_queue();
   init_logging(1);
   configure_backtrace_handler(get_version());
 





More information about the tor-commits mailing list