parlib: Add an alarm helper
authorBarret Rhoden <brho@cs.berkeley.edu>
Fri, 7 Apr 2017 15:20:02 +0000 (11:20 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Wed, 3 May 2017 16:13:02 +0000 (12:13 -0400)
Anyone working with timespecs and parlib/alarm.h will use this.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
user/parlib/include/parlib/alarm.h

index 682cffa..3068df0 100644 (file)
@@ -77,6 +77,15 @@ void set_awaiter_abs_unix(struct alarm_waiter *waiter, uint64_t abs_usec);
 void set_awaiter_rel(struct alarm_waiter *waiter, uint64_t usleep);
 void set_awaiter_inc(struct alarm_waiter *waiter, uint64_t usleep);
 
+/* Helper: converts a timespec to the units of the #alarm service (usec).  A
+ * common usage:
+ *             set_awaiter_abs_unix(w, timespec_to_alarm_time(abs_ts));
+ */
+static uint64_t timespec_to_alarm_time(const struct timespec *ts)
+{
+       return ts->tv_nsec / 1000 + ts->tv_sec * 1000000ULL;
+}
+
 /* Arms/disarms the alarm */
 void __set_alarm(struct alarm_waiter *waiter);
 void set_alarm(struct alarm_waiter *waiter);