]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/windows/thread.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.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 alloc::boxed::FnBox;
14 use cmp;
15 use io;
16 use libc::{self, c_void, DWORD};
17 use mem;
18 use ptr;
19 use sys::c;
20 use sys::handle::Handle;
21 use sys_common::stack::RED_ZONE;
22 use sys_common::thread::*;
23 use time::Duration;
24
25 pub struct Thread {
26 handle: Handle
27 }
28
29 impl Thread {
30 pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>)
31 -> io::Result<Thread> {
32 let p = box p;
33
34 // FIXME On UNIX, we guard against stack sizes that are too small but
35 // that's because pthreads enforces that stacks are at least
36 // PTHREAD_STACK_MIN bytes big. Windows has no such lower limit, it's
37 // just that below a certain threshold you can't do anything useful.
38 // That threshold is application and architecture-specific, however.
39 // For now, the only requirement is that it's big enough to hold the
40 // red zone. Round up to the next 64 kB because that's what the NT
41 // kernel does, might as well make it explicit. With the current
42 // 20 kB red zone, that makes for a 64 kB minimum stack.
43 let stack_size = (cmp::max(stack, RED_ZONE) + 0xfffe) & (-0xfffe - 1);
44 let ret = c::CreateThread(ptr::null_mut(), stack_size as libc::size_t,
45 thread_start, &*p as *const _ as *mut _,
46 0, 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 #[no_stack_check]
56 extern "system" fn thread_start(main: *mut libc::c_void) -> DWORD {
57 unsafe { start_thread(main); }
58 0
59 }
60 }
61
62 pub fn set_name(_name: &str) {
63 // Windows threads are nameless
64 // The names in MSVC debugger are obtained using a "magic" exception,
65 // which requires a use of MS C++ extensions.
66 // See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
67 }
68
69 pub fn join(self) {
70 use libc::consts::os::extra::INFINITE;
71 unsafe { c::WaitForSingleObject(self.handle.raw(), INFINITE); }
72 }
73
74 pub fn yield_now() {
75 // This function will return 0 if there are no other threads to execute,
76 // but this also means that the yield was useless so this isn't really a
77 // case that needs to be worried about.
78 unsafe { c::SwitchToThread(); }
79 }
80
81 pub fn sleep(dur: Duration) {
82 unsafe {
83 c::Sleep(super::dur2timeout(dur))
84 }
85 }
86 }
87
88 pub mod guard {
89 pub unsafe fn main() -> usize { 0 }
90 pub unsafe fn current() -> usize { 0 }
91 pub unsafe fn init() {}
92 }