Skip to content

Commit 3d99249

Browse files
committed
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>
1 parent 88a2cc1 commit 3d99249

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

lib/posix/options/posix_clock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdint.h>
1212
#include <time.h>
1313

14+
uint32_t timespec_to_clock_timeoutms(clockid_t clock_id, const struct timespec *abstime);
1415
uint32_t timespec_to_timeoutms(const struct timespec *abstime);
1516

1617
__syscall int __posix_clock_get_base(clockid_t clock_id, struct timespec *ts);

lib/posix/options/timespec_to_timeout.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
#include <ksched.h>
1111
#include <zephyr/posix/time.h>
1212

13-
uint32_t timespec_to_timeoutms(const struct timespec *abstime)
13+
uint32_t timespec_to_clock_timeoutms(clockid_t clock_id, const struct timespec *abstime)
1414
{
1515
int64_t milli_secs, secs, nsecs;
1616
struct timespec curtime;
1717

18-
/* FIXME: Zephyr does have CLOCK_REALTIME to get time.
19-
* As per POSIX standard time should be calculated wrt CLOCK_REALTIME.
20-
* Zephyr deviates from POSIX 1003.1 standard on this aspect.
21-
*/
22-
clock_gettime(CLOCK_MONOTONIC, &curtime);
18+
clock_gettime(clock_id, &curtime);
2319
secs = abstime->tv_sec - curtime.tv_sec;
2420
nsecs = abstime->tv_nsec - curtime.tv_nsec;
2521

@@ -31,3 +27,8 @@ uint32_t timespec_to_timeoutms(const struct timespec *abstime)
3127

3228
return milli_secs;
3329
}
30+
31+
uint32_t timespec_to_timeoutms(const struct timespec *abstime)
32+
{
33+
return timespec_to_clock_timeoutms(CLOCK_MONOTONIC, abstime);
34+
}

0 commit comments

Comments
 (0)