From ab96b5f3f5247c7c7500a64536ceb345e0b8a65f Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 9 Nov 2009 18:30:33 +0000 Subject: [PATCH] Add an option to disable the timeval cache. svn:r1518 --- ChangeLog | 2 +- event-internal.h | 2 ++ event.c | 28 +++++++++++++++++++++------- include/event2/event.h | 6 +++++- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index b06128fe..c720db76 100644 --- a/ChangeLog +++ b/ChangeLog @@ -40,7 +40,7 @@ Changes in 2.0.3-alpha: o Add a new bufferevent_socket_connect_hostname() to encapsulate the resolve-then-connect operation. o Build kqueue.c correctly on GNU/kFreeBSD platforms. Patch pulled upstream from Debian. o Alternative queue-based timeout algorithm for programs that use a large number of timeouts with the same value. - + o New event_base_config option to disable the timeval cache entirely. Changes in 2.0.2-alpha: o Add a new flag to bufferevents to make all callbacks automatically deferred. diff --git a/event-internal.h b/event-internal.h index 662e23d6..4fef73c1 100644 --- a/event-internal.h +++ b/event-internal.h @@ -181,6 +181,8 @@ struct event_base { struct event_iocp_port *iocp; #endif + enum event_base_config_flag flags; + /* Notify main thread to wake up break, etc. */ int th_notify_fd[2]; struct event th_notify; diff --git a/event.c b/event.c index 2f1a8429..327546e2 100644 --- a/event.c +++ b/event.c @@ -180,6 +180,20 @@ gettime(struct event_base *base, struct timeval *tp) return (evutil_gettimeofday(tp, NULL)); } +static inline void +clear_time_cache(struct event_base *base) +{ + base->tv_cache.tv_sec = 0; +} + +static inline void +update_time_cache(struct event_base *base) +{ + base->tv_cache.tv_sec = 0; + if (!(base->flags & EVENT_BASE_FLAG_NO_CACHE_TIME)) + gettime(base, &base->tv_cache); +} + struct event_base * event_init(void) { @@ -281,6 +295,8 @@ event_base_new_with_config(struct event_config *cfg) event_deferred_cb_queue_init(&base->defer_queue); base->defer_queue.notify_fn = notify_base_cbq_callback; base->defer_queue.notify_arg = base; + if (cfg) + base->flags = cfg->flags; evmap_io_initmap(&base->io); evmap_signal_initmap(&base->sigmap); @@ -1054,8 +1070,7 @@ event_base_loop(struct event_base *base, int flags) * as we invoke user callbacks. */ EVBASE_ACQUIRE_LOCK(base, EVTHREAD_WRITE, th_base_lock); - /* clear time cache */ - base->tv_cache.tv_sec = 0; + clear_time_cache(base); if (base->sig.ev_signal_added) evsig_base = base; @@ -1099,14 +1114,14 @@ event_base_loop(struct event_base *base, int flags) /* update last old time */ gettime(base, &base->event_tv); - /* clear time cache */ - base->tv_cache.tv_sec = 0; + clear_time_cache(base); res = evsel->dispatch(base, tv_p); if (res == -1) return (-1); - gettime(base, &base->tv_cache); + + update_time_cache(base); timeout_process(base); @@ -1118,8 +1133,7 @@ event_base_loop(struct event_base *base, int flags) done = 1; } - /* clear time cache */ - base->tv_cache.tv_sec = 0; + clear_time_cache(base); EVBASE_RELEASE_LOCK(base, EVTHREAD_WRITE, th_base_lock); diff --git a/include/event2/event.h b/include/event2/event.h index 4cc9bff8..7fe6dc73 100644 --- a/include/event2/event.h +++ b/include/event2/event.h @@ -167,7 +167,11 @@ enum event_base_config_flag { an event_base. */ EVENT_BASE_FLAG_IGNORE_ENV = 0x02, /** Windows only: enable the IOCP dispatcher at startup */ - EVENT_BASE_FLAG_STARTUP_IOCP = 0x04 + EVENT_BASE_FLAG_STARTUP_IOCP = 0x04, + /** Instead of checking the current time every time the event loop is + ready to run timeout callbacks, check after each timeout callback. + */ + EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08 }; /** -- 2.40.0