From: Rusty Russell Date: Wed, 30 Apr 2025 01:55:57 +0000 (+0930) Subject: time: add timemono_after, fix example compilation. X-Git-Url: https://git.ozlabs.org/?a=commitdiff_plain;h=734087370994b06caf4b91eb4027e039e15dd77a;p=ccan time: add timemono_after, fix example compilation. Signed-off-by: Rusty Russell --- diff --git a/ccan/time/test/run-monotonic.c b/ccan/time/test/run-monotonic.c index 878d6249..5eded26c 100644 --- a/ccan/time/test/run-monotonic.c +++ b/ccan/time/test/run-monotonic.c @@ -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(); } diff --git a/ccan/time/time.h b/ccan/time/time.h index 48b8d1d2..cbfeefa0 100644 --- a/ccan/time/time.h +++ b/ccan/time/time.h @@ -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 + * * // How long does it take to do a fork? * static struct timerel forking_time(void) * {