[PATCH weston v2 01/12] shared: Add timespec_is_zero helper

Alexandros Frantzis alexandros.frantzis at collabora.com
Mon Nov 27 08:54:49 UTC 2017


Add a helper function to check if a struct timespec is zero. This helper
will be used in the upcoming commits to transition the Weston codebase
to struct timespec.

Signed-off-by: Alexandros Frantzis <alexandros.frantzis at collabora.com>
---

Changes in v2:
 - Use designated initializers to initialize members of struct timespec
   in test.

 shared/timespec-util.h | 12 ++++++++++++
 tests/timespec-test.c  | 11 +++++++++++
 2 files changed, 23 insertions(+)

diff --git a/shared/timespec-util.h b/shared/timespec-util.h
index 34a120ae..7260dc8b 100644
--- a/shared/timespec-util.h
+++ b/shared/timespec-util.h
@@ -29,6 +29,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <time.h>
+#include <stdbool.h>
 
 #define NSEC_PER_SEC 1000000000
 
@@ -133,6 +134,17 @@ timespec_sub_to_msec(const struct timespec *a, const struct timespec *b)
 	return timespec_sub_to_nsec(a, b) / 1000000;
 }
 
+/* Check if a timespec is zero
+ *
+ * \param a timespec
+ * \return whether the timespec is zero
+ */
+static inline bool
+timespec_is_zero(const struct timespec *a)
+{
+	return a->tv_sec == 0 && a->tv_nsec == 0;
+}
+
 /* Convert milli-Hertz to nanoseconds
  *
  * \param mhz frequency in mHz, not zero
diff --git a/tests/timespec-test.c b/tests/timespec-test.c
index a5039110..4e83605d 100644
--- a/tests/timespec-test.c
+++ b/tests/timespec-test.c
@@ -164,3 +164,14 @@ ZUC_TEST(timespec_test, timespec_sub_to_msec)
 	b.tv_nsec = 1000000L;
 	ZUC_ASSERT_EQ((998 * 1000) + 1, timespec_sub_to_msec(&a, &b));
 }
+
+ZUC_TEST(timespec_test, timespec_is_zero)
+{
+	struct timespec zero = { 0 };
+	struct timespec non_zero_sec = { .tv_sec = 1, .tv_nsec = 0 };
+	struct timespec non_zero_nsec = { .tv_sec = 0, .tv_nsec = 1 };
+
+	ZUC_ASSERT_TRUE(timespec_is_zero(&zero));
+	ZUC_ASSERT_FALSE(timespec_is_zero(&non_zero_nsec));
+	ZUC_ASSERT_FALSE(timespec_is_zero(&non_zero_sec));
+}
-- 
2.14.1



More information about the wayland-devel mailing list