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/sys/clock.h>
17 #include <zephyr/toolchain.h>
18
clock_nanosleep(clockid_t clock_id,int flags,const struct timespec * rqtp,struct timespec * rmtp)19 int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
20 struct timespec *rmtp)
21 {
22 int ret;
23
24 if (rqtp == NULL) {
25 errno = EFAULT;
26 return -1;
27 }
28
29 ret = sys_clock_nanosleep(sys_clock_from_clockid((int)clock_id), flags, rqtp, rmtp);
30 if (ret < 0) {
31 errno = -ret;
32 return -1;
33 }
34
35 return 0;
36 }
37
pthread_condattr_getclock(const pthread_condattr_t * ZRESTRICT att,clockid_t * ZRESTRICT clock_id)38 int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,
39 clockid_t *ZRESTRICT clock_id)
40 {
41 struct posix_condattr *const attr = (struct posix_condattr *)att;
42
43 if ((attr == NULL) || !attr->initialized) {
44 return EINVAL;
45 }
46
47 *clock_id = attr->clock;
48
49 return 0;
50 }
51
pthread_condattr_setclock(pthread_condattr_t * att,clockid_t clock_id)52 int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id)
53 {
54 struct posix_condattr *const attr = (struct posix_condattr *)att;
55
56 if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC) {
57 return -EINVAL;
58 }
59
60 if ((attr == NULL) || !attr->initialized) {
61 return EINVAL;
62 }
63
64 attr->clock = clock_id;
65
66 return 0;
67 }
68