-
Notifications
You must be signed in to change notification settings - Fork 7.3k
posix + tests: correct timing functions to use CLOCK_REALTIME #88762
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
posix + tests: correct timing functions to use CLOCK_REALTIME #88762
Conversation
f35cc31
to
f3c0a2e
Compare
95b410c
to
4def3d8
Compare
9bd13da
to
af01824
Compare
An aside, I think it would be really useful to have an API call to perform common operations on That API and function signatures are good, but I would almost prefer if the entire implementation were in a header file, as static inlines, and additionally be able to use https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html Many of the algorithms can also be branchless as well, which will speed things up on processors that use branch prediction. Additionally, we would want to move many runtime checks to assertions so that they can be disabled for production firmware. Lots of small optimizations for bare metal. And of course, |
Add definitions for struct posix_condattr and struct posix_cond, which are internal variants of the external pthread_condattr_t and pthread_cond_t types. Signed-off-by: Chris Friedt <[email protected]>
af01824
to
27bb2cf
Compare
|
6fa3884
to
21aaee9
Compare
Use struct posix_cond and struct posix_condattr internally. Signed-off-by: Chris Friedt <[email protected]>
Check whenther a pthread_condattr_t has been initialized in pthread_condattr_*() functions. Signed-off-by: Chris Friedt <[email protected]>
Use the clock specified via pthread_condattr_t in pthread_cond_timedwait(). Signed-off-by: Chris Friedt <[email protected]>
Add a common private function timespec_is_valid() that can be used to check if a timespec object is valid, and use that consistently in lib/posix/options. Signed-off-by: Chris Friedt <[email protected]>
Use CLOCK_REALTIME for the default clock source throughout the POSIX implementation and tests so that we are consistent with the specification. Signed-off-by: Chris Friedt <[email protected]>
Always require the clockid_t argument to timespec_to_timeoutms() and remove the unused variant that accepts no clockid_t parameter. Signed-off-by: Chris Friedt <[email protected]>
Prevent doxygen from parsing internal functions declared in posix_clock.h . Signed-off-by: Chris Friedt <[email protected]>
21aaee9
to
ee68f25
Compare
|
Move somewhat useful (but private and internal functions) that deal with struct timespec to posix_clock.h until there is a better API available for dealing with operations on struct timespec. Signed-off-by: Chris Friedt <[email protected]>
Use the tp_diff() macro as a means of converting an absolute timeout with respect to a specific clock to a relative timeout, in ms. Clamp the result between 0 and UINT32_MAX. Signed-off-by: Chris Friedt <[email protected]>
ee68f25
to
9aff67e
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request updates various POSIX timing functions and their corresponding tests to use CLOCK_REALTIME by default, in line with the POSIX specification. Key changes include:
- Replacing CLOCK_MONOTONIC with CLOCK_REALTIME in time-related functions and tests.
- Refactoring timeout conversion functions to use the new posix_clock.h utilities.
- Standardizing error checking for invalid timespec objects across multiple modules.
Reviewed Changes
Copilot reviewed 16 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
tests/posix/timers/src/clock.c | Removed custom inline time conversion functions in favor of posix_clock.h helpers. |
tests/posix/rwlocks/src/main.c | Updated timeout setup to use clock_gettime(CLOCK_REALTIME, …). |
tests/posix/common/src/pthread.c | Adjusted error checking in pthread_timedjoin_np for timespec validation. |
tests/posix/common/src/cond.c | Updated clock attribute expectations from CLOCK_MONOTONIC to CLOCK_REALTIME. |
tests/lib/c_lib/thrd/src/cnd.c | Modified timedwait to use CLOCK_REALTIME instead of CLOCK_MONOTONIC. |
lib/posix/options/timespec_to_timeout.c | Updated function signature to include clock_id and use CLAMP with tp_diff. |
lib/posix/options/timer.c | Replaced legacy timeout conversion macros with ts_to_ms and validated timespecs. |
lib/posix/options/semaphore.c | Updated sem_timedwait to use the new timespec_to_timeoutms conversion. |
lib/posix/options/rwlock.c | Modified read/write lock acquire functions to use uint32_t timeouts. |
lib/posix/options/pthread.c | Adjusted pthread_timedjoin_np to validate the abstime using the new utility. |
lib/posix/options/posix_internal.h | Introduced new posix_condattr structure for condition variable attributes. |
lib/posix/options/posix_clock.h | Added common timespec utility functions (e.g. timespec_is_valid, ts_to_ns). |
lib/posix/options/mutex.c | Updated pthread_mutex_timedlock to check timespec validity and use CLOCK_REALTIME. |
lib/posix/options/mqueue.c | Changed both mq_timedsend and mq_timedreceive to convert times using CLOCK_REALTIME. |
lib/posix/options/cond.c | Refactored condition variable functions to use posix_cond structures and updated attribute handling. |
include/zephyr/posix/time.h | Removed unused inline timeout conversion functions. |
Files not reviewed (1)
- tests/posix/timers/CMakeLists.txt: Language not supported
} | ||
|
||
int pthread_cond_init(pthread_cond_t *cvar, const pthread_condattr_t *att) | ||
{ | ||
struct k_condvar *cv; | ||
struct posix_cond *cv; | ||
struct posix_condattr *attr = (struct posix_condattr *)attr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The local variable 'attr' is incorrectly initialized by casting itself instead of the function parameter. Change it to 'struct posix_condattr *attr = (struct posix_condattr *)att;' to correctly reference the passed attribute.
struct posix_condattr *attr = (struct posix_condattr *)attr; | |
struct posix_condattr *attr = (struct posix_condattr *)att; |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#89076 :)
Use
CLOCK_REALTIME
by default as per the POSIX specification for the following functions:mq_timedreceive()
mq_timedsend()
pthread_cond_timedwait()
pthread_mutex_timedlock()
pthread_rwlock_timedrdlock()
pthread_rwlock_timedwrlock()
sem_timedwait()
and additionally for the following non-portable functions:
pthread_timedjoin_np()
Fixes #88587
Should be merged after #88767 and #88769