diff --git a/arch/sim/src/sim/posix/sim_hostmisc.c b/arch/sim/src/sim/posix/sim_hostmisc.c index 009005bff4484..a2338ebbd16cd 100644 --- a/arch/sim/src/sim/posix/sim_hostmisc.c +++ b/arch/sim/src/sim/posix/sim_hostmisc.c @@ -24,6 +24,7 @@ * Included Files ****************************************************************************/ +#include #include #include #include diff --git a/arch/sim/src/sim/posix/sim_hosttime.c b/arch/sim/src/sim/posix/sim_hosttime.c index d03419253a3b2..23ae98e80c206 100644 --- a/arch/sim/src/sim/posix/sim_hosttime.c +++ b/arch/sim/src/sim/posix/sim_hosttime.c @@ -1,6 +1,8 @@ /**************************************************************************** * arch/sim/src/sim/posix/sim_hosttime.c * + * SPDX-License-Identifier: Apache-2.0 + * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. The @@ -36,8 +38,9 @@ * Private Data ****************************************************************************/ +/* Start time of the simulation in nanoseconds (monotonic clock) */ + static uint64_t g_start; -static timer_t g_timer; /**************************************************************************** * Public Functions @@ -50,18 +53,11 @@ static timer_t g_timer; int host_inittimer(void) { struct timespec tp; - struct sigevent sigev = - { - 0 - }; clock_gettime(CLOCK_MONOTONIC, &tp); - g_start = 1000000000ull * tp.tv_sec + tp.tv_nsec; - sigev.sigev_notify = SIGEV_SIGNAL; - sigev.sigev_signo = SIGALRM; - return timer_create(CLOCK_MONOTONIC, &sigev, &g_timer); + return 0; } /**************************************************************************** @@ -105,33 +101,28 @@ void host_sleepuntil(uint64_t nsec) /**************************************************************************** * Name: host_settimer - * - * Description: - * Set up a timer to send periodic signals. - * - * Input Parameters: - * nsec - timer expire time - * - * Returned Value: - * On success, (0) zero value is returned, otherwise a negative value. - * ****************************************************************************/ int host_settimer(uint64_t nsec) { - struct itimerspec tspec = - { - 0 - }; + struct itimerval it; + uint64_t now; + uint64_t usec; + + now = host_gettime(false); + + usec = (nsec <= now) ? 1 : (nsec - now) / 1000; + usec = (usec == 0) ? 1 : usec; - /* Convert to microseconds and set minimum timer to 1 microsecond. */ + it.it_value.tv_sec = usec / 1000000; + it.it_value.tv_usec = usec % 1000000; - nsec += g_start; + /* One-shot timer */ - tspec.it_value.tv_sec = nsec / 1000000000; - tspec.it_value.tv_nsec = nsec % 1000000000; + it.it_interval.tv_sec = 0; + it.it_interval.tv_usec = 0; - return timer_settime(g_timer, TIMER_ABSTIME, &tspec, NULL); + return setitimer(ITIMER_REAL, &it, NULL); } /****************************************************************************