[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Add monotime functions for clearing monotonic times



commit 4c877ae87483d6e63c9e0309eb8abc009f9b9b87
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Wed Dec 13 08:28:04 2017 -0500

    Add monotime functions for clearing monotonic times
    
    We need this to replace some of our "msec" users with monotime
    users.
---
 src/common/compat_time.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 src/common/compat_time.h | 13 +++++++++++++
 src/test/test_util.c     | 23 +++++++++++++++++++++++
 3 files changed, 79 insertions(+)

diff --git a/src/common/compat_time.c b/src/common/compat_time.c
index c0cd73c74..fa8dbdfac 100644
--- a/src/common/compat_time.c
+++ b/src/common/compat_time.c
@@ -351,6 +351,12 @@ monotime_coarse_to_stamp(const monotime_coarse_t *t)
   return (uint32_t)(t->abstime_ >> monotime_shift);
 }
 
+int
+monotime_is_zero(const monotime_t *val)
+{
+  return val->abstime_ == 0;
+}
+
 /* end of "__APPLE__" */
 #elif defined(HAVE_CLOCK_GETTIME)
 
@@ -441,6 +447,12 @@ monotime_coarse_to_stamp(const monotime_coarse_t *t)
   return (sec * STAMP_TICKS_PER_SECOND) + (nsec >> 20);
 }
 
+int
+monotime_is_zero(const monotime_t *val)
+{
+  return val->ts_.tv_sec == 0 && val->ts_.tv_nsec == 0;
+}
+
 /* end of "HAVE_CLOCK_GETTIME" */
 #elif defined (_WIN32)
 
@@ -581,6 +593,18 @@ monotime_coarse_to_stamp(const monotime_coarse_t *t)
   return (uint32_t) t->tick_count_;
 }
 
+int
+monotime_is_zero(const monotime_t *val)
+{
+  return val->pcount_ == 0;
+}
+
+int
+monotime_coarse_is_zero(const monotime_coarse_t *val)
+{
+  return val->tick_count_ == 0;
+}
+
 /* end of "_WIN32" */
 #elif defined(MONOTIME_USING_GETTIMEOFDAY)
 
@@ -628,6 +652,12 @@ monotime_coarse_to_stamp(const monotime_coarse_t *t)
   return (sec * STAMP_TICKS_PER_SECOND) | (nsec >> 10);
 }
 
+int
+monotime_is_zero(const monotime_t *val)
+{
+  return val->tv_.tv_sec == 0 && val->tv_.tv_usec == 0;
+}
+
 /* end of "MONOTIME_USING_GETTIMEOFDAY" */
 #else
 #error "No way to implement monotonic timers."
@@ -650,6 +680,19 @@ monotime_init(void)
   }
 }
 
+void
+monotime_zero(monotime_t *out)
+{
+  memset(out, 0, sizeof(*out));
+}
+#ifdef MONOTIME_COARSE_TYPE_IS_DIFFERENT
+void
+monotime_coarse_zero(monotime_coarse_t *out)
+{
+  memset(out, 0, sizeof(*out));
+}
+#endif
+
 int64_t
 monotime_diff_usec(const monotime_t *start,
                    const monotime_t *end)
diff --git a/src/common/compat_time.h b/src/common/compat_time.h
index bcf469e27..68c11fb60 100644
--- a/src/common/compat_time.h
+++ b/src/common/compat_time.h
@@ -105,6 +105,15 @@ uint64_t monotime_absolute_usec(void);
  */
 uint64_t monotime_absolute_msec(void);
 
+/**
+ * Set <b>out</b> to zero.
+ */
+void monotime_zero(monotime_t *out);
+/**
+ * Return true iff <b>out</b> is zero
+ */
+int monotime_is_zero(const monotime_t *out);
+
 #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
 /**
  * Set <b>out</b> to the current coarse time.
@@ -144,10 +153,14 @@ int64_t monotime_coarse_diff_usec(const monotime_coarse_t *start,
     const monotime_coarse_t *end);
 int64_t monotime_coarse_diff_msec(const monotime_coarse_t *start,
     const monotime_coarse_t *end);
+void monotime_coarse_zero(monotime_coarse_t *out);
+int monotime_coarse_is_zero(const monotime_coarse_t *val);
 #else /* !(defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)) */
 #define monotime_coarse_diff_nsec monotime_diff_nsec
 #define monotime_coarse_diff_usec monotime_diff_usec
 #define monotime_coarse_diff_msec monotime_diff_msec
+#define monotime_coarse_zero monotime_zero
+#define monotime_coarse_is_zero monotime_is_zero
 #endif /* defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT) */
 
 void tor_gettimeofday(struct timeval *timeval);
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 7856b506b..7210f1674 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -5926,6 +5926,28 @@ test_util_monotonic_time_ratchet(void *arg)
 }
 
 static void
+test_util_monotonic_time_zero(void *arg)
+{
+  (void) arg;
+  monotime_t t1;
+  monotime_coarse_t ct1;
+  monotime_init();
+  /* Check 1: The current time is not zero. */
+  monotime_get(&t1);
+  monotime_coarse_get(&ct1);
+  tt_assert(!monotime_is_zero(&t1));
+  tt_assert(!monotime_coarse_is_zero(&ct1));
+
+  /* Check 2: The _zero() makes the time zero. */
+  monotime_zero(&t1);
+  monotime_coarse_zero(&ct1);
+  tt_assert(monotime_is_zero(&t1));
+  tt_assert(monotime_coarse_is_zero(&ct1));
+ done:
+  ;
+}
+
+static void
 test_util_htonll(void *arg)
 {
   (void)arg;
@@ -6158,6 +6180,7 @@ struct testcase_t util_tests[] = {
   UTIL_TEST(calloc_check, 0),
   UTIL_TEST(monotonic_time, 0),
   UTIL_TEST(monotonic_time_ratchet, TT_FORK),
+  UTIL_TEST(monotonic_time_zero, 0),
   UTIL_TEST(htonll, 0),
   UTIL_TEST(get_unquoted_path, 0),
   END_OF_TESTCASES



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits