posix: timers: use newly added timespec util functions
Use the newly added timespec util functions to manipulate and compare timespec structures with overflow detection. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
This commit is contained in:
parent
03099f30da
commit
8a5c744213
2 changed files with 13 additions and 14 deletions
|
@ -15,6 +15,7 @@
|
|||
#include <zephyr/posix/unistd.h>
|
||||
#include <zephyr/internal/syscall_handler.h>
|
||||
#include <zephyr/sys/sem.h>
|
||||
#include <zephyr/sys/timeutil.h>
|
||||
|
||||
/*
|
||||
* `k_uptime_get` returns a timestamp based on an always increasing
|
||||
|
@ -82,11 +83,9 @@ int z_clock_gettime(clockid_t clock_id, struct timespec *ts)
|
|||
/* For ns 32 bit conversion can be used since its smaller than 1sec. */
|
||||
ts->tv_nsec = (int32_t)k_ticks_to_ns_floor32(nremainder);
|
||||
|
||||
ts->tv_sec += base.tv_sec;
|
||||
ts->tv_nsec += base.tv_nsec;
|
||||
if (ts->tv_nsec >= NSEC_PER_SEC) {
|
||||
ts->tv_sec++;
|
||||
ts->tv_nsec -= NSEC_PER_SEC;
|
||||
if (unlikely(!timespec_normalize(ts)) || unlikely(!timespec_add(ts, &base))) {
|
||||
errno = EOVERFLOW;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -101,7 +100,7 @@ int z_clock_settime(clockid_t clock_id, const struct timespec *tp)
|
|||
return -1;
|
||||
}
|
||||
|
||||
if (tp->tv_nsec < 0 || tp->tv_nsec >= NSEC_PER_SEC) {
|
||||
if (!timespec_is_valid(tp)) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
@ -112,6 +111,11 @@ int z_clock_settime(clockid_t clock_id, const struct timespec *tp)
|
|||
base.tv_sec = delta / NSEC_PER_SEC;
|
||||
base.tv_nsec = delta % NSEC_PER_SEC;
|
||||
|
||||
if (unlikely(!timespec_normalize(&base))) {
|
||||
errno = EOVERFLOW;
|
||||
return -1;
|
||||
}
|
||||
|
||||
SYS_SEM_LOCK(&rt_clock_base_lock) {
|
||||
rt_clock_base = base;
|
||||
}
|
||||
|
@ -137,7 +141,7 @@ int z_clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp
|
|||
return -1;
|
||||
}
|
||||
|
||||
if ((rqtp->tv_sec < 0) || (rqtp->tv_nsec < 0) || (rqtp->tv_nsec >= NSEC_PER_SEC)) {
|
||||
if ((rqtp->tv_sec < 0) || !timespec_is_valid(rqtp)) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include <zephyr/sys_clock.h>
|
||||
#include <zephyr/sys/__assert.h>
|
||||
#include <zephyr/posix/sys/time.h>
|
||||
#include <zephyr/sys/timeutil.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -23,12 +24,6 @@ extern "C" {
|
|||
|
||||
/** @cond INTERNAL_HIDDEN */
|
||||
|
||||
static inline bool timespec_is_valid(const struct timespec *ts)
|
||||
{
|
||||
__ASSERT_NO_MSG(ts != NULL);
|
||||
return (ts->tv_nsec >= 0) && (ts->tv_nsec < NSEC_PER_SEC);
|
||||
}
|
||||
|
||||
static inline int64_t ts_to_ns(const struct timespec *ts)
|
||||
{
|
||||
return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
|
||||
|
@ -47,7 +42,7 @@ static inline void tv_to_ts(const struct timeval *tv, struct timespec *ts)
|
|||
|
||||
static inline bool tp_ge(const struct timespec *a, const struct timespec *b)
|
||||
{
|
||||
return ts_to_ns(a) >= ts_to_ns(b);
|
||||
return timespec_compare(a, b) >= 0;
|
||||
}
|
||||
|
||||
static inline int64_t tp_diff(const struct timespec *a, const struct timespec *b)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue