]> git.ozlabs.org Git - ccan/commitdiff
time: add timemono_after, fix example compilation.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 30 Apr 2025 01:55:57 +0000 (11:25 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 30 Apr 2025 01:55:57 +0000 (11:25 +0930)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/time/test/run-monotonic.c
ccan/time/time.h

index 878d62496a5d40dc45228307fa9b462ad3e90ab1..5eded26c91f19aab8e209c9f1c5125279964ad9e 100644 (file)
@@ -7,7 +7,7 @@ int main(void)
        struct timemono t1, t2;
        struct timerel t3;
 
-       plan_tests(7);
+       plan_tests(10);
 
        /* Test time_mono */
        t1 = time_mono();
@@ -26,6 +26,9 @@ int main(void)
 
        ok1(timemono_sub(timemono_add(t1, t3), t3).ts.tv_sec == t1.ts.tv_sec);
        ok1(timemono_sub(timemono_add(t1, t3), t3).ts.tv_nsec == t1.ts.tv_nsec);
-       
+
+       ok1(timemono_after(timemono_add(t1, t3), t1));
+       ok1(!timemono_after(t1, timemono_add(t1, t3)));
+       ok1(!timemono_after(t1, t1));
        return exit_status();
 }
index 48b8d1d2a4c8274deebb1616e4c0ca489d8ca2b2..cbfeefa055c04daff279a3a976bc5adbeda6059d 100644 (file)
@@ -193,6 +193,23 @@ static inline bool time_greater(struct timerel a, struct timerel b)
        return time_greater_(a.ts, b.ts);
 }
 
+/**
+ * timemono_after - is a after b?
+ * @a: one monotonic time.
+ * @b: another monotonic time.
+ *
+ * Example:
+ *     static bool timed_out(const struct timemono *start)
+ *     {
+ *     #define TIMEOUT time_from_msec(1000)
+ *             return timemono_after(time_mono(), timemono_add(*start, TIMEOUT));
+ *     }
+ */
+static inline bool timemono_after(struct timemono a, struct timemono b)
+{
+       return time_greater_(a.ts, b.ts);
+}
+
 static inline bool time_less_(struct timespec a, struct timespec b)
 {
        if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
@@ -528,6 +545,8 @@ static inline struct timerel timerel_add(struct timerel a, struct timerel b)
  * @div: number to divide it by.
  *
  * Example:
+ *     #include <sys/wait.h>
+ *
  *     // How long does it take to do a fork?
  *     static struct timerel forking_time(void)
  *     {