diff --git a/ccan/README b/ccan/README index eaf436160..926a82363 100644 --- a/ccan/README +++ b/ccan/README @@ -1,3 +1,3 @@ CCAN imported from http://ccodearchive.net. -CCAN version: init-2590-gaf04734d +CCAN version: init-2593-gca094039 diff --git a/ccan/ccan/fdpass/fdpass.c b/ccan/ccan/fdpass/fdpass.c index 7331468f9..af1a7fdb9 100644 --- a/ccan/ccan/fdpass/fdpass.c +++ b/ccan/ccan/fdpass/fdpass.c @@ -3,6 +3,7 @@ #include #include #include +#include bool fdpass_send(int sockout, int fd) { diff --git a/ccan/ccan/time/test/run-monotonic.c b/ccan/ccan/time/test/run-monotonic.c index 2da492d44..5eded26c9 100644 --- a/ccan/ccan/time/test/run-monotonic.c +++ b/ccan/ccan/time/test/run-monotonic.c @@ -7,13 +7,13 @@ int main(void) struct timemono t1, t2; struct timerel t3; - plan_tests(5); + plan_tests(10); /* Test time_mono */ t1 = time_mono(); t2 = time_mono(); - ok1(!time_less_(t2.ts, t1.ts)); + ok1(!timemono_before(t2, t1)); t3.ts.tv_sec = 1; t3.ts.tv_nsec = 0; @@ -24,5 +24,11 @@ int main(void) ok1(timemono_add(t1, t3).ts.tv_sec == t1.ts.tv_sec + 1); ok1(timemono_add(t2, t3).ts.tv_nsec == t2.ts.tv_nsec); + 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/ccan/time/time.h b/ccan/ccan/time/time.h index 2fc8161e7..cbfeefa05 100644 --- a/ccan/ccan/time/time.h +++ b/ccan/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) @@ -220,6 +237,23 @@ static inline bool time_before(struct timeabs a, struct timeabs b) return time_less_(a.ts, b.ts); } +/** + * timemono_before - is a before b? + * @a: one monotonic time. + * @b: another monotonic time. + * + * Example: + * static bool still_valid(const struct timemono *start) + * { + * #define TIMEOUT time_from_msec(1000) + * return timemono_before(time_mono(), timemono_add(*start, TIMEOUT)); + * } + */ +static inline bool timemono_before(struct timemono a, struct timemono b) +{ + return time_less_(a.ts, b.ts); +} + /** * time_less - is a before b? * @a: one relative time. @@ -404,6 +438,29 @@ static inline struct timeabs timeabs_sub(struct timeabs abs, struct timerel rel) return t; } +/** + * timemono_sub - subtract a relative time from a monotonic time + * @mono: the monotonic time. + * @rel: the relative time. + * + * This returns a well formed struct timemono of @mono - @rel. + * + * Example: + * // We do one every second. + * static struct timemono previous_time(void) + * { + * return timemono_sub(time_mono(), time_from_msec(1000)); + * } + */ +static inline struct timemono timemono_sub(struct timemono mono, struct timerel rel) +{ + struct timemono t; + + t.ts = time_sub_(mono.ts, rel.ts); + return t; +} + + static inline struct timespec time_add_(struct timespec a, struct timespec b) { struct timespec sum; @@ -488,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) * {