Skip to content
Closed
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/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ config ARCH_RISCV
config ARCH_SIM
bool "Simulation"
select ARCH_HAVE_BACKTRACE
select ARCH_HAVE_CLOCKID_TRANSLATE if HOST_MACOS
select ARCH_HAVE_MULTICPU if !CONFIG_WINDOWS_NATIVE
select ARCH_HAVE_RTC_SUBSECONDS
select ARCH_HAVE_SERIAL_TERMIOS
Expand Down
3 changes: 3 additions & 0 deletions arch/sim/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ ifeq ($(CONFIG_WINDOWS_NATIVE),y)
VPATH += :sim/win
else
VPATH += :sim/posix
ifeq ($(CONFIG_ARCH_HAVE_CLOCKID_TRANSLATE),y)
CSRCS += sim_clockid.c
endif
endif
DEPPATH = $(patsubst %,--dep-path %,$(subst :, ,$(VPATH)))

Expand Down
4 changes: 4 additions & 0 deletions arch/sim/src/sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ list(
sim_hosttime.c
sim_hostuart.c)

if(CONFIG_ARCH_HAVE_CLOCKID_TRANSLATE)
list(APPEND SRCS posix/sim_clockid.c)
endif()

if(CONFIG_HOST_MACOS AND CONFIG_HAVE_CXXINITIALIZE)
# Keep classic __mod_init_func format so post-link lief patching works
target_link_options(nuttx PRIVATE -Wl,-ld_classic,-no_fixup_chains)
Expand Down
69 changes: 69 additions & 0 deletions arch/sim/src/sim/posix/sim_clockid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/****************************************************************************
* arch/sim/src/sim/posix/sim_clockid.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
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/arch.h>
#include <nuttx/config.h>
#include <time.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/* Rust std uses CLOCK_UPTIME_RAW for Instant on Apple targets. That Darwin
* clock ID is not part of the NuttX clockid_t namespace, so translate it at
* the sim/posix boundary instead of teaching the common clock_gettime()
* implementation about host ABI values.
*
* Do not translate Darwin CLOCK_MONOTONIC_RAW (4) or
* CLOCK_MONOTONIC_RAW_APPROX (5): those values overlap with NuttX
* CLOCK_BOOTTIME and dynamic clock IDs.
*/

#define DARWIN_CLOCK_MONOTONIC 6
#define DARWIN_CLOCK_UPTIME_RAW 8
#define DARWIN_CLOCK_UPTIME_RAW_APPROX 9

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: up_translate_clockid
****************************************************************************/

clockid_t up_translate_clockid(clockid_t clockid)
{
switch (clockid)
{
case DARWIN_CLOCK_MONOTONIC:
case DARWIN_CLOCK_UPTIME_RAW:
case DARWIN_CLOCK_UPTIME_RAW_APPROX:
return CLOCK_MONOTONIC;

default:
return clockid;
}
}
13 changes: 13 additions & 0 deletions include/nuttx/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -1999,6 +1999,19 @@ int up_timer_gettime(FAR struct timespec *ts);
int up_timer_gettick(FAR clock_t *ticks);
void up_timer_getmask(FAR clock_t *mask);

/****************************************************************************
* Name: up_translate_clockid
*
* Description:
* Translate architecture-specific clock IDs before the common clock layer
* handles them.
*
****************************************************************************/

#ifdef CONFIG_ARCH_HAVE_CLOCKID_TRANSLATE
clockid_t up_translate_clockid(clockid_t clockid);
#endif

/****************************************************************************
* Name: up_alarm_cancel
*
Expand Down
4 changes: 4 additions & 0 deletions sched/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ config ARCH_HAVE_TIMEKEEPING
bool
default n

config ARCH_HAVE_CLOCKID_TRANSLATE
bool
default n

config CLOCK_TIMEKEEPING
bool "Support timekeeping algorithms"
default n
Expand Down
4 changes: 4 additions & 0 deletions sched/clock/clock_gettime.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ int clock_gettime(clockid_t clock_id, FAR struct timespec *tp)
{
int ret;

#ifdef CONFIG_ARCH_HAVE_CLOCKID_TRANSLATE
clock_id = up_translate_clockid(clock_id);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but, why rust tokio pass the macOS(not NuttX) clock id?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see. I'll look into the implementation flow of tokio::time, std::time, and clock_gettime().

#endif

ret = nxclock_gettime(clock_id, tp);
if (ret < 0)
{
Expand Down
Loading