aboutsummaryrefslogtreecommitdiff
path: root/src/or/main.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-05-09 12:40:45 -0400
committerNick Mathewson <nickm@torproject.org>2018-05-10 09:01:56 -0400
commite722bba263e6c4648fff4259a14677993697337c (patch)
treef87f3600309992e37b0c0112599766e0c55b9d8e /src/or/main.c
parent4218511ecd8a1facbd38d744b1bd518c05517988 (diff)
downloadtor-e722bba263e6c4648fff4259a14677993697337c.tar.gz
tor-e722bba263e6c4648fff4259a14677993697337c.zip
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.
Diffstat (limited to 'src/or/main.c')
-rw-r--r--src/or/main.c42
1 files changed, 31 insertions, 11 deletions
diff --git a/src/or/main.c b/src/or/main.c
index 682ec29ab9..0493596aaf 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;