Skip to content

Commit

Permalink
Auto merge of rust-lang#123550 - GnomedDev:remove-initial-arc, r=<try>
Browse files Browse the repository at this point in the history
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 currently does not have a regression test to prevent future changes from re-adding allocations pre-main as I'm [having trouble](GnomedDev@6f7be53) implementing it, but if wanted I can draft this PR until that test is ready.
  • Loading branch information
bors committed Apr 6, 2024
2 parents 83d0a94 + d5b8b00 commit 666bbff
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 46 deletions.
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
#![feature(str_internals)]
#![feature(strict_provenance)]
#![feature(strict_provenance_atomic_ptr)]
#![feature(sync_unsafe_cell)]
// tidy-alphabetical-end
//
// Library features (alloc):
Expand Down
123 changes: 77 additions & 46 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@
mod tests;

use crate::any::Any;
use crate::cell::SyncUnsafeCell;
use crate::cell::{OnceCell, UnsafeCell};
use crate::ffi::{CStr, CString};
use crate::fmt;
use crate::io;
use crate::marker::PhantomData;
use crate::mem::MaybeUninit;
use crate::mem::{self, forget};
use crate::num::NonZero;
use crate::panic;
Expand Down Expand Up @@ -511,7 +513,7 @@ impl Builder {

let f = MaybeDangling::new(f);
let main = move || {
if let Some(name) = their_thread.cname() {
if let Some(name) = their_thread.0.name() {
imp::Thread::set_name(name);
}

Expand Down Expand Up @@ -1143,7 +1145,7 @@ pub fn park_timeout(dur: Duration) {
let guard = PanicGuard;
// SAFETY: park_timeout is called on the parker owned by this thread.
unsafe {
current().inner.as_ref().parker().park_timeout(dur);
current().0.parker().park_timeout(dur);
}
// No panic occurred, do not abort.
forget(guard);
Expand Down Expand Up @@ -1182,7 +1184,12 @@ pub fn park_timeout(dur: Duration) {
pub struct ThreadId(NonZero<u64>);

impl ThreadId {
// Generate a new unique thread ID.
/// Generate a new unique thread ID.
///
/// The current implementation starts at 2 and increments from there.
///
/// This is as `1` is the value for the main thread, so std::thread::Thread does not
/// have to store this value when creating the main thread's information.
fn new() -> ThreadId {
#[cold]
fn exhausted() -> ! {
Expand All @@ -1193,7 +1200,7 @@ impl ThreadId {
if #[cfg(target_has_atomic = "64")] {
use crate::sync::atomic::{AtomicU64, Ordering::Relaxed};

static COUNTER: AtomicU64 = AtomicU64::new(0);
static COUNTER: AtomicU64 = AtomicU64::new(1);

let mut last = COUNTER.load(Relaxed);
loop {
Expand All @@ -1209,7 +1216,7 @@ impl ThreadId {
} else {
use crate::sync::{Mutex, PoisonError};

static COUNTER: Mutex<u64> = Mutex::new(0);
static COUNTER: Mutex<u64> = Mutex::new(1);

let mut counter = COUNTER.lock().unwrap_or_else(PoisonError::into_inner);
let Some(id) = counter.checked_add(1) else {
Expand All @@ -1226,6 +1233,11 @@ impl ThreadId {
}
}

/// Creates a ThreadId with the ID of the main thread.
fn new_main() -> Self {
Self(NonZero::<u64>::MIN)
}

/// This returns a numeric identifier for the thread identified by this
/// `ThreadId`.
///
Expand All @@ -1245,23 +1257,54 @@ impl ThreadId {
// Thread
////////////////////////////////////////////////////////////////////////////////

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

/// The internal representation of a `Thread` handle
struct Inner {
name: ThreadName, // Guaranteed to be UTF-8
/// The internal representation of a `Thread` that is not the main thread.
struct OtherInner {
name: Option<CString>, // Guaranteed to be UTF-8
id: ThreadId,
parker: Parker,
}

/// The internal representation of a `Thread` handle.
#[derive(Clone)]
enum Inner {
Main,
Other(Pin<Arc<OtherInner>>),
}

impl Inner {
fn parker(self: Pin<&Self>) -> Pin<&Parker> {
unsafe { Pin::map_unchecked(self, |inner| &inner.parker) }
fn id(&self) -> ThreadId {
match self {
Self::Main => ThreadId::new_main(),
Self::Other(other) => other.id,
}
}

fn name(&self) -> Option<&CStr> {
match self {
Self::Main => Some(c"main"),
Self::Other(other) => other.name.as_deref(),
}
}

fn parker(&self) -> Pin<&Parker> {
match self {
Self::Main => {
// Safety: MAIN_PARKER only ever has a mutable reference when Inner::Main is initialised.
let uninit_ref: &'static MaybeUninit<Parker> = unsafe { &*MAIN_PARKER.get() };

// Safety: MAIN_PARKER is initialised when Inner::Main is initialised.
let parker_ref = unsafe { uninit_ref.assume_init_ref() };

Pin::static_ref(parker_ref)
}
Self::Other(inner) => unsafe {
Pin::map_unchecked(inner.as_ref(), |inner| &inner.parker)
},
}
}
}

Expand All @@ -1285,41 +1328,37 @@ impl Inner {
/// docs of [`Builder`] and [`spawn`] for more details.
///
/// [`thread::current`]: current
pub struct Thread {
inner: Pin<Arc<Inner>>,
}
pub struct Thread(Inner);

impl Thread {
// Used only internally to construct a thread object without spawning
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,
// which is required for the UNIX implementation.
//
// SAFETY: We pin the Arc immediately after creation, so its address never
// changes.
let inner = unsafe {
let mut arc = Arc::<Inner>::new_uninit();
let mut arc = Arc::<OtherInner>::new_uninit();
let ptr = Arc::get_mut_unchecked(&mut arc).as_mut_ptr();
addr_of_mut!((*ptr).name).write(name);
addr_of_mut!((*ptr).id).write(ThreadId::new());
Parker::new_in_place(addr_of_mut!((*ptr).parker));
Pin::new_unchecked(arc.assume_init())
};

Thread { inner }
Self(Inner::Other(inner))
}

/// # Safety
///
/// This must only ever be called once, and must be called on the main thread.
pub(crate) unsafe fn new_main() -> Thread {
// Safety: Caller responsible for holding the mutable invariant and
// Parker::new_in_place does not read from the uninit value.
unsafe { Parker::new_in_place(MAIN_PARKER.get().cast()) }

Self(Inner::Main)
}

/// Like the public [`park`], but callable on any handle. This is used to
Expand All @@ -1328,7 +1367,7 @@ impl Thread {
/// # Safety
/// May only be called from the thread to which this handle belongs.
pub(crate) unsafe fn park(&self) {
unsafe { self.inner.as_ref().parker().park() }
unsafe { self.0.parker().park() }
}

/// Atomically makes the handle's token available if it is not already.
Expand Down Expand Up @@ -1364,7 +1403,7 @@ impl Thread {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn unpark(&self) {
self.inner.as_ref().parker().unpark();
self.0.parker().unpark();
}

/// Gets the thread's unique identifier.
Expand All @@ -1384,7 +1423,7 @@ impl Thread {
#[stable(feature = "thread_id", since = "1.19.0")]
#[must_use]
pub fn id(&self) -> ThreadId {
self.inner.id
self.0.id()
}

/// Gets the thread's name.
Expand Down Expand Up @@ -1427,15 +1466,7 @@ impl Thread {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
pub fn name(&self) -> Option<&str> {
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) })
}

fn cname(&self) -> Option<&CStr> {
match &self.inner.name {
ThreadName::Main => Some(c"main"),
ThreadName::Other(other) => Some(&other),
ThreadName::Unnamed => None,
}
self.0.name().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) })
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/rustdoc/demo-allocator-54478.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
//! }
//!
//! fn main() {
//! drop(std::env::var("PWD"));
//! assert!(unsafe { HIT });
//! }
//! ```

0 comments on commit 666bbff

Please sign in to comment.