Skip to content

Rollup of 9 pull requests #127998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Jul 20, 2024
Merged
Changes from 5 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
83e1efb
Replace a long inline "autoref" comment with method docs
Zalathar Jul 10, 2024
ec6e07b
Migrate `crate-hash-rustc-version` to `rmake`
Rejyr Jul 13, 2024
eea6502
Use `llvm-readobj` for `run-make/crate-hash-rustc-version`
Rejyr Jul 15, 2024
a605e2f
Safely enforce thread name requirements
ChrisDenton Jul 18, 2024
939ee38
Make `Thread::new_inner` a safe function
ChrisDenton Jul 18, 2024
8e4a920
Style change
ChrisDenton Jul 18, 2024
9432955
Move ThreadName conversions to &cstr/&str
ChrisDenton Jul 18, 2024
9747a2c
fixes panic error
surechen Jul 19, 2024
a8d7121
uefi: Add process
Ayush1325 Mar 26, 2024
6737a02
uefi: process: Add support to capture stdout
Ayush1325 Mar 29, 2024
87d7a07
uefi: process: Add stderr support
Ayush1325 Mar 29, 2024
7253765
uefi: process: Add null protocol
Ayush1325 Mar 29, 2024
d44b3fb
uefi: process Implement inherit
Ayush1325 Mar 29, 2024
29c198c
uefi: process: Add support for args
Ayush1325 Mar 29, 2024
c899e05
uefi: process: Add CommandArgs support
Ayush1325 Mar 29, 2024
56e2a57
uefi: process: Final Touchups
Ayush1325 Mar 29, 2024
e290398
uefi: process: Fixes from PR
Ayush1325 Mar 30, 2024
ae82726
Conditionally build `wasm-component-ld`
alexcrichton Jul 17, 2024
f0a2b5b
Add a change tracker entry
alexcrichton Jul 18, 2024
aef0e34
Avoid ref when using format! in compiler
nyurik Jul 19, 2024
3ff7588
More accurate suggestion for `-> Box<dyn Trait>` or `-> impl Trait`
estebank Jul 19, 2024
8bcf0b4
Avoid ref when using format! in compiler
nyurik Jul 19, 2024
bc86893
Rollup merge of #123196 - Ayush1325:uefi-process, r=joboet
matthiaskrgr Jul 20, 2024
dfee7ed
Rollup merge of #127556 - Zalathar:autoref, r=Nadrieril
matthiaskrgr Jul 20, 2024
aa6ae4b
Rollup merge of #127693 - Rejyr:migrate-crate-hash-rustc-version-rmak…
matthiaskrgr Jul 20, 2024
4f20ee5
Rollup merge of #127866 - alexcrichton:disable-wasm-component-ld-by-d…
matthiaskrgr Jul 20, 2024
4da2869
Rollup merge of #127918 - ChrisDenton:thread-name-string, r=joboet
matthiaskrgr Jul 20, 2024
767b3cb
Rollup merge of #127948 - surechen:fix_127915, r=compiler-errors
matthiaskrgr Jul 20, 2024
cd8c5f7
Rollup merge of #127980 - nyurik:compiler-refs, r=oli-obk
matthiaskrgr Jul 20, 2024
40cfc88
Rollup merge of #127984 - nyurik:src-refs, r=onur-ozkan
matthiaskrgr Jul 20, 2024
89798e9
Rollup merge of #127987 - estebank:impl-trait-sugg, r=cjgillot
matthiaskrgr Jul 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 51 additions & 23 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ mod tests;
use crate::any::Any;
use crate::cell::{Cell, OnceCell, UnsafeCell};
use crate::env;
use crate::ffi::{CStr, CString};
use crate::ffi::CStr;
use crate::fmt;
use crate::io;
use crate::marker::PhantomData;
Expand Down Expand Up @@ -487,11 +487,7 @@ impl Builder {
amt
});

let my_thread = name.map_or_else(Thread::new_unnamed, |name| unsafe {
Thread::new(
CString::new(name).expect("thread name may not contain interior null bytes"),
)
});
let my_thread = name.map_or_else(Thread::new_unnamed, Thread::new);
let their_thread = my_thread.clone();

let my_packet: Arc<Packet<'scope, T>> = Arc::new(Packet {
Expand Down Expand Up @@ -1299,10 +1295,51 @@ impl ThreadId {
/// The internal representation of a `Thread`'s name.
enum ThreadName {
Main,
Other(CString),
Other(ThreadNameString),
Unnamed,
}

// This module ensures private fields are kept private, which is necessary to enforce the safety requirements.
mod thread_name_string {
use super::ThreadName;
use crate::ffi::{CStr, CString};
use core::str;

/// Like a `String` it's guaranteed UTF-8 and like a `CString` it's null terminated.
pub(crate) struct ThreadNameString {
inner: CString,
}
impl core::ops::Deref for ThreadNameString {
type Target = CStr;
fn deref(&self) -> &CStr {
&self.inner
}
}
impl From<String> for ThreadNameString {
fn from(s: String) -> Self {
Self {
inner: CString::new(s).expect("thread name may not contain interior null bytes"),
}
}
}
impl ThreadName {
pub fn as_cstr(&self) -> Option<&CStr> {
match self {
ThreadName::Main => Some(c"main"),
ThreadName::Other(other) => Some(other),
ThreadName::Unnamed => None,
}
}

pub fn as_str(&self) -> Option<&str> {
// SAFETY: `as_cstr` can only return `Some` for a fixed CStr or a `ThreadNameString`,
// which is guaranteed to be UTF-8.
self.as_cstr().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) })
}
}
}
pub(crate) use thread_name_string::ThreadNameString;

/// The internal representation of a `Thread` handle
struct Inner {
name: ThreadName, // Guaranteed to be UTF-8
Expand Down Expand Up @@ -1342,25 +1379,20 @@ pub struct Thread {

impl Thread {
/// Used only internally to construct a thread object without spawning.
///
/// # Safety
/// `name` must be valid UTF-8.
pub(crate) unsafe fn new(name: CString) -> Thread {
unsafe { Self::new_inner(ThreadName::Other(name)) }
pub(crate) fn new(name: String) -> Thread {
Self::new_inner(ThreadName::Other(name.into()))
}

pub(crate) fn new_unnamed() -> Thread {
unsafe { Self::new_inner(ThreadName::Unnamed) }
Self::new_inner(ThreadName::Unnamed)
}

// Used in runtime to construct main thread
pub(crate) fn new_main() -> Thread {
unsafe { Self::new_inner(ThreadName::Main) }
Self::new_inner(ThreadName::Main)
}

/// # Safety
/// If `name` is `ThreadName::Other(_)`, the contained string must be valid UTF-8.
unsafe fn new_inner(name: ThreadName) -> Thread {
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.
//
Expand Down Expand Up @@ -1483,15 +1515,11 @@ 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()) })
self.inner.name.as_str()
}

fn cname(&self) -> Option<&CStr> {
match &self.inner.name {
ThreadName::Main => Some(c"main"),
ThreadName::Other(other) => Some(&other),
ThreadName::Unnamed => None,
}
self.inner.name.as_cstr()
}
}

Expand Down