]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/thread.rs
Rollup merge of #35786 - GuillaumeGomez:paths_doc, r=steveklabnik
[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 alloc::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 pub struct Thread {
23     handle: Handle
24 }
25
26 impl Thread {
27     pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>)
28                           -> io::Result<Thread> {
29         let p = box p;
30
31         // FIXME On UNIX, we guard against stack sizes that are too small but
32         // that's because pthreads enforces that stacks are at least
33         // PTHREAD_STACK_MIN bytes big.  Windows has no such lower limit, it's
34         // just that below a certain threshold you can't do anything useful.
35         // That threshold is application and architecture-specific, however.
36         // Round up to the next 64 kB because that's what the NT kernel does,
37         // might as well make it explicit.
38         let stack_size = (stack + 0xfffe) & (!0xfffe);
39         let ret = c::CreateThread(ptr::null_mut(), stack_size,
40                                   thread_start, &*p as *const _ as *mut _,
41                                   0, ptr::null_mut());
42
43         return if ret as usize == 0 {
44             Err(io::Error::last_os_error())
45         } else {
46             mem::forget(p); // ownership passed to CreateThread
47             Ok(Thread { handle: Handle::new(ret) })
48         };
49
50         extern "system" fn thread_start(main: *mut c_void) -> c::DWORD {
51             unsafe { start_thread(main); }
52             0
53         }
54     }
55
56     pub fn set_name(_name: &CStr) {
57         // Windows threads are nameless
58         // The names in MSVC debugger are obtained using a "magic" exception,
59         // which requires a use of MS C++ extensions.
60         // See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
61     }
62
63     pub fn join(self) {
64         unsafe { c::WaitForSingleObject(self.handle.raw(), c::INFINITE); }
65     }
66
67     pub fn yield_now() {
68         // This function will return 0 if there are no other threads to execute,
69         // but this also means that the yield was useless so this isn't really a
70         // case that needs to be worried about.
71         unsafe { c::SwitchToThread(); }
72     }
73
74     pub fn sleep(dur: Duration) {
75         unsafe {
76             c::Sleep(super::dur2timeout(dur))
77         }
78     }
79
80     pub fn handle(&self) -> &Handle { &self.handle }
81
82     pub fn into_handle(self) -> Handle { self.handle }
83 }
84
85 #[cfg_attr(test, allow(dead_code))]
86 pub mod guard {
87     pub unsafe fn current() -> Option<usize> { None }
88     pub unsafe fn init() -> Option<usize> { None }
89 }