diff options
author | Alexandros Frantzis <alexandros.frantzis@collabora.com> | 2017-11-16 18:20:51 +0200 |
---|---|---|
committer | Pekka Paalanen <pekka.paalanen@collabora.co.uk> | 2017-11-27 11:18:25 +0200 |
commit | 6c2752a863becf41a43c1b0af99e1b713698e0b1 (patch) | |
tree | 329a19a85c0923c86de5ffd9484b0f2173d8314b /shared | |
parent | e2a5f9e02d22f2271f6f0d6be10ba3d1b320dca2 (diff) | |
download | weston-6c2752a863becf41a43c1b0af99e1b713698e0b1.tar.gz |
shared: Add helpers to convert between various time units and timespec
Add helper functions to make it easy and less error-prone to convert
between values in various time units (nsec, usec, msec) and struct
timespec. These helpers are going to be used in the upcoming commits to
transition the Weston codebase to struct timespec.
Signed-off-by: Alexandros Frantzis <alexandros.frantzis@collabora.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Diffstat (limited to 'shared')
-rw-r--r-- | shared/timespec-util.h | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/shared/timespec-util.h b/shared/timespec-util.h index 7260dc8b..f9736c27 100644 --- a/shared/timespec-util.h +++ b/shared/timespec-util.h @@ -134,6 +134,53 @@ timespec_sub_to_msec(const struct timespec *a, const struct timespec *b) return timespec_sub_to_nsec(a, b) / 1000000; } +/* Convert timespec to microseconds + * + * \param a timespec + * \return microseconds + * + * Rounding to integer microseconds happens always down (floor()). + */ +static inline int64_t +timespec_to_usec(const struct timespec *a) +{ + return (int64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000; +} + +/* Convert nanoseconds to timespec + * + * \param a timespec + * \param b nanoseconds + */ +static inline void +timespec_from_nsec(struct timespec *a, int64_t b) +{ + a->tv_sec = b / NSEC_PER_SEC; + a->tv_nsec = b % NSEC_PER_SEC; +} + +/* Convert microseconds to timespec + * + * \param a timespec + * \param b microseconds + */ +static inline void +timespec_from_usec(struct timespec *a, int64_t b) +{ + timespec_from_nsec(a, b * 1000); +} + +/* Convert milliseconds to timespec + * + * \param a timespec + * \param b milliseconds + */ +static inline void +timespec_from_msec(struct timespec *a, int64_t b) +{ + timespec_from_nsec(a, b * 1000000); +} + /* Check if a timespec is zero * * \param a timespec |