Skip to content

Commit c8f2cae

Browse files
committed
Use futex.rs for Windows thread parking
1 parent 2823cfb commit c8f2cae

File tree

8 files changed

+38
-26
lines changed

8 files changed

+38
-26
lines changed

library/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 of 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
//

library/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 of 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.

library/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 of 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.

library/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 of 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
}

library/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,

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use crate::pin::Pin;
2-
use crate::sync::atomic::AtomicU32;
32
use crate::sync::atomic::Ordering::{Acquire, Release};
4-
use crate::sys::futex::{futex_wait, futex_wake};
3+
use crate::sys::futex::{self, futex_wait, futex_wake};
54
use crate::time::Duration;
65

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

1113
pub struct Parker {
12-
state: AtomicU32,
14+
state: Atomic,
1315
}
1416

1517
// Notes about memory ordering:
@@ -36,7 +38,7 @@ impl Parker {
3638
/// Construct the futex parker. The UNIX parker implementation
3739
/// requires this to happen in-place.
3840
pub unsafe fn new_in_place(parker: *mut Parker) {
39-
parker.write(Self { state: AtomicU32::new(EMPTY) });
41+
parker.write(Self { state: Atomic::new(EMPTY) });
4042
}
4143

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

library/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)