]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | ||
85aaf69f | 11 | use prelude::v1::*; |
1a4d82fc | 12 | |
d9579d0f | 13 | use alloc::boxed::FnBox; |
1a4d82fc | 14 | use cmp; |
85aaf69f | 15 | use io; |
d9579d0f | 16 | use libc::{self, c_void, DWORD}; |
c34b1796 AL |
17 | use mem; |
18 | use ptr; | |
d9579d0f AL |
19 | use sys::c; |
20 | use sys::handle::Handle; | |
1a4d82fc JJ |
21 | use sys_common::stack::RED_ZONE; |
22 | use sys_common::thread::*; | |
c34b1796 | 23 | use time::Duration; |
1a4d82fc | 24 | |
d9579d0f AL |
25 | pub struct Thread { |
26 | handle: Handle | |
1a4d82fc JJ |
27 | } |
28 | ||
d9579d0f AL |
29 | impl Thread { |
30 | pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>) | |
31 | -> io::Result<Thread> { | |
32 | let p = box p; | |
1a4d82fc | 33 | |
d9579d0f AL |
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()); | |
c34b1796 | 47 | |
d9579d0f AL |
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 | }; | |
85aaf69f | 54 | |
d9579d0f AL |
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 | } | |
1a4d82fc | 61 | |
d9579d0f AL |
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 | } | |
1a4d82fc | 68 | |
d9579d0f AL |
69 | pub fn join(self) { |
70 | use libc::consts::os::extra::INFINITE; | |
71 | unsafe { c::WaitForSingleObject(self.handle.raw(), INFINITE); } | |
72 | } | |
1a4d82fc | 73 | |
d9579d0f AL |
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 | } | |
1a4d82fc | 80 | |
d9579d0f AL |
81 | pub fn sleep(dur: Duration) { |
82 | unsafe { | |
83 | c::Sleep(super::dur2timeout(dur)) | |
c34b1796 | 84 | } |
c34b1796 AL |
85 | } |
86 | } | |
87 | ||
d9579d0f AL |
88 | pub mod guard { |
89 | pub unsafe fn main() -> usize { 0 } | |
90 | pub unsafe fn current() -> usize { 0 } | |
91 | pub unsafe fn init() {} | |
1a4d82fc | 92 | } |