timespec: Implement saturating timespec substraction

Signed-off-by: Sebastian Wick <sebastian.wick@redhat.com>
This commit is contained in:
Sebastian Wick 2024-02-07 18:53:16 +01:00 committed by Derek Foreman
parent 9d5de6062b
commit 893e4fc46d

View file

@ -290,4 +290,22 @@ timespec_add(struct timespec *r,
} }
} }
/**
* Saturating timespec subtraction
*
* \param r[out] result: max(a - b, 0)
* \param a[in] operand
* \param b[in] operand
*/
static inline void
timespec_sub_saturate(struct timespec *r,
const struct timespec *a, const struct timespec *b)
{
timespec_sub(r, a, b);
if (r->tv_sec < 0) {
r->tv_sec = 0;
r->tv_nsec = 0;
}
}
#endif /* TIMESPEC_UTIL_H */ #endif /* TIMESPEC_UTIL_H */