summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2014-11-27 08:46:56 (GMT)
committer Vincent Sanders <vince@kyllikki.org>2014-11-27 08:46:56 (GMT)
commit0f0358f2856e594fbbd0d3eed9725d1017cd120f (patch)
tree956ef5ab05a97f4a9bd15954eac41e217c103f5f
parentb8e45926f66fd928bccf2b03377f20788f52774b (diff)
downloadlibnsutils-0f0358f2856e594fbbd0d3eed9725d1017cd120f.tar.gz
libnsutils-0f0358f2856e594fbbd0d3eed9725d1017cd120f.tar.bz2
add fallback for RISC OS monotonic timer
-rw-r--r--src/time.c32
1 files changed, 26 insertions, 6 deletions
diff --git a/src/time.c b/src/time.c
index 582bc9d..1753ad4 100644
--- a/src/time.c
+++ b/src/time.c
@@ -15,26 +15,46 @@
#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)
+nsuerror nsu_getmonotonic_ms(uint64_t *current_out)
{
-#if (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)
+ uint64_t current;
+ static uint64_t prev = 0; /* previous time so we never go backwards */
+
+#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)
+#include <time.h>
struct timespec tp;
+
clock_gettime(CLOCK_MONOTONIC, &tp);
- *current = (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000);
+ current = (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000);
+#elif defined(riscos)
+#include "oslib/os.h"
+ os_t time;
+
+ time = os_read_monotonic_time();
+ current = time * 10;
#else
#warning "Using dodgy gettimeofday() fallback"
+#include <sys/time.h>
/** \todo Implement this properly! */
struct timeval tv;
gettimeofday(&tv, NULL);
- *current = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
+ current = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
#endif
+
+ /* ensure time never goes backwards */
+ if (current > prev) {
+ *current_out = current;
+ prev = current;
+ } else {
+ /** \todo is 10ms really correct or can we calculate a delta going forwards? */
+ prev += 10;
+ *current_out = prev;
+ }
return NSUERROR_OK;
}