]> git.ozlabs.org Git - ccan/blobdiff - ccan/time/time.h
time: timemono_add.
[ccan] / ccan / time / time.h
index 46568425c7cd44594e3964d3f8435f316d11eed4..f2b335cf45de0af387c2b0a9e1cba09c73ceb86a 100644 (file)
@@ -66,7 +66,7 @@ struct timemono {
  * Otherwise time_mono() just refers to time_now().  Your code might
  * test this if you really need a monotonic clock.
  */
-#if (HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_IN_LIBRT) && defined(CLOCK_MONOTONIC)
+#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
 #define TIME_HAVE_MONOTONIC 1
 #endif
 
@@ -127,7 +127,7 @@ struct timeabs time_now(void);
  * This value is only really useful for measuring time intervals.
  *
  * See also:
- *     time_since()
+ *     timemono_since()
  */
 struct timemono time_mono(void);
 
@@ -249,7 +249,7 @@ static inline bool timeabs_eq(struct timeabs a, struct timeabs b)
  *                     exit(0);
  *             }
  *             wait(NULL);
- *             diff = time_between(start, time_now());
+ *             diff = time_between(time_now(), start);
  *             return timerel_eq(diff, zero);
  *     }
  */
@@ -320,6 +320,17 @@ static inline struct timerel timemono_between(struct timemono recent,
        return t;
 }
 
+/**
+ * timemono_since - elapsed monotonic time since @old
+ * @old: a monotonic time from the past.
+ */
+static inline struct timerel timemono_since(struct timemono old)
+{
+       struct timemono now = time_mono();
+
+       return timemono_between(now, old);
+}
+
 /**
  * timeabs_sub - subtract a relative time from an absolute time
  * @abs: the absolute time.
@@ -377,6 +388,28 @@ static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
        return t;
 }
 
+/**
+ * timemono_add - add a relative to a monotonic time
+ * @a: the monotonic time.
+ * @b: a relative time.
+ *
+ * The times must not overflow, or the results are undefined.
+ *
+ * Example:
+ *     // We do one every second.
+ *     static struct timemono next_timem(void)
+ *     {
+ *             return timemono_add(time_mono(), time_from_msec(1000));
+ *     }
+ */
+static inline struct timemono timemono_add(struct timemono a, struct timerel b)
+{
+       struct timemono t;
+
+       t.ts = time_add_(a.ts, b.ts);
+       return t;
+}
+
 /**
  * timerel_add - add two relative times
  * @a: one relative time.