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.
13 use alloc
::boxed
::FnBox
;
16 use libc
::{self, c_void, DWORD}
;
20 use sys
::handle
::Handle
;
21 use sys_common
::stack
::RED_ZONE
;
22 use sys_common
::thread
::*;
30 pub unsafe fn new
<'a
>(stack
: usize, p
: Box
<FnBox() + 'a
>)
31 -> io
::Result
<Thread
> {
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 _
,
48 return if ret
as usize == 0 {
49 Err(io
::Error
::last_os_error())
51 mem
::forget(p
); // ownership passed to CreateThread
52 Ok(Thread { handle: Handle::new(ret) }
)
56 extern "system" fn thread_start(main
: *mut libc
::c_void
) -> DWORD
{
57 unsafe { start_thread(main); }
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
70 use libc
::consts
::os
::extra
::INFINITE
;
71 unsafe { c::WaitForSingleObject(self.handle.raw(), INFINITE); }
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(); }
81 pub fn sleep(dur
: Duration
) {
83 c
::Sleep(super::dur2timeout(dur
))
89 pub unsafe fn main() -> usize { 0 }
90 pub unsafe fn current() -> usize { 0 }
91 pub unsafe fn init() {}