about summary refs log tree commit diff
path: root/src/rt/sync/timer.cpp
diff options
context:
space:
mode:
authorMichael Bebenita <mbebenita@mozilla.com>2010-08-11 21:23:34 -0700
committerMichael Bebenita <mbebenita@mozilla.com>2010-08-11 21:24:04 -0700
commit988695a96cee1eb825435260a1874b8daa0e590a (patch)
treeee92e117a653c8c6fad100e7416afe5468073ff3 /src/rt/sync/timer.cpp
parent88d9a79ac8b05c5631efeef6a70dec35480ecaab (diff)
downloadrust-988695a96cee1eb825435260a1874b8daa0e590a.tar.gz
rust-988695a96cee1eb825435260a1874b8daa0e590a.zip
Added support for task sleeping in the scheduler.
Diffstat (limited to 'src/rt/sync/timer.cpp')
-rw-r--r--src/rt/sync/timer.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/rt/sync/timer.cpp b/src/rt/sync/timer.cpp
new file mode 100644
index 00000000000..e45dd633314
--- /dev/null
+++ b/src/rt/sync/timer.cpp
@@ -0,0 +1,61 @@
+#include "../globals.h"
+#include "timer.h"
+
+#if defined(__APPLE__)
+#include <mach/mach_time.h>
+#endif
+
+timer::timer() {
+    reset(0);
+#if __WIN32__
+    uint64_t ticks_per_second;
+    QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_second);
+    _ticks_per_us = ticks_per_second / 1000000;
+#endif
+}
+
+void
+timer::reset(uint64_t timeout) {
+    _start = get_time();
+    _timeout = timeout;
+}
+
+uint64_t
+timer::get_elapsed_time() {
+    return get_time() - _start;
+}
+
+int64_t
+timer::get_timeout() {
+    return _timeout - get_elapsed_time();
+}
+
+bool
+timer::has_timed_out() {
+    return get_timeout() <= 0;
+}
+
+uint64_t
+timer::get_time() {
+#ifdef __APPLE__
+    uint64_t time = mach_absolute_time();
+    mach_timebase_info_data_t info = {0, 0};
+    if (info.denom == 0) {
+        mach_timebase_info(&info);
+    }
+    uint64_t time_nano = time * (info.numer / info.denom);
+    return time_nano / 1000;
+#elif __WIN32__
+    uint64_t ticks;
+    QueryPerformanceCounter((LARGE_INTEGER *)&ticks);
+    return ticks / _ticks_per_us;
+#else
+    timespec ts;
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    return (ts.tv_sec * 1000000000LL + ts.tv_nsec) / 1000;
+#endif
+}
+
+timer::~timer() {
+    // Nop.
+}