]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/thread.rs
auto merge of #20655 : nikomatsakis/rust/carl-ice, r=aturon
[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 core::prelude::*;
12
13 use boxed::Box;
14 use cmp;
15 use mem;
16 use ptr;
17 use libc;
18 use libc::types::os::arch::extra::{LPSECURITY_ATTRIBUTES, SIZE_T, BOOL,
19                                    LPVOID, DWORD, LPDWORD, HANDLE};
20 use thunk::Thunk;
21 use sys_common::stack::RED_ZONE;
22 use sys_common::thread::*;
23
24 pub type rust_thread = HANDLE;
25 pub type rust_thread_return = DWORD;
26
27 pub type StartFn = extern "system" fn(*mut libc::c_void) -> rust_thread_return;
28
29 #[no_stack_check]
30 pub extern "system" fn thread_start(main: *mut libc::c_void) -> rust_thread_return {
31     return start_thread(main);
32 }
33
34 pub mod guard {
35     pub unsafe fn main() -> uint {
36         0
37     }
38
39     pub unsafe fn current() -> uint {
40         0
41     }
42
43     pub unsafe fn init() {
44     }
45 }
46
47 pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
48     let arg: *mut libc::c_void = mem::transmute(box p);
49     // FIXME On UNIX, we guard against stack sizes that are too small but
50     // that's because pthreads enforces that stacks are at least
51     // PTHREAD_STACK_MIN bytes big.  Windows has no such lower limit, it's
52     // just that below a certain threshold you can't do anything useful.
53     // That threshold is application and architecture-specific, however.
54     // For now, the only requirement is that it's big enough to hold the
55     // red zone.  Round up to the next 64 kB because that's what the NT
56     // kernel does, might as well make it explicit.  With the current
57     // 20 kB red zone, that makes for a 64 kB minimum stack.
58     let stack_size = (cmp::max(stack, RED_ZONE) + 0xfffe) & (-0xfffe - 1);
59     let ret = CreateThread(ptr::null_mut(), stack_size as libc::size_t,
60                            thread_start, arg, 0, ptr::null_mut());
61
62     if ret as uint == 0 {
63         // be sure to not leak the closure
64         let _p: Box<Thunk> = mem::transmute(arg);
65         panic!("failed to spawn native thread: {:?}", ret);
66     }
67     return ret;
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 #[allow(non_snake_case)]
87 extern "system" {
88     fn CreateThread(lpThreadAttributes: LPSECURITY_ATTRIBUTES,
89                     dwStackSize: SIZE_T,
90                     lpStartAddress: StartFn,
91                     lpParameter: LPVOID,
92                     dwCreationFlags: DWORD,
93                     lpThreadId: LPDWORD) -> HANDLE;
94     fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
95     fn SwitchToThread() -> BOOL;
96 }