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.
15 use libc::{self, c_void};
16 use libc::types::os::arch::extra::{LPSECURITY_ATTRIBUTES, SIZE_T, BOOL,
17 LPVOID, DWORD, LPDWORD, HANDLE};
20 use sys_common::stack::RED_ZONE;
21 use sys_common::thread::*;
25 pub type rust_thread = HANDLE;
28 pub unsafe fn main() -> uint { 0 }
29 pub unsafe fn current() -> uint { 0 }
30 pub unsafe fn init() {}
33 pub unsafe fn create(stack: usize, p: Thunk) -> io::Result<rust_thread> {
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 // For now, the only requirement is that it's big enough to hold the
41 // red zone. Round up to the next 64 kB because that's what the NT
42 // kernel does, might as well make it explicit. With the current
43 // 20 kB red zone, that makes for a 64 kB minimum stack.
44 let stack_size = (cmp::max(stack, RED_ZONE) + 0xfffe) & (-0xfffe - 1);
45 let ret = CreateThread(ptr::null_mut(), stack_size as libc::size_t,
46 thread_start, &*p as *const _ as *mut _,
49 return if ret as usize == 0 {
50 Err(io::Error::last_os_error())
52 mem::forget(p); // ownership passed to CreateThread
57 extern "system" fn thread_start(main: *mut libc::c_void) -> DWORD {
63 pub unsafe fn set_name(_name: &str) {
64 // Windows threads are nameless
65 // The names in MSVC debugger are obtained using a "magic" exception,
66 // which requires a use of MS C++ extensions.
67 // See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
70 pub unsafe fn join(native: rust_thread) {
71 use libc::consts::os::extra::INFINITE;
72 WaitForSingleObject(native, INFINITE);
75 pub unsafe fn detach(native: rust_thread) {
76 assert!(libc::CloseHandle(native) != 0);
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.
86 pub fn sleep(dur: Duration) {
88 if dur < Duration::zero() {
91 let ms = dur.num_milliseconds();
92 // if we have a fractional number of milliseconds then add an extra
93 // millisecond to sleep for
94 let extra = dur - Duration::milliseconds(ms);
95 let ms = ms + if extra.is_zero() {0} else {1};
100 #[allow(non_snake_case)]
102 fn CreateThread(lpThreadAttributes: LPSECURITY_ATTRIBUTES,
104 lpStartAddress: extern "system" fn(*mut c_void) -> DWORD,
106 dwCreationFlags: DWORD,
107 lpThreadId: LPDWORD) -> HANDLE;
108 fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
109 fn SwitchToThread() -> BOOL;
110 fn Sleep(dwMilliseconds: DWORD);