Skip to content

Commit 17114c0

Browse files
committed
Auto merge of rust-lang#123550 - GnomedDev:remove-initial-arc, r=Nilstrieb
Remove last rt::init allocation for thread info Removes the last allocation pre-main by just not storing anything in std::thread::Thread for the main thread. - The thread name can just be a hard coded literal, as was done in rust-lang#123433. - The ThreadId is always the `1` value, so `ThreadId::new` now starts at `2` and can fabricate the `1` value when needed. - Storing Parker in a static that is initialized once at startup. This uses SyncUnsafeCell and MaybeUninit as this is quite performance critical and we don't need synchronization or to store a tag value and possibly leave in a panic. This also adds a UI test to make sure that allocations do not occur before main ever again. try-job: dist-x86_64-linux
2 parents 51917ba + a8256af commit 17114c0

File tree

6 files changed

+151
-42
lines changed

6 files changed

+151
-42
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@
352352
#![feature(str_internals)]
353353
#![feature(strict_provenance)]
354354
#![feature(strict_provenance_atomic_ptr)]
355+
#![feature(sync_unsafe_cell)]
355356
#![feature(ub_checks)]
356357
// tidy-alphabetical-end
357358
//

library/std/src/thread/mod.rs

+90-40
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@
159159
mod tests;
160160

