Auto merge of #123433 - GnomedDev:remove-threadname-alloc, r=joboet
Remove rt::init allocation for thread name This removes one of the allocations in a `fn main() {}` program.
This commit is contained in:
commit
30840c53f4
@ -16,8 +16,6 @@
|
|||||||
#![deny(unsafe_op_in_unsafe_fn)]
|
#![deny(unsafe_op_in_unsafe_fn)]
|
||||||
#![allow(unused_macros)]
|
#![allow(unused_macros)]
|
||||||
|
|
||||||
use crate::ffi::CString;
|
|
||||||
|
|
||||||
// Re-export some of our utilities which are expected by other crates.
|
// Re-export some of our utilities which are expected by other crates.
|
||||||
pub use crate::panicking::{begin_panic, panic_count};
|
pub use crate::panicking::{begin_panic, panic_count};
|
||||||
pub use core::panicking::{panic_display, panic_fmt};
|
pub use core::panicking::{panic_display, panic_fmt};
|
||||||
@ -96,7 +94,7 @@ unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
|
|||||||
sys::init(argc, argv, sigpipe);
|
sys::init(argc, argv, sigpipe);
|
||||||
|
|
||||||
// Set up the current thread to give it the right name.
|
// Set up the current thread to give it the right name.
|
||||||
let thread = Thread::new(Some(rtunwrap!(Ok, CString::new("main"))));
|
let thread = Thread::new_main();
|
||||||
thread::set_current(thread);
|
thread::set_current(thread);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1245,9 +1245,16 @@ pub fn as_u64(&self) -> NonZero<u64> {
|
|||||||
// Thread
|
// Thread
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// The internal representation of a `Thread`'s name.
|
||||||
|
enum ThreadName {
|
||||||
|
Main,
|
||||||
|
Other(CString),
|
||||||
|
Unnamed,
|
||||||
|
}
|
||||||
|
|
||||||
/// The internal representation of a `Thread` handle
|
/// The internal representation of a `Thread` handle
|
||||||
struct Inner {
|
struct Inner {
|
||||||
name: Option<CString>, // Guaranteed to be UTF-8
|
name: ThreadName, // Guaranteed to be UTF-8
|
||||||
id: ThreadId,
|
id: ThreadId,
|
||||||
parker: Parker,
|
parker: Parker,
|
||||||
}
|
}
|
||||||
@ -1284,8 +1291,20 @@ 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
|
||||||
// Panics if the name contains nuls.
|
|
||||||
pub(crate) fn new(name: Option<CString>) -> Thread {
|
pub(crate) fn new(name: Option<CString>) -> Thread {
|
||||||
|
if let Some(name) = name {
|
||||||
|
Self::new_inner(ThreadName::Other(name))
|
||||||
|
} else {
|
||||||
|
Self::new_inner(ThreadName::Unnamed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Used in runtime to construct main thread
|
||||||
|
pub(crate) fn new_main() -> Thread {
|
||||||
|
Self::new_inner(ThreadName::Main)
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
//
|
//
|
||||||
@ -1412,7 +1431,11 @@ pub fn name(&self) -> Option<&str> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn cname(&self) -> Option<&CStr> {
|
fn cname(&self) -> Option<&CStr> {
|
||||||
self.inner.name.as_deref()
|
match &self.inner.name {
|
||||||
|
ThreadName::Main => Some(c"main"),
|
||||||
|
ThreadName::Other(other) => Some(&other),
|
||||||
|
ThreadName::Unnamed => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user