-
Notifications
You must be signed in to change notification settings - Fork 7.4k
sys: clock: add sys_clock api and remove posix from iso c time #90096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9e85c18
22dba1a
bde1584
9eadeb2
ba43655
148300e
05c133a
8140cda
b4f3161
c2f8bd3
09d3282
e188f9b
7b8aab0
db80e00
5e38e6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A new sys/clock.h header may seem a bit confusing given that we have a sys_clock.h already There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with this point. This can be a bit confusing, and given that this is a new API class, would you be ok to discuss this tomorrow in the Architecture WG @cfriedt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course - I agree that it was a little weird how we have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @carlescufi - what do you suggest we do in terms of Would it be sufficient to merge the two files into We could at some point deprecate The alternative is moving There seem to be no conflicting toolchain paths that would interfere with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here one can have quite different opinions, so just my two cents: To me it looks like this is a addon that would fit better in timeutil.h/c than in sys_clock. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Although the added sys_clock API uses timeutil APIs, those are quite different and have very different purposes. I originally thought that this code should have the The added code does what much of the sys_clock code does today, which is to deal with ticks and represent system clock interfaces (in more or less abstract ways).
If you re-read my suggestion, there was no mention of renaming sys_clock.h . It was to merge the contents of the two headers (sys_clock.h and sys/clock.h) into one, keep them both, have one include the other. Presumably into One file could eventually be deprecated (the smaller of the two), but there is no rush to do that. So no "churn". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that simply merging the contents of the two headers will result in compile failures, so I don't think it will be done in this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally don't mind either way, as long as we do not introduce this confusion of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cfriedt I still see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see you are testing it in a draft PR :) thanks! |
||
* Copyright (c) 2025 Tenstorrent AI ULC | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* @file | ||
* @brief System clock APIs | ||
* | ||
* APIs for getting, setting, and sleeping with respect to system clocks. | ||
*/ | ||
|
||
#ifndef ZEPHYR_INCLUDE_SYSCLOCK_H_ | ||
#define ZEPHYR_INCLUDE_SYSCLOCK_H_ | ||
|
||
#include <time.h> | ||
|
||
#include <zephyr/toolchain.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @addtogroup clock_apis | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the PR title/description and content, I'm not sure if this PR tries to add a new API set (and happened to also use by mistake the sys_clock prefix), or tries to expand the "System Clock APIs". If this is meant to be a new set: In any case, I think it would need to follow this process: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR is expanding the "System Clock APIs". It's hardly experimental. The same code has been used already in Zephyr for many years, and for the most part, this PR is just shuffling existing code around. But sure, I will put together an RFC, likely not in time for tomorrow's arch meeting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given there are a few RFCs on this topic already, I think we can skip making a new one. |
||
* @{ | ||
*/ | ||
|
||
/** | ||
* @brief The real-time clock (i.e. "wall clock") | ||
* | ||
* This clock is used to measure time since the epoch (1970-01-01 00:00:00 UTC). | ||
* | ||
* It is not a steady clock; i.e. it may be adjusted for a number of reasons from initialization | ||
* of a hardware real-time-clock, to network-time synchronization, to manual adjustment from the | ||
* application. | ||
*/ | ||
#define SYS_CLOCK_REALTIME 1 | ||
|
||
/** | ||
* @brief The monotonic clock | ||
* | ||
* This steady clock is used to measure time since the system booted. Time from this clock is | ||
* always monotonically increasing. | ||
*/ | ||
#define SYS_CLOCK_MONOTONIC 4 | ||
|
||
/** | ||
* @brief The flag used for specifying absolute timeouts | ||
* | ||
* This flag may be passed to @ref sys_clock_nanosleep to indicate the requested timeout is an | ||
* absolute time with respect to the specified clock. | ||
*/ | ||
#define SYS_TIMER_ABSTIME 4 | ||
|
||
/** | ||
* @brief Get the offset @ref SYS_CLOCK_REALTIME with respect to @ref SYS_CLOCK_MONOTONIC | ||
* | ||
* The "wall clock" (i.e. @ref SYS_CLOCK_REALTIME) depends on a base time that is set by the | ||
* system. The base time may be updated for a number of reasons, such as initialization of a | ||
* hardware real-time-clock (RTC), network time protocol (NTP) synchronization, or manual | ||
* adjustment by the application. | ||
* | ||
* This function retrieves the current time offset, as a `timespec` object, for | ||
* @ref SYS_CLOCK_REALTIME, with respect to @ref SYS_CLOCK_MONOTONIC, and writes it to the | ||
* provided memory location pointed-to by @a tp. | ||
* | ||
* @note This function may assert if @a tp is NULL. | ||
* | ||
* @param tp Pointer to memory where time will be written. | ||
*/ | ||
__syscall void sys_clock_getrtoffset(struct timespec *tp); | ||
|
||
/** | ||
* @brief Get the current time from the specified clock | ||
* | ||
* @param clock_id The clock from which to query time. | ||
* @param tp Pointer to memory where time will be written. | ||
* @retval 0 on success. | ||
* @retval -EINVAL when an invalid @a clock_id is specified. | ||
*/ | ||
int sys_clock_gettime(int clock_id, struct timespec *tp); | ||
|
||
/** | ||
* @brief Set the current time for the specified clock | ||
* | ||
* @param clock_id The clock for which the time should be set. | ||
* @param tp Pointer to memory specifying the desired time. | ||
* @retval 0 on success. | ||
* @retval -EINVAL when an invalid @a clock_id is specified or when @a tp contains nanoseconds | ||
* outside of the range `[0, 999999999]`. | ||
*/ | ||
__syscall int sys_clock_settime(int clock_id, const struct timespec *tp); | ||
|
||
/** | ||
* @brief Sleep for the specified amount of time with respect to the specified clock. | ||
* | ||
* This function will cause the calling thread to sleep either | ||
* - until the absolute time specified by @a rqtp (if @a flags includes @ref SYS_TIMER_ABSTIME), or | ||
* - until the relative time specified by @a rqtp (if @a flags does not include | ||
* @ref SYS_TIMER_ABSTIME). | ||
* | ||
* The accepted values for @a clock_id include | ||
* - @ref SYS_CLOCK_REALTIME | ||
* - @ref SYS_CLOCK_MONOTONIC | ||
* | ||
* If @a rmtp is not NULL, and the thread is awoken prior to the time specified by @a rqtp, then | ||
* any remaining time will be written to @a rmtp. If the thread has slept for at least the time | ||
* specified by @a rqtp, then @a rmtp will be set to zero. | ||
* | ||
* @param clock_id The clock to by which to sleep. | ||
* @param flags Flags to modify the behavior of the sleep operation. | ||
* @param rqtp Pointer to the requested time to sleep. | ||
* @param rmtp Pointer to memory into which to copy the remaining time, if any. | ||
* | ||
* @retval 0 on success. | ||
* @retval -EINVAL when an invalid @a clock_id, when @a rqtp contains nanoseconds outside of the | ||
* range `[0, 999999999]`, or when @a rqtp contains a negative value. | ||
*/ | ||
__syscall int sys_clock_nanosleep(int clock_id, int flags, const struct timespec *rqtp, | ||
struct timespec *rmtp); | ||
|
||
/** | ||
* @} | ||
*/ | ||
|
||
#include <zephyr/syscalls/clock.h> | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
Uh oh!
There was an error while loading. Please reload this page.