]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/windows/thread.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libstd / sys / windows / thread.rs
CommitLineData
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 11use prelude::v1::*;
1a4d82fc 12
d9579d0f 13use alloc::boxed::FnBox;
85aaf69f 14use io;
54a0048b 15use ffi::CStr;
c34b1796 16use mem;
92a42be0 17use libc::c_void;
c34b1796 18use ptr;
d9579d0f
AL
19use sys::c;
20use sys::handle::Handle;
1a4d82fc 21use sys_common::thread::*;
c34b1796 22use time::Duration;
1a4d82fc 23
d9579d0f
AL
24pub struct Thread {
25 handle: Handle
1a4d82fc
JJ
26}
27
d9579d0f
AL
28impl Thread {
29 pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>)
30 -> io::Result<Thread> {
31 let p = box p;
1a4d82fc 32
d9579d0f
AL
33 // FIXME On UNIX, we guard against stack sizes that are too small but
34 // that's because pthreads enforces that stacks are at least
35 // PTHREAD_STACK_MIN bytes big. Windows has no such lower limit, it's
36 // just that below a certain threshold you can't do anything useful.
37 // That threshold is application and architecture-specific, however.
e9174d1e
SL
38 // Round up to the next 64 kB because that's what the NT kernel does,
39 // might as well make it explicit.
40 let stack_size = (stack + 0xfffe) & (!0xfffe);
92a42be0 41 let ret = c::CreateThread(ptr::null_mut(), stack_size,
d9579d0f
AL
42 thread_start, &*p as *const _ as *mut _,
43 0, ptr::null_mut());
c34b1796 44
d9579d0f
AL
45 return if ret as usize == 0 {
46 Err(io::Error::last_os_error())
47 } else {
48 mem::forget(p); // ownership passed to CreateThread
49 Ok(Thread { handle: Handle::new(ret) })
50 };
85aaf69f 51
92a42be0 52 extern "system" fn thread_start(main: *mut c_void) -> c::DWORD {
d9579d0f
AL
53 unsafe { start_thread(main); }
54 0
55 }
56 }
1a4d82fc 57
54a0048b 58 pub fn set_name(_name: &CStr) {
d9579d0f
AL
59 // Windows threads are nameless
60 // The names in MSVC debugger are obtained using a "magic" exception,
61 // which requires a use of MS C++ extensions.
62 // See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
63 }
1a4d82fc 64
d9579d0f 65 pub fn join(self) {
92a42be0 66 unsafe { c::WaitForSingleObject(self.handle.raw(), c::INFINITE); }
d9579d0f 67 }
1a4d82fc 68
d9579d0f
AL
69 pub fn yield_now() {
70 // This function will return 0 if there are no other threads to execute,
71 // but this also means that the yield was useless so this isn't really a
72 // case that needs to be worried about.
73 unsafe { c::SwitchToThread(); }
74 }
1a4d82fc 75
d9579d0f
AL
76 pub fn sleep(dur: Duration) {
77 unsafe {
78 c::Sleep(super::dur2timeout(dur))
c34b1796 79 }
c34b1796 80 }
92a42be0
SL
81
82 pub fn handle(&self) -> &Handle { &self.handle }
83
84 pub fn into_handle(self) -> Handle { self.handle }
c34b1796
AL
85}
86
7453a54e 87#[cfg_attr(test, allow(dead_code))]
d9579d0f 88pub mod guard {
c1a9b12d
SL
89 pub unsafe fn current() -> Option<usize> { None }
90 pub unsafe fn init() -> Option<usize> { None }
1a4d82fc 91}