[tor-commits] [tor/master] sched: Make the scheduler object static

nickm at torproject.org nickm at torproject.org
Fri Sep 15 16:07:57 UTC 2017


commit 734dbfa590baf560741f0754bd81c07dfb829485
Author: David Goulet <dgoulet at torproject.org>
Date:   Thu Sep 14 14:47:59 2017 -0400

    sched: Make the scheduler object static
    
    Each type of scheduler implements its own static scheduler_t object and
    returns a reference to it.
    
    This commit also makes it a const pointer that is it can only change inside
    the scheduler type subsystem but not outside for extra protection.
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/or/scheduler.c         |  5 ++---
 src/or/scheduler.h         |  2 +-
 src/or/scheduler_kist.c    | 26 ++++++++++++--------------
 src/or/scheduler_vanilla.c | 27 ++++++++++++---------------
 src/test/test_scheduler.c  |  4 ++--
 5 files changed, 29 insertions(+), 35 deletions(-)

diff --git a/src/or/scheduler.c b/src/or/scheduler.c
index 8d2dfac08..af4d9e5f6 100644
--- a/src/or/scheduler.c
+++ b/src/or/scheduler.c
@@ -148,7 +148,7 @@
  * outside the scheduling system)
  *****************************************************************************/
 
-STATIC scheduler_t *the_scheduler;
+STATIC const scheduler_t *the_scheduler;
 
 /*
  * We keep a list of channels that are pending - i.e, have cells to write
@@ -317,7 +317,7 @@ select_scheduler(void)
 static void
 set_scheduler(void)
 {
-  scheduler_t *old_scheduler = the_scheduler;
+  const scheduler_t *old_scheduler = the_scheduler;
 
   /* From the options, select the scheduler type to set. */
   select_scheduler();
@@ -395,7 +395,6 @@ scheduler_free_all(void)
   if (the_scheduler && the_scheduler->free_all) {
     the_scheduler->free_all();
   }
-  tor_free(the_scheduler);
   the_scheduler = NULL;
 }
 
diff --git a/src/or/scheduler.h b/src/or/scheduler.h
index c69f763ff..7766b2142 100644
--- a/src/or/scheduler.h
+++ b/src/or/scheduler.h
@@ -147,7 +147,7 @@ void scheduler_ev_add(const struct timeval *next_run);
 #ifdef TOR_UNIT_TESTS
 extern smartlist_t *channels_pending;
 extern struct event *run_sched_ev;
-extern scheduler_t *the_scheduler;
+extern const scheduler_t *the_scheduler;
 void scheduler_touch_channel(channel_t *chan);
 #endif /* TOR_UNIT_TESTS */
 
diff --git a/src/or/scheduler_kist.c b/src/or/scheduler_kist.c
index f35d0fae5..262d61816 100644
--- a/src/or/scheduler_kist.c
+++ b/src/or/scheduler_kist.c
@@ -103,8 +103,6 @@ static monotime_t scheduler_last_run;
 static double sock_buf_size_factor = 1.0;
 /* How often the scheduler runs. */
 STATIC int32_t sched_run_interval = 10;
-/* Stores the kist scheduler function pointers. */
-static scheduler_t *kist_scheduler = NULL;
 
 /*****************************************************************************
  * Internally called function implementations
@@ -637,23 +635,23 @@ kist_scheduler_run(void)
  * Externally called function implementations not called through scheduler_t
  *****************************************************************************/
 
