Skip to content

Commit 7668418

Browse files
committed
std: move thread::current TLS variable out of thread_info
1 parent 5b9d7ab commit 7668418

File tree

16 files changed

+29
-176
lines changed

16 files changed

+29
-176
lines changed

library/std/src/panicking.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::sync::atomic::{AtomicBool, Ordering};
2121
use crate::sync::{PoisonError, RwLock};
2222
use crate::sys::stdio::panic_output;
2323
use crate::sys_common::backtrace;
24-
use crate::sys_common::thread_info;
2524
use crate::thread;
2625

2726
#[cfg(not(test))]
@@ -256,7 +255,7 @@ fn default_hook(info: &PanicInfo<'_>) {
256255
None => "Box<dyn Any>",
257256
},
258257
};
259-
let thread = thread_info::current_thread();
258+
let thread = thread::try_current();
260259
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
261260

262261
let write = |err: &mut dyn crate::io::Write| {

library/std/src/rt.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ pub use core::panicking::{panic_display, panic_fmt};
2424

2525
use crate::sync::Once;
2626
use crate::sys;
27-
use crate::sys_common::thread_info;
28-
use crate::thread::Thread;
27+
use crate::thread::{self, Thread};
2928

3029
// Prints to the "panic output", depending on the platform this may be:
3130
// - the standard error output
@@ -96,13 +95,12 @@ unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
9695
unsafe {
9796
sys::init(argc, argv, sigpipe);
9897

99-
let main_guard = sys::thread::guard::init();
10098
// Next, set up the current Thread with the guard information we just
10199
// created. Note that this isn't necessary in general for new threads,
102100
// but we just do this to name the main thread and to give it correct
103101
// info about the stack bounds.
104102
let thread = Thread::new(Some(rtunwrap!(Ok, CString::new("main"))));
105-
thread_info::set(main_guard, thread);
103+
thread::set_current(thread);
106104
}
107105
}
108106

library/std/src/sys/pal/hermit/thread.rs

-10
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,3 @@ impl Thread {
104104
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
105105
unsafe { Ok(NonZero::new_unchecked(abi::get_processor_count())) }
106106
}
107-
108-
pub mod guard {
109-
pub type Guard = !;
110-
pub unsafe fn current() -> Option<Guard> {
111-
None
112-
}
113-
pub unsafe fn init() -> Option<Guard> {
114-
None
115-
}
116-
}

library/std/src/sys/pal/itron/thread.rs

-10
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,6 @@ impl Drop for Thread {
312312
}
313313
}
314314

315-
pub mod guard {
316-
pub type Guard = !;
317-
pub unsafe fn current() -> Option<Guard> {
318-
None
319-
}
320-
pub unsafe fn init() -> Option<Guard> {
321-
None
322-
}
323-
}
324-
325315
/// Terminate and delete the specified task.
326316
///
327317
/// This function will abort if `deleted_task` refers to the calling task.

library/std/src/sys/pal/sgx/thread.rs

-10
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,3 @@ impl Thread {
149149
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
150150
unsupported()
151151
}
152-
153-
pub mod guard {
154-
pub type Guard = !;
155-
pub unsafe fn current() -> Option<Guard> {
156-
None
157-
}
158-
pub unsafe fn init() -> Option<Guard> {
159-
None
160-
}
161-
}

library/std/src/sys/pal/teeos/thread.rs

-12
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,6 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
151151
))
152152
}
153153

154-
// stub
155-
pub mod guard {
156-
use crate::ops::Range;
157-
pub type Guard = Range<usize>;
158-
pub unsafe fn current() -> Option<Guard> {
159-
None
160-
}
161-
pub unsafe fn init() -> Option<Guard> {
162-
None
163-
}
164-
}
165-
166154
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
167155
libc::PTHREAD_STACK_MIN.try_into().expect("Infallible")
168156
}

library/std/src/sys/pal/uefi/thread.rs

-10
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,3 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
5252
// UEFI is single threaded
5353
Ok(NonZero::new(1).unwrap())
5454
}
55-
56-
pub mod guard {
57-
pub type Guard = !;
58-
pub unsafe fn current() -> Option<Guard> {
59-
None
60-
}
61-
pub unsafe fn init() -> Option<Guard> {
62-
None
63-
}
64-
}

library/std/src/sys/pal/unix/thread.rs

-11
Original file line numberDiff line numberDiff line change
@@ -729,17 +729,6 @@ mod cgroups {
729729
}
730730
}
731731

732-
pub mod guard {
733-
use crate::ops::Range;
734-
pub type Guard = Range<usize>;
735-
pub unsafe fn current() -> Option<Guard> {
736-
None
737-
}
738-
pub unsafe fn init() -> Option<Guard> {
739-
None
740-
}
741-
}
742-
743732
// glibc >= 2.15 has a __pthread_get_minstack() function that returns
744733
// PTHREAD_STACK_MIN plus bytes needed for thread-local storage.
745734
// We need that information to avoid blowing up when a small stack

library/std/src/sys/pal/unsupported/thread.rs

-10
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,3 @@ impl Thread {
3838
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
3939
unsupported()
4040
}
41-
42-
pub mod guard {
43-
pub type Guard = !;
44-
pub unsafe fn current() -> Option<Guard> {
45-
None
46-
}
47-
pub unsafe fn init() -> Option<Guard> {
48-
None
49-
}
50-
}

library/std/src/sys/pal/wasi/thread.rs

