]> git.lizzy.rs Git - rust.git/commitdiff
std: rename `Parker::new` to `Parker::new_in_place`, add safe `Parker::new` construct...
authorjoboet <jonasboettiger@icloud.com>
Fri, 30 Dec 2022 14:49:47 +0000 (15:49 +0100)
committerjoboet <jonasboettiger@icloud.com>
Fri, 30 Dec 2022 14:49:47 +0000 (15:49 +0100)
library/std/src/sys/sgx/thread.rs
library/std/src/sys/unix/thread_parking/darwin.rs
library/std/src/sys/unix/thread_parking/pthread.rs
library/std/src/sys/windows/thread_parking.rs
library/std/src/sys_common/thread_parking/futex.rs
library/std/src/sys_common/thread_parking/generic.rs
library/std/src/sys_common/thread_parking/id.rs
library/std/src/sys_common/thread_parking/wait_flag.rs
library/std/src/thread/mod.rs

index 8c3d8c37a192d4f21ebfbefba3d7dd0786d5ca36..1608b8cb642dc858ba13482b2d1f5c72a0ba6bfa 100644 (file)
@@ -65,7 +65,6 @@ pub(super) fn lock() -> MutexGuard<'static, Vec<Task>> {
 /// execution. The signal is sent once all TLS destructors have finished at
 /// which point no new thread locals should be created.
 pub mod wait_notify {
-    use crate::mem::MaybeUninit;
     use crate::pin::Pin;
     use crate::sync::Arc;
     use crate::sys_common::thread_parking::Parker;
@@ -88,25 +87,14 @@ impl Waiter {
         /// called, this will return immediately, otherwise the current thread
         /// is blocked until notified.
         pub fn wait(self) {
-            // This is not actually `unsafe`, but it uses the `Parker` API,
-            // which needs `unsafe` on some platforms.
+            // SAFETY:
+            // This is only ever called on one thread.
             unsafe { Pin::new(&*self.0).park() }
         }
     }
 
     pub fn new() -> (Notifier, Waiter) {
-        // Safety:
-        // Some other platforms (looking at you, UNIX!) require that the thread
-        // parker is constructed in-place. This is just a noisy way of writing:
-        // ```rust
-        // let parker = Parker::new();
-        // ```
-        let parker = unsafe {
-            let mut place = MaybeUninit::uninit();
-            Parker::new(place.as_mut_ptr());
-            place.assume_init()
-        };
-        let inner = Arc::new(parker);
+        let inner = Arc::new(Parker::new());
         (Notifier(inner.clone()), Waiter(inner))
     }
 }
index 2f5356fe2276bbb31df2c0dea47d115364860fdc..b709fada3b4a805f1421f7c408f9ac266caf9115 100644 (file)
@@ -46,7 +46,7 @@ unsafe impl Sync for Parker {}
 unsafe impl Send for Parker {}
 
 impl Parker {
-    pub unsafe fn new(parker: *mut Parker) {
+    pub unsafe fn new_in_place(parker: *mut Parker) {
         let semaphore = dispatch_semaphore_create(0);
         assert!(
             !semaphore.is_null(),
index 510168a010fcd3541fcd69e4fcf993648eb154f0..082d25e68f587ad9a424b076b348a2814b011894 100644 (file)
@@ -99,7 +99,7 @@ impl Parker {
     ///
     /// # Safety
     /// The constructed parker must never be moved.
-    pub unsafe fn new(parker: *mut Parker) {
+    pub unsafe fn new_in_place(parker: *mut Parker) {
         // Use the default mutex implementation to allow for simpler initialization.
         // This could lead to undefined behaviour when deadlocking. This is avoided
         // by not deadlocking. Note in particular the unlocking operation before any
index 2f7ae863b6a453b48e389671e24d932646b1668f..5d43676adbb11f0428d99c796fe58eaa3b3b2c45 100644 (file)
@@ -97,7 +97,7 @@ pub struct Parker {
 impl Parker {
     /// Construct the Windows parker. The UNIX parker implementation
     /// requires this to happen in-place.
-    pub unsafe fn new(parker: *mut Parker) {
+    pub unsafe fn new_in_place(parker: *mut Parker) {
         parker.write(Self { state: AtomicI8::new(EMPTY) });
     }
 
index d9e2f39e3451848bea88a071b3a293d24b82e517..588e7b27826f6633ed6ba4c6b8f8f95192c52aac 100644 (file)
@@ -35,7 +35,7 @@ pub struct Parker {
 impl Parker {
     /// Construct the futex parker. The UNIX parker implementation
     /// requires this to happen in-place.
-    pub unsafe fn new(parker: *mut Parker) {
+    pub unsafe fn new_in_place(parker: *mut Parker) {
         parker.write(Self { state: AtomicU32::new(EMPTY) });
     }
 
index f3d8b34d3fd39279b20887f50081e2788b65f470..3209bffe353ed81b775a8b7869789824d1a052b1 100644 (file)
@@ -19,7 +19,7 @@ pub struct Parker {
 impl Parker {
     /// Construct the generic parker. The UNIX parker implementation
     /// requires this to happen in-place.
-    pub unsafe fn new(parker: *mut Parker) {
+    pub unsafe fn new_in_place(parker: *mut Parker) {
         parker.write(Parker {
             state: AtomicUsize::new(EMPTY),
             lock: Mutex::new(()),
index 32e2195b80828256aed5a91dfc91335770dde638..e98169597c378011d3e044251d69e813fbd119f6 100644 (file)
@@ -26,9 +26,13 @@ pub struct Parker {
 const NOTIFIED: i8 = 1;
 
 impl Parker {
+    pub fn new() -> Parker {
+        Parker { state: AtomicI8::new(EMPTY), tid: UnsafeCell::new(None) }
+    }
+
     /// Create a new thread parker. UNIX requires this to happen in-place.
-    pub unsafe fn new(parker: *mut Parker) {
-        parker.write(Parker { state: AtomicI8::new(EMPTY), tid: UnsafeCell::new(None) })
+    pub unsafe fn new_in_place(parker: *mut Parker) {
+        parker.write(Parker::new())
     }
 
     /// # Safety
index 6561c186655a5cbbb61e1f9194394c22b6981211..d0f8899a94eb8507ff233f5d8321bf576fc8827e 100644 (file)
@@ -41,7 +41,7 @@ pub struct Parker {
 impl Parker {
     /// Construct a parker for the current thread. The UNIX parker
     /// implementation requires this to happen in-place.
-    pub unsafe fn new(parker: *mut Parker) {
+    pub unsafe fn new_in_place(parker: *mut Parker) {
         parker.write(Parker { state: AtomicI8::new(EMPTY), wait_flag: WaitFlag::new() })
     }
 
index 4add4b85ee6bfe5e616a5fb63e92bf93268fd3a8..7acda8e98f18fcf75bf3b8f97334832b949bb0f6 100644 (file)
@@ -1216,7 +1216,7 @@ pub(crate) fn new(name: Option<CString>) -> Thread {
             let ptr = Arc::get_mut_unchecked(&mut arc).as_mut_ptr();
             addr_of_mut!((*ptr).name).write(name);
             addr_of_mut!((*ptr).id).write(ThreadId::new());
-            Parker::new(addr_of_mut!((*ptr).parker));
+            Parker::new_in_place(addr_of_mut!((*ptr).parker));
             Pin::new_unchecked(arc.assume_init())
         };