X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftime%2Ftime.h;h=5c916f4c4c2ef04b9648791d862bb3eea731a6cd;hb=c8e75cdce11b3ad7db6c1fff580c587395b59965;hp=7bb3547d6227630fb5eb682544dc7e2eb4c2e6aa;hpb=c92b496623f29cfb3ac1ade8e52ffbfa95ab5bcf;p=ccan diff --git a/ccan/time/time.h b/ccan/time/time.h index 7bb3547d..5c916f4c 100644 --- a/ccan/time/time.h +++ b/ccan/time/time.h @@ -48,6 +48,28 @@ struct timeabs { struct timespec ts; }; +/** + * struct timemono - a monotonic time. + * @ts: the actual timespec value. + * + * This comes from the monotonic clock (if available), so it's useful + * for measuring intervals as it won't change even if the system clock + * is moved for some reason. + */ +struct timemono { + struct timespec ts; +}; + +/** + * TIME_HAVE_MONOTONIC - defined if we really have a monotonic clock. + * + * Otherwise time_mono() just refers to time_now(). Your code might + * test this if you really need a monotonic clock. + */ +#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC) +#define TIME_HAVE_MONOTONIC 1 +#endif + struct timespec time_check_(struct timespec in, const char *abortstr); /** @@ -99,6 +121,16 @@ struct timeabs timeabs_check(struct timeabs in, const char *abortstr); */ struct timeabs time_now(void); +/** + * time_mono - return the current monotonic time + * + * This value is only really useful for measuring time intervals. + * + * See also: + * time_since() + */ +struct timemono time_mono(void); + static inline bool time_greater_(struct timespec a, struct timespec b) { if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec) @@ -217,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); * } */ @@ -272,6 +304,22 @@ static inline struct timerel time_between(struct timeabs recent, struct timeabs return t; } +/** + * timemono_between - time between two monotonic times + * @recent: the larger time. + * @old: the smaller time. + * + * This returns a well formed struct timerel of @recent - @old. + */ +static inline struct timerel timemono_between(struct timemono recent, + struct timemono old) +{ + struct timerel t; + + t.ts = time_sub_(recent.ts, old.ts); + return t; +} + /** * timeabs_sub - subtract a relative time from an absolute time * @abs: the absolute time.