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.frant...@collabora.com> --- shared/timespec-util.h | 47 ++++++++++++++++++++++++++++++++ tests/timespec-test.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) 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 diff --git a/tests/timespec-test.c b/tests/timespec-test.c index 551bc1be..7a26d528 100644 --- a/tests/timespec-test.c +++ b/tests/timespec-test.c @@ -60,6 +60,15 @@ ZUC_TEST(timespec_test, timespec_to_nsec) ZUC_ASSERT_EQ(timespec_to_nsec(&a), (NSEC_PER_SEC * 4ULL) + 4); } +ZUC_TEST(timespec_test, timespec_to_usec) +{ + struct timespec a; + + a.tv_sec = 4; + a.tv_nsec = 4000; + ZUC_ASSERT_EQ(timespec_to_usec(&a), (4000000ULL) + 4); +} + ZUC_TEST(timespec_test, timespec_to_msec) { struct timespec a; @@ -165,6 +174,69 @@ ZUC_TEST(timespec_test, timespec_sub_to_msec) ZUC_ASSERT_EQ((998 * 1000) + 1, timespec_sub_to_msec(&a, &b)); } +ZUC_TEST(timespec_test, timespec_from_nsec) +{ + struct timespec a; + + timespec_from_nsec(&a, 0); + ZUC_ASSERT_EQ(0, a.tv_sec); + ZUC_ASSERT_EQ(0, a.tv_nsec); + + timespec_from_nsec(&a, NSEC_PER_SEC - 1); + ZUC_ASSERT_EQ(0, a.tv_sec); + ZUC_ASSERT_EQ(NSEC_PER_SEC - 1, a.tv_nsec); + + timespec_from_nsec(&a, NSEC_PER_SEC); + ZUC_ASSERT_EQ(1, a.tv_sec); + ZUC_ASSERT_EQ(0, a.tv_nsec); + + timespec_from_nsec(&a, (5L * NSEC_PER_SEC) + 1); + ZUC_ASSERT_EQ(5, a.tv_sec); + ZUC_ASSERT_EQ(1, a.tv_nsec); +} + +ZUC_TEST(timespec_test, timespec_from_usec) +{ + struct timespec a; + + timespec_from_usec(&a, 0); + ZUC_ASSERT_EQ(0, a.tv_sec); + ZUC_ASSERT_EQ(0, a.tv_nsec); + + timespec_from_usec(&a, 999999); + ZUC_ASSERT_EQ(0, a.tv_sec); + ZUC_ASSERT_EQ(999999 * 1000, a.tv_nsec); + + timespec_from_usec(&a, 1000000); + ZUC_ASSERT_EQ(1, a.tv_sec); + ZUC_ASSERT_EQ(0, a.tv_nsec); + + timespec_from_usec(&a, 5000001); + ZUC_ASSERT_EQ(5, a.tv_sec); + ZUC_ASSERT_EQ(1000, a.tv_nsec); +} + +ZUC_TEST(timespec_test, timespec_from_msec) +{ + struct timespec a; + + timespec_from_msec(&a, 0); + ZUC_ASSERT_EQ(0, a.tv_sec); + ZUC_ASSERT_EQ(0, a.tv_nsec); + + timespec_from_msec(&a, 999); + ZUC_ASSERT_EQ(0, a.tv_sec); + ZUC_ASSERT_EQ(999 * 1000000, a.tv_nsec); + + timespec_from_msec(&a, 1000); + ZUC_ASSERT_EQ(1, a.tv_sec); + ZUC_ASSERT_EQ(0, a.tv_nsec); + + timespec_from_msec(&a, 5001); + ZUC_ASSERT_EQ(5, a.tv_sec); + ZUC_ASSERT_EQ(1000000, a.tv_nsec); +} + ZUC_TEST(timespec_test, timespec_is_zero) { struct timespec zero = { 0 }; -- 2.14.1 _______________________________________________ wayland-devel mailing list wayland-devel@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/wayland-devel