diff --git a/include/patty/timer.h b/include/patty/timer.h index 681a98c..ba5a8c9 100644 --- a/include/patty/timer.h +++ b/include/patty/timer.h @@ -9,7 +9,7 @@ enum patty_timer_flags { }; typedef struct _patty_timer { - struct timeval t; + struct timespec t; uint32_t flags; } patty_timer; @@ -24,7 +24,11 @@ void patty_timer_start(patty_timer *timer, void patty_timer_stop(patty_timer *timer); +void patty_timer_sub(struct timespec *a, + struct timespec *b, + struct timespec *c); + void patty_timer_tick(patty_timer *timer, - struct timeval *elapsed); + struct timespec *elapsed); #endif /* _PATTY_TIMER_H */ diff --git a/src/server.c b/src/server.c index 2443482..c188a52 100644 --- a/src/server.c +++ b/src/server.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -20,7 +21,7 @@ struct _patty_ax25_server { int fd, /* fd of UNIX domain socket */ fd_max; - struct timeval elapsed; + struct timespec elapsed; fd_set fds_watch, /* fds to monitor with select() */ fds_r; /* fds select()ed for reading */ @@ -2018,14 +2019,15 @@ int patty_ax25_server_run(patty_ax25_server *server) { while (1) { int nready; - struct timeval timeout = { 1, 0 }, - before, - after; + struct timeval timeout = { 1, 0 }; + + struct timespec before, + after; memcpy(&server->fds_r, &server->fds_watch, sizeof(server->fds_r)); - if (gettimeofday(&before, NULL) < 0) { - goto error_gettimeofday; + if (clock_gettime(CLOCK_MONOTONIC, &before) < 0) { + goto error_clock_gettime; } if ((nready = select( server->fd_max, @@ -2036,11 +2038,11 @@ int patty_ax25_server_run(patty_ax25_server *server) { goto error_io; } - if (gettimeofday(&after, NULL) < 0) { - goto error_gettimeofday; + if (clock_gettime(CLOCK_MONOTONIC, &after) < 0) { + goto error_clock_gettime; } - timersub(&after, &before, &server->elapsed); + patty_timer_sub(&after, &before, &server->elapsed); if (handle_socks(server) < 0) { goto error_io; @@ -2065,7 +2067,7 @@ int patty_ax25_server_run(patty_ax25_server *server) { return 0; -error_gettimeofday: +error_clock_gettime: error_io: close(server->fd); diff --git a/src/timer.c b/src/timer.c index c11758d..7d55634 100644 --- a/src/timer.c +++ b/src/timer.c @@ -5,12 +5,17 @@ int patty_timer_running(patty_timer *timer) { } int patty_timer_expired(patty_timer *timer) { - return (timer->flags & PATTY_TIMER_RUNNING) && timer->t.tv_sec <= 0? 1: 0; + if (!(timer->flags & PATTY_TIMER_RUNNING)) { + return 0; + } + + return timer->t.tv_sec < 0 + || (timer->t.tv_sec == 0 && timer->t.tv_nsec <= 0)? 1: 0; } void patty_timer_clear(patty_timer *timer) { timer->t.tv_sec = 0; - timer->t.tv_usec = 0; + timer->t.tv_nsec = 0; timer->flags &= ~PATTY_TIMER_RUNNING; } @@ -18,7 +23,7 @@ void patty_timer_clear(patty_timer *timer) { void patty_timer_start(patty_timer *timer, time_t ms) { timer->t.tv_sec = ms / 1000; - timer->t.tv_usec = (ms % 1000) * 1000; + timer->t.tv_nsec = (ms % 1000) * 1000000; timer->flags |= PATTY_TIMER_RUNNING; } @@ -27,16 +32,24 @@ void patty_timer_stop(patty_timer *timer) { timer->flags &= ~PATTY_TIMER_RUNNING; } +void patty_timer_sub(struct timespec *a, + struct timespec *b, + struct timespec *c) { + c->tv_nsec = a->tv_nsec - b->tv_nsec; + c->tv_sec = a->tv_sec - b->tv_sec - (c->tv_nsec / 1000000000); + c->tv_nsec %= 1000000000; +} + void patty_timer_tick(patty_timer *timer, - struct timeval *elapsed) { - struct timeval res; + struct timespec *elapsed) { + struct timespec res; if (!(timer->flags & PATTY_TIMER_RUNNING)) { return; } - timersub(&timer->t, elapsed, &res); + patty_timer_sub(&timer->t, elapsed, &res); timer->t.tv_sec = res.tv_sec; - timer->t.tv_usec = res.tv_usec; + timer->t.tv_nsec = res.tv_nsec; }