Skip to content

Commit

Permalink
Remove rt::init allocation for thread name
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Apr 3, 2024
1 parent 703dc9c commit 11cb7e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 1 addition & 3 deletions library/std/src/rt.rs
Expand Up @@ -16,8 +16,6 @@
#![deny(unsafe_op_in_unsafe_fn)]
#![allow(unused_macros)]

use crate::ffi::CString;

// Re-export some of our utilities which are expected by other crates.
pub use crate::panicking::{begin_panic, panic_count};
pub use core::panicking::{panic_display, panic_fmt};
Expand Down Expand Up @@ -96,7 +94,7 @@ unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
sys::init(argc, argv, sigpipe);

// 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);
}
}
Expand Down
13 changes: 11 additions & 2 deletions library/std/src/thread/mod.rs
Expand Up @@ -172,6 +172,7 @@ use crate::pin::Pin;
use crate::ptr::addr_of_mut;
use crate::str;
use crate::sync::Arc;
use crate::borrow::Cow;
use crate::sys::thread as imp;
use crate::sys_common::thread;
use crate::sys_common::thread_parking::Parker;
Expand Down Expand Up @@ -1249,7 +1250,7 @@ impl ThreadId {

/// The internal representation of a `Thread` handle
struct Inner {
name: Option<CString>, // Guaranteed to be UTF-8
name: Option<Cow<'static, CStr>>, // Guaranteed to be UTF-8
id: ThreadId,
parker: Parker,
}
Expand Down Expand Up @@ -1286,8 +1287,16 @@ pub struct Thread {

impl Thread {
// Used only internally to construct a thread object without spawning
// Panics if the name contains nuls.
pub(crate) fn new(name: Option<CString>) -> Thread {
Self::new_cow(name.map(Cow::Owned))
}

// Used in runtime to construct main thread
pub(crate) fn new_main() -> Thread {
Self::new_cow(Some(Cow::Borrowed(c"main")))
}

fn new_cow(name: Option<Cow<'static, CStr>>) -> Thread {
// We have to use `unsafe` here to construct the `Parker` in-place,
// which is required for the UNIX implementation.
//
Expand Down

0 comments on commit 11cb7e1

Please sign in to comment.