161161
use crate::any::Any;
162+
use crate::cell::SyncUnsafeCell;
162163
use crate::cell::{OnceCell, UnsafeCell};
163164
use crate::env;
164165
use crate::ffi::{CStr, CString};
165166
use crate::fmt;
166167
use crate::io;
167168
use crate::marker::PhantomData;
169+
use crate::mem::MaybeUninit;
168170
use crate::mem::{self, forget};
169171
use crate::num::NonZero;
170172
use crate::panic;
@@ -530,7 +532,7 @@ impl Builder {
530532

531533
let f = MaybeDangling::new(f);
532534
let main = move || {
533-
if let Some(name) = their_thread.cname() {
535+
if let Some(name) = their_thread.0.name() {
534536
imp::Thread::set_name(name);
535537
}
536538

@@ -1168,7 +1170,7 @@ pub fn park_timeout(dur: Duration) {
11681170
let guard = PanicGuard;
11691171
// SAFETY: park_timeout is called on the parker owned by this thread.
11701172
unsafe {
1171-
current().inner.as_ref().parker().park_timeout(dur);
1173+
current().0.parker().park_timeout(dur);
11721174
}
11731175
// No panic occurred, do not abort.
11741176
forget(guard);
@@ -1207,7 +1209,12 @@ pub fn park_timeout(dur: Duration) {
12071209
pub struct ThreadId(NonZero<u64>);
12081210

12091211
impl ThreadId {
1210-
// Generate a new unique thread ID.
1212+
/// Generate a new unique thread ID.
1213+
///
1214+
/// The current implementation starts at 2 and increments from there.
1215+
///
1216+
/// This is as `1` is the value for the main thread, so std::thread::Thread does not
1217+
/// have to store this value when creating the main thread's information.
12111218
fn new() -> ThreadId {
12121219
#[cold]
12131220
fn exhausted() -> ! {
@@ -1218,7 +1225,7 @@ impl ThreadId {
12181225
if #[cfg(target_has_atomic = "64")] {
12191226
use crate::sync::atomic::AtomicU64;
12201227

1221-
static COUNTER: AtomicU64 = AtomicU64::new(0);
1228+
static COUNTER: AtomicU64 = AtomicU64::new(1);
12221229

12231230
let mut last = COUNTER.load(Ordering::Relaxed);
12241231
loop {
@@ -1234,7 +1241,7 @@ impl ThreadId {
12341241
} else {
12351242
use crate::sync::{Mutex, PoisonError};
12361243

1237-
static COUNTER: Mutex<u64> = Mutex::new(0);
1244+
static COUNTER: Mutex<u64> = Mutex::new(1);
12381245

12391246
let mut counter = COUNTER.lock().unwrap_or_else(PoisonError::into_inner);
12401247
let Some(id) = counter.checked_add(1) else {
@@ -1251,6 +1258,11 @@ impl ThreadId {
12511258
}
12521259
}
12531260

1261+
/// Creates a ThreadId with the ID of the main thread.
1262+
fn new_main() -> Self {
1263+
Self(NonZero::<u64>::MIN)
1264+
}
1265+
12541266
/// This returns a numeric identifier for the thread identified by this
12551267
/// `ThreadId`.
12561268
///
@@ -1270,23 +1282,59 @@ impl ThreadId {
12701282
// Thread
12711283
////////////////////////////////////////////////////////////////////////////////
12721284

1273-
/// The internal representation of a `Thread`'s name.
1274-
enum ThreadName {
1275-
Main,
1276-
Other(CString),
1277-
Unnamed,
1278-
}
1285+
/// The parker for the main thread. This avoids having to allocate an Arc in `fn main() {}`.
1286+
static MAIN_PARKER: SyncUnsafeCell<MaybeUninit<Parker>> =
1287+
SyncUnsafeCell::new(MaybeUninit::uninit());
12791288

1280-
/// The internal representation of a `Thread` handle
1281-
struct Inner {
1282-
name: ThreadName, // Guaranteed to be UTF-8
1289+
/// The internal representation of a `Thread` that is not the main thread.
1290+
struct OtherInner {
1291+
name: Option<CString>, // Guaranteed to be UTF-8
12831292
id: ThreadId,
12841293
parker: Parker,
12851294
}
12861295

1296+
/// The internal representation of a `Thread` handle.
1297+
#[derive(Clone)]
1298+
enum Inner {
1299+
/// Represents the main thread. May only be constructed by Thread::new_main.
1300+
Main,
1301+
/// Represents any other thread.
1302+
Other(Pin<Arc<OtherInner>>),
1303+
}
1304+
12871305
impl Inner {
1288-
fn parker(self: Pin<&Self>) -> Pin<&Parker> {
1289-
unsafe { Pin::map_unchecked(self, |inner| &inner.parker) }
1306+
fn id(&self) -> ThreadId {
1307+
match self {
1308+
Self::Main => ThreadId::new_main(),
1309+
Self::Other(other) => other.id,
1310+
}
1311+
}
1312+
1313+
fn name(&self) -> Option<&CStr> {
1314+
match self {
1315+
Self::Main => Some(c"main"),
1316+
Self::Other(other) => other.name.as_deref(),
1317+
}
1318+
}
1319+
1320+
fn parker(&self) -> Pin<&Parker> {
1321+
match self {
1322+
Self::Main => {
1323+
// Safety: MAIN_PARKER is only ever read in this function, which requires access
1324+
// to an existing `&Inner` value, which can only be accessed via the main thread
1325+
// giving away such an instance from `current()`, implying that initialization,
1326+
// the only write to `MAIN_PARKER`, has been completed.
1327+
let static_ref: &'static MaybeUninit<Parker> = unsafe { &*MAIN_PARKER.get() };
1328+
1329+
// Safety: MAIN_PARKER is initialised when Inner::Main is initialised.
1330+
let parker_ref = unsafe { static_ref.assume_init_ref() };
1331+
1332+
Pin::static_ref(parker_ref)
1333+
}
1334+
Self::Other(inner) => unsafe {
1335+
Pin::map_unchecked(inner.as_ref(), |inner| &inner.parker)
1336+
},
1337+
}
12901338
}
12911339
}
12921340

@@ -1310,46 +1358,56 @@ impl Inner {
13101358
/// docs of [`Builder`] and [`spawn`] for more details.
13111359
///
13121360
/// [`thread::current`]: current
1313-
pub struct Thread {
1314-
inner: Pin<Arc<Inner>>,
1315-
}
1361+
pub struct Thread(Inner);
13161362

13171363
impl Thread {
13181364
/// Used only internally to construct a thread object without spawning.
13191365
///
13201366
/// # Safety
13211367
/// `name` must be valid UTF-8.
13221368
pub(crate) unsafe fn new(name: CString) -> Thread {
1323-
unsafe { Self::new_inner(ThreadName::Other(name)) }
1369+
unsafe { Self::new_inner(Some(name)) }
13241370
}
13251371

13261372
pub(crate) fn new_unnamed() -> Thread {
1327-
unsafe { Self::new_inner(ThreadName::Unnamed) }
1373+
unsafe { Self::new_inner(None) }
13281374
}
13291375

1330-
// Used in runtime to construct main thread
1331-
pub(crate) fn new_main() -> Thread {
1332-
unsafe { Self::new_inner(ThreadName::Main) }
1376+
/// Used in runtime to construct main thread
1377+
///
1378+
/// # Safety
1379+
///
1380+
/// This must only ever be called once, and must be called on the main thread.
1381+
pub(crate) unsafe fn new_main() -> Thread {
1382+
// Safety: As this is only called once and on the main thread, nothing else is accessing MAIN_PARKER
1383+
// as the only other read occurs in Inner::parker *after* Inner::Main has been constructed,
1384+
// and this function is the only one that constructs Inner::Main.
1385+
//
1386+
// Pre-main thread spawning cannot hit this either, as the caller promises that this is only called on the main thread.
1387+
unsafe { Parker::new_in_place(MAIN_PARKER.get().cast()) }
1388+
1389+
Self(Inner::Main)
13331390
}
13341391

13351392
/// # Safety
1336-
/// If `name` is `ThreadName::Other(_)`, the contained string must be valid UTF-8.
1337-
unsafe fn new_inner(name: ThreadName) -> Thread {
1393+
///
1394+
/// If `name` is `Some(_)`, the contained string must be valid UTF-8.
1395+
unsafe fn new_inner(name: Option<CString>) -> Thread {
13381396
// We have to use `unsafe` here to construct the `Parker` in-place,
13391397
// which is required for the UNIX implementation.
13401398
//
13411399
// SAFETY: We pin the Arc immediately after creation, so its address never
13421400
// changes.
13431401
let inner = unsafe {
1344-
let mut arc = Arc::<Inner>::new_uninit();
1402+
let mut arc = Arc::<OtherInner>::new_uninit();
13451403
let ptr = Arc::get_mut_unchecked(&mut arc).as_mut_ptr();
13461404
addr_of_mut!((*ptr).name).write(name);
13471405
addr_of_mut!((*ptr).id).write(ThreadId::new());
13481406
Parker::new_in_place(addr_of_mut!((*ptr).parker));
13491407
Pin::new_unchecked(arc.assume_init())
13501408
};
13511409

1352-
Thread { inner }
1410+
Self(Inner::Other(inner))
13531411
}
13541412

13551413
/// Like the public [`park`], but callable on any handle. This is used to
@@ -1358,7 +1416,7 @@ impl Thread {
13581416
/// # Safety
13591417
/// May only be called from the thread to which this handle belongs.
13601418
pub(crate) unsafe fn park(&self) {
1361-
unsafe { self.inner.as_ref().parker().park() }
1419+
unsafe { self.0.parker().park() }
13621420
}
13631421

13641422
/// Atomically makes the handle's token available if it is not already.
@@ -1394,7 +1452,7 @@ impl Thread {
13941452
#[stable(feature = "rust1", since = "1.0.0")]
13951453
#[inline]
13961454
pub fn unpark(&self) {
1397-
self.inner.as_ref().parker().unpark();
1455+
self.0.parker().unpark();
13981456
}
13991457

14001458
/// Gets the thread's unique identifier.
@@ -1414,7 +1472,7 @@ impl Thread {
14141472
#[stable(feature = "thread_id", since = "1.19.0")]
14151473
#[must_use]
14161474
pub fn id(&self) -> ThreadId {
1417-
self.inner.id
1475+
self.0.id()
14181476
}
14191477

14201478
/// Gets the thread's name.
@@ -1457,15 +1515,7 @@ impl Thread {
14571515
#[stable(feature = "rust1", since = "1.0.0")]
14581516
#[must_use]
14591517
pub fn name(&self) -> Option<&str> {
1460-
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) })
1461-
}
1462-
1463-
fn cname(&self) -> Option<&CStr> {
1464-
match &self.inner.name {
1465-
ThreadName::Main => Some(c"main"),
1466-
ThreadName::Other(other) => Some(&other),
1467-
ThreadName::Unnamed => None,
1468-
}
1518+
self.0.name().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) })
14691519
}
14701520
}
14711521

tests/debuginfo/thread.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
// cdb-check:join_handle,d [Type: std::thread::JoinHandle<tuple$<> >]
1313
// cdb-check: [...] __0 [Type: std::thread::JoinInner<tuple$<> >]
1414
//
15-
// cdb-command:dx t,d
15+
// cdb-command:dx -r3 t,d
1616
// cdb-check:t,d : [...] [Type: std::thread::Thread *]
17-
// cdb-check:[...] inner [...][Type: core::pin::Pin<alloc::sync::Arc<std::thread::Inner,alloc::alloc::Global> >]
17+
// cdb-check: [...] __0 : Other [Type: enum2$<std::thread::Inner>]
18+
// cdb-check: [...] __0 [Type: core::pin::Pin<alloc::sync::Arc<std::thread::OtherInner,[...]> >]
1819

1920
use std::thread;
2021

tests/rustdoc/demo-allocator-54478.rs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
//! }
4141
//!
4242
//! fn main() {
43+
//! drop(String::from("An allocation"));
4344
//! assert!(unsafe { HIT });
4445
//! }
4546
//! ```

tests/ui/runtime/aborting-alloc.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Helper for 'no-allocation-before-main'.
2+
//!
3+
//! This also contains a meta-test to make sure that the AbortingAllocator does indeed abort.
4+
//!
5+
//! -Cprefer-dynamic=no is required as otherwise #[global_allocator] does nothing.
6+
//@ run-fail
7+
//@ compile-flags: -Cprefer-dynamic=no
8+
9+
use std::{sync::atomic::{AtomicBool, Ordering}, alloc::System};
10+
11+
static ABORT: AtomicBool = AtomicBool::new(true);
12+
13+
pub struct AbortingAllocator(System);
14+
15+
unsafe impl std::alloc::GlobalAlloc for AbortingAllocator {
16+
unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
17+
if ABORT.swap(false, Ordering::SeqCst) {
18+
println!("{}", std::backtrace::Backtrace::force_capture());
19+
std::process::abort();
20+
}
21+
22+
self.0.alloc(layout)
23+
}
24+
25+
unsafe fn dealloc(&self, ptr: *mut u8, layout: std::alloc::Layout) {
26+
if ABORT.swap(false, Ordering::SeqCst) {
27+
println!("{}", std::backtrace::Backtrace::force_capture());
28+
std::process::abort();
29+
}
30+
31+
self.0.dealloc(ptr, layout)
32+
}
33+
}
34+
35+
#[global_allocator]
36+
static ALLOCATOR: AbortingAllocator = AbortingAllocator(System);
37+
38+
fn main() {
39+
std::hint::black_box(String::from("An allocation"));
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Tests that a program with no body does not allocate.
2+
//!
3+
//! The initial runtime should not allocate for performance/binary size reasons.
4+
//!
5+
//! -Cprefer-dynamic=no is required as otherwise #[global_allocator] does nothing.
6+
//! We only test linux-gnu as other targets currently need allocation for thread dtors.
7+
//@ run-pass
8+
//@ compile-flags: -Cprefer-dynamic=no -Cdebuginfo=full
9+
//@ only-linux
10+
//@ only-gnu
11+
12+
#[allow(dead_code)]
13+
#[path = "aborting-alloc.rs"]
14+
mod aux;
15+
16+
fn main() {}

0 commit comments

Comments
 (0)