about summary refs log tree commit diff
path: root/src/libstd/sys/windows
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-03-12 20:36:31 -0700
committerAlex Crichton <alex@alexcrichton.com>2015-03-17 09:34:42 -0700
commit04cf5344111c357ad80335b88709281bb4bfaa0a (patch)
tree2a0788a1caed1c284a74330305cbf2aceebd5387 /src/libstd/sys/windows
parent970baad714d4119e5f93647163f197c9474d6ace (diff)
downloadrust-04cf5344111c357ad80335b88709281bb4bfaa0a.tar.gz
rust-04cf5344111c357ad80335b88709281bb4bfaa0a.zip
std: Implement `thread::sleep`
This function is the current replacement for `std::old_io::timer` which will
soon be deprecated. This function is unstable and has its own feature gate as it
does not yet have an RFC nor has it existed for very long.
Diffstat (limited to 'src/libstd/sys/windows')
-rw-r--r--src/libstd/sys/windows/thread.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/libstd/sys/windows/thread.rs b/src/libstd/sys/windows/thread.rs
index b361faba0f6..d1d4ad90081 100644
--- a/src/libstd/sys/windows/thread.rs
+++ b/src/libstd/sys/windows/thread.rs
@@ -20,6 +20,7 @@ use ptr;
 use sys_common::stack::RED_ZONE;
 use sys_common::thread::*;
 use thunk::Thunk;
+use time::Duration;
 
 pub type rust_thread = HANDLE;
 
@@ -82,6 +83,20 @@ pub unsafe fn yield_now() {
     SwitchToThread();
 }
 
+pub fn sleep(dur: Duration) {
+    unsafe {
+        if dur < Duration::zero() {
+            return yield_now()
+        }
+        let ms = dur.num_milliseconds();
+        // if we have a fractional number of milliseconds then add an extra
+        // millisecond to sleep for
+        let extra = dur - Duration::milliseconds(ms);
+        let ms = ms + if extra.is_zero() {0} else {1};
+        Sleep(ms as DWORD);
+    }
+}
+
 #[allow(non_snake_case)]
 extern "system" {
     fn CreateThread(lpThreadAttributes: LPSECURITY_ATTRIBUTES,
@@ -92,4 +107,5 @@ extern "system" {
                     lpThreadId: LPDWORD) -> HANDLE;
     fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
     fn SwitchToThread() -> BOOL;
+    fn Sleep(dwMilliseconds: DWORD);
 }