Skip to content

Commit ae4a1db

Browse files
cfriedtnashif
authored andcommitted
posix: implement the POSIX_CLOCK_SELECTION Option Group
Implement the POSIX_CLOCK_SELECTION Option Group. This was mostly already done, but compiled / linked in the wrong places. E.g. pthread_condattr_getclock() and pthread_condattr_setclock() were in pthread.c and part of POSIX_THREADS_BASE. clock_nanosleep() was in clock.c and part of POSIX_TIMERS. This change builds them as part of clock_selection.c with CONFIG_POSIX_CLOCK_SELECTION. Signed-off-by: Chris Friedt <[email protected]>
1 parent 4c9fb2e commit ae4a1db

File tree

4 files changed

+77
-32
lines changed

4 files changed

+77
-32
lines changed

lib/posix/options/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
set(GEN_DIR ${ZEPHYR_BINARY_DIR}/include/generated)
44

5+
zephyr_syscall_header_ifdef(CONFIG_POSIX_CLOCK_SELECTION posix_clock.h)
56
zephyr_syscall_header_ifdef(CONFIG_POSIX_TIMERS posix_clock.h)
67
zephyr_syscall_header_ifdef(CONFIG_XSI_SINGLE_PROCESS posix_clock.h)
78

@@ -45,6 +46,13 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_BARRIERS)
4546
zephyr_library_sources_ifdef(CONFIG_POSIX_BARRIERS barrier.c)
4647
endif()
4748

49+
if (NOT CONFIG_TC_PROVIDES_POSIX_CLOCK_SELECTION)
50+
zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK_SELECTION
51+
clock_common.c
52+
clock_selection.c
53+
)
54+
endif()
55+
4856
if (NOT CONFIG_TC_PROVIDES_POSIX_C_LIB_EXT)
4957
zephyr_library_sources_ifdef(CONFIG_POSIX_C_LIB_EXT
5058
fnmatch.c
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2025 Tenstorrent AI ULC
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
config POSIX_CLOCK_SELECTION
6+
bool "POSIX Clock Selection"
7+
help
8+
Select 'y' here and Zephyr will provide an implementation of the POSIX_CLOCK_SELECTION Option
9+
Group, which includes the functions clock_nanosleep(), pthread_condattr_getclock(),
10+
pthread_condattr_setclock().
11+
12+
For more information, please see
13+
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html

lib/posix/options/clock_selection.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2023 Meta
3+
* Copyright (c) 2025 Tenstorrent AI ULC
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include "posix_clock.h"
9+
#include "posix_internal.h"
10+
11+
#include <stddef.h>
12+
#include <time.h>
13+
#include <errno.h>
14+
15+
#include <zephyr/posix/time.h>
16+
#include <zephyr/toolchain.h>
17+
18+
extern int z_clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
19+
struct timespec *rmtp);
20+
21+
extern int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
22+
struct timespec *rmtp)
23+
{
24+
return z_clock_nanosleep(clock_id, flags, rqtp, rmtp);
25+
}
26+
27+
int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,
28+
clockid_t *ZRESTRICT clock_id)
29+
{
30+
struct posix_condattr *const attr = (struct posix_condattr *)att;
31+
32+
if ((attr == NULL) || !attr->initialized) {
33+
return EINVAL;
34+
}
35+
36+
*clock_id = attr->clock;
37+
38+
return 0;
39+
}
40+
41+
int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id)
42+
{
43+
struct posix_condattr *const attr = (struct posix_condattr *)att;
44+
45+
if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC) {
46+
return -EINVAL;
47+
}
48+
49+
if ((attr == NULL) || !attr->initialized) {
50+
return EINVAL;
51+
}
52+
53+
attr->clock = clock_id;
54+
55+
return 0;
56+
}

lib/posix/options/cond.c

-32
Original file line numberDiff line numberDiff line change
@@ -276,36 +276,4 @@ int pthread_condattr_destroy(pthread_condattr_t *att)
276276
return 0;
277277
}
278278

279-
int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,
280-
clockid_t *ZRESTRICT clock_id)
281-
{
282-
struct posix_condattr *const attr = (struct posix_condattr *)att;
283-
284-
if ((attr == NULL) || !attr->initialized) {
285-
LOG_DBG("%s %s initialized", "attribute", "not");
286-
return EINVAL;
287-
}
288-
289-
*clock_id = attr->clock;
290-
291-
return 0;
292-
}
293-
294-
int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id)
295-
{
296-
struct posix_condattr *const attr = (struct posix_condattr *)att;
297-
298-
if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC) {
299-
return -EINVAL;
300-
}
301-
302-
if ((attr == NULL) || !attr->initialized) {
303-
LOG_DBG("%s %s initialized", "attribute", "not");
304-
return EINVAL;
305-
}
306-
307-
attr->clock = clock_id;
308-
309-
return 0;
310-
}
311279
SYS_INIT(pthread_cond_pool_init, PRE_KERNEL_1, 0);

0 commit comments

Comments
 (0)