From 93f417fbf3aa358ffff40530b6cf5d2446dcd8b7 Mon Sep 17 00:00:00 2001 From: Bjoern Brandenburg Date: Wed, 16 Dec 2015 10:49:23 +0100 Subject: Add monotime() wrapper for CLOCK_MONOTONIC --- include/litmus.h | 6 ++++++ src/clocks.c | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/litmus.h b/include/litmus.h index 72f1ad4..2c5d4df 100644 --- a/include/litmus.h +++ b/include/litmus.h @@ -330,6 +330,12 @@ double cputime(void); */ double wctime(void); +/** + * Obtain CLOCK_MONOTONIC time + * @return CLOCK_MONOTONIC time in seconds + */ +double monotime(void); + /***** semaphore allocation ******/ /** * Allocate a semaphore following the FMLP protocol diff --git a/src/clocks.c b/src/clocks.c index 74c2fb4..5af4c1f 100644 --- a/src/clocks.c +++ b/src/clocks.c @@ -24,6 +24,17 @@ double wctime(void) return (tv.tv_sec + 1E-6 * tv.tv_usec); } +/* current time, according to CLOCK_MONOTONIC */ +double monotime(void) +{ + struct timespec ts; + int err; + err = clock_gettime(CLOCK_MONOTONIC, &ts); + if (err != 0) + perror("clock_gettime"); + return (ts.tv_sec + 1E-9 * ts.tv_nsec); +} + int lt_sleep(lt_t timeout) { struct timespec delay; -- cgit v1.2.2