Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arch/sim/src/sim/posix/sim_hostmisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* Included Files
****************************************************************************/

#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
Expand Down
47 changes: 19 additions & 28 deletions arch/sim/src/sim/posix/sim_hosttime.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

/****************************************************************************
Expand Down Expand Up @@ -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);
Copy link
Contributor

@xiaoxiang781216 xiaoxiang781216 Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use setitimer on Linux too? https://linux.die.net/man/2/setitimer

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because linux has a better api : timer_create() + timer_settime()
in macos , we have to use that time_create exist but unreliable and uncompleted and time_settime() is not properly supported

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to go the same code path if we can, so don't need double the test effort.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiaoxiang781216 Ok i will do that
Any suggestion about linker
for to use sim in macos , we need to disable the gcov

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiaoxiang781216 now set timer is being shared bet linux and macOS

}

/****************************************************************************
Expand Down
Loading