Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unsafe to two functions with safety invariants #123867

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions library/std/src/sys/pal/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,31 @@ impl Thread {
Err(io::Error::last_os_error())
};

extern "system" fn thread_start(main: *mut c_void) -> c::DWORD {
unsafe {
// Next, reserve some stack space for if we otherwise run out of stack.
stack_overflow::reserve_stack();
// Finally, let's run some code.
Box::from_raw(main as *mut Box<dyn FnOnce()>)();
}
unsafe extern "system" fn thread_start(main: *mut c_void) -> c::DWORD {
// Next, reserve some stack space for if we otherwise run out of stack.
stack_overflow::reserve_stack();
// Finally, let's run some code.
Box::from_raw(main as *mut Box<dyn FnOnce()>)();
0
}
}

pub fn set_name(name: &CStr) {
if let Ok(utf8) = name.to_str() {
if let Ok(utf16) = to_u16s(utf8) {
Self::set_name_wide(&utf16)
unsafe {
// SAFETY: the vec returned by `to_u16s` ends with a zero value
Self::set_name_wide(&utf16)
}
};
};
}

pub fn set_name_wide(name: &[u16]) {
unsafe {
c::SetThreadDescription(c::GetCurrentThread(), name.as_ptr());
};
/// # Safety
///
/// `name` must end with a zero value
pub unsafe fn set_name_wide(name: &[u16]) {
c::SetThreadDescription(c::GetCurrentThread(), name.as_ptr());
}

pub fn join(self) {
Expand Down
Loading