summaryrefslogtreecommitdiffstats
path: root/src/time.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/time.c')
-rw-r--r--src/time.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/time.c b/src/time.c
new file mode 100644
index 0000000..2147c52
--- /dev/null
+++ b/src/time.c
@@ -0,0 +1,36 @@
+#include "jemalloc/internal/jemalloc_internal.h"
+
+bool
+time_update(struct timespec *time)
+{
+ struct timespec old_time;
+
+ memcpy(&old_time, time, sizeof(struct timespec));
+
+#ifdef _WIN32
+ FILETIME ft;
+ uint64_t ticks;
+ GetSystemTimeAsFileTime(&ft);
+ ticks = (ft.dwHighDateTime << 32) | ft.dWLowDateTime;
+ time->tv_sec = ticks / 10000;
+ time->tv_nsec = ((ticks % 10000) * 100);
+#elif JEMALLOC_CLOCK_GETTIME
+ if (sysconf(_SC_MONOTONIC_CLOCK) > 0)
+ clock_gettime(CLOCK_MONOTONIC, time);
+ else
+ clock_gettime(CLOCK_REALTIME, time);
+#else
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ time->tv_sec = tv.tv_sec;
+ time->tv_nsec = tv.tv_usec * 1000;
+#endif
+
+ /* Handle non-monotonic clocks. */
+ if (unlikely(old_time.tv_sec > time->tv_sec))
+ return (true);
+ if (unlikely(old_time.tv_sec == time->tv_sec))
+ return old_time.tv_nsec > time->tv_nsec;
+
+ return (false);
+}