Skip to content

Commit

Permalink
Merge pull request #20049 from MrKevinWeiss/backport/2023.10/native_t…
Browse files Browse the repository at this point in the history
…imer_bug_work_around

cpu/native: fix bug in periph_timer [backport 2023.10]
  • Loading branch information
MrKevinWeiss authored Nov 15, 2023
2 parents 24b3992 + 273f9e0 commit 4bf2864
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
42 changes: 26 additions & 16 deletions cpu/native/periph/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
* @}
*/

#include <time.h>
#include <sys/time.h>
#include <err.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <sys/time.h>
#include <time.h>

#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"
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Check warning on line 124 in cpu/native/periph/timer.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
}

int timer_set(tim_t dev, int channel, unsigned int offset)
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions cpu/native/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 4bf2864

Please sign in to comment.