Skip to content

Commit 89c1f15

Browse files
cfriedtkartben
authored andcommitted
posix: move timespec functions to posix_clock.h
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]>
1 parent d40be58 commit 89c1f15

File tree

5 files changed

+44
-36
lines changed

5 files changed

+44
-36
lines changed

include/zephyr/posix/time.h

-5
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ extern "C" {
8585
#define TIMER_ABSTIME 4
8686
#endif
8787

88-
static inline int32_t _ts_to_ms(const struct timespec *to)
89-
{
90-
return (int32_t)(to->tv_sec * MSEC_PER_SEC) + (int32_t)(to->tv_nsec / NSEC_PER_MSEC);
91-
}
92-
9388
int clock_gettime(clockid_t clock_id, struct timespec *ts);
9489
int clock_getres(clockid_t clock_id, struct timespec *ts);
9590
int clock_settime(clockid_t clock_id, const struct timespec *ts);

lib/posix/options/posix_clock.h

+37
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include <zephyr/sys_clock.h>
1717
#include <zephyr/sys/__assert.h>
1818

19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
1923
/** @cond INTERNAL_HIDDEN */
2024

2125
static inline bool timespec_is_valid(const struct timespec *ts)
@@ -24,6 +28,35 @@ static inline bool timespec_is_valid(const struct timespec *ts)
2428
return (ts->tv_nsec >= 0) && (ts->tv_nsec < NSEC_PER_SEC);
2529
}
2630

31+
static inline int64_t ts_to_ns(const struct timespec *ts)
32+
{
33+
return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
34+
}
35+
36+
static inline int64_t ts_to_ms(const struct timespec *ts)
37+
{
38+
return ts->tv_sec * MSEC_PER_SEC + ts->tv_nsec / NSEC_PER_MSEC;
39+
}
40+
41+
static inline bool tp_ge(const struct timespec *a, const struct timespec *b)
42+
{
43+
return ts_to_ns(a) >= ts_to_ns(b);
44+
}
45+
46+
static inline int64_t tp_diff(const struct timespec *a, const struct timespec *b)
47+
{
48+
return ts_to_ns(a) - ts_to_ns(b);
49+
}
50+
51+
/* lo <= (a - b) < hi */
52+
static inline bool tp_diff_in_range_ns(const struct timespec *a, const struct timespec *b,
53+
int64_t lo, int64_t hi)
54+
{
55+
int64_t diff = tp_diff(a, b);
56+
57+
return diff >= lo && diff < hi;
58+
}
59+
2760
uint32_t timespec_to_timeoutms(clockid_t clock_id, const struct timespec *abstime);
2861

2962
__syscall int __posix_clock_get_base(clockid_t clock_id, struct timespec *ts);
@@ -32,4 +65,8 @@ __syscall int __posix_clock_get_base(clockid_t clock_id, struct timespec *ts);
3265

3366
#include <zephyr/syscalls/posix_clock.h>
3467

68+
#ifdef __cplusplus
69+
}
70+
#endif
71+
3572
#endif

lib/posix/options/timer.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
267267
}
268268

269269
/* Calculate timer period */
270-
timer->reload = _ts_to_ms(&value->it_interval);
270+
timer->reload = ts_to_ms(&value->it_interval);
271271
timer->interval.tv_sec = value->it_interval.tv_sec;
272272
timer->interval.tv_nsec = value->it_interval.tv_nsec;
273273

274274
/* Calculate timer duration */
275-
duration = _ts_to_ms(&(value->it_value));
275+
duration = ts_to_ms(&(value->it_value));
276276
if ((flags & TIMER_ABSTIME) != 0) {
277277
current = k_timer_remaining_get(&timer->ztimer);
278278

tests/posix/timers/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ FILE(GLOB app_sources src/*.c)
99
target_sources(app PRIVATE ${app_sources})
1010

1111
target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)
12+
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/lib/posix/options)

tests/posix/timers/src/clock.c

+4-29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
7+
8+
/* for tp_ge(), tp_diff() */
9+
#include "posix_clock.h"
10+
711
#include <sys/time.h>
812
#include <time.h>
913
#include <unistd.h>
@@ -29,35 +33,6 @@ static const bool settable[] = {
2933
true,
3034
};
3135

32-
static inline int64_t ts_to_ns(const struct timespec *ts)
33-
{
34-
return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
35-
}
36-
37-
#define _tp_op(_a, _b, _op) (ts_to_ns(_a) _op ts_to_ns(_b))
38-
39-
#define _decl_op(_type, _name, _op) \
40-
__used static inline _type _name(const struct timespec *_a, const struct timespec *_b) \
41-
{ \
42-
return _tp_op(_a, _b, _op); \
43-
}
44-
45-
_decl_op(bool, tp_eq, ==); /* a == b */
46-
_decl_op(bool, tp_lt, <); /* a < b */
47-
_decl_op(bool, tp_gt, >); /* a > b */
48-
_decl_op(bool, tp_le, <=); /* a <= b */
49-
_decl_op(bool, tp_ge, >=); /* a >= b */
50-
_decl_op(int64_t, tp_diff, -); /* a - b */
51-
52-
/* lo <= (a - b) < hi */
53-
__used static inline bool tp_diff_in_range_ns(const struct timespec *a, const struct timespec *b,
54-
int64_t lo, int64_t hi)
55-
{
56-
int64_t diff = tp_diff(a, b);
57-
58-
return diff >= lo && diff < hi;
59-
}
60-
6136
ZTEST(posix_timers, test_clock_gettime)
6237
{
6338
struct timespec ts;

0 commit comments

Comments
 (0)