Skip to content

Commit 11cb7e1

Browse files
committed
Remove rt::init allocation for thread name
1 parent 703dc9c commit 11cb7e1

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

library/std/src/rt.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#![deny(unsafe_op_in_unsafe_fn)]
1717
#![allow(unused_macros)]
1818

19-
use crate::ffi::CString;
20-
2119
// Re-export some of our utilities which are expected by other crates.
2220
pub use crate::panicking::{begin_panic, panic_count};
2321
pub use core::panicking::{panic_display, panic_fmt};
@@ -96,7 +94,7 @@ unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
9694
sys::init(argc, argv, sigpipe);
9795

9896
// Set up the current thread to give it the right name.
99-
let thread = Thread::new(Some(rtunwrap!(Ok, CString::new("main"))));
97+
let thread = Thread::new_main();
10098
thread::set_current(thread);
10199
}
102100
}

library/std/src/thread/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ use crate::pin::Pin;
172172
use crate::ptr::addr_of_mut;
173173
use crate::str;
174174
use crate::sync::Arc;
175+
use crate::borrow::Cow;
175176
use crate::sys::thread as imp;
176177
use crate::sys_common::thread;
177178
use crate::sys_common::thread_parking::Parker;
@@ -1249,7 +1250,7 @@ impl ThreadId {
12491250

12501251
/// The internal representation of a `Thread` handle
12511252
struct Inner {
1252-
name: Option<CString>, // Guaranteed to be UTF-8
1253+
name: Option<Cow<'static, CStr>>, // Guaranteed to be UTF-8
12531254
id: ThreadId,
12541255
parker: Parker,
12551256
}
@@ -1286,8 +1287,16 @@ pub struct Thread {
12861287

12871288
impl Thread {
12881289
// Used only internally to construct a thread object without spawning
1289-
// Panics if the name contains nuls.
12901290
pub(crate) fn new(name: Option<CString>) -> Thread {
1291+
Self::new_cow(name.map(Cow::Owned))
1292+
}
1293+
1294+
// Used in runtime to construct main thread
1295+
pub(crate) fn new_main() -> Thread {
1296+
Self::new_cow(Some(Cow::Borrowed(c"main")))
1297+
}
1298+
1299+
fn new_cow(name: Option<Cow<'static, CStr>>) -> Thread {
12911300
// We have to use `unsafe` here to construct the `Parker` in-place,
12921301
// which is required for the UNIX implementation.
12931302
//

0 commit comments

Comments
 (0)