Skip to content

posix: options: add custom Zephyr POSIX subprofile #88547

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 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1dae514
posix: cond + mutex: log messages generate warnings for 32-bit timeouts
cfriedt May 27, 2025
7d5421e
sys: clock: additional sys_clock api calls
cfriedt May 18, 2025
17e0c2e
doc: release: 4.2.0: add sys_clock gettime settime nanosleep notes
cfriedt May 22, 2025
0bba722
logging: use sys_clock_gettime()
cfriedt May 27, 2025
e4ebd10
libc: common: time: use sys_clock api rather than posix
cfriedt May 18, 2025
cbfc8ab
libc: use the common libc time() implementation for most libcs
cfriedt May 18, 2025
558501a
libc: common: thrd: use sys_clock_nanosleep() instead of nanosleep()
cfriedt May 18, 2025
ae6101a
tests: libc: thrd: compare with thrd_success rather than ok or zero
cfriedt May 21, 2025
8a3f6b7
tests: libc: thrd: do not pass NULL for thrd_sleep() duration
cfriedt May 21, 2025
e9c8ce5
tests: libc: thrd: use timespec_from_timeout()
cfriedt May 18, 2025
5a55a2f
tests: libc: thrd: use pthread barriers for qemu_x86_64/atom
cfriedt May 26, 2025
67bac21
tests: lib: move time testsuite to c_lib
cfriedt May 18, 2025
6565eaf
posix: use sys_clock implementation
cfriedt May 18, 2025
1164ee5
net: remove dependency on posix for iso c time() function
cfriedt May 18, 2025
1094d07
samples: net: remove POSIX_TIMERS and XSI_SINGLE_PROCESS
cfriedt May 18, 2025
d1f678a
eventfd: bring config to top-level of posix dir, since it is not posix
cfriedt Apr 12, 2025
1690f8d
posix: separate option groups into c library ext and system interfaces
cfriedt Apr 12, 2025
e704b4a
posix: profiles: add custom Zephyr POSIX subprofile
cfriedt Apr 12, 2025
bb33d26
posix: profiles: make POSIX_AEP_CHOICE_ZEPHYR the default
cfriedt Apr 12, 2025
764da0b
posix: options: add keep-sorted-start and -stop
cfriedt Apr 12, 2025
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 @@ -2556,7 +2556,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 @@ -239,6 +239,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>
#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
134 changes: 134 additions & 0 deletions include/zephyr/sys/clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* 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.
*/
#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 offset @ref SYS_CLOCK_REALTIME with respect to @ref SYS_CLOCK_MONOTONIC
*
* The "wall clock" (i.e. @ref SYS_CLOCK_REALTIME) depends on a base time that is set by the
* system. The base time may be updated for a number of reasons, such as initialization of a
* hardware real-time-clock (RTC), network time protocol (NTP) synchronization, or manual
* adjustment by the application.
*
* This function retrieves the current time offset, as a `timespec` object, for
* @ref SYS_CLOCK_REALTIME, with respect to @ref SYS_CLOCK_MONOTONIC, and writes it to the
* provided memory location pointed-to by @a tp.
*
* @note This function may assert if @a tp is NULL.
*
* @param tp Pointer to memory where time will be written.
*/
__syscall void sys_clock_getrtoffset(struct timespec *tp);

/**
* @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.
*/
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
Loading