Skip to content

Commit 0299bb5

Browse files
authored
Rollup merge of rust-lang#127807 - ChrisDenton:win-parking, r=joboet
Use futex.rs for Windows thread parking If I'm not overlooking anything then the Windows 10+ thread parking implementation is practically the same as the futex.rs implementation. So we may as well use the same implementation for both. The old version is still kept around for Windows 7 support. r? ````@joboet```` if you wouldn't mind double checking I've not missed something
2 parents 61af010 + 1b631e5 commit 0299bb5

File tree

8 files changed

+39
-26
lines changed

8 files changed

+39
-26
lines changed

std/src/sys/pal/hermit/futex.rs

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use crate::ptr::null;
33
use crate::sync::atomic::AtomicU32;
44
use crate::time::Duration;
55

6+
/// An atomic for use as a futex that is at least 8-bits but may be larger.
7+
pub type SmallAtomic = AtomicU32;
8+
/// Must be the underlying type of SmallAtomic
9+
pub type SmallPrimitive = u32;
10+
611
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
712
// Calculate the timeout as a relative timespec.
813
//

std/src/sys/pal/unix/futex.rs

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
use crate::sync::atomic::AtomicU32;
1212
use crate::time::Duration;
1313

14+
/// An atomic for use as a futex that is at least 8-bits but may be larger.
15+
pub type SmallAtomic = AtomicU32;
16+
/// Must be the underlying type of SmallAtomic
17+
pub type SmallPrimitive = u32;
18+
1419
/// Wait for a futex_wake operation to wake us.
1520
///
1621
/// Returns directly if the futex doesn't hold the expected value.

std/src/sys/pal/wasm/atomics/futex.rs

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ use core::arch::wasm64 as wasm;
66
use crate::sync::atomic::AtomicU32;
77
use crate::time::Duration;
88

9+
/// An atomic for use as a futex that is at least 8-bits but may be larger.
10+
pub type SmallAtomic = AtomicU32;
11+
/// Must be the underlying type of SmallAtomic
12+
pub type SmallPrimitive = u32;
13+
914
/// Wait for a futex_wake operation to wake us.
1015
///
1116
/// Returns directly if the futex doesn't hold the expected value.

std/src/sys/pal/windows/futex.rs

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ use core::sync::atomic::{
1010
};
1111
use core::time::Duration;
1212

13+
/// An atomic for use as a futex that is at least 8-bits but may be larger.
14+
pub type SmallAtomic = AtomicU8;
15+
/// Must be the underlying type of SmallAtomic
16+
pub type SmallPrimitive = u8;
17+
1318
pub unsafe trait Waitable {
1419
type Atomic;
1520
}

std/src/sys/sync/mutex/futex.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
use crate::sync::atomic::{
2-
self,
3-
Ordering::{Acquire, Relaxed, Release},
4-
};
5-
use crate::sys::futex::{futex_wait, futex_wake};
6-
7-
cfg_if::cfg_if! {
8-
if #[cfg(windows)] {
9-
// On Windows we can have a smol futex
10-
type Atomic = atomic::AtomicU8;
11-
type State = u8;
12-
} else {
13-
type Atomic = atomic::AtomicU32;
14-
type State = u32;
15-
}
16-
}
1+
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
2+
use crate::sys::futex::{self, futex_wait, futex_wake};
3+
4+
type Atomic = futex::SmallAtomic;
5+
type State = futex::SmallPrimitive;
176

187
pub struct Mutex {
198
futex: Atomic,

std/src/sys/sync/thread_parking/futex.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
#![forbid(unsafe_op_in_unsafe_fn)]
12
use crate::pin::Pin;
2-
use crate::sync::atomic::AtomicU32;
33
use crate::sync::atomic::Ordering::{Acquire, Release};
4-
use crate::sys::futex::{futex_wait, futex_wake};
4+
use crate::sys::futex::{self, futex_wait, futex_wake};
55
use crate::time::Duration;
66

7-
const PARKED: u32 = u32::MAX;
8-
const EMPTY: u32 = 0;
9-
const NOTIFIED: u32 = 1;
7+
type Atomic = futex::SmallAtomic;
8+
type State = futex::SmallPrimitive;
9+
10+
const PARKED: State = State::MAX;
11+
const EMPTY: State = 0;
12+
const NOTIFIED: State = 1;
1013

1114
pub struct Parker {
12-
state: AtomicU32,
15+
state: Atomic,
1316
}
1417

1518
// Notes about memory ordering:
@@ -36,7 +39,7 @@ impl Parker {
3639
/// Construct the futex parker. The UNIX parker implementation
3740
/// requires this to happen in-place.
3841
pub unsafe fn new_in_place(parker: *mut Parker) {
39-
parker.write(Self { state: AtomicU32::new(EMPTY) });
42+
unsafe { parker.write(Self { state: Atomic::new(EMPTY) }) };
4043
}
4144

4245
// Assumes this is only called by the thread that owns the Parker,

std/src/sys/sync/thread_parking/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
cfg_if::cfg_if! {
22
if #[cfg(any(
3+
all(target_os = "windows", not(target_vendor = "win7")),
34
target_os = "linux",
45
target_os = "android",
56
all(target_arch = "wasm32", target_feature = "atomics"),
@@ -18,9 +19,9 @@ cfg_if::cfg_if! {
1819
))] {
1920
mod id;
2021
pub use id::Parker;
21-
} else if #[cfg(target_os = "windows")] {
22-
mod windows;
23-
pub use windows::Parker;
22+
} else if #[cfg(target_vendor = "win7")] {
23+
mod windows7;
24+
pub use windows7::Parker;
2425
} else if #[cfg(all(target_vendor = "apple", not(miri)))] {
2526
mod darwin;
2627
pub use darwin::Parker;

0 commit comments

Comments
 (0)