posix: provide timespec_to_clock_timeoutms

Provide a private internal function timespec_to_clock_timeoutms() to
complement timespec_to_timeoutms(). This new variant accepts a clock_t
parameter that allows the caller to specify which clock to use.

The original timespec_to_timeoutms() then just becomes a static inline
wrapper around the original.

Note: timespec_to_clock_timeoutms() and timespec_to_timeoutms() might
have a limited lifespan, since it might make sense to create a
common timespec manipulation library.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
This commit is contained in:
Chris Friedt 2025-04-23 07:00:37 -04:00 committed by Benjamin Cabé
commit 241469af9a
2 changed files with 8 additions and 6 deletions

View file

@ -11,6 +11,7 @@
#include <stdint.h>
#include <time.h>
uint32_t timespec_to_clock_timeoutms(clockid_t clock_id, const struct timespec *abstime);
uint32_t timespec_to_timeoutms(const struct timespec *abstime);
__syscall int __posix_clock_get_base(clockid_t clock_id, struct timespec *ts);

View file

@ -10,16 +10,12 @@
#include <ksched.h>
#include <zephyr/posix/time.h>
uint32_t timespec_to_timeoutms(const struct timespec *abstime)
uint32_t timespec_to_clock_timeoutms(clockid_t clock_id, const struct timespec *abstime)
{
int64_t milli_secs, secs, nsecs;
struct timespec curtime;
/* FIXME: Zephyr does have CLOCK_REALTIME to get time.
* As per POSIX standard time should be calculated wrt CLOCK_REALTIME.
* Zephyr deviates from POSIX 1003.1 standard on this aspect.
*/
clock_gettime(CLOCK_MONOTONIC, &curtime);
clock_gettime(clock_id, &curtime);
secs = abstime->tv_sec - curtime.tv_sec;
nsecs = abstime->tv_nsec - curtime.tv_nsec;
@ -31,3 +27,8 @@ uint32_t timespec_to_timeoutms(const struct timespec *abstime)
return milli_secs;
}
uint32_t timespec_to_timeoutms(const struct timespec *abstime)
{
return timespec_to_clock_timeoutms(CLOCK_MONOTONIC, abstime);
}