From 5f6e8b2bb6d707615157aa786652ede3136453db Mon Sep 17 00:00:00 2001 From: aditya Date: Tue, 3 Feb 2026 12:36:56 +0530 Subject: [PATCH 1/3] sim: posix: use setitimer on Darwin, POSIX timers on Linux --- arch/sim/src/sim/posix/sim_hostmisc.c | 1 + arch/sim/src/sim/posix/sim_hosttime.c | 63 ++++++++++++++++++++------- 2 files changed, 48 insertions(+), 16 deletions(-) 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..544d1a0c67b76 100644 --- a/arch/sim/src/sim/posix/sim_hosttime.c +++ b/arch/sim/src/sim/posix/sim_hosttime.c @@ -21,6 +21,7 @@ /**************************************************************************** * Included Files ****************************************************************************/ +#define _POSIX_C_SOURCE 200809L #include #include @@ -32,12 +33,23 @@ #include "sim_internal.h" +/**************************************************************************** + * Platform detection + ****************************************************************************/ + +#if defined(__APPLE__) && defined(__MACH__) +# define NUTTX_DARWIN +#endif + /**************************************************************************** * Private Data ****************************************************************************/ static uint64_t g_start; -static timer_t g_timer; + +#ifndef NUTTX_DARWIN +static timer_t g_timer; +#endif /**************************************************************************** * Public Functions @@ -50,18 +62,21 @@ 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; + +#ifdef NUTTX_DARWIN + /* macOS: setitimer requires no explicit timer creation */ + return 0; +#else + struct sigevent sigev; sigev.sigev_notify = SIGEV_SIGNAL; sigev.sigev_signo = SIGALRM; + sigev.sigev_value.sival_ptr = NULL; return timer_create(CLOCK_MONOTONIC, &sigev, &g_timer); +#endif } /**************************************************************************** @@ -94,9 +109,8 @@ void host_sleep(uint64_t nsec) void host_sleepuntil(uint64_t nsec) { - uint64_t now; + uint64_t now = host_gettime(false); - now = host_gettime(false); if (nsec > now + 1000) { usleep((nsec - now) / 1000); @@ -119,19 +133,36 @@ void host_sleepuntil(uint64_t nsec) int host_settimer(uint64_t nsec) { - struct itimerspec tspec = - { - 0 - }; +#ifdef NUTTX_DARWIN + /* macOS implementation using setitimer() */ + + struct itimerval it; + uint64_t usec = nsec / 1000; + + it.it_value.tv_sec = usec / 1000000; + it.it_value.tv_usec = usec % 1000000; + + /* One-shot timer */ + it.it_interval.tv_sec = 0; + it.it_interval.tv_usec = 0; + + return setitimer(ITIMER_REAL, &it, NULL); + +#else + + struct itimerspec tspec; + uint64_t abs; - /* Convert to microseconds and set minimum timer to 1 microsecond. */ + abs = nsec + g_start; - nsec += g_start; + tspec.it_value.tv_sec = abs / 1000000000; + tspec.it_value.tv_nsec = abs % 1000000000; - tspec.it_value.tv_sec = nsec / 1000000000; - tspec.it_value.tv_nsec = nsec % 1000000000; + tspec.it_interval.tv_sec = 0; + tspec.it_interval.tv_nsec = 0; return timer_settime(g_timer, TIMER_ABSTIME, &tspec, NULL); +#endif } /**************************************************************************** From d82561bc0b9f7d7b01f0a188c261e2e923e086bb Mon Sep 17 00:00:00 2001 From: Aditya Yadav <166515021+aditya0yadav@users.noreply.github.com> Date: Wed, 4 Feb 2026 11:57:43 +0000 Subject: [PATCH 2/3] now setimer is shared between linux and macos and remove the use of #define _POSIX_C_SOURCE 200809L --- arch/sim/src/sim/posix/sim_hosttime.c | 70 ++++++--------------------- 1 file changed, 15 insertions(+), 55 deletions(-) diff --git a/arch/sim/src/sim/posix/sim_hosttime.c b/arch/sim/src/sim/posix/sim_hosttime.c index 544d1a0c67b76..1449620387a9b 100644 --- a/arch/sim/src/sim/posix/sim_hosttime.c +++ b/arch/sim/src/sim/posix/sim_hosttime.c @@ -1,5 +1,7 @@ /**************************************************************************** - * arch/sim/src/sim/posix/sim_hosttime.c + * arch/sim/src/sim/win/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 @@ -21,7 +23,6 @@ /**************************************************************************** * Included Files ****************************************************************************/ -#define _POSIX_C_SOURCE 200809L #include #include @@ -33,23 +34,13 @@ #include "sim_internal.h" -/**************************************************************************** - * Platform detection - ****************************************************************************/ - -#if defined(__APPLE__) && defined(__MACH__) -# define NUTTX_DARWIN -#endif - /**************************************************************************** * Private Data ****************************************************************************/ -static uint64_t g_start; +/* Start time of the simulation in nanoseconds (monotonic clock) */ -#ifndef NUTTX_DARWIN -static timer_t g_timer; -#endif +static uint64_t g_start; /**************************************************************************** * Public Functions @@ -66,17 +57,7 @@ int host_inittimer(void) clock_gettime(CLOCK_MONOTONIC, &tp); g_start = 1000000000ull * tp.tv_sec + tp.tv_nsec; -#ifdef NUTTX_DARWIN - /* macOS: setitimer requires no explicit timer creation */ return 0; -#else - struct sigevent sigev; - sigev.sigev_notify = SIGEV_SIGNAL; - sigev.sigev_signo = SIGALRM; - sigev.sigev_value.sival_ptr = NULL; - - return timer_create(CLOCK_MONOTONIC, &sigev, &g_timer); -#endif } /**************************************************************************** @@ -109,8 +90,9 @@ void host_sleep(uint64_t nsec) void host_sleepuntil(uint64_t nsec) { - uint64_t now = host_gettime(false); + uint64_t now; + now = host_gettime(false); if (nsec > now + 1000) { usleep((nsec - now) / 1000); @@ -119,50 +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) { -#ifdef NUTTX_DARWIN - /* macOS implementation using setitimer() */ - struct itimerval it; - uint64_t usec = nsec / 1000; + uint64_t now; + uint64_t usec; + + now = host_gettime(false); + + usec = (nsec <= now) ? 1 : (nsec - now) / 1000; + usec = (usec == 0) ? 1 : usec; it.it_value.tv_sec = usec / 1000000; it.it_value.tv_usec = usec % 1000000; /* One-shot timer */ + it.it_interval.tv_sec = 0; it.it_interval.tv_usec = 0; return setitimer(ITIMER_REAL, &it, NULL); - -#else - - struct itimerspec tspec; - uint64_t abs; - - abs = nsec + g_start; - - tspec.it_value.tv_sec = abs / 1000000000; - tspec.it_value.tv_nsec = abs % 1000000000; - - tspec.it_interval.tv_sec = 0; - tspec.it_interval.tv_nsec = 0; - - return timer_settime(g_timer, TIMER_ABSTIME, &tspec, NULL); -#endif } /**************************************************************************** From 6603580400456503f8b6d307c452a3aef0feb0c6 Mon Sep 17 00:00:00 2001 From: Aditya Yadav <166515021+aditya0yadav@users.noreply.github.com> Date: Wed, 4 Feb 2026 12:05:56 +0000 Subject: [PATCH 3/3] fix the word problem --- arch/sim/src/sim/posix/sim_hosttime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/sim/src/sim/posix/sim_hosttime.c b/arch/sim/src/sim/posix/sim_hosttime.c index 1449620387a9b..23ae98e80c206 100644 --- a/arch/sim/src/sim/posix/sim_hosttime.c +++ b/arch/sim/src/sim/posix/sim_hosttime.c @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/sim/src/sim/win/sim_hosttime.c + * arch/sim/src/sim/posix/sim_hosttime.c * * SPDX-License-Identifier: Apache-2.0 *