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:
XANTRONIX Development 2024-10-13 22:48:49 -04:00
parent 05ee17a9fc
commit 2222f2052a
3 changed files with 25 additions and 25 deletions

View file

@ -10,7 +10,7 @@ enum patty_timer_flags {
typedef struct _patty_timer { typedef struct _patty_timer {
time_t ms; time_t ms;
struct timespec t; struct timeval t;
uint32_t flags; uint32_t flags;
} patty_timer; } patty_timer;
@ -26,11 +26,11 @@ void patty_timer_start(patty_timer *timer);
void patty_timer_stop(patty_timer *timer); void patty_timer_stop(patty_timer *timer);
void patty_timer_sub(struct timespec *a, void patty_timer_sub(struct timeval *a,
struct timespec *b, struct timeval *b,
struct timespec *c); struct timeval *c);
void patty_timer_tick(patty_timer *timer, void patty_timer_tick(patty_timer *timer,
struct timespec *elapsed); struct timeval *elapsed);
#endif /* _PATTY_TIMER_H */ #endif /* _PATTY_TIMER_H */

View file

@ -27,7 +27,7 @@ struct _patty_ax25_server {
int fd, /* fd of UNIX domain socket */ int fd, /* fd of UNIX domain socket */
fd_max; fd_max;
struct timespec elapsed; struct timeval elapsed;
fd_set fds_watch, /* fds to monitor with select() */ fd_set fds_watch, /* fds to monitor with select() */
fds_r; /* fds select()ed for reading */ 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 timeval timeout = { 1, 0 };
struct timespec before, struct timeval before,
after; after;
memcpy(&server->fds_r, &server->fds_watch, sizeof(server->fds_r)); memcpy(&server->fds_r, &server->fds_watch, sizeof(server->fds_r));
if (clock_gettime(CLOCK_MONOTONIC, &before) < 0) { if (gettimeofday(&before, NULL) < 0) {
goto error_clock_gettime; goto error_gettimeofday;
} }
if ((nready = select( server->fd_max, if ((nready = select( server->fd_max,
@ -2388,8 +2388,8 @@ int patty_ax25_server_event_handle(patty_ax25_server *server) {
goto error_io; goto error_io;
} }
if (clock_gettime(CLOCK_MONOTONIC, &after) < 0) { if (gettimeofday(&after, NULL) < 0) {
goto error_clock_gettime; goto error_gettimeofday;
} }
patty_timer_sub(&after, &before, &server->elapsed); patty_timer_sub(&after, &before, &server->elapsed);
@ -2414,7 +2414,7 @@ int patty_ax25_server_event_handle(patty_ax25_server *server) {
return 0; return 0;
error_clock_gettime: error_gettimeofday:
error_io: error_io:
return -1; return -1;
} }

View file

@ -14,19 +14,19 @@ int patty_timer_expired(patty_timer *timer) {
} }
return timer->t.tv_sec < 0 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) { void patty_timer_clear(patty_timer *timer) {
timer->t.tv_sec = 0; timer->t.tv_sec = 0;
timer->t.tv_nsec = 0; timer->t.tv_usec = 0;
timer->flags &= ~PATTY_TIMER_RUNNING; timer->flags &= ~PATTY_TIMER_RUNNING;
} }
void patty_timer_start(patty_timer *timer) { void patty_timer_start(patty_timer *timer) {
timer->t.tv_sec = timer->ms / 1000; 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; timer->flags |= PATTY_TIMER_RUNNING;
} }
@ -35,17 +35,17 @@ void patty_timer_stop(patty_timer *timer) {
timer->flags &= ~PATTY_TIMER_RUNNING; timer->flags &= ~PATTY_TIMER_RUNNING;
} }
void patty_timer_sub(struct timespec *a, void patty_timer_sub(struct timeval *a,
struct timespec *b, struct timeval *b,
struct timespec *c) { struct timeval *c) {
c->tv_nsec = a->tv_nsec - b->tv_nsec; c->tv_usec = a->tv_usec - b->tv_usec;
c->tv_sec = a->tv_sec - b->tv_sec - (c->tv_nsec / 1000000000); c->tv_sec = a->tv_sec - b->tv_sec - (c->tv_usec / 1000000);
c->tv_nsec %= 1000000000; c->tv_usec %= 1000000;
} }
void patty_timer_tick(patty_timer *timer, void patty_timer_tick(patty_timer *timer,
struct timespec *elapsed) { struct timeval *elapsed) {
struct timespec res; struct timeval res;
if (!(timer->flags & PATTY_TIMER_RUNNING)) { if (!(timer->flags & PATTY_TIMER_RUNNING)) {
return; return;
@ -54,5 +54,5 @@ void patty_timer_tick(patty_timer *timer,
patty_timer_sub(&timer->t, elapsed, &res); patty_timer_sub(&timer->t, elapsed, &res);
timer->t.tv_sec = res.tv_sec; timer->t.tv_sec = res.tv_sec;
timer->t.tv_nsec = res.tv_nsec; timer->t.tv_usec = res.tv_usec;
} }