+/* Stores the kist scheduler function pointers. */
+static scheduler_t kist_scheduler = {
+  .free_all = kist_free_all,
+  .on_channel_free = kist_on_channel_free,
+  .init = kist_scheduler_init,
+  .on_new_consensus = kist_scheduler_on_new_consensus,
+  .schedule = kist_scheduler_schedule,
+  .run = kist_scheduler_run,
+  .on_new_options = kist_scheduler_on_new_options,
+};
+
 /* Return the KIST scheduler object. If it didn't exists, return a newly
  * allocated one but init() is not called. */
 scheduler_t *
 get_kist_scheduler(void)
 {
-  if (!kist_scheduler) {
-    log_debug(LD_SCHED, "Allocating kist scheduler struct");
-    kist_scheduler = tor_malloc_zero(sizeof(*kist_scheduler));
-    kist_scheduler->free_all = kist_free_all;
-    kist_scheduler->on_channel_free = kist_on_channel_free;
-    kist_scheduler->init = kist_scheduler_init;
-    kist_scheduler->on_new_consensus = kist_scheduler_on_new_consensus;
-    kist_scheduler->schedule = kist_scheduler_schedule;
-    kist_scheduler->run = kist_scheduler_run;
-    kist_scheduler->on_new_options = kist_scheduler_on_new_options;
-  }
-  return kist_scheduler;
+  return &kist_scheduler;
 }
 
 /* Check the torrc for the configured KIST scheduler run interval.
diff --git a/src/or/scheduler_vanilla.c b/src/or/scheduler_vanilla.c
index 541ee8006..09653f445 100644
--- a/src/or/scheduler_vanilla.c
+++ b/src/or/scheduler_vanilla.c
@@ -17,9 +17,6 @@
 /* Maximum cells to flush in a single call to channel_flush_some_cells(); */
 #define MAX_FLUSH_CELLS 1000
 
-/* Stores the vanilla scheduler function pointers. */
-static scheduler_t *vanilla_scheduler = NULL;
-
 /*****************************************************************************
  * Externally called function implementations
  *****************************************************************************/
@@ -180,20 +177,20 @@ vanilla_scheduler_run(void)
             n_chans_before - n_chans_after, n_chans_before);
 }
 
+/* Stores the vanilla scheduler function pointers. */
+static scheduler_t vanilla_scheduler = {
+  .free_all = NULL,
+  .on_channel_free = NULL,
+  .init = NULL,
+  .on_new_consensus = NULL,
+  .schedule = vanilla_scheduler_schedule,
+  .run = vanilla_scheduler_run,
+  .on_new_options = NULL,
+};
+
 scheduler_t *
 get_vanilla_scheduler(void)
 {
-  if (!vanilla_scheduler) {
-    log_debug(LD_SCHED, "Initializing vanilla scheduler struct");
-    vanilla_scheduler = tor_malloc_zero(sizeof(*vanilla_scheduler));
-    vanilla_scheduler->free_all = NULL;
-    vanilla_scheduler->on_channel_free = NULL;
-    vanilla_scheduler->init = NULL;
-    vanilla_scheduler->on_new_consensus = NULL;
-    vanilla_scheduler->schedule = vanilla_scheduler_schedule;
-    vanilla_scheduler->run = vanilla_scheduler_run;
-    vanilla_scheduler->on_new_options = NULL;
-  }
-  return vanilla_scheduler;
+  return &vanilla_scheduler;
 }
 
diff --git a/src/test/test_scheduler.c b/src/test/test_scheduler.c
index 0cc6d88db..633a08922 100644
--- a/src/test/test_scheduler.c
+++ b/src/test/test_scheduler.c
@@ -408,7 +408,7 @@ perform_channel_state_tests(int KISTSchedRunInterval)
    * Disable scheduler_run so we can just check the state transitions
    * without having to make everything it might call work too.
    */
-  the_scheduler->run = scheduler_run_noop_mock;
+  ((scheduler_t *) the_scheduler)->run = scheduler_run_noop_mock;
 
   tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
 
@@ -617,7 +617,7 @@ test_scheduler_loop_vanilla(void *arg)
    * without having to make everything it might call work too.
    */
   run_func_ptr = the_scheduler->run;
-  the_scheduler->run = scheduler_run_noop_mock;
+  ((scheduler_t *) the_scheduler)->run = scheduler_run_noop_mock;
 
   tt_int_op(smartlist_len(channels_pending), OP_EQ, 0);
 





More information about the tor-commits mailing list