std\sys\pal\windows/
thread.rs

1use core::ffi::c_void;
2
3use super::time::WaitableTimer;
4use super::to_u16s;
5use crate::ffi::CStr;
6use crate::num::NonZero;
7use crate::os::windows::io::{AsRawHandle, HandleOrNull};
8use crate::sys::handle::Handle;
9use crate::sys::{c, stack_overflow};
10use crate::sys_common::FromInner;
11use crate::time::Duration;
12use crate::{io, ptr};
13
14pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
15
16pub struct Thread {
17    handle: Handle,
18}
19
20impl Thread {
21    // unsafe: see thread::Builder::spawn_unchecked for safety requirements
22    #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
23    pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
24        let p = Box::into_raw(Box::new(p));
25
26        // CreateThread rounds up values for the stack size to the nearest page size (at least 4kb).
27        // If a value of zero is given then the default stack size is used instead.
28        // SAFETY: `thread_start` has the right ABI for a thread's entry point.
29        // `p` is simply passed through to the new thread without being touched.
30        let ret = unsafe {
31            let ret = c::CreateThread(
32                ptr::null_mut(),
33                stack,
34                Some(thread_start),
35                p as *mut _,
36                c::STACK_SIZE_PARAM_IS_A_RESERVATION,
37                ptr::null_mut(),
38            );
39            HandleOrNull::from_raw_handle(ret)
40        };
41        return if let Ok(handle) = ret.try_into() {
42            Ok(Thread { handle: Handle::from_inner(handle) })
43        } else {
44            // The thread failed to start and as a result p was not consumed. Therefore, it is
45            // safe to reconstruct the box so that it gets deallocated.
46            unsafe { drop(Box::from_raw(p)) };
47            Err(io::Error::last_os_error())
48        };
49
50        unsafe extern "system" fn thread_start(main: *mut c_void) -> u32 {
51            // Next, reserve some stack space for if we otherwise run out of stack.
52            stack_overflow::reserve_stack();
53            // Finally, let's run some code.
54            // SAFETY: We are simply recreating the box that was leaked earlier.
55            // It's the responsibility of the one who call `Thread::new` to ensure this is safe to call here.
56            unsafe { Box::from_raw(main as *mut Box<dyn FnOnce()>)() };
57            0
58        }
59    }
60
61    pub fn set_name(name: &CStr) {
62        if let Ok(utf8) = name.to_str() {
63            if let Ok(utf16) = to_u16s(utf8) {
64                unsafe {
65                    // SAFETY: the vec returned by `to_u16s` ends with a zero value
66                    Self::set_name_wide(&utf16)
67                }
68            };
69        };
70    }
71
72    /// # Safety
73    ///
74    /// `name` must end with a zero value
75    pub unsafe fn set_name_wide(name: &[u16]) {
76        unsafe { c::SetThreadDescription(c::GetCurrentThread(), name.as_ptr()) };
77    }
78
79    pub fn join(self) {
80        let rc = unsafe { c::WaitForSingleObject(self.handle.as_raw_handle(), c::INFINITE) };
81        if rc == c::WAIT_FAILED {
82            panic!("failed to join on thread: {}", io::Error::last_os_error());
83        }
84    }
85
86    pub 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.
90        unsafe {
91            c::SwitchToThread();
92        }
93    }
94
95    pub fn sleep(dur: Duration) {
96        fn high_precision_sleep(dur: Duration) -> Result<(), ()> {
97            let timer = WaitableTimer::high_resolution()?;
98            timer.set(dur)?;
99            timer.wait()
100        }
101        // Attempt to use high-precision sleep (Windows 10, version 1803+).
102        // On error fallback to the standard `Sleep` function.
103        // Also preserves the zero duration behavior of `Sleep`.
104        if dur.is_zero() || high_precision_sleep(dur).is_err() {
105            unsafe { c::Sleep(super::dur2timeout(dur)) }
106        }
107    }
108
109    pub fn handle(&self) -> &Handle {
110        &self.handle
111    }
112
113    pub fn into_handle(self) -> Handle {
114        self.handle
115    }
116}
117
118pub fn available_parallelism() -> io::Result<NonZero<usize>> {
119    let res = unsafe {
120        let mut sysinfo: c::SYSTEM_INFO = crate::mem::zeroed();
121        c::GetSystemInfo(&mut sysinfo);
122        sysinfo.dwNumberOfProcessors as usize
123    };
124    match res {
125        0 => Err(io::Error::UNKNOWN_THREAD_COUNT),
126        cpus => Ok(unsafe { NonZero::new_unchecked(cpus) }),
127    }
128}