Skip to content

kernel: add k_clock api: remove posix dependency 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

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented May 18, 2025

  • kernel: add k_clock api
  • sys: timeutil: add timespec_to_timeout() and timespec_from_timeout()
  • doc: kernel: add timespec_to_timeout() and timespec_from_timeout()
  • libc: common: time: use k_clock api rather than posix
  • libc: use the common libc time() implementation for most libcs
  • libc: common: thrd: use k_clock_nanosleep() instead of nanosleep()
  • tests: libc: thrd: use timespec_from_timeout()
  • tests: lib: move time testsuite to c_lib
  • posix: use kernel k_clock implementation
  • net: remove dependency on posix for iso c time() function
  • samples: net: remove POSIX_TIMERS and XSI_SINGLE_PROCESS

Should be merged after #90060

@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch 2 times, most recently from a403e85 to 83dc940 Compare May 18, 2025 02:00
@cfriedt cfriedt changed the title timeutil: correct position of doxygen end-group comment kernel: add k_clock api May 18, 2025
@cfriedt cfriedt changed the title kernel: add k_clock api kernel: add k_clock api: remove posix dependency from iso c time May 18, 2025
@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch 7 times, most recently from 375c774 to 7872928 Compare May 18, 2025 05:00
cfriedt added 6 commits May 18, 2025 11:00
swap the relative position of the __cplusplus closing
bracket and the doxygen end-group comment.

Signed-off-by: Chris Friedt <[email protected]>
Add a number of utility functions for manipulating struct timespec.

* timespec_add()
* timespec_compare()
* timespec_equal()
* timespec_is_valid()
* timespec_negate()
* timespec_normalize()
* timespec_sub()

If the `__builtin_add_overflow()` function is available, then the
API is mostly branchless, which should provide decent performance on
systems with an instruction cache and branch prediction. Otherwise,
manually written alternatives exist that are also perhaps more
readable.

Signed-off-by: Chris Friedt <[email protected]>
Add a timespec util testsuite. This should have reasonably high enough
coverage to be useful.

I would have preferred to add this as an architecture-independent
unit test (for the unit_testing platform) under tests/unit/timeutil but
there is an inconsistency about the size of time_t on the unit_testing
and native_sim/native platforms. On every other platform supported by
Zephyr, time_t is 64-bits. However, on those platforms, time_t is only
32-bits.

Signed-off-by: Chris Friedt <[email protected]>
Use the newly added timespec util functions to manipulate and
compare timespec structures with overflow detection.

Signed-off-by: Chris Friedt <[email protected]>
Add documentation in the timeutil group for recently added functions
for validating, comparing, and manipulating `struct timespec`
objects.

Signed-off-by: Chris Friedt <[email protected]>
Add the k_clock API, comprised of:

* k_clock_gettime()
* k_clock_settime()
* k_clock_nanosleep()

along with the constants

* K_CLOCK_REALTIME
* K_CLOCK_MONOTONIC
* K_TIMER_ABSTIME

The primary motivation for this API is so that libc and other
libraries have a familiar-enough API to reach to when POSIX
is not available, since POSIX is optional in Zephyr.

By adding this API to the Zephyr kernel, we also eliminate
dependency cycles between libc and posix, as the kernel is
a mutual dependency.

Signed-off-by: Chris Friedt <[email protected]>
@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch 3 times, most recently from 7536ee9 to cd3e327 Compare May 18, 2025 15:16
cfriedt added 5 commits May 18, 2025 11:42
Add functions to convert durations between representation as struct
timespec and k_timeout_t.

Signed-off-by: Chris Friedt <[email protected]>
Add documentation to timeutil.rst for timespec_to_timeout() and
timespec_from_timeout().

Signed-off-by: Chris Friedt <[email protected]>
Remove POSIX clock_gettime() from the common libc time implementation,
since POSIX should not be a dependency for ISO C.

Signed-off-by: Chris Friedt <[email protected]>
Use the implementation of time() from the common libc, since there
it no longer pulls in POSIX.

Use is implied for minimal, newlib, and picolibc, and selected
for IAR.

Signed-off-by: Chris Friedt <[email protected]>
Reduce the dependency on POSIX by taking advantage of the newly added
k_clock_nanosleep().

Signed-off-by: Chris Friedt <[email protected]>
@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch from cd3e327 to b0c459f Compare May 18, 2025 15:42
cfriedt added 4 commits May 18, 2025 11:43
Use `timespec_from_timeout(K_MSEC(msec), &ts)` instead of leaning on
lazily-crafted timespecs with invalid tv_nsec fields.

Signed-off-by: Chris Friedt <[email protected]>
Presumably the time testsuite was separate from the c library set of
testsuites because it had a depedency on POSIX.

Since that dependency no longer exists, colocate the time testsuite
with the other c library testsuites.

Signed-off-by: Chris Friedt <[email protected]>
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]>
The ISO C function time() should not depend on POSIX and this was
corrected recently via the common libc time() implementation.

Remove this dependency from the network subsystem where it has
been unduly needed for some time.

Similarly, XSI_SINGLE_PROCESS was a dependency for time() via
picolibc, because the time() implementation in picolibc relies
on the POSIX gettimeofday() call.

However, since Zephyr's common libc time() implementation no longer
depends on that, it can be removed as a dependency in the network
subsystem as well.

Signed-off-by: Chris Friedt <[email protected]>
Remove POSIX_TIMERS and XSI_SINGLE_PROCESS dependencies from the aws
cloud sample and the lwm2m client sample, as they are no longer
required.

Signed-off-by: Chris Friedt <[email protected]>
@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch from b0c459f to b8923f3 Compare May 18, 2025 15:43
WIP: this isn't quite done yet.

Add tests for timespec_to_timeout() and timespec_from_timeout()

Signed-off-by: Chris Friedt <[email protected]>
@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch from db7b057 to fe430cf Compare May 18, 2025 17:57
@cfriedt
Copy link
Member Author

cfriedt commented May 18, 2025

  • adding tests for timespec_to_timeout() and timeout_from_timespec()

I'd like to get those in better shape before marking this ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant