[tor-commits] [tor/master] Rename and clarify some functions for periodic events

asn at torproject.org asn at torproject.org
Tue Apr 30 16:18:52 UTC 2019


commit b7cc631d2391d55c078e0693303f6a0d4d9e963a
Author: Nick Mathewson <nickm at torproject.org>
Date:   Fri Apr 26 13:17:35 2019 -0400

    Rename and clarify some functions for periodic events
    
    When we tell the periodic event manager about an event, we are
    "registering" that event.  The event sits around without being
    usable, however, until we "connect" the event to libevent.  In the
    end, we "disconnect" the event and remove its libevent parts.
    
    Previously, we called these operations "add", "setup", and
    "destroy", which led to confusion.
---
 src/core/mainloop/mainloop.c           |  6 ++---
 src/core/mainloop/periodic.c           | 46 +++++++++++++++++++++-------------
 src/core/mainloop/periodic.h           | 10 ++++----
 src/feature/dirauth/dirauth_periodic.c | 12 ++++-----
 src/feature/dirauth/dirauth_periodic.h |  2 +-
 src/feature/dirauth/dirauth_sys.c      |  2 +-
 src/test/test_periodic_event.c         |  6 ++---
 7 files changed, 48 insertions(+), 36 deletions(-)

diff --git a/src/core/mainloop/mainloop.c b/src/core/mainloop/mainloop.c
index 0c825bb1b..30dad956a 100644
--- a/src/core/mainloop/mainloop.c
+++ b/src/core/mainloop/mainloop.c
@@ -1540,7 +1540,7 @@ initialize_periodic_events(void)
   periodic_events_initialized = 1;
 
   for (int i = 0; mainloop_periodic_events[i].name; ++i) {
-    periodic_events_add(&mainloop_periodic_events[i]);
+    periodic_events_register(&mainloop_periodic_events[i]);
   }
 
   /* Set up all periodic events. We'll launch them by roles. */
@@ -1559,7 +1559,7 @@ initialize_periodic_events(void)
 STATIC void
 teardown_periodic_events(void)
 {
-  periodic_events_destroy_all();
+  periodic_events_disconnect_all();
   check_descriptor_event = NULL;
   fetch_networkstatus_event = NULL;
   launch_descriptor_fetches_event = NULL;
@@ -2630,7 +2630,7 @@ do_main_loop(void)
   tor_assert(periodic_events_initialized);
   initialize_mainloop_events();
 
-  periodic_events_setup_all();
+  periodic_events_connect_all();
 
   struct timeval one_second = { 1, 0 };
   initialize_periodic_events_event = tor_evtimer_new(
diff --git a/src/core/mainloop/periodic.c b/src/core/mainloop/periodic.c
index ceef74099..dbc4553a7 100644
--- a/src/core/mainloop/periodic.c
+++ b/src/core/mainloop/periodic.c
@@ -12,7 +12,12 @@
  *
  * This module manages a global list of periodic_event_item_t objects,
  * each corresponding to a single event.  To register an event, pass it to
- * periodic_events_add() when initializing your subsystem.
+ * periodic_events_register() when initializing your subsystem.
+ *
+ * Registering an event makes the periodic event subsystem know about it, but
+ * doesn't cause the event to get created immediately.  Before the event can
+ * be started, periodic_event_connect_all() must be called by mainloop.c to
+ * connect all the events to Libevent.
  *
  * We expect that periodic_event_item_t objects will be statically allocated;
  * we set them up and tear them down here, but we don't take ownership of
@@ -33,7 +38,8 @@
 static const int MAX_INTERVAL = 10 * 365 * 86400;
 
 /**
- *
+ * Global list of periodic events that have been registered with
+ * <b>periodic_event_register</a>.
  **/
 static smartlist_t *the_periodic_events = NULL;
 
@@ -106,9 +112,10 @@ periodic_event_reschedule(periodic_event_item_t *event)
   }
 }
 
-/** Initializes the libevent backend for a periodic event. */
+/** Connects a periodic event to the Libevent backend.  Does not launch the
+ * event immediately. */
 void
