Skip to content

Commit 5f2c480

Browse files
committed
posix: use kernel k_clock implementation
Use the newly added k_clock API in the kernel for * clock_gettime() * clock_settime() * clock_nanosleep() and nanosleep() * gettimeofday() Signed-off-by: Chris Friedt <[email protected]>
1 parent e1fef28 commit 5f2c480

File tree

7 files changed

+57
-238
lines changed

7 files changed

+57
-238
lines changed

include/zephyr/posix/time.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extern "C" {
6666
#endif
6767

6868
#ifndef CLOCK_REALTIME
69-
#define CLOCK_REALTIME 1
69+
#define CLOCK_REALTIME K_CLOCK_REALTIME
7070
#endif
7171

7272
#ifndef CLOCK_PROCESS_CPUTIME_ID
@@ -78,7 +78,7 @@ extern "C" {
7878
#endif
7979

8080
#ifndef CLOCK_MONOTONIC
81-
#define CLOCK_MONOTONIC 4
81+
#define CLOCK_MONOTONIC K_CLOCK_MONOTONIC
8282
#endif
8383

8484
#ifndef TIMER_ABSTIME

lib/posix/options/CMakeLists.txt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

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

5-
zephyr_syscall_header_ifdef(CONFIG_POSIX_CLOCK_SELECTION posix_clock.h)
6-
zephyr_syscall_header_ifdef(CONFIG_POSIX_TIMERS posix_clock.h)
7-
zephyr_syscall_header_ifdef(CONFIG_XSI_SINGLE_PROCESS posix_clock.h)
8-
95
if(CONFIG_POSIX_API)
106
zephyr_include_directories(${ZEPHYR_BASE}/include/zephyr/posix)
117
endif()
@@ -47,10 +43,7 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_BARRIERS)
4743
endif()
4844

4945
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-
)
46+
zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK_SELECTION clock_selection.c)
5447
endif()
5548

5649
if (NOT CONFIG_TC_PROVIDES_POSIX_C_LIB_EXT)
@@ -124,7 +117,6 @@ endif()
124117
if (NOT CONFIG_TC_PROVIDES_POSIX_TIMERS)
125118
zephyr_library_sources_ifdef(CONFIG_POSIX_TIMERS
126119
clock.c
127-
clock_common.c
128120
timer.c
129121
timespec_to_timeout.c
130122
)
@@ -166,7 +158,6 @@ zephyr_library_sources_ifdef(CONFIG_XOPEN_STREAMS stropts.c)
166158

167159
if (NOT CONFIG_TC_PROVIDES_XSI_SINGLE_PROCESS)
168160
zephyr_library_sources_ifdef(CONFIG_XSI_SINGLE_PROCESS
169-
clock_common.c
170161
env_common.c
171162
xsi_single_process.c
172163
)

lib/posix/options/clock.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88
#include <errno.h>
99
#include <time.h>
1010

11+
#include <zephyr/kernel.h>
1112
#include <zephyr/posix/time.h>
1213
#include <zephyr/posix/sys/time.h>
1314
#include <zephyr/posix/unistd.h>
1415

15-
extern int z_clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
16-
struct timespec *rmtp);
17-
extern int z_clock_gettime(clockid_t clock_id, struct timespec *ts);
18-
extern int z_clock_settime(clockid_t clock_id, const struct timespec *tp);
19-
2016
int clock_gettime(clockid_t clock_id, struct timespec *ts)
2117
{
22-
return z_clock_gettime(clock_id, ts);
18+
int ret;
19+
20+
ret = k_clock_gettime(clock_id, ts);
21+
if (ret < 0) {
22+
errno = -ret;
23+
return -1;
24+
}
25+
26+
return 0;
2327
}
2428

2529
int clock_getres(clockid_t clock_id, struct timespec *res)
@@ -54,7 +58,15 @@ int clock_getres(clockid_t clock_id, struct timespec *res)
5458
*/
5559
int clock_settime(clockid_t clock_id, const struct timespec *tp)
5660
{
57-
return z_clock_settime(clock_id, tp);
61+
int ret;
62+
63+
ret = k_clock_settime(clock_id, tp);
64+
if (ret < 0) {
65+
errno = -ret;
66+
return -1;
67+
}
68+
69+
return 0;
5870
}
5971

6072
/*
@@ -88,7 +100,20 @@ int usleep(useconds_t useconds)
88100

89101
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
90102
{
91-
return z_clock_nanosleep(CLOCK_MONOTONIC, 0, rqtp, rmtp);
103+
int ret;
104+
105+
if (rqtp == NULL) {
106+
errno = EFAULT;
107+
return -1;
108+
}
109+
110+
ret = k_clock_nanosleep(K_CLOCK_MONOTONIC, 0, rqtp, rmtp);
111+
if (ret < 0) {
112+
errno = -ret;
113+
return -1;
114+
}
115+
116+
return 0;
92117
}
93118

94119
int clock_getcpuclockid(pid_t pid, clockid_t *clock_id)

lib/posix/options/clock_common.c

Lines changed: 0 additions & 204 deletions
This file was deleted.

lib/posix/options/clock_selection.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@
1515
#include <zephyr/posix/time.h>
1616
#include <zephyr/toolchain.h>
1717

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)
18+
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
19+
struct timespec *rmtp)
2320
{
24-
return z_clock_nanosleep(clock_id, flags, rqtp, rmtp);
21+
int ret;
22+
23+
if (rqtp == NULL) {
24+
errno = EFAULT;
25+
return -1;
26+
}
27+
28+
ret = k_clock_nanosleep((int)clock_id, flags, rqtp, rmtp);
29+
if (ret < 0) {
30+
errno = -ret;
31+
return -1;
32+
}
33+
34+
return 0;
2535
}
2636

2737
int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,

lib/posix/options/posix_clock.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,8 @@ static inline bool tp_diff_in_range_ns(const struct timespec *a, const struct ti
6161

6262
uint32_t timespec_to_timeoutms(clockid_t clock_id, const struct timespec *abstime);
6363

64-
__syscall int __posix_clock_get_base(clockid_t clock_id, struct timespec *ts);
65-
6664
/** INTERNAL_HIDDEN @endcond */
6765

68-
#include <zephyr/syscalls/posix_clock.h>
69-
7066
#ifdef __cplusplus
7167
}
7268
#endif

0 commit comments

Comments
 (0)