Skip to content

sys: clock: add sys_clock api and remove posix from iso c time #90096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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: 0 additions & 1 deletion MAINTAINERS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2552,7 +2552,6 @@ Utilities:
- tests/unit/list/
- tests/unit/intmath/
- tests/unit/pot/
- tests/lib/time/
- tests/lib/onoff/
- tests/lib/sys_util/
- tests/lib/sprintf/
Expand Down
3 changes: 3 additions & 0 deletions doc/releases/release-notes-4.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ New APIs and options

* :c:func:`util_eq`
* :c:func:`util_memeq`
* :c:func:`sys_clock_gettime`
* :c:func:`sys_clock_settime`
* :c:func:`sys_clock_nanosleep`

* LoRaWAN
* :c:func:`lorawan_request_link_check`
Expand Down
7 changes: 4 additions & 3 deletions include/zephyr/posix/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ struct itimerspec {
#include <errno.h>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Commit message refers to k_clock instead of sys_clock

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed!

#include <zephyr/posix/posix_types.h>
#include <zephyr/posix/signal.h>
#include <zephyr/sys/clock.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME 1
#define CLOCK_REALTIME SYS_CLOCK_REALTIME
#endif

#ifndef CLOCK_PROCESS_CPUTIME_ID
Expand All @@ -78,11 +79,11 @@ extern "C" {
#endif

#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC 4
#define CLOCK_MONOTONIC SYS_CLOCK_MONOTONIC
#endif

#ifndef TIMER_ABSTIME
#define TIMER_ABSTIME 4
#define TIMER_ABSTIME SYS_TIMER_ABSTIME
#endif

int clock_gettime(clockid_t clock_id, struct timespec *ts);
Expand Down
117 changes: 117 additions & 0 deletions include/zephyr/sys/clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2025 Tenstorrent AI ULC
*
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file
* @brief System clock APIs
*
* APIs for getting, setting, and sleeping with respect to system clocks.
*/

#ifndef ZEPHYR_INCLUDE_SYSCLOCK_H_
#define ZEPHYR_INCLUDE_SYSCLOCK_H_

#include <time.h>

#include <zephyr/toolchain.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @addtogroup clock_apis
* @{
*/

/**
* @brief The real-time clock (i.e. "wall clock")
*
* This clock is used to measure time since the epoch (1970-01-01 00:00:00 UTC).
*
* It is not a steady clock; i.e. it may be adjusted for a number of reasons from initialization
* of a hardware real-time-clock, to network-time synchronization, to manual adjustment from the
* application.
*/
#define SYS_CLOCK_REALTIME 1

/**
* @brief The monotonic clock
*
* This steady clock is used to measure time since the system booted. Time from this clock is
* always monotonically increasing, modulo the upper bound of `{.tv_sec = INT64_MAX, .tv_nsec =
* 1000000000}`.
*/
#define SYS_CLOCK_MONOTONIC 4

/**
* @brief The flag used for specifying absolute timeouts
*
* This flag may be passed to @ref sys_clock_nanosleep to indicate the requested timeout is an
* absolute time with respect to the specified clock.
*/
#define SYS_TIMER_ABSTIME 4

/**
* @brief Get the current time from the specified clock
*
* @param clock_id The clock from which to query time.
* @param tp Pointer to memory where time will be written.
* @retval 0 on success.
* @retval -EINVAL when an invalid @a clock_id is specified.
*/
__syscall int sys_clock_gettime(int clock_id, struct timespec *tp);

/**
* @brief Set the current time for the specified clock
*
* @param clock_id The clock for which the time should be set.
* @param tp Pointer to memory specifying the desired time.
* @retval 0 on success.
* @retval -EINVAL when an invalid @a clock_id is specified or when @a tp contains nanoseconds
* outside of the range `[0, 999999999]`.
*/
__syscall int sys_clock_settime(int clock_id, const struct timespec *tp);

/**
* @brief Sleep for the specified amount of time with respect to the specified clock.
*
* This function will cause the calling thread to sleep either
* - until the absolute time specified by @a rqtp (if @a flags includes @ref SYS_TIMER_ABSTIME), or
* - until the relative time specified by @a rqtp (if @a flags does not include
* @ref SYS_TIMER_ABSTIME).
*
* The accepted values for @a clock_id include
* - @ref SYS_CLOCK_REALTIME
* - @ref SYS_CLOCK_MONOTONIC
*
* If @a rmtp is not NULL, and the thread is awoken prior to the time specified by @a rqtp, then
* any remaining time will be written to @a rmtp. If the thread has slept for at least the time
* specified by @a rqtp, then @a rmtp will be set to zero.
*
* @param clock_id The clock to by which to sleep.
* @param flags Flags to modify the behavior of the sleep operation.
* @param rqtp Pointer to the requested time to sleep.
* @param rmtp Pointer to memory into which to copy the remaining time, if any.
*
* @retval 0 on success.
* @retval -EINVAL when an invalid @a clock_id, when @a rqtp contains nanoseconds outside of the
* range `[0, 999999999]`, or when @a rqtp contains a negative value.
*/
__syscall int sys_clock_nanosleep(int clock_id, int flags, const struct timespec *rqtp,
struct timespec *rmtp);

/**
* @}
*/

#include <zephyr/syscalls/clock.h>

#ifdef __cplusplus
}
#endif

#endif
5 changes: 4 additions & 1 deletion lib/libc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ config MINIMAL_LIBC
imply COMMON_LIBC_MALLOC
imply COMMON_LIBC_CALLOC
imply COMMON_LIBC_REALLOCARRAY
imply COMMON_LIBC_TIME
help
Build with minimal C library.

Expand All @@ -96,6 +97,7 @@ config PICOLIBC
select TC_PROVIDES_POSIX_C_LANG_SUPPORT_R
imply COMMON_LIBC_MALLOC
imply COMMON_LIBC_ABORT
imply COMMON_LIBC_TIME
depends on PICOLIBC_SUPPORTED
help
Build with picolibc library. The picolibc library is built as
Expand All @@ -116,6 +118,7 @@ config NEWLIB_LIBC
imply POSIX_FILE_SYSTEM_ALIAS_FSTAT
imply POSIX_MULTI_PROCESS_ALIAS_GETPID
imply POSIX_SIGNALS_ALIAS_KILL
imply COMMON_LIBC_TIME
help
Build with newlib library. The newlib library is expected to be
part of the SDK in this case.
Expand All @@ -137,7 +140,7 @@ config IAR_LIBC
depends on IAR_LIBC_SUPPORTED
depends on "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "iar"
select COMMON_LIBC_STRNLEN
select COMMON_LIBC_TIME if POSIX_TIMERS
select COMMON_LIBC_TIME
help
Use the full IAR Compiler runtime libraries.
A reduced Zephyr minimal libc will be used for library functionality
Expand Down
7 changes: 6 additions & 1 deletion lib/libc/common/source/thrd/thrd.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <zephyr/kernel.h>
#include <zephyr/posix/pthread.h>
#include <zephyr/posix/sched.h>
#include <zephyr/sys/clock.h>

struct thrd_trampoline_arg {
thrd_start_t func;
Expand Down Expand Up @@ -44,7 +45,11 @@ thrd_t thrd_current(void)

int thrd_sleep(const struct timespec *duration, struct timespec *remaining)
{
return nanosleep(duration, remaining);
if (sys_clock_nanosleep(SYS_CLOCK_REALTIME, 0, duration, remaining) != 0) {
return thrd_error;
}

return thrd_success;
}

void thrd_yield(void)
Expand Down
9 changes: 5 additions & 4 deletions lib/libc/common/source/time/time.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/*
* Copyright (c) 2021 Golioth, Inc.
* Copyright (c) 2025 Tenstorrent AI ULC
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <errno.h>
#include <time.h>

/* clock_gettime() prototype */
#include <zephyr/posix/time.h>
#include <zephyr/sys/clock.h>

time_t time(time_t *tloc)
{
struct timespec ts;
int ret;

ret = clock_gettime(CLOCK_REALTIME, &ts);
ret = sys_clock_gettime(SYS_CLOCK_REALTIME, &ts);
if (ret < 0) {
/* errno is already set by clock_gettime */
errno = -ret;
return (time_t) -1;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/os/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# SPDX-License-Identifier: Apache-2.0

zephyr_syscall_header(
${ZEPHYR_BASE}/include/zephyr/sys/clock.h
${ZEPHYR_BASE}/include/zephyr/sys/mutex.h
)

zephyr_sources(
cbprintf_packaged.c
clock.c
printk.c
sem.c
thread_entry.c
Expand Down
Loading