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.
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.
18 use libc::types::os::arch::extra::{LPSECURITY_ATTRIBUTES, SIZE_T, BOOL,
19 LPVOID, DWORD, LPDWORD, HANDLE};
21 use sys_common::stack::RED_ZONE;
22 use sys_common::thread::*;
24 pub type rust_thread = HANDLE;
25 pub type rust_thread_return = DWORD;
27 pub type StartFn = extern "system" fn(*mut libc::c_void) -> rust_thread_return;
30 pub extern "system" fn thread_start(main: *mut libc::c_void) -> rust_thread_return {
31 return start_thread(main);
35 pub unsafe fn main() -> uint {
39 pub unsafe fn current() -> uint {
43 pub unsafe fn init() {
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());
63 // be sure to not leak the closure
64 let _p: Box<Thunk> = mem::transmute(arg);
65 panic!("failed to spawn native thread: {:?}", ret);
70 pub unsafe fn set_name(name: &str) {
71 // Windows threads are nameless
72 // The names in MSVC debugger are obtained using a "magic" exception,
73 // which requires a use of C++ macros.
74 // See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
77 pub unsafe fn join(native: rust_thread) {
78 use libc::consts::os::extra::INFINITE;
79 WaitForSingleObject(native, INFINITE);
82 pub unsafe fn detach(native: rust_thread) {
83 assert!(libc::CloseHandle(native) != 0);
86 pub unsafe fn yield_now() {
87 // This function will return 0 if there are no other threads to execute,
88 // but this also means that the yield was useless so this isn't really a
89 // case that needs to be worried about.
93 #[allow(non_snake_case)]
95 fn CreateThread(lpThreadAttributes: LPSECURITY_ATTRIBUTES,
97 lpStartAddress: StartFn,
99 dwCreationFlags: DWORD,
100 lpThreadId: LPDWORD) -> HANDLE;
101 fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
102 fn SwitchToThread() -> BOOL;