diff options
author | Nick Mathewson <nickm@torproject.org> | 2010-06-25 15:31:46 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2010-06-25 15:31:46 -0400 |
commit | ad2d8ac073e576f538813a75183a3a8a477dfd5b (patch) | |
tree | 2b26c6794a1a25e0aae82ba33680ffe0fbddd2d6 /src/or | |
parent | deb9e4aff7347860d5cb41426e4c02a4efc16253 (diff) | |
download | tor-ad2d8ac073e576f538813a75183a3a8a477dfd5b.tar.gz tor-ad2d8ac073e576f538813a75183a3a8a477dfd5b.zip |
Use Libevent 2.0's periodic timers where available.
These timers behave better with non-monotonic clocks than our old
ones, and also try harder to make once-per-second events get called
one second apart, rather than one-plus-epsilon seconds apart.
This fixes bug 943 for everybody using Libevent 2.0 or later.
Diffstat (limited to 'src/or')
-rw-r--r-- | src/or/main.c | 41 |
1 files changed, 19 insertions, 22 deletions
diff --git a/src/or/main.c b/src/or/main.c index 0c3e6d5425..0dcc45464a 100644 --- a/src/or/main.c +++ b/src/or/main.c @@ -33,7 +33,7 @@ static void dumpstats(int severity); /* log stats */ static void conn_read_callback(int fd, short event, void *_conn); static void conn_write_callback(int fd, short event, void *_conn); static void signal_callback(int fd, short events, void *arg); -static void second_elapsed_callback(int fd, short event, void *args); +static void second_elapsed_callback(periodic_timer_t *timer, void *args); static int conn_close_if_marked(int i); static void connection_start_reading_from_linked_conn(connection_t *conn); static int connection_should_read_from_linked_conn(connection_t *conn); @@ -1205,39 +1205,30 @@ run_scheduled_events(time_t now) } } -/** Libevent timer: used to invoke second_elapsed_callback() once per - * second. */ -static struct event *timeout_event = NULL; +/** Timer: used to invoke second_elapsed_callback() once per second. */ +static periodic_timer_t *second_timer = NULL; /** Number of libevent errors in the last second: we die if we get too many. */ static int n_libevent_errors = 0; /** Libevent callback: invoked once every second. */ static void -second_elapsed_callback(int fd, short event, void *args) +second_elapsed_callback(periodic_timer_t *timer, void *arg) { /* XXXX This could be sensibly refactored into multiple callbacks, and we * could use Libevent's timers for this rather than checking the current * time against a bunch of timeouts every second. */ - static struct timeval one_second; static time_t current_second = 0; time_t now; size_t bytes_written; size_t bytes_read; int seconds_elapsed; or_options_t *options = get_options(); - (void)fd; - (void)event; - (void)args; - if (!timeout_event) { - timeout_event = tor_evtimer_new(tor_libevent_get_base(), - second_elapsed_callback, NULL); - one_second.tv_sec = 1; - one_second.tv_usec = 0; - } + (void)timer; + (void)arg; n_libevent_errors = 0; - /* log_fn(LOG_NOTICE, "Tick."); */ + /* log_notice(LD_GENERAL, "Tick."); */ now = time(NULL); update_approx_time(now); @@ -1302,10 +1293,6 @@ second_elapsed_callback(int fd, short event, void *args) run_scheduled_events(now); current_second = now; /* remember which second it is, for next time */ - - if (event_add(timeout_event, &one_second)) - log_err(LD_NET, - "Error from libevent when setting one-second timeout event"); } #ifndef MS_WINDOWS @@ -1492,7 +1479,17 @@ do_main_loop(void) } /* set up once-a-second callback. */ - second_elapsed_callback(0,0,NULL); + if (! second_timer) { + struct timeval one_second; + one_second.tv_sec = 1; + one_second.tv_usec = 0; + + second_timer = periodic_timer_new(tor_libevent_get_base(), + &one_second, + second_elapsed_callback, + NULL); + tor_assert(second_timer); + } for (;;) { if (nt_service_is_stopping()) @@ -2013,7 +2010,7 @@ tor_free_all(int postfork) smartlist_free(connection_array); smartlist_free(closeable_connection_lst); smartlist_free(active_linked_connection_lst); - tor_free(timeout_event); + periodic_timer_free(second_timer); if (!postfork) { release_lockfile(); } |