about summary refs log tree commit diff
path: root/library/std/src/sys/sync/rwlock
diff options
context:
space:
mode:
authorjoboet <jonasboettiger@icloud.com>2024-11-07 17:13:33 +0100
committerjoboet <jonasboettiger@icloud.com>2024-11-18 17:55:36 +0100
commit5a856b82f3a1bf335e0e62d92c800d8436977af1 (patch)
treee976294f3011f40f9bc26551ffe508a1c4314e03 /library/std/src/sys/sync/rwlock
parentc602e9aeaab636302d52b17da64e84044462a230 (diff)
downloadrust-5a856b82f3a1bf335e0e62d92c800d8436977af1.tar.gz
rust-5a856b82f3a1bf335e0e62d92c800d8436977af1.zip
std: allow after-main use of synchronization primitives
By creating an unnamed thread handle when the actual one has already been destroyed, synchronization primitives using thread parking can be used even outside the Rust runtime.

This also fixes an inefficiency in the queue-based `RwLock`: if `thread::current` was not initialized yet, it will create a new handle on every parking attempt without initializing `thread::current`. The private `current_or_unnamed` function introduced here fixes this.
Diffstat (limited to 'library/std/src/sys/sync/rwlock')
-rw-r--r--library/std/src/sys/sync/rwlock/queue.rs6
1 files changed, 2 insertions, 4 deletions
diff --git a/library/std/src/sys/sync/rwlock/queue.rs b/library/std/src/sys/sync/rwlock/queue.rs
index 51330f8fafe..bd15f8ee952 100644
--- a/library/std/src/sys/sync/rwlock/queue.rs
+++ b/library/std/src/sys/sync/rwlock/queue.rs
@@ -118,7 +118,7 @@ use crate::mem;
 use crate::ptr::{self, NonNull, null_mut, without_provenance_mut};
 use crate::sync::atomic::Ordering::{AcqRel, Acquire, Relaxed, Release};
 use crate::sync::atomic::{AtomicBool, AtomicPtr};
-use crate::thread::{self, Thread, ThreadId};
+use crate::thread::{self, Thread};
 
 /// The atomic lock state.
 type AtomicState = AtomicPtr<()>;
@@ -217,9 +217,7 @@ impl Node {
     /// Prepare this node for waiting.
     fn prepare(&mut self) {
         // Fall back to creating an unnamed `Thread` handle to allow locking in TLS destructors.
-        self.thread.get_or_init(|| {
-            thread::try_current().unwrap_or_else(|| Thread::new_unnamed(ThreadId::new()))
-        });
+        self.thread.get_or_init(thread::current_or_unnamed);
         self.completed = AtomicBool::new(false);
     }