diff options
Diffstat (limited to 'src/rt/sync')
| -rw-r--r-- | src/rt/sync/sync.cpp | 12 | ||||
| -rw-r--r-- | src/rt/sync/sync.h | 2 | ||||
| -rw-r--r-- | src/rt/sync/timer.cpp | 5 | ||||
| -rw-r--r-- | src/rt/sync/timer.h | 1 |
4 files changed, 20 insertions, 0 deletions
diff --git a/src/rt/sync/sync.cpp b/src/rt/sync/sync.cpp index c754392a..be874a11 100644 --- a/src/rt/sync/sync.cpp +++ b/src/rt/sync/sync.cpp @@ -11,6 +11,18 @@ void sync::yield() { #endif } +void sync::sleep(size_t timeout_in_ms) { +#ifdef __WIN32__ + Sleep(timeout_in_ms); +#else + usleep(timeout_in_ms * 1000); +#endif +} + +void sync::random_sleep(size_t max_timeout_in_ms) { + sleep(rand() % max_timeout_in_ms); +} + rust_thread::rust_thread() : _is_running(false), thread(0) { // Nop. } diff --git a/src/rt/sync/sync.h b/src/rt/sync/sync.h index 3829dafe..bd755e1f 100644 --- a/src/rt/sync/sync.h +++ b/src/rt/sync/sync.h @@ -4,6 +4,8 @@ class sync { public: static void yield(); + static void sleep(size_t timeout_in_ms); + static void random_sleep(size_t max_timeout_in_ms); template <class T> static bool compare_and_swap(T *address, T oldValue, T newValue) { diff --git a/src/rt/sync/timer.cpp b/src/rt/sync/timer.cpp index 0487b397..e6fe3688 100644 --- a/src/rt/sync/timer.cpp +++ b/src/rt/sync/timer.cpp @@ -25,6 +25,11 @@ timer::get_elapsed_time() { return get_time() - _start; } +double +timer::get_elapsed_time_in_ms() { + return (double) get_elapsed_time() / 1000.0; +} + int64_t timer::get_timeout() { return _timeout - get_elapsed_time(); diff --git a/src/rt/sync/timer.h b/src/rt/sync/timer.h index 509fd22c..aae098a1 100644 --- a/src/rt/sync/timer.h +++ b/src/rt/sync/timer.h @@ -17,6 +17,7 @@ public: timer(); void reset(uint64_t timeout); uint64_t get_elapsed_time(); + double get_elapsed_time_in_ms(); int64_t get_timeout(); bool has_timed_out(); virtual ~timer(); |