]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/thread.rs
85588cc6c8e85e074e3d3d52c085a9defbb46a16
[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 boxed::FnBox;
12 use io;
13 use ffi::CStr;
14 use mem;
15 use libc::c_void;
16 use ptr;
17 use sys::c;
18 use sys::handle::Handle;
19 use sys_common::thread::*;
20 use time::Duration;
21
22 use super::to_u16s;
23
24 pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
25
26 pub struct Thread {
27     handle: Handle
28 }
29
30 impl Thread {
31     pub unsafe fn new<'a>(stack: usize, p: Box<dyn FnBox() + 'a>)
32                           -> io::Result<Thread> {
33         let p = box p;
34
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         // Round up to the next 64 kB because that's what the NT kernel does,
41         // might as well make it explicit.
42         let stack_size = (stack + 0xfffe) & (!0xfffe);
43         let ret = c::CreateThread(ptr::null_mut(), stack_size,
44                                   thread_start, &*p as *const _ as *mut _,
45                                   c::STACK_SIZE_PARAM_IS_A_RESERVATION,
46                                   ptr::null_mut());
47
48         return if ret as usize == 0 {
49             Err(io::Error::last_os_error())
50         } else {
51             mem::forget(p); // ownership passed to CreateThread
52             Ok(Thread { handle: Handle::new(ret) })
53         };
54
55         extern "system" fn thread_start(main: *mut c_void) -> c::DWORD {
56             unsafe { start_thread(main as *mut u8); }
57             0
58         }
59     }
60
61     pub fn set_name(name: &CStr) {
62         if let Ok(utf8) = name.to_str() {
63             if let Ok(utf16) = to_u16s(utf8) {
64                 unsafe { c::SetThreadDescription(c::GetCurrentThread(), utf16.as_ptr()); };
65             };
66         };
67     }
68
69     pub fn join(self) {
70         let rc = unsafe { c::WaitForSingleObject(self.handle.raw(), c::INFINITE) };
71         if rc == c::WAIT_FAILED {
72             panic!("failed to join on thread: {}",
73                    io::Error::last_os_error());
74         }
75     }
76
77     pub fn yield_now() {
78         // This function will return 0 if there are no other threads to execute,
79         // but this also means that the yield was useless so this isn't really a
80         // case that needs to be worried about.
81         unsafe { c::SwitchToThread(); }
82     }
83
84     pub fn sleep(dur: Duration) {
85         unsafe {
86             c::Sleep(super::dur2timeout(dur))
87         }
88     }
89
90     pub fn handle(&self) -> &Handle { &self.handle }
91
92     pub fn into_handle(self) -> Handle { self.handle }
93 }
94
95 #[cfg_attr(test, allow(dead_code))]
96 pub mod guard {
97     pub type Guard = !;
98     pub unsafe fn current() -> Option<Guard> { None }
99     pub unsafe fn init() -> Option<Guard> { None }
100     pub unsafe fn deinit() {}
101 }