diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index 0ef4e52b6e9..f12904aefb6 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -2,6 +2,7 @@ set(GEN_DIR ${ZEPHYR_BINARY_DIR}/include/generated) +zephyr_syscall_header_ifdef(CONFIG_POSIX_CLOCK_SELECTION posix_clock.h) zephyr_syscall_header_ifdef(CONFIG_POSIX_TIMERS posix_clock.h) zephyr_syscall_header_ifdef(CONFIG_XSI_SINGLE_PROCESS posix_clock.h) @@ -45,6 +46,13 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_BARRIERS) zephyr_library_sources_ifdef(CONFIG_POSIX_BARRIERS barrier.c) endif() +if (NOT CONFIG_TC_PROVIDES_POSIX_CLOCK_SELECTION) + zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK_SELECTION + clock_common.c + clock_selection.c + ) +endif() + if (NOT CONFIG_TC_PROVIDES_POSIX_C_LIB_EXT) zephyr_library_sources_ifdef(CONFIG_POSIX_C_LIB_EXT fnmatch.c diff --git a/lib/posix/options/Kconfig.clock_selection b/lib/posix/options/Kconfig.clock_selection new file mode 100644 index 00000000000..34b12ff8f23 --- /dev/null +++ b/lib/posix/options/Kconfig.clock_selection @@ -0,0 +1,13 @@ +# Copyright (c) 2025 Tenstorrent AI ULC +# +# SPDX-License-Identifier: Apache-2.0 + +config POSIX_CLOCK_SELECTION + bool "POSIX Clock Selection" + help + Select 'y' here and Zephyr will provide an implementation of the POSIX_CLOCK_SELECTION Option + Group, which includes the functions clock_nanosleep(), pthread_condattr_getclock(), + pthread_condattr_setclock(). + + For more information, please see + https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html diff --git a/lib/posix/options/clock_selection.c b/lib/posix/options/clock_selection.c new file mode 100644 index 00000000000..6bddc321208 --- /dev/null +++ b/lib/posix/options/clock_selection.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Meta + * Copyright (c) 2025 Tenstorrent AI ULC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "posix_clock.h" +#include "posix_internal.h" + +#include +#include +#include + +#include +#include + +extern int z_clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, + struct timespec *rmtp); + +extern int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, + struct timespec *rmtp) +{ + return z_clock_nanosleep(clock_id, flags, rqtp, rmtp); +} + +int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att, + clockid_t *ZRESTRICT clock_id) +{ + struct posix_condattr *const attr = (struct posix_condattr *)att; + + if ((attr == NULL) || !attr->initialized) { + return EINVAL; + } + + *clock_id = attr->clock; + + return 0; +} + +int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id) +{ + struct posix_condattr *const attr = (struct posix_condattr *)att; + + if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC) { + return -EINVAL; + } + + if ((attr == NULL) || !attr->initialized) { + return EINVAL; + } + + attr->clock = clock_id; + + return 0; +} diff --git a/lib/posix/options/cond.c b/lib/posix/options/cond.c index c93386ab539..f2b5af94ad7 100644 --- a/lib/posix/options/cond.c +++ b/lib/posix/options/cond.c @@ -276,36 +276,4 @@ int pthread_condattr_destroy(pthread_condattr_t *att) return 0; } -int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att, - clockid_t *ZRESTRICT clock_id) -{ - struct posix_condattr *const attr = (struct posix_condattr *)att; - - if ((attr == NULL) || !attr->initialized) { - LOG_DBG("%s %s initialized", "attribute", "not"); - return EINVAL; - } - - *clock_id = attr->clock; - - return 0; -} - -int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id) -{ - struct posix_condattr *const attr = (struct posix_condattr *)att; - - if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC) { - return -EINVAL; - } - - if ((attr == NULL) || !attr->initialized) { - LOG_DBG("%s %s initialized", "attribute", "not"); - return EINVAL; - } - - attr->clock = clock_id; - - return 0; -} SYS_INIT(pthread_cond_pool_init, PRE_KERNEL_1, 0);