diff options
| author | Ralf Jung <post@ralfj.de> | 2024-11-28 08:05:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-28 08:05:16 +0000 |
| commit | 4624dadc209f595fffc87ef54a54156a5d31cec7 (patch) | |
| tree | dd441ff27ed00a8826fbb8fd042eef1a2293b260 /library/std/src/thread/current.rs | |
| parent | 987d642de5bf3c8e263c477ab728db5cd88c0ec3 (diff) | |
| parent | 8e2d72bac8e29cda5b1457720dcdc66def70d9d4 (diff) | |
Merge pull request #4063 from rust-lang/rustup-2024-11-28
Automatic Rustup
Diffstat (limited to 'library/std/src/thread/current.rs')
| -rw-r--r-- | library/std/src/thread/current.rs | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/library/std/src/thread/current.rs b/library/std/src/thread/current.rs index e6eb90c4c30..1048ef97356 100644 --- a/library/std/src/thread/current.rs +++ b/library/std/src/thread/current.rs @@ -165,6 +165,23 @@ pub(crate) fn try_current() -> Option<Thread> { } } +/// Gets a handle to the thread that invokes it. If the handle stored in thread- +/// local storage was already destroyed, this creates a new unnamed temporary +/// handle to allow thread parking in nearly all situations. +pub(crate) fn current_or_unnamed() -> Thread { + let current = CURRENT.get(); + if current > DESTROYED { + unsafe { + let current = ManuallyDrop::new(Thread::from_raw(current)); + (*current).clone() + } + } else if current == DESTROYED { + Thread::new_unnamed(id::get_or_init()) + } else { + init_current(current) + } +} + /// Gets a handle to the thread that invokes it. /// /// # Examples @@ -226,17 +243,17 @@ fn init_current(current: *mut ()) -> Thread { // a particular API should be entirely allocation-free, feel free to open // an issue on the Rust repository, we'll see what we can do. rtabort!( - "\n - Attempted to access thread-local data while allocating said data.\n - Do not access functions that allocate in the global allocator!\n - This is a bug in the global allocator.\n - " + "\n\ + Attempted to access thread-local data while allocating said data.\n\ + Do not access functions that allocate in the global allocator!\n\ + This is a bug in the global allocator.\n\ + " ) } else { debug_assert_eq!(current, DESTROYED); panic!( - "use of std::thread::current() is not possible after the thread's - local data has been destroyed" + "use of std::thread::current() is not possible after the thread's \ + local data has been destroyed" ) } } |
