Switch to gettimeofday() from clock_gettime()
Switch to gettimeofday() from clock_gettime() for portability with older versions of Mac OS X
This commit is contained in:
parent
05ee17a9fc
commit
2222f2052a
3 changed files with 25 additions and 25 deletions
|
@ -10,7 +10,7 @@ enum patty_timer_flags {
|
|||
|
||||
typedef struct _patty_timer {
|
||||
time_t ms;
|
||||
struct timespec t;
|
||||
struct timeval t;
|
||||
uint32_t flags;
|
||||
} patty_timer;
|
||||
|
||||
|
@ -26,11 +26,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_sub(struct timeval *a,
|
||||
struct timeval *b,
|
||||
struct timeval *c);
|
||||
|
||||
void patty_timer_tick(patty_timer *timer,
|
||||
struct timespec *elapsed);
|
||||
struct timeval *elapsed);
|
||||
|
||||
#endif /* _PATTY_TIMER_H */
|
||||
|
|
16
src/server.c
16
src/server.c
|
@ -27,7 +27,7 @@ struct _patty_ax25_server {
|
|||
int fd, /* fd of UNIX domain socket */
|
||||
fd_max;
|
||||
|
||||
struct timespec elapsed;
|
||||
struct timeval elapsed;
|
||||
|
||||
fd_set fds_watch, /* fds to monitor with select() */
|
||||
fds_r; /* fds select()ed for reading */
|
||||
|
@ -2371,13 +2371,13 @@ int patty_ax25_server_event_handle(patty_ax25_server *server) {
|
|||
|
||||
struct timeval timeout = { 1, 0 };
|
||||
|
||||
struct timespec before,
|
||||
after;
|
||||
struct timeval before,
|
||||
after;
|
||||
|
||||
memcpy(&server->fds_r, &server->fds_watch, sizeof(server->fds_r));
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &before) < 0) {
|
||||
goto error_clock_gettime;
|
||||
if (gettimeofday(&before, NULL) < 0) {
|
||||
goto error_gettimeofday;
|
||||
}
|
||||
|
||||
if ((nready = select( server->fd_max,
|
||||
|
@ -2388,8 +2388,8 @@ int patty_ax25_server_event_handle(patty_ax25_server *server) {
|
|||
goto error_io;
|
||||
}
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &after) < 0) {
|
||||
goto error_clock_gettime;
|
||||
if (gettimeofday(&after, NULL) < 0) {
|
||||
goto error_gettimeofday;
|
||||
}
|
||||
|
||||
patty_timer_sub(&after, &before, &server->elapsed);
|
||||
|
@ -2414,7 +2414,7 @@ int patty_ax25_server_event_handle(patty_ax25_server *server) {
|
|||
|
||||
return 0;
|
||||
|
||||
error_clock_gettime:
|
||||
error_gettimeofday:
|
||||
error_io:
|
||||
return -1;
|
||||
}
|
||||
|
|
24
src/timer.c
24
src/timer.c
|
@ -14,19 +14,19 @@ int patty_timer_expired(patty_timer *timer) {
|
|||
}
|
||||
|
||||
return timer->t.tv_sec < 0
|
||||
|| (timer->t.tv_sec == 0 && timer->t.tv_nsec <= 0)? 1: 0;
|
||||
|| (timer->t.tv_sec == 0 && timer->t.tv_usec <= 0)? 1: 0;
|
||||
}
|
||||
|
||||
void patty_timer_clear(patty_timer *timer) {
|
||||
timer->t.tv_sec = 0;
|
||||
timer->t.tv_nsec = 0;
|
||||
timer->t.tv_usec = 0;
|
||||
|
||||
timer->flags &= ~PATTY_TIMER_RUNNING;
|
||||
}
|
||||
|
||||
void patty_timer_start(patty_timer *timer) {
|
||||
timer->t.tv_sec = timer->ms / 1000;
|
||||
timer->t.tv_nsec = (timer->ms % 1000) * 1000000;
|
||||
timer->t.tv_usec = (timer->ms % 1000) * 1000;
|
||||
|
||||
timer->flags |= PATTY_TIMER_RUNNING;
|
||||
}
|
||||
|
@ -35,17 +35,17 @@ 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_sub(struct timeval *a,
|
||||
struct timeval *b,
|
||||
struct timeval *c) {
|
||||
c->tv_usec = a->tv_usec - b->tv_usec;
|
||||
c->tv_sec = a->tv_sec - b->tv_sec - (c->tv_usec / 1000000);
|
||||
c->tv_usec %= 1000000;
|
||||
}
|
||||
|
||||
void patty_timer_tick(patty_timer *timer,
|
||||
struct timespec *elapsed) {
|
||||
struct timespec res;
|
||||
struct timeval *elapsed) {
|
||||
struct timeval res;
|
||||
|
||||
if (!(timer->flags & PATTY_TIMER_RUNNING)) {
|
||||
return;
|
||||
|
@ -54,5 +54,5 @@ void patty_timer_tick(patty_timer *timer,
|
|||
patty_timer_sub(&timer->t, elapsed, &res);
|
||||
|
||||
timer->t.tv_sec = res.tv_sec;
|
||||
timer->t.tv_nsec = res.tv_nsec;
|
||||
timer->t.tv_usec = res.tv_usec;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue