diff options
| author | joboet <jonasboettiger@icloud.com> | 2024-07-18 13:59:48 +0200 |
|---|---|---|
| committer | joboet <jonasboettiger@icloud.com> | 2024-10-02 18:04:21 +0200 |
| commit | d868fdce6b9ddef6abcc8de86b3ba8459def36a2 (patch) | |
| tree | b03a6ba5ff19869e0ebdeefb996e4387a5bb13b9 /library/std/src/thread/local | |
| parent | 07f08ffb2dbc864d2127abedf7a5917b965c0a4b (diff) | |
std: make `thread::current` available in all `thread_local!` destructors
Diffstat (limited to 'library/std/src/thread/local')
| -rw-r--r-- | library/std/src/thread/local/tests.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/library/std/src/thread/local/tests.rs b/library/std/src/thread/local/tests.rs index 6abb9b85a2e..9d4f52a0921 100644 --- a/library/std/src/thread/local/tests.rs +++ b/library/std/src/thread/local/tests.rs @@ -1,7 +1,7 @@ use crate::cell::{Cell, UnsafeCell}; use crate::sync::atomic::{AtomicU8, Ordering}; use crate::sync::{Arc, Condvar, Mutex}; -use crate::thread::{self, LocalKey}; +use crate::thread::{self, Builder, LocalKey}; use crate::thread_local; #[derive(Clone, Default)] @@ -343,3 +343,34 @@ fn join_orders_after_tls_destructors() { jh2.join().unwrap(); } } + +// Test that thread::current is still available in TLS destructors. +#[test] +fn thread_current_in_dtor() { + // Go through one round of TLS destruction first. + struct Defer; + impl Drop for Defer { + fn drop(&mut self) { + RETRIEVE.with(|_| {}); + } + } + + struct RetrieveName; + impl Drop for RetrieveName { + fn drop(&mut self) { + *NAME.lock().unwrap() = Some(thread::current().name().unwrap().to_owned()); + } + } + + static NAME: Mutex<Option<String>> = Mutex::new(None); + + thread_local! { + static DEFER: Defer = const { Defer }; + static RETRIEVE: RetrieveName = const { RetrieveName }; + } + + Builder::new().name("test".to_owned()).spawn(|| DEFER.with(|_| {})).unwrap().join().unwrap(); + let name = NAME.lock().unwrap(); + let name = name.as_ref().unwrap(); + assert_eq!(name, "test"); +} |
