From 2d500897899487677468088e4932c97f63db2500 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 23 Oct 2023 13:11:23 +0200 Subject: [PATCH 1/2] cpu/native: fix bug in periph_timer Also use `CLOCK_MONOTONIC` for the timeouts, not just for `timer_read()`. This fixes mismatches between when a timeout occurs and what is expected in the context of the values returned by `timer_read()`. (cherry picked from commit cea7fcec2f16acf5c13912524405c4e5bd09fa96) --- cpu/native/periph/timer.c | 42 ++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/cpu/native/periph/timer.c b/cpu/native/periph/timer.c index 229ae6efaf28..e275dc5574e2 100644 --- a/cpu/native/periph/timer.c +++ b/cpu/native/periph/timer.c @@ -26,18 +26,19 @@ * @} */ -#include -#include +#include #include #include #include #include -#include +#include +#include #include "cpu.h" #include "cpu_conf.h" #include "native_internal.h" #include "periph/timer.h" +#include "time_units.h" #define ENABLE_DEBUG 0 #include "debug.h" @@ -49,7 +50,9 @@ static unsigned long time_null; static timer_cb_t _callback; static void *_cb_arg; -static struct itimerval itv; +static struct itimerspec its; + +static timer_t itimer_monotonic; /** * returns ticks for give timespec @@ -89,8 +92,15 @@ int timer_init(tim_t dev, uint32_t freq, timer_cb_t cb, void *arg) _callback = cb; _cb_arg = arg; + + if (timer_create(CLOCK_MONOTONIC, NULL, &itimer_monotonic) != 0) { + DEBUG_PUTS("Failed to create a monotonic itimer"); + return -1; + } + if (register_interrupt(SIGALRM, native_isr_timer) != 0) { - DEBUG("darn!\n\n"); + DEBUG_PUTS("Failed to register SIGALRM handler"); + return -1; } return 0; @@ -104,14 +114,14 @@ static void do_timer_set(unsigned int offset, bool periodic) offset = NATIVE_TIMER_MIN_RES; } - memset(&itv, 0, sizeof(itv)); - itv.it_value.tv_sec = (offset / 1000000); - itv.it_value.tv_usec = offset % 1000000; + memset(&its, 0, sizeof(its)); + its.it_value.tv_sec = offset / NATIVE_TIMER_SPEED; + its.it_value.tv_nsec = (offset % NATIVE_TIMER_SPEED) * (NS_PER_SEC / NATIVE_TIMER_SPEED); if (periodic) { - itv.it_interval = itv.it_value; + its.it_interval = its.it_value; } - DEBUG("timer_set(): setting %lu.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec); + DEBUG("timer_set(): setting %lu.%09lu\n", (unsigned long)its.it_value.tv_sec, its.it_value.tv_nsec); } int timer_set(tim_t dev, int channel, unsigned int offset) @@ -171,8 +181,8 @@ void timer_start(tim_t dev) DEBUG("%s\n", __func__); _native_syscall_enter(); - if (real_setitimer(ITIMER_REAL, &itv, NULL) == -1) { - err(EXIT_FAILURE, "timer_arm: setitimer"); + if (timer_settime(itimer_monotonic, 0, &its, NULL) == -1) { + err(EXIT_FAILURE, "timer_start: timer_settime"); } _native_syscall_leave(); } @@ -183,13 +193,13 @@ void timer_stop(tim_t dev) DEBUG("%s\n", __func__); _native_syscall_enter(); - struct itimerval zero = {0}; - if (real_setitimer(ITIMER_REAL, &zero, &itv) == -1) { - err(EXIT_FAILURE, "timer_arm: setitimer"); + struct itimerspec zero = {0}; + if (timer_settime(itimer_monotonic, 0, &zero, &its) == -1) { + err(EXIT_FAILURE, "timer_stop: timer_settime"); } _native_syscall_leave(); - DEBUG("time left: %lu.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec); + DEBUG("time left: %lu.%09lu\n", (unsigned long)its.it_value.tv_sec, its.it_value.tv_nsec); } unsigned int timer_read(tim_t dev) From 273f9e03a4d30139b5c83190f0b149244cfff174 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 26 Oct 2023 11:12:58 +0200 Subject: [PATCH 2/2] cpu/native: drop unused `real_setitimer` (cherry picked from commit 50b841e1546233f24c9d92772c2a2a127c6ecc7c) --- cpu/native/syscalls.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/cpu/native/syscalls.c b/cpu/native/syscalls.c index 6b4dcaaf8fe3..82de52e303f1 100644 --- a/cpu/native/syscalls.c +++ b/cpu/native/syscalls.c @@ -88,8 +88,6 @@ int (*real_pause)(void); int (*real_pipe)(int[2]); int (*real_select)(int nfds, ...); int (*real_poll)(struct pollfd *fds, ...); -int (*real_setitimer)(int which, const struct itimerval - *restrict value, struct itimerval *restrict ovalue); int (*real_setsid)(void); int (*real_setsockopt)(int socket, ...); int (*real_socket)(int domain, int type, int protocol); @@ -535,7 +533,6 @@ void _native_init_syscalls(void) *(void **)(&real_dup2) = dlsym(RTLD_NEXT, "dup2"); *(void **)(&real_select) = dlsym(RTLD_NEXT, "select"); *(void **)(&real_poll) = dlsym(RTLD_NEXT, "poll"); - *(void **)(&real_setitimer) = dlsym(RTLD_NEXT, "setitimer"); *(void **)(&real_setsid) = dlsym(RTLD_NEXT, "setsid"); *(void **)(&real_setsockopt) = dlsym(RTLD_NEXT, "setsockopt"); *(void **)(&real_socket) = dlsym(RTLD_NEXT, "socket");