summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2014-11-26 23:41:33 (GMT)
committer Vincent Sanders <vince@kyllikki.org>2014-11-26 23:41:33 (GMT)
commitb8e45926f66fd928bccf2b03377f20788f52774b (patch)
tree0f1d4aa8f322160c5f6e0852ca90ddc3460a6346
parent5470716c0d36351175407aa833c86495195569b8 (diff)
downloadlibnsutils-b8e45926f66fd928bccf2b03377f20788f52774b.tar.gz
libnsutils-b8e45926f66fd928bccf2b03377f20788f52774b.tar.bz2
move to using POSIX monotonic clock if available
-rw-r--r--Makefile1
-rw-r--r--src/time.c10
2 files changed, 10 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index be20bdd..7d3f128 100644
--- a/Makefile
+++ b/Makefile
@@ -23,6 +23,7 @@ else
# __inline__ is a GCCism
CFLAGS := $(CFLAGS) -Dinline="__inline__"
endif
+CFLAGS := $(CFLAGS) -D_POSIX_C_SOURCE=200809L
TESTCFLAGGS := -g -O2
TESTLDFLAGS := -lm -l$(COMPONENT) $(TESTLDFLAGS)
diff --git a/src/time.c b/src/time.c
index b29c2eb..582bc9d 100644
--- a/src/time.c
+++ b/src/time.c
@@ -14,19 +14,27 @@
#include <stdint.h>
#include <stdlib.h>
+#include <unistd.h>
#include <sys/time.h>
+#include <time.h>
#include "nsutils/time.h"
/* exported interface documented in nsutils/time.h */
nsuerror nsu_getmonotonic_ms(uint64_t *current)
{
+#if (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)
+ struct timespec tp;
+ clock_gettime(CLOCK_MONOTONIC, &tp);
+ *current = (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000);
+#else
+#warning "Using dodgy gettimeofday() fallback"
/** \todo Implement this properly! */
struct timeval tv;
gettimeofday(&tv, NULL);
*current = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
-
+#endif
return NSUERROR_OK;
}