aboutsummaryrefslogtreecommitdiff
path: root/src/or/periodic.h
diff options
context:
space:
mode:
authorKevin Butler <haqkrs@gmail.com>2015-11-02 09:48:18 -0500
committerNick Mathewson <nickm@torproject.org>2015-11-13 16:24:44 -0500
commitfbeff307f7780b26c9645ec7dbd685807add6581 (patch)
treedaaf4b9c1cfc2d3a98af4dac873c514ba100acc3 /src/or/periodic.h
parentfaba114a3484a7b85649a6892b2296cf9ac8a41e (diff)
downloadtor-fbeff307f7780b26c9645ec7dbd685807add6581.tar.gz
tor-fbeff307f7780b26c9645ec7dbd685807add6581.zip
Infrastructure for replacing global periodic events in main.c
(This is from Kevin's bug3199 patch series; nick extracted it into a new file and changed the interface a little, then did some API tweaks on it.)
Diffstat (limited to 'src/or/periodic.h')
-rw-r--r--src/or/periodic.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/or/periodic.h b/src/or/periodic.h
new file mode 100644
index 0000000000..7d5b1b47bf
--- /dev/null
+++ b/src/or/periodic.h
@@ -0,0 +1,54 @@
+/* Copyright (c) 2015, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_PERIODIC_H
+#define TOR_PERIODIC_H
+
+/** Callback function for a periodic event to take action.
+* The return value influences the next time the function will get called.
+* Return -1 to not update <b>last_action_time</b> and be polled again in
+* the next second. If a positive value is returned it will update the
+* interval time. If the returned value is larger than <b>now</b> then it
+* is assumed to be a future time to poll again. */
+typedef int (*periodic_event_helper_t)(time_t now,
+ const or_options_t *options);
+
+
+/** A single item for the periodic-events-function table. */
+typedef struct periodic_event_item_t {
+ periodic_event_helper_t fn; /**< The function to run the event */
+ int interval; /**< The interval for running the function (In seconds). */
+ time_t last_action_time; /**< The last time the function did something */
+ periodic_timer_t *timer; /**< Timer object for this event */
+ const char *name; /**< Name of the function -- for debug */
+} periodic_event_item_t;
+
+/** events will get their interval from first execution */
+#define PERIODIC_EVENT(fn) { fn##_callback, 0, 0, NULL, #fn }
+
+#if 0
+/** Refactor test, check the last_action_time was now or (now - delta - 1)
+* It returns an incremented <b>now</b> value and accounts for the current
+* implementation's off by one error in it's comparisons. */
+#define INCREMENT_DELTA_AND_TEST(id, now, delta) \
+ (now+delta); \
+ STMT_BEGIN \
+ periodic_event_item_t *ev = &periodic_events[id]; \
+ if (ev->last_action_time != now - delta - 1 && \
+ ev->last_action_time != now) { \
+ log_err(LD_BUG, "[Refactor Bug] Missed an interval " \
+ "for %s, Got %lu, wanted %lu or %lu.", ev->name, \
+ ev->last_action_time, now, now-delta); \
+ tor_assert(0); \
+ } \
+ STMT_END
+#endif
+
+void periodic_event_assert_in_range(periodic_event_item_t *event,
+ time_t start, time_t end);
+void periodic_event_launch(periodic_event_item_t *event);
+void periodic_event_destroy(periodic_event_item_t *event);
+void periodic_event_reschedule(periodic_event_item_t *event);
+
+#endif
+