From e7b5730d36740035b9cf58b759be87d0d88515a7 Mon Sep 17 00:00:00 2001 From: joboet Date: Tue, 2 Apr 2024 11:27:34 +0200 Subject: [PATCH 1/3] std: reduce code size of `set_current` --- library/std/src/thread/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index f7eb92bc61e2f..a97122ca12034 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -684,9 +684,12 @@ 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()); + CURRENT.with(|current| match current.set(thread) { + Ok(()) => {} + Err(_) => rtabort!("should only be set once"), + }); } /// Gets a handle to the thread that invokes it. From 061d8731fb0ab1d57d7a289fd0ded173ceef8a64 Mon Sep 17 00:00:00 2001 From: joboet Date: Wed, 3 Apr 2024 14:33:43 +0200 Subject: [PATCH 2/3] std: add comment about abort motivation --- library/std/src/thread/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index a97122ca12034..ae3dfef487069 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -686,6 +686,8 @@ thread_local! { /// /// Aborts if the handle has been set already to reduce code size. pub(crate) fn set_current(thread: Thread) { + // 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!("should only be set once"), From 37c1758214bfa52d791d43ff73e07573e5db653d Mon Sep 17 00:00:00 2001 From: joboet Date: Mon, 8 Apr 2024 12:17:19 +0200 Subject: [PATCH 3/3] std: update abort message in `thread::set_current` --- library/std/src/thread/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index ae3dfef487069..9cdc20b1f282f 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -690,7 +690,7 @@ pub(crate) fn set_current(thread: Thread) { // control over where this is called, so just abort if there is a bug. CURRENT.with(|current| match current.set(thread) { Ok(()) => {} - Err(_) => rtabort!("should only be set once"), + Err(_) => rtabort!("thread::set_current should only be called once per thread"), }); }