From 80f582ae1841d262edd7f55260f350d949294c0c Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 9 May 2018 12:37:47 -0400 Subject: Add functions to enable/disable periodic_event_t objects. --- src/common/compat_libevent.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/common/compat_libevent.c') diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c index fa00fb836b..cf3b7298c2 100644 --- a/src/common/compat_libevent.c +++ b/src/common/compat_libevent.c @@ -253,10 +253,39 @@ periodic_timer_new(struct event_base *base, } timer->cb = cb; timer->data = data; - event_add(timer->ev, (struct timeval *)tv); /*drop const for old libevent*/ + periodic_timer_launch(timer, tv); return timer; } +/** + * Launch the timer timer to run at tv from now, and every + * tv thereafter. + * + * If the timer is already enabled, this function does nothing. + */ +void +periodic_timer_launch(periodic_timer_t *timer, const struct timeval *tv) +{ + tor_assert(timer); + if (event_pending(timer->ev, EV_TIMEOUT, NULL)) + return; + event_add(timer->ev, (struct timeval *)tv); /*drop const for old libevent*/ +} + +/** + * Disable the provided timer, but do not free it. + * + * You can reenable the same timer later with periodic_timer_launch. + * + * If the timer is already disabled, this function does nothing. + */ +void +periodic_timer_disable(periodic_timer_t *timer) +{ + tor_assert(timer); + event_del(timer->ev); +} + /** Stop and free a periodic timer */ void periodic_timer_free_(periodic_timer_t *timer) -- cgit v1.2.3-54-g00ecf