summary refs log tree commit diff
path: root/library/std/src/thread
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-05-04 12:37:20 +0200
committerGitHub <noreply@github.com>2024-05-04 12:37:20 +0200
commitb8fa047398188dc773c5797c9191f6baf7d6da7f (patch)
treef28ed261386cd5ed2e023eb172ff2a47d85b78cd /library/std/src/thread
parent6d721dd0b80ad5382d38481a2477f7404b73e717 (diff)
parent37c1758214bfa52d791d43ff73e07573e5db653d (diff)
downloadrust-b8fa047398188dc773c5797c9191f6baf7d6da7f.tar.gz
rust-b8fa047398188dc773c5797c9191f6baf7d6da7f.zip
Rollup merge of #123356 - joboet:set_current_size, r=ChrisDenton
Reduce code size of `thread::set_current`

#123265 introduced a rather large binary size regression, because it added an `unwrap()` call on a `Result<(), Thread>`, which in turn pulled its rather heavy `Debug` implementation. This PR fixes this by readding the `rtassert!` that was removed.
Diffstat (limited to 'library/std/src/thread')
-rw-r--r--library/std/src/thread/mod.rs9
1 files changed, 7 insertions, 2 deletions
diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs
index 604eb05040b..297432c8ca9 100644
--- a/library/std/src/thread/mod.rs
+++ b/library/std/src/thread/mod.rs
@@ -703,9 +703,14 @@ thread_local! {
 
 /// Sets the thread handle for the current thread.
 ///
-/// Panics if the handle has been set already or when called from a TLS destructor.
+/// Aborts if the handle has been set already to reduce code size.
 pub(crate) fn set_current(thread: Thread) {
-    CURRENT.with(|current| current.set(thread).unwrap());
+    // Using `unwrap` here can add ~3kB to the binary size. We have complete
+    // control over where this is called, so just abort if there is a bug.
+    CURRENT.with(|current| match current.set(thread) {
+        Ok(()) => {}
+        Err(_) => rtabort!("thread::set_current should only be called once per thread"),
+    });
 }
 
 /// Gets a handle to the thread that invokes it.