-10
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,3 @@ impl Thread {
193193
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
194194
unsupported()
195195
}
196-
197-
pub mod guard {
198-
pub type Guard = !;
199-
pub unsafe fn current() -> Option<Guard> {
200-
None
201-
}
202-
pub unsafe fn init() -> Option<Guard> {
203-
None
204-
}
205-
}

library/std/src/sys/pal/windows/thread.rs

-11
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,3 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
144144
cpus => Ok(unsafe { NonZero::new_unchecked(cpus) }),
145145
}
146146
}
147-
148-
#[cfg_attr(test, allow(dead_code))]
149-
pub mod guard {
150-
pub type Guard = !;
151-
pub unsafe fn current() -> Option<Guard> {
152-
None
153-
}
154-
pub unsafe fn init() -> Option<Guard> {
155-
None
156-
}
157-
}

library/std/src/sys/pal/xous/thread.rs

-10
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,3 @@ pub fn available_parallelism() -> io::Result<NonZero<usize>> {
140140
// We're unicore right now.
141141
Ok(unsafe { NonZero::new_unchecked(1) })
142142
}
143-
144-
pub mod guard {
145-
pub type Guard = !;
146-
pub unsafe fn current() -> Option<Guard> {
147-
None
148-
}
149-
pub unsafe fn init() -> Option<Guard> {
150-
None
151-
}
152-
}

library/std/src/sys/sync/rwlock/queue.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ use crate::sync::atomic::{
115115
AtomicBool, AtomicPtr,
116116
Ordering::{AcqRel, Acquire, Relaxed, Release},
117117
};
118-
use crate::sys_common::thread_info;
119-
use crate::thread::Thread;
118+
use crate::thread::{self, Thread};
120119

121120
// Locking uses exponential backoff. `SPIN_COUNT` indicates how many times the
122121
// locking operation will be retried.
@@ -203,8 +202,7 @@ impl Node {
203202
fn prepare(&mut self) {
204203
// Fall back to creating an unnamed `Thread` handle to allow locking in
205204
// TLS destructors.
206-
self.thread
207-
.get_or_init(|| thread_info::current_thread().unwrap_or_else(|| Thread::new(None)));
205+
self.thread.get_or_init(|| thread::try_current().unwrap_or_else(|| Thread::new(None)));
208206
self.completed = AtomicBool::new(false);
209207
}
210208

library/std/src/sys_common/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub mod io;
2626
pub mod lazy_box;
2727
pub mod process;
2828
pub mod thread;
29-
pub mod thread_info;
3029
pub mod thread_local_dtor;
3130
pub mod thread_parking;
3231
pub mod wstr;

library/std/src/sys_common/thread_info.rs

-53
This file was deleted.

library/std/src/thread/mod.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
mod tests;
160160

161161
use crate::any::Any;
162-
use crate::cell::UnsafeCell;
162+
use crate::cell::{OnceCell, UnsafeCell};
163163
use crate::ffi::{CStr, CString};
164164
use crate::fmt;
165165
use crate::io;
@@ -174,7 +174,6 @@ use crate::str;
174174
use crate::sync::Arc;
175175
use crate::sys::thread as imp;
176176
use crate::sys_common::thread;
177-
use crate::sys_common::thread_info;
178177
use crate::sys_common::thread_parking::Parker;
179178
use crate::sys_common::{AsInner, IntoInner};
180179
use crate::time::{Duration, Instant};
@@ -518,12 +517,8 @@ impl Builder {
518517

519518
crate::io::set_output_capture(output_capture);
520519

521-
// SAFETY: we constructed `f` initialized.
522520
let f = f.into_inner();
523-
// SAFETY: the stack guard passed is the one for the current thread.
524-
// This means the current thread's stack and the new thread's stack
525-
// are properly set and protected from each other.
526-
thread_info::set(unsafe { imp::guard::current() }, their_thread);
521+
set_current(their_thread);
527522
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
528523
crate::sys_common::backtrace::__rust_begin_short_backtrace(f)
529524
}));
@@ -683,6 +678,27 @@ where
683678
Builder::new().spawn(f).expect("failed to spawn thread")
684679
}
685680

681+
thread_local! {
682+
static CURRENT: OnceCell<Thread> = const { OnceCell::new() };
683+
}
684+
685+
/// Sets the thread handle for the current thread.
686+
///
687+
/// Panics if the handle has been set already or when called from a TLS destructor.
688+
pub(crate) fn set_current(thread: Thread) {
689+
CURRENT.with(|current| current.set(thread).unwrap());
690+
}
691+
692+
/// Gets a handle to the thread that invokes it.
693+
///
694+
/// In contrast to the public `current` function, this will not panic if called
695+
/// from inside a TLS destructor.
696+
pub(crate) fn try_current() -> Option<Thread> {
697+
CURRENT
698+
.try_with(|current| current.get_or_init(|| Thread::new(imp::Thread::get_name())).clone())
699+
.ok()
700+
}
701+
686702
/// Gets a handle to the thread that invokes it.
687703
///
688704
/// # Examples
@@ -705,7 +721,7 @@ where
705721
#[must_use]
706722
#[stable(feature = "rust1", since = "1.0.0")]
707723
pub fn current() -> Thread {
708-
thread_info::current_thread().expect(
724+
try_current().expect(
709725
"use of std::thread::current() is not possible \
710726
after the thread's local data has been destroyed",
711727
)

0 commit comments

Comments
 (0)