]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/thread.rs
check: Reword the warning to be more prescriptive
[rust.git] / src / libstd / sys / windows / thread.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use prelude::v1::*;
12
13 use cmp;
14 use io;
15 use libc::{self, c_void};
16 use libc::types::os::arch::extra::{LPSECURITY_ATTRIBUTES, SIZE_T, BOOL,
17                                    LPVOID, DWORD, LPDWORD, HANDLE};
18 use mem;
19 use ptr;
20 use sys_common::stack::RED_ZONE;
21 use sys_common::thread::*;
22 use thunk::Thunk;
23 use time::Duration;
24
25 pub type rust_thread = HANDLE;
26
27 pub mod guard {
28     pub unsafe fn main() -> uint { 0 }
29     pub unsafe fn current() -> uint { 0 }
30     pub unsafe fn init() {}
31 }
32
33 pub unsafe fn create(stack: usize, p: Thunk) -> io::Result<rust_thread> {
34     let p = box p;
35     // FIXME On UNIX, we guard against stack sizes that are too small but
36     // that's because pthreads enforces that stacks are at least
37     // PTHREAD_STACK_MIN bytes big.  Windows has no such lower limit, it's
38     // just that below a certain threshold you can't do anything useful.
39     // That threshold is application and architecture-specific, however.
40     // For now, the only requirement is that it's big enough to hold the
41     // red zone.  Round up to the next 64 kB because that's what the NT
42     // kernel does, might as well make it explicit.  With the current
43     // 20 kB red zone, that makes for a 64 kB minimum stack.
44     let stack_size = (cmp::max(stack, RED_ZONE) + 0xfffe) & (-0xfffe - 1);
45     let ret = CreateThread(ptr::null_mut(), stack_size as libc::size_t,
46                            thread_start, &*p as *const _ as *mut _,
47                            0, ptr::null_mut());
48
49     return if ret as usize == 0 {
50         Err(io::Error::last_os_error())
51     } else {
52         mem::forget(p); // ownership passed to CreateThread
53         Ok(ret)
54     };
55
56     #[no_stack_check]
57     extern "system" fn thread_start(main: *mut libc::c_void) -> DWORD {
58         start_thread(main);
59         0
60     }
61 }
62
63 pub unsafe fn set_name(_name: &str) {
64     // Windows threads are nameless
65     // The names in MSVC debugger are obtained using a "magic" exception,
66     // which requires a use of MS C++ extensions.
67     // See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
68 }
69
70 pub unsafe fn join(native: rust_thread) {
71     use libc::consts::os::extra::INFINITE;
72     WaitForSingleObject(native, INFINITE);
73 }
74
75 pub unsafe fn detach(native: rust_thread) {
76     assert!(libc::CloseHandle(native) != 0);
77 }
78
79 pub unsafe fn yield_now() {
80     // This function will return 0 if there are no other threads to execute,
81     // but this also means that the yield was useless so this isn't really a
82     // case that needs to be worried about.
83     SwitchToThread();
84 }
85
86 pub fn sleep(dur: Duration) {
87     unsafe {
88         if dur < Duration::zero() {
89             return yield_now()
90         }
91         let ms = dur.num_milliseconds();
92         // if we have a fractional number of milliseconds then add an extra
93         // millisecond to sleep for
94         let extra = dur - Duration::milliseconds(ms);
95         let ms = ms + if extra.is_zero() {0} else {1};
96         Sleep(ms as DWORD);
97     }
98 }
99
100 #[allow(non_snake_case)]
101 extern "system" {
102     fn CreateThread(lpThreadAttributes: LPSECURITY_ATTRIBUTES,
103                     dwStackSize: SIZE_T,
104                     lpStartAddress: extern "system" fn(*mut c_void) -> DWORD,
105                     lpParameter: LPVOID,
106                     dwCreationFlags: DWORD,
107                     lpThreadId: LPDWORD) -> HANDLE;
108     fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
109     fn SwitchToThread() -> BOOL;
110     fn Sleep(dwMilliseconds: DWORD);
111 }