-periodic_event_setup(periodic_event_item_t *event)
+periodic_event_connect(periodic_event_item_t *event)
 {
   if (event->ev) { /* Already setup? This is a bug */
     log_err(LD_BUG, "Initial dispatch should only be done once.");
@@ -126,7 +133,7 @@ void
 periodic_event_launch(periodic_event_item_t *event)
 {
   if (! event->ev) { /* Not setup? This is a bug */
-    log_err(LD_BUG, "periodic_event_launch without periodic_event_setup");
+    log_err(LD_BUG, "periodic_event_launch without periodic_event_connect");
     tor_assert(0);
   }
   /* Event already enabled? This is a bug */
@@ -140,9 +147,9 @@ periodic_event_launch(periodic_event_item_t *event)
   periodic_event_dispatch(event->ev, event);
 }
 
-/** Release all storage associated with <b>event</b> */
-void
-periodic_event_destroy(periodic_event_item_t *event)
+/** Disconnect and unregister the periodic event in <b>event</b> */
+static void
+periodic_event_disconnect(periodic_event_item_t *event)
 {
   if (!event)
     return;
@@ -205,7 +212,7 @@ periodic_event_schedule_and_disable(periodic_event_item_t *event)
  * take ownership of it.
  **/
 void
-periodic_events_add(periodic_event_item_t *item)
+periodic_events_register(periodic_event_item_t *item)
 {
   if (!the_periodic_events)
     the_periodic_events = smartlist_new();
@@ -216,9 +223,11 @@ periodic_events_add(periodic_event_item_t *item)
   smartlist_add(the_periodic_events, item);
 }
 
-/** Set up all not-previously setup periodic events. */
+/**
+ * Make all registered periodic events connect to the libevent backend.
+ */
 void
-periodic_events_setup_all(void)
+periodic_events_connect_all(void)
 {
   if (! the_periodic_events)
     return;
@@ -226,11 +235,12 @@ periodic_events_setup_all(void)
   SMARTLIST_FOREACH_BEGIN(the_periodic_events, periodic_event_item_t *, item) {
     if (item->ev)
       continue;
-    periodic_event_setup(item);
+    periodic_event_connect(item);
   } SMARTLIST_FOREACH_END(item);
 }
 
-/** Reset all the registered periodic events so we'll do all our actions again
+/**
+ * Reset all the registered periodic events so we'll do all our actions again
  * as if we just started up.
  *
  * Useful if our clock just moved back a long time from the future,
@@ -307,18 +317,20 @@ periodic_events_rescan_by_roles(int roles, bool net_disabled)
   } SMARTLIST_FOREACH_END(item);
 }
 
-/** Invoked at shutdown: free resources used in this module.
+/**
+ * Invoked at shutdown: disconnect and unregister all periodic events.
  *
  * Does not free the periodic_event_item_t object themselves, because we do
- * not own them. */
+ * not own them.
+ */
 void
-periodic_events_destroy_all(void)
+periodic_events_disconnect_all(void)
 {
   if (! the_periodic_events)
     return;
 
   SMARTLIST_FOREACH_BEGIN(the_periodic_events, periodic_event_item_t *, item) {
-    periodic_event_destroy(item);
+    periodic_event_disconnect(item);
   } SMARTLIST_FOREACH_END(item);
 
   smartlist_free(the_periodic_events);
diff --git a/src/core/mainloop/periodic.h b/src/core/mainloop/periodic.h
index 4e4e8b2c9..a9aa46196 100644
--- a/src/core/mainloop/periodic.h
+++ b/src/core/mainloop/periodic.h
@@ -83,19 +83,19 @@ periodic_event_is_enabled(const periodic_event_item_t *item)
 }
 
 void periodic_event_launch(periodic_event_item_t *event);
-void periodic_event_setup(periodic_event_item_t *event);
-void periodic_event_destroy(periodic_event_item_t *event);
+void periodic_event_connect(periodic_event_item_t *event);
+//void periodic_event_disconnect(periodic_event_item_t *event);
 void periodic_event_reschedule(periodic_event_item_t *event);
 void periodic_event_enable(periodic_event_item_t *event);
 void periodic_event_disable(periodic_event_item_t *event);
 void periodic_event_schedule_and_disable(periodic_event_item_t *event);
 
-void periodic_events_add(periodic_event_item_t *item);
-void periodic_events_setup_all(void);
+void periodic_events_register(periodic_event_item_t *item);
+void periodic_events_connect_all(void);
 void periodic_events_reset_all(void);
 periodic_event_item_t *periodic_events_find(const char *name);
 void periodic_events_rescan_by_roles(int roles, bool net_disabled);
-void periodic_events_destroy_all(void);
+void periodic_events_disconnect_all(void);
 
 int safe_timer_diff(time_t now, time_t next);
 
diff --git a/src/feature/dirauth/dirauth_periodic.c b/src/feature/dirauth/dirauth_periodic.c
index 1fa0ca9cf..cfbb156b9 100644
--- a/src/feature/dirauth/dirauth_periodic.c
+++ b/src/feature/dirauth/dirauth_periodic.c
@@ -132,11 +132,11 @@ downrate_stability_callback(time_t now, const or_options_t *options)
 DECLARE_EVENT(downrate_stability, AUTHORITIES, 0);
 
 void
-dirauth_add_periodic_events(void)
+dirauth_register_periodic_events(void)
 {
-  periodic_events_add(&downrate_stability_event);
-  periodic_events_add(&launch_reachability_tests_event);
-  periodic_events_add(&save_stability_event);
-  periodic_events_add(&check_authority_cert_event);
-  periodic_events_add(&dirvote_event);
+  periodic_events_register(&downrate_stability_event);
+  periodic_events_register(&launch_reachability_tests_event);
+  periodic_events_register(&save_stability_event);
+  periodic_events_register(&check_authority_cert_event);
+  periodic_events_register(&dirvote_event);
 }
diff --git a/src/feature/dirauth/dirauth_periodic.h b/src/feature/dirauth/dirauth_periodic.h
index 680c0c4be..de14cbb3c 100644
--- a/src/feature/dirauth/dirauth_periodic.h
+++ b/src/feature/dirauth/dirauth_periodic.h
@@ -9,7 +9,7 @@
 
 #ifdef HAVE_MODULE_DIRAUTH
 
-void dirauth_add_periodic_events(void);
+void dirauth_register_periodic_events(void);
 void reschedule_dirvote(const or_options_t *options);
 
 #else
diff --git a/src/feature/dirauth/dirauth_sys.c b/src/feature/dirauth/dirauth_sys.c
index 6845e62c2..bb482f268 100644
--- a/src/feature/dirauth/dirauth_sys.c
+++ b/src/feature/dirauth/dirauth_sys.c
@@ -14,7 +14,7 @@
 static int
 subsys_dirauth_initialize(void)
 {
-  dirauth_add_periodic_events();
+  dirauth_register_periodic_events();
   return 0;
 }
 
diff --git a/src/test/test_periodic_event.c b/src/test/test_periodic_event.c
index 19325ed3b..961a8be69 100644
--- a/src/test/test_periodic_event.c
+++ b/src/test/test_periodic_event.c
@@ -51,7 +51,7 @@ test_pe_initialize(void *arg)
    * need to run the main loop and then wait for a second delaying the unit
    * tests. Instead, we'll test the callback work indepedently elsewhere. */
   initialize_periodic_events();
-  periodic_events_setup_all();
+  periodic_events_connect_all();
   set_network_participation(false);
   rescan_periodic_events(get_options());
 
@@ -111,7 +111,7 @@ test_pe_launch(void *arg)
 #endif
 
   initialize_periodic_events();
-  periodic_events_setup_all();
+  periodic_events_connect_all();
 
   /* Now that we've initialized, rescan the list to launch. */
   periodic_events_on_new_options(options);
@@ -302,7 +302,7 @@ test_pe_hs_service(void *arg)
   consider_hibernation(time(NULL));
   /* Initialize the events so we can enable them */
   initialize_periodic_events();
-  periodic_events_setup_all();
+  periodic_events_connect_all();
 
   /* Hack: We'll set a dumb fn() of each events so they don't get called when
    * dispatching them. We just want to test the state of the callbacks, not





More information about the tor-commits mailing list