[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Add a new function to enable/disable the per-second timer as needed



commit e722bba263e6c4648fff4259a14677993697337c
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Wed May 9 12:40:45 2018 -0400

    Add a new function to enable/disable the per-second timer as needed
    
    We're about to use this to turn off the per-second timer when the
    network is disabled and there aren't any per-second controller
    events enabled.
---
 src/or/main.c | 42 +++++++++++++++++++++++++++++++-----------
 src/or/main.h |  1 +
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/or/main.c b/src/or/main.c
index 682ec29ab..0493596aa 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -2491,6 +2491,36 @@ hs_service_callback(time_t now, const or_options_t *options)
 
 /** Timer: used to invoke second_elapsed_callback() once per second. */
 static periodic_timer_t *second_timer = NULL;
+
+/**
+ * Enable or disable the per-second timer as appropriate, creating it if
+ * necessary.
+ */
+void
+reschedule_per_second_timer(void)
+{
+  struct timeval one_second;
+  one_second.tv_sec = 1;
+  one_second.tv_usec = 0;
+
+  if (! second_timer) {
+    second_timer = periodic_timer_new(tor_libevent_get_base(),
+                                      &one_second,
+                                      second_elapsed_callback,
+                                      NULL);
+    tor_assert(second_timer);
+  }
+
+  const bool run_per_second_events =
+    control_any_per_second_event_enabled() || ! net_is_completely_disabled();
+
+  if (run_per_second_events) {
+    periodic_timer_launch(second_timer, &one_second);
+  } else {
+    periodic_timer_disable(second_timer);
+  }
+}
+
 /** Number of libevent errors in the last second: we die if we get too many. */
 static int n_libevent_errors = 0;
 /** Last time that second_elapsed_callback was called. */
@@ -2824,17 +2854,7 @@ do_main_loop(void)
   }
 
   /* set up once-a-second callback. */
-  if (! second_timer) {
-    struct timeval one_second;
-    one_second.tv_sec = 1;
-    one_second.tv_usec = 0;
-
-    second_timer = periodic_timer_new(tor_libevent_get_base(),
-                                      &one_second,
-                                      second_elapsed_callback,
-                                      NULL);
-    tor_assert(second_timer);
-  }
+  reschedule_per_second_timer();
 
 #ifdef HAVE_SYSTEMD_209
   uint64_t watchdog_delay;
diff --git a/src/or/main.h b/src/or/main.h
index a312b51e0..3cfa9c82b 100644
--- a/src/or/main.h
+++ b/src/or/main.h
@@ -92,6 +92,7 @@ uint64_t get_main_loop_error_count(void);
 uint64_t get_main_loop_idle_count(void);
 
 void periodic_events_on_new_options(const or_options_t *options);
+void reschedule_per_second_timer(void);
 
 extern time_t time_of_process_start;
 extern int quiet_level;



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits