Skip to content

Commit c5bbd4f

Browse files
committed
sys: clock: rename k_clock functions to sys_clock
Rename K_CLOCK_* and k_clock_*() to SYS_CLOCK_* and sys_clock_*(). Signed-off-by: Chris Friedt <[email protected]>
1 parent 19947db commit c5bbd4f

File tree

13 files changed

+165
-132
lines changed

13 files changed

+165
-132
lines changed

include/zephyr/kernel.h

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ BUILD_ASSERT(sizeof(intptr_t) == sizeof(long));
6969
#define Z_DECL_POLL_EVENT
7070
#endif
7171

72-
struct timespec;
73-
7472
struct k_thread;
7573
struct k_mutex;
7674
struct k_sem;
@@ -6488,92 +6486,6 @@ void k_sys_runtime_stats_enable(void);
64886486
*/
64896487
void k_sys_runtime_stats_disable(void);
64906488

6491-
/**
6492-
* @defgroup kernel_clock_apis Kernel Clock APIs
6493-
* @ingroup kernel_apis
6494-
* @{
6495-
*/
6496-
6497-
/**
6498-
* @brief The real-time clock (i.e. "wall clock")
6499-
*
6500-
* This clock is used to measure time since the epoch (1970-01-01 00:00:00 UTC).
6501-
*
6502-
* It is not a steady clock; i.e. it may be adjusted for a number of reasons from initialization
6503-
* of a hardware real-time-clock, to network-time synchronization, to manual adjustment from the
6504-
* application.
6505-
*/
6506-
#define K_CLOCK_REALTIME 1
6507-
6508-
/**
6509-
* @brief The monotonic clock
6510-
*
6511-
* This steady clock is used to measure time since the system booted. Time from this clock is
6512-
* always monotonically increasing, modulo the upper bound of `{.tv_sec = INT64_MAX, .tv_nsec =
6513-
* 1000000000}`.
6514-
*/
6515-
#define K_CLOCK_MONOTONIC 4
6516-
6517-
/**
6518-
* @brief The flag used for specifying absolute timeouts
6519-
*
6520-
* This flag may be passed to @ref k_clock_nanosleep to indicate the requested timeout is an
6521-
* absolute time with respect to the specified clock.
6522-
*/
6523-
#define K_TIMER_ABSTIME 4
6524-
6525-
/**
6526-
* @brief Get the current time from the specified clock
6527-
*
6528-
* @param clock_id The clock from which to query time.
6529-
* @param tp Pointer to memory where time will be written.
6530-
* @retval 0 on success.
6531-
* @retval -EINVAL when an invalid @a clock_id is specified.
6532-
*/
6533-
__syscall int k_clock_gettime(int clock_id, struct timespec *tp);
6534-
6535-
/**
6536-
* @brief Set the current time for the specified clock
6537-
*
6538-
* @param clock_id The clock for which the time should be set.
6539-
* @param tp Pointer to memory specifying the desired time.
6540-
* @retval 0 on success.
6541-
* @retval -EINVAL when an invalid @a clock_id is specified or when @a tp contains nanoseconds
6542-
* outside of the range `[0, 999999999]`.
6543-
*/
6544-
__syscall int k_clock_settime(int clock_id, const struct timespec *tp);
6545-
6546-
/**
6547-
* @brief Sleep for the specified amount of time with respect to the specified clock.
6548-
*
6549-
* This function will cause the calling thread to sleep either
6550-
* - until the absolute time specified by @a rqtp (if @a flags includes @ref K_TIMER_ABSTIME), or
6551-
* - until the relative time specified by @a rqtp (if @a flags does not include
6552-
* @ref K_TIMER_ABSTIME).
6553-
*
6554-
* The accepted values for @a clock_id include
6555-
* - @ref K_CLOCK_REALTIME
6556-
* - @ref K_CLOCK_MONOTONIC
6557-
*
6558-
* If @a rmtp is not NULL, and the thread is awoken prior to the time specified by @a rqtp, then
6559-
* any remaining time will be written to @a rmtp. If the thread has slept for at least the time
6560-
* specified by @a rqtp, then @a rmtp will be set to zero.
6561-
*
6562-
* @param clock_id The clock to by which to sleep.
6563-
* @param flags Flags to modify the behavior of the sleep operation.
6564-
* @param rqtp Pointer to the requested time to sleep.
6565-
* @param rmtp Pointer to memory into which to copy the remaining time, if any.
6566-
*
6567-
* @retval 0 on success.
6568-
* @retval -EINVAL when an invalid @a clock_id, when @a rqtp contains nanoseconds outside of the
6569-
* range `[0, 999999999]`, or when @a rqtp contains a negative value.
6570-
*/
6571-
__syscall int k_clock_nanosleep(int clock_id, int flags, const struct timespec *rqtp,
6572-
struct timespec *rmtp);
6573-
/**
6574-
* @}
6575-
*/
6576-
65776489
#ifdef __cplusplus
65786490
}
65796491
#endif

include/zephyr/posix/time.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ struct itimerspec {
6060
#include <errno.h>
6161
#include <zephyr/posix/posix_types.h>
6262
#include <zephyr/posix/signal.h>
63+
#include <zephyr/sys/clock.h>
6364

6465
#ifdef __cplusplus
6566
extern "C" {
6667
#endif
6768

6869
#ifndef CLOCK_REALTIME
69-
#define CLOCK_REALTIME K_CLOCK_REALTIME
70+
#define CLOCK_REALTIME SYS_CLOCK_REALTIME
7071
#endif
7172

7273
#ifndef CLOCK_PROCESS_CPUTIME_ID
@@ -78,11 +79,11 @@ extern "C" {
7879
#endif
7980

8081
#ifndef CLOCK_MONOTONIC
81-
#define CLOCK_MONOTONIC K_CLOCK_MONOTONIC
82+
#define CLOCK_MONOTONIC SYS_CLOCK_MONOTONIC
8283
#endif
8384

8485
#ifndef TIMER_ABSTIME
85-
#define TIMER_ABSTIME 4
86+
#define TIMER_ABSTIME SYS_TIMER_ABSTIME
8687
#endif
8788

8889
int clock_gettime(clockid_t clock_id, struct timespec *ts);

include/zephyr/sys/clock.h

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2025 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
*
10+
* @ingroup public sys_clock APIs
11+
*/
12+
13+
#ifndef ZEPHYR_INCLUDE_SYSCLOCK_H_
14+
#define ZEPHYR_INCLUDE_SYSCLOCK_H_
15+
16+
#include <time.h>
17+
18+
#include <zephyr/internal/syscall_handler.h>
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
/**
25+
* @defgroup sys_clock_apis System Clock APIs
26+
* @{
27+
*/
28+
29+
/**
30+
* @brief The real-time clock (i.e. "wall clock")
31+
*
32+
* This clock is used to measure time since the epoch (1970-01-01 00:00:00 UTC).
33+
*
34+
* It is not a steady clock; i.e. it may be adjusted for a number of reasons from initialization
35+
* of a hardware real-time-clock, to network-time synchronization, to manual adjustment from the
36+
* application.
37+
*/
38+
#define SYS_CLOCK_REALTIME 1
39+
40+
/**
41+
* @brief The monotonic clock
42+
*
43+
* This steady clock is used to measure time since the system booted. Time from this clock is
44+
* always monotonically increasing, modulo the upper bound of `{.tv_sec = INT64_MAX, .tv_nsec =
45+
* 1000000000}`.
46+
*/
47+
#define SYS_CLOCK_MONOTONIC 4
48+
49+
/**
50+
* @brief The flag used for specifying absolute timeouts
51+
*
52+
* This flag may be passed to @ref sys_clock_nanosleep to indicate the requested timeout is an
53+
* absolute time with respect to the specified clock.
54+
*/
55+
#define SYS_TIMER_ABSTIME 4
56+
57+
/**
58+
* @brief Get the current time from the specified clock
59+
*
60+
* @param clock_id The clock from which to query time.
61+
* @param tp Pointer to memory where time will be written.
62+
* @retval 0 on success.
63+
* @retval -EINVAL when an invalid @a clock_id is specified.
64+
*/
65+
__syscall int sys_clock_gettime(int clock_id, struct timespec *tp);
66+
67+
/**
68+
* @brief Set the current time for the specified clock
69+
*
70+
* @param clock_id The clock for which the time should be set.
71+
* @param tp Pointer to memory specifying the desired time.
72+
* @retval 0 on success.
73+
* @retval -EINVAL when an invalid @a clock_id is specified or when @a tp contains nanoseconds
74+
* outside of the range `[0, 999999999]`.
75+
*/
76+
__syscall int sys_clock_settime(int clock_id, const struct timespec *tp);
77+
78+
/**
79+
* @brief Sleep for the specified amount of time with respect to the specified clock.
80+
*
81+
* This function will cause the calling thread to sleep either
82+
* - until the absolute time specified by @a rqtp (if @a flags includes @ref SYS_TIMER_ABSTIME), or
83+
* - until the relative time specified by @a rqtp (if @a flags does not include
84+
* @ref SYS_TIMER_ABSTIME).
85+
*
86+
* The accepted values for @a clock_id include
87+
* - @ref SYS_CLOCK_REALTIME
88+
* - @ref SYS_CLOCK_MONOTONIC
89+
*
90+
* If @a rmtp is not NULL, and the thread is awoken prior to the time specified by @a rqtp, then
91+
* any remaining time will be written to @a rmtp. If the thread has slept for at least the time
92+
* specified by @a rqtp, then @a rmtp will be set to zero.
93+
*
94+
* @param clock_id The clock to by which to sleep.
95+
* @param flags Flags to modify the behavior of the sleep operation.
96+
* @param rqtp Pointer to the requested time to sleep.
97+
* @param rmtp Pointer to memory into which to copy the remaining time, if any.
98+
*
99+
* @retval 0 on success.
100+
* @retval -EINVAL when an invalid @a clock_id, when @a rqtp contains nanoseconds outside of the
101+
* range `[0, 999999999]`, or when @a rqtp contains a negative value.
102+
*/
103+
__syscall int sys_clock_nanosleep(int clock_id, int flags, const struct timespec *rqtp,
104+
struct timespec *rmtp);
105+
106+
/**
107+
* @}
108+
*/
109+
110+
#include <zephyr/syscalls/clock.h>
111+
112+
#ifdef __cplusplus
113+
}
114+
#endif
115+
116+
#endif

kernel/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ list(APPEND kernel_files
5151
main_weak.c
5252
banner.c
5353
busy_wait.c
54-
clock.c
5554
device.c
5655
errno.c
5756
fatal.c

lib/libc/common/source/thrd/thrd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zephyr/kernel.h>
1111
#include <zephyr/posix/pthread.h>
1212
#include <zephyr/posix/sched.h>
13+
#include <zephyr/sys/clock.h>
1314

1415
struct thrd_trampoline_arg {
1516
thrd_start_t func;
@@ -44,7 +45,7 @@ thrd_t thrd_current(void)
4445

4546
int thrd_sleep(const struct timespec *duration, struct timespec *remaining)
4647
{
47-
if (k_clock_nanosleep(K_CLOCK_MONOTONIC, 0, duration, remaining) != 0) {
48+
if (sys_clock_nanosleep(SYS_CLOCK_REALTIME, 0, duration, remaining) != 0) {
4849
return thrd_error;
4950
}
5051

lib/libc/common/source/time/time.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
#include <time.h>
99

10-
#include <zephyr/kernel.h>
10+
#include <zephyr/sys/clock.h>
1111

1212
time_t time(time_t *tloc)
1313
{
1414
struct timespec ts;
1515
int ret;
1616

17-
ret = k_clock_gettime(K_CLOCK_REALTIME, &ts);
17+
ret = sys_clock_gettime(SYS_CLOCK_REALTIME, &ts);
1818
if (ret < 0) {
1919
errno = -ret;
2020
return (time_t) -1;

lib/os/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
zephyr_syscall_header(
4+
${ZEPHYR_BASE}/include/zephyr/sys/clock.h
45
${ZEPHYR_BASE}/include/zephyr/sys/mutex.h
56
)
67

78
zephyr_sources(
89
cbprintf_packaged.c
10+
clock.c
911
printk.c
1012
sem.c
1113
thread_entry.c

0 commit comments

Comments
 (0)