Skip to content

posix: c_lib_ext: complete the POSIX_C_LIB_EXT Option Group #90449

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

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
25e3ea4
sys: clock: additional sys_clock api calls
cfriedt May 18, 2025
232e470
doc: release: 4.2.0: add sys_clock gettime settime nanosleep notes
cfriedt May 22, 2025
d26f68e
libc: common: time: use sys_clock api rather than posix
cfriedt May 18, 2025
2e2f674
libc: use the common libc time() implementation for most libcs
cfriedt May 18, 2025
cd2ec96
libc: common: thrd: use sys_clock_nanosleep() instead of nanosleep()
cfriedt May 18, 2025
ffe47aa
tests: libc: thrd: compare with thrd_success rather than ok or zero
cfriedt May 21, 2025
e5f9083
tests: libc: thrd: do not pass NULL for thrd_sleep() duration
cfriedt May 21, 2025
645e772
tests: libc: thrd: use timespec_from_timeout()
cfriedt May 18, 2025
1e0c9e2
tests: lib: move time testsuite to c_lib
cfriedt May 18, 2025
7ea1422
posix: use kernel sys_clock implementation
cfriedt May 18, 2025
bc02dc3
net: remove dependency on posix for iso c time() function
cfriedt May 18, 2025
a4f6b31
samples: net: remove POSIX_TIMERS and XSI_SINGLE_PROCESS
cfriedt May 18, 2025
88b5e5d
eventfd: bring config to top-level of posix dir, since it is not posix
cfriedt Apr 12, 2025
4ee7957
posix: separate option groups into c library ext and system interfaces
cfriedt Apr 12, 2025
0893e24
posix: profiles: add custom Zephyr POSIX subprofile
cfriedt Apr 12, 2025
1b155a8
posix: profiles: make POSIX_AEP_CHOICE_ZEPHYR the default
cfriedt Apr 12, 2025
b1e87fc
posix: options: add keep-sorted-start and -stop
cfriedt Apr 12, 2025
558cd49
posix: getopt: use configurable log level
cfriedt May 25, 2025
c4971c1
posix: c_lib_ext: implement getsubopt()
cfriedt May 25, 2025
5cf7c4c
tests: posix: c_lib_ext: add tests for getsubopt()
cfriedt May 25, 2025
eeaf04d
posix: c_lib_ext: add remaining string functions
cfriedt May 25, 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 @@ -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>
#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