]> git.ozlabs.org Git - ccan/blobdiff - ccan/time/time.h
fdpass: fix complilation on FreeBSD.
[ccan] / ccan / time / time.h
index 70ebdc9a7751ca5be434329421618c58f177c9c7..cbfeefa055c04daff279a3a976bc5adbeda6059d 100644 (file)
@@ -42,7 +42,7 @@ struct timerel {
 };
 
 /**
- * struct timeabs - an absolue time.
+ * struct timeabs - an absolute time.
  * @ts: the actual timespec value.
  *
  * For example, Midnight UTC January 1st, 1970: ts.tv_sec = 0, ts.tv_nsec = 0
@@ -71,6 +71,8 @@ struct timemono {
  */
 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
 #define TIME_HAVE_MONOTONIC 1
+#else
+#define TIME_HAVE_MONOTONIC 0
 #endif
 
 struct timespec time_check_(struct timespec in, const char *abortstr);
@@ -191,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)
@@ -218,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.
@@ -402,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;
@@ -486,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)
  *     {