[tor-commits] [tor/master] Add a function to schedule a periodic event once, then disable it
nickm at torproject.org
nickm at torproject.org
Mon Nov 26 21:36:44 UTC 2018
commit 6d84972eb8e27d5e9f1adea36fcc9a9879d718ad
Author: Nick Mathewson <nickm at torproject.org>
Date: Mon Nov 5 16:24:10 2018 -0500
Add a function to schedule a periodic event once, then disable it
---
src/core/mainloop/mainloop.c | 6 +++++-
src/core/mainloop/periodic.c | 22 +++++++++++++++++-----
src/core/mainloop/periodic.h | 7 ++++++-
3 files changed, 28 insertions(+), 7 deletions(-)
diff --git a/src/core/mainloop/mainloop.c b/src/core/mainloop/mainloop.c
index 12820888f..42f6fb50c 100644
--- a/src/core/mainloop/mainloop.c
+++ b/src/core/mainloop/mainloop.c
@@ -1600,7 +1600,11 @@ rescan_periodic_events(const or_options_t *options)
periodic_event_enable(item);
} else {
log_debug(LD_GENERAL, "Disabling periodic event %s", item->name);
- periodic_event_disable(item);
+ if (item->flags & PERIODIC_EVENT_FLAG_FLUSH_ON_DISABLE) {
+ periodic_event_flush_and_disable(item);
+ } else {
+ periodic_event_disable(item);
+ }
}
}
}
diff --git a/src/core/mainloop/periodic.c b/src/core/mainloop/periodic.c
index c1785eb38..c290c3744 100644
--- a/src/core/mainloop/periodic.c
+++ b/src/core/mainloop/periodic.c
@@ -45,10 +45,6 @@ periodic_event_dispatch(mainloop_event_t *ev, void *data)
periodic_event_item_t *event = data;
tor_assert(ev == event->ev);
- if (BUG(!periodic_event_is_enabled(event))) {
- return;
- }
-
time_t now = time(NULL);
update_current_time(now);
const or_options_t *options = get_options();
@@ -57,7 +53,7 @@ periodic_event_dispatch(mainloop_event_t *ev, void *data)
int next_interval = 0;
if (!periodic_event_is_enabled(event)) {
- /* The event got disabled from inside its callback; no need to
+ /* The event got disabled from inside its callback, or before: no need to
* reschedule. */
return;
}
@@ -172,3 +168,19 @@ periodic_event_disable(periodic_event_item_t *event)
mainloop_event_cancel(event->ev);
event->enabled = 0;
}
+
+/**
+ * Disable an event, then schedule it to run once.
+ * Do nothing if the event was already disabled.
+ */
+void
+periodic_event_flush_and_disable(periodic_event_item_t *event)
+{
+ tor_assert(event);
+ if (!periodic_event_is_enabled(event))
+ return;
+
+ periodic_event_disable(event);
+
+ mainloop_event_activate(event->ev);
+}
diff --git a/src/core/mainloop/periodic.h b/src/core/mainloop/periodic.h
index 4c8c3c96c..7c71be7bc 100644
--- a/src/core/mainloop/periodic.h
+++ b/src/core/mainloop/periodic.h
@@ -39,6 +39,11 @@
* the net_is_disabled() check. */
#define PERIODIC_EVENT_FLAG_NEED_NET (1U << 0)
+/* Indicate that it the event is enabled, it event needs to be run once before
+ * it becomes disabled.
+ */
+#define PERIODIC_EVENT_FLAG_FLUSH_ON_DISABLE (1U << 1)
+
/** Callback function for a periodic event to take action. The return value
* influences the next time the function will get called. Return
* PERIODIC_EVENT_NO_UPDATE to not update <b>last_action_time</b> and be polled
@@ -83,6 +88,6 @@ void periodic_event_destroy(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_flush_and_disable(periodic_event_item_t *event);
#endif /* !defined(TOR_PERIODIC_H) */
-
More information about the tor-commits
mailing list