Skip to content

Commit 9abda03

Browse files
committed
std: rename Parker::new to Parker::new_in_place, add safe Parker::new constructor for SGX
1 parent 3076f4e commit 9abda03

File tree

9 files changed

+16
-24
lines changed

9 files changed

+16
-24
lines changed

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ mod task_queue {
6565
/// execution. The signal is sent once all TLS destructors have finished at
6666
/// which point no new thread locals should be created.
6767
pub mod wait_notify {
68-
use crate::mem::MaybeUninit;
6968
use crate::pin::Pin;
7069
use crate::sync::Arc;
7170
use crate::sys_common::thread_parking::Parker;
@@ -88,25 +87,14 @@ pub mod wait_notify {
8887
/// called, this will return immediately, otherwise the current thread
8988
/// is blocked until notified.
9089
pub fn wait(self) {
91-
// This is not actually `unsafe`, but it uses the `Parker` API,
92-
// which needs `unsafe` on some platforms.
90+
// SAFETY:
91+
// This is only ever called on one thread.
9392
unsafe { Pin::new(&*self.0).park() }
9493
}
9594
}
9695

9796
pub fn new() -> (Notifier, Waiter) {
98-
// Safety:
99-
// Some other platforms (looking at you, UNIX!) require that the thread
100-
// parker is constructed in-place. This is just a noisy way of writing:
101-
// ```rust
102-
// let parker = Parker::new();
103-
// ```
104-
let parker = unsafe {
105-
let mut place = MaybeUninit::uninit();
106-
Parker::new(place.as_mut_ptr());
107-
place.assume_init()
108-
};
109-
let inner = Arc::new(parker);
97+
let inner = Arc::new(Parker::new());
11098
(Notifier(inner.clone()), Waiter(inner))
11199
}
112100
}

library/std/src/sys/unix/thread_parking/darwin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ unsafe impl Sync for Parker {}
4646
unsafe impl Send for Parker {}
4747

4848
impl Parker {
49-
pub unsafe fn new(parker: *mut Parker) {
49+
pub unsafe fn new_in_place(parker: *mut Parker) {
5050
let semaphore = dispatch_semaphore_create(0);
5151
assert!(
5252
!semaphore.is_null(),

library/std/src/sys/unix/thread_parking/pthread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl Parker {
9999
///
100100
/// # Safety
101101
/// The constructed parker must never be moved.
102-
pub unsafe fn new(parker: *mut Parker) {
102+
pub unsafe fn new_in_place(parker: *mut Parker) {
103103
// Use the default mutex implementation to allow for simpler initialization.
104104
// This could lead to undefined behaviour when deadlocking. This is avoided
105105
// by not deadlocking. Note in particular the unlocking operation before any

library/std/src/sys/windows/thread_parking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const NOTIFIED: i8 = 1;
9797
impl Parker {
9898
/// Construct the Windows parker. The UNIX parker implementation
9999
/// requires this to happen in-place.
100-
pub unsafe fn new(parker: *mut Parker) {
100+
pub unsafe fn new_in_place(parker: *mut Parker) {
101101
parker.write(Self { state: AtomicI8::new(EMPTY) });
102102
}
103103

library/std/src/sys_common/thread_parking/futex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct Parker {
3535
impl Parker {
3636
/// Construct the futex parker. The UNIX parker implementation
3737
/// requires this to happen in-place.
38-
pub unsafe fn new(parker: *mut Parker) {
38+
pub unsafe fn new_in_place(parker: *mut Parker) {
3939
parker.write(Self { state: AtomicU32::new(EMPTY) });
4040
}
4141

library/std/src/sys_common/thread_parking/generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct Parker {
1919
impl Parker {
2020
/// Construct the generic parker. The UNIX parker implementation
2121
/// requires this to happen in-place.
22-
pub unsafe fn new(parker: *mut Parker) {
22+
pub unsafe fn new_in_place(parker: *mut Parker) {
2323
parker.write(Parker {
2424
state: AtomicUsize::new(EMPTY),
2525
lock: Mutex::new(()),

library/std/src/sys_common/thread_parking/id.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ const EMPTY: i8 = 0;
2626
const NOTIFIED: i8 = 1;
2727

2828
impl Parker {
29+
pub fn new() -> Parker {
30+
Parker { state: AtomicI8::new(EMPTY), tid: UnsafeCell::new(None) }
31+
}
32+
2933
/// Create a new thread parker. UNIX requires this to happen in-place.
30-
pub unsafe fn new(parker: *mut Parker) {
31-
parker.write(Parker { state: AtomicI8::new(EMPTY), tid: UnsafeCell::new(None) })
34+
pub unsafe fn new_in_place(parker: *mut Parker) {
35+
parker.write(Parker::new())
3236
}
3337

3438
/// # Safety

library/std/src/sys_common/thread_parking/wait_flag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub struct Parker {
4141
impl Parker {
4242
/// Construct a parker for the current thread. The UNIX parker
4343
/// implementation requires this to happen in-place.
44-
pub unsafe fn new(parker: *mut Parker) {
44+
pub unsafe fn new_in_place(parker: *mut Parker) {
4545
parker.write(Parker { state: AtomicI8::new(EMPTY), wait_flag: WaitFlag::new() })
4646
}
4747

library/std/src/thread/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ impl Thread {
12161216
let ptr = Arc::get_mut_unchecked(&mut arc).as_mut_ptr();
12171217
addr_of_mut!((*ptr).name).write(name);
12181218
addr_of_mut!((*ptr).id).write(ThreadId::new());
1219-
Parker::new(addr_of_mut!((*ptr).parker));
1219+
Parker::new_in_place(addr_of_mut!((*ptr).parker));
12201220
Pin::new_unchecked(arc.assume_init())
12211221
};
12221222

0 commit comments

Comments
 (0)