Add missing unsafe to internal std:🧵:Thread creation functions

This commit is contained in:
beetrees 2024-04-12 22:44:36 +01:00
parent 53f55c6635
commit 126c762b85
No known key found for this signature in database
GPG Key ID: 8791BD754191EBD6
2 changed files with 21 additions and 14 deletions

View File

@ -202,7 +202,7 @@ impl Node {
fn prepare(&mut self) { fn prepare(&mut self) {
// Fall back to creating an unnamed `Thread` handle to allow locking in // Fall back to creating an unnamed `Thread` handle to allow locking in
// TLS destructors. // TLS destructors.
self.thread.get_or_init(|| thread::try_current().unwrap_or_else(|| Thread::new(None))); self.thread.get_or_init(|| thread::try_current().unwrap_or_else(Thread::new_unnamed));
self.completed = AtomicBool::new(false); self.completed = AtomicBool::new(false);
} }

View File

@ -470,9 +470,11 @@ impl Builder {
let stack_size = stack_size.unwrap_or_else(thread::min_stack); let stack_size = stack_size.unwrap_or_else(thread::min_stack);
let my_thread = Thread::new(name.map(|name| { let my_thread = name.map_or_else(Thread::new_unnamed, |name| unsafe {
CString::new(name).expect("thread name may not contain interior null bytes") Thread::new(
})); CString::new(name).expect("thread name may not contain interior null bytes"),
)
});
let their_thread = my_thread.clone(); let their_thread = my_thread.clone();
let my_packet: Arc<Packet<'scope, T>> = Arc::new(Packet { let my_packet: Arc<Packet<'scope, T>> = Arc::new(Packet {
@ -694,7 +696,7 @@ pub(crate) fn set_current(thread: Thread) {
/// In contrast to the public `current` function, this will not panic if called /// In contrast to the public `current` function, this will not panic if called
/// from inside a TLS destructor. /// from inside a TLS destructor.
pub(crate) fn try_current() -> Option<Thread> { pub(crate) fn try_current() -> Option<Thread> {
CURRENT.try_with(|current| current.get_or_init(|| Thread::new(None)).clone()).ok() CURRENT.try_with(|current| current.get_or_init(|| Thread::new_unnamed()).clone()).ok()
} }
/// Gets a handle to the thread that invokes it. /// Gets a handle to the thread that invokes it.
@ -1290,21 +1292,26 @@ pub struct Thread {
} }
impl Thread { impl Thread {
// Used only internally to construct a thread object without spawning /// Used only internally to construct a thread object without spawning.
pub(crate) fn new(name: Option<CString>) -> Thread { ///
if let Some(name) = name { /// # Safety
Self::new_inner(ThreadName::Other(name)) /// `name` must be valid UTF-8.
} else { pub(crate) unsafe fn new(name: CString) -> Thread {
Self::new_inner(ThreadName::Unnamed) unsafe { Self::new_inner(ThreadName::Other(name)) }
} }
pub(crate) fn new_unnamed() -> Thread {
unsafe { Self::new_inner(ThreadName::Unnamed) }
} }
// Used in runtime to construct main thread // Used in runtime to construct main thread
pub(crate) fn new_main() -> Thread { pub(crate) fn new_main() -> Thread {
Self::new_inner(ThreadName::Main) unsafe { Self::new_inner(ThreadName::Main) }
} }
fn new_inner(name: ThreadName) -> Thread { /// # Safety
/// If `name` is `ThreadName::Other(_)`, the contained string must be valid UTF-8.
unsafe fn new_inner(name: ThreadName) -> Thread {
// We have to use `unsafe` here to construct the `Parker` in-place, // We have to use `unsafe` here to construct the `Parker` in-place,
// which is required for the UNIX implementation. // which is required for the UNIX implementation.
// //