diff options
| author | David Thomas <david2005thomas@gmail.com> | 2024-04-04 23:17:15 +0100 |
|---|---|---|
| committer | David Thomas <david2005thomas@gmail.com> | 2024-04-04 23:17:15 +0100 |
| commit | 0989416d2140ffc985dfe9644503e15322e2e412 (patch) | |
| tree | 517e0d1c72e1e3d4b6a6eda6891d2cf1aecf5807 /library/std/src/thread | |
| parent | 703dc9ce64d9b31a239a7280d9b5f9ddd85ffed6 (diff) | |
| download | rust-0989416d2140ffc985dfe9644503e15322e2e412.tar.gz rust-0989416d2140ffc985dfe9644503e15322e2e412.zip | |
Remove rt::init allocation for thread name
Diffstat (limited to 'library/std/src/thread')
| -rw-r--r-- | library/std/src/thread/mod.rs | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index f7eb92bc61e..25219d352aa 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -1247,9 +1247,16 @@ impl ThreadId { // Thread //////////////////////////////////////////////////////////////////////////////// +/// The internal representation of a `Thread`'s name. +enum ThreadName { + Main, + Other(CString), + Unnamed, +} + /// The internal representation of a `Thread` handle struct Inner { - name: Option<CString>, // Guaranteed to be UTF-8 + name: ThreadName, // Guaranteed to be UTF-8 id: ThreadId, parker: Parker, } @@ -1286,8 +1293,20 @@ 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 { + 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. // @@ -1414,7 +1433,11 @@ impl Thread { } fn cname(&self) -> Option<&CStr> { - self.inner.name.as_deref() + match &self.inner.name { + ThreadName::Main => Some(c"main"), + ThreadName::Other(other) => Some(&other), + ThreadName::Unnamed => None, + } } } |
