Skip to content

Commit 508a095

Browse files
committed
Windows: Implement mutex using futex
Well, the Windows equivalent: `WaitOnAddress`, `WakeByAddressSingle` and `WakeByAddressAll`.
1 parent 9e73597 commit 508a095

File tree

16 files changed

+171
-104
lines changed

16 files changed

+171
-104
lines changed

library/std/src/sys/locks/condvar/futex.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
use crate::sync::atomic::{AtomicU32, Ordering::Relaxed};
2+
#[cfg(windows)]
3+
use crate::sys::api::{
4+
wait_on_address as futex_wait, wake_by_address_all as futex_wake_all,
5+
wake_by_address_single as futex_wake,
6+
};
7+
#[cfg(not(windows))]
28
use crate::sys::futex::{futex_wait, futex_wake, futex_wake_all};
39
use crate::sys::locks::Mutex;
410
use crate::time::Duration;

library/std/src/sys/locks/condvar/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
target_os = "freebsd",
@@ -14,9 +15,9 @@ cfg_if::cfg_if! {
1415
} else if #[cfg(target_family = "unix")] {
1516
mod pthread;
1617
pub use pthread::Condvar;
17-
} else if #[cfg(target_os = "windows")] {
18-
mod windows;
19-
pub use windows::Condvar;
18+
} else if #[cfg(all(target_os = "windows", target_vendor = "win7"))] {
19+
mod windows7;
20+
pub use windows7::Condvar;
2021
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
2122
mod sgx;
2223
pub use sgx::Condvar;
+7-94
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,9 @@
1-
use crate::sync::atomic::{
2-
AtomicU32,
3-
Ordering::{Acquire, Relaxed, Release},
4-
};
5-
use crate::sys::futex::{futex_wait, futex_wake};
6-
7-
pub struct Mutex {
8-
/// 0: unlocked
9-
/// 1: locked, no other threads waiting
10-
/// 2: locked, and other threads waiting (contended)
11-
futex: AtomicU32,
12-
}
13-
14-
impl Mutex {
15-
#[inline]
16-
pub const fn new() -> Self {
17-
Self { futex: AtomicU32::new(0) }
18-
}
19-
20-
#[inline]
21-
pub fn try_lock(&self) -> bool {
22-
self.futex.compare_exchange(0, 1, Acquire, Relaxed).is_ok()
23-
}
24-
25-
#[inline]
26-
pub fn lock(&self) {
27-
if self.futex.compare_exchange(0, 1, Acquire, Relaxed).is_err() {
28-
self.lock_contended();
29-
}
30-
}
31-
32-
#[cold]
33-
fn lock_contended(&self) {
34-
// Spin first to speed things up if the lock is released quickly.
35-
let mut state = self.spin();
36-
37-
// If it's unlocked now, attempt to take the lock
38-
// without marking it as contended.
39-
if state == 0 {
40-
match self.futex.compare_exchange(0, 1, Acquire, Relaxed) {
41-
Ok(_) => return, // Locked!
42-
Err(s) => state = s,
43-
}
44-
}
45-
46-
loop {
47-
// Put the lock in contended state.
48-
// We avoid an unnecessary write if it as already set to 2,
49-
// to be friendlier for the caches.
50-
if state != 2 && self.futex.swap(2, Acquire) == 0 {
51-
// We changed it from 0 to 2, so we just successfully locked it.
52-
return;
53-
}
54-
55-
// Wait for the futex to change state, assuming it is still 2.
56-
futex_wait(&self.futex, 2, None);
57-
58-
// Spin again after waking up.
59-
state = self.spin();
60-
}
61-
}
62-
63-
fn spin(&self) -> u32 {
64-
let mut spin = 100;
65-
loop {
66-
// We only use `load` (and not `swap` or `compare_exchange`)
67-
// while spinning, to be easier on the caches.
68-
let state = self.futex.load(Relaxed);
69-
70-
// We stop spinning when the mutex is unlocked (0),
71-
// but also when it's contended (2).
72-
if state != 1 || spin == 0 {
73-
return state;
74-
}
75-
76-
crate::hint::spin_loop();
77-
spin -= 1;
78-
}
79-
}
80-
81-
#[inline]
82-
pub unsafe fn unlock(&self) {
83-
if self.futex.swap(0, Release) == 2 {
84-
// We only wake up one thread. When that thread locks the mutex, it
85-
// will mark the mutex as contended (2) (see lock_contended above),
86-
// which makes sure that any other waiting threads will also be
87-
// woken up eventually.
88-
self.wake();
89-
}
90-
}
91-
92-
#[cold]
93-
fn wake(&self) {
94-
futex_wake(&self.futex);
1+
cfg_if::cfg_if! {
2+
if #[cfg(windows)] {
3+
mod windows;
4+
pub use windows::*;
5+
} else {
6+
mod unix;
7+
pub use unix::*;
958
}
969
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use crate::sync::atomic::{
2+
AtomicI8,
3+
Ordering::{Acquire, Relaxed, Release},
4+
};
5+
use crate::sys::api;
6+
7+
pub struct Mutex {
8+
state: AtomicI8,
9+
}
10+
11+
const UNLOCKED: i8 = 0;
12+
const LOCKED: i8 = 1;
13+
const CONTENDED: i8 = 2;
14+
15+
impl Mutex {
16+
#[inline]
17+
pub const fn new() -> Mutex {
18+
Mutex { state: AtomicI8::new(UNLOCKED) }
19+
}
20+
21+
#[inline]
22+
pub fn lock(&self) {
23+
if let Err(state) = self.state.compare_exchange(UNLOCKED, LOCKED, Acquire, Relaxed) {
24+
self.lock_contended(state)
25+
}
26+
}
27+
28+
#[cold]
29+
fn lock_contended(&self, mut state: i8) {
30+
// Note: WaitOnAddress is already quite spin-happy so we don't do any further spinning on top.
31+
loop {
32+
// Put the lock in contended state.
33+
// We avoid an unnecessary write if it as already set to CONTENDED,
34+
// to be friendlier for the caches.
35+
if state != CONTENDED && self.state.swap(CONTENDED, Acquire) == UNLOCKED {
36+
// We changed it from UNLOCKED to CONTENDED, so we just successfully locked it.
37+
return;
38+
}
39+
// Wait for the futex to change state, assuming it is still CONTENDED.
40+
api::wait_on_address(&self.state, CONTENDED, None);
41+
state = self.state.load(Relaxed);
42+
}
43+
}
44+
45+
#[inline]
46+
pub fn try_lock(&self) -> bool {
47+
self.state.compare_exchange(UNLOCKED, LOCKED, Acquire, Relaxed).is_ok()
48+
}
49+
50+
#[inline]
51+
pub unsafe fn unlock(&self) {
52+
if self.state.swap(UNLOCKED, Release) == CONTENDED {
53+
// We only wake up one thread. When that thread locks the mutex, it
54+
// will mark the mutex as CONTENDED (see lock_contended above),
55+
// which makes sure that any other waiting threads will also be
56+
// woken up eventually.
57+
self.wake();
58+
}
59+
}
60+
61+
#[cold]
62+
fn wake(&self) {
63+
api::wake_by_address_single(&self.state);
64+
}
65+
}

library/std/src/sys/locks/mutex/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
target_os = "freebsd",
@@ -19,9 +20,9 @@ cfg_if::cfg_if! {
1920
))] {
2021
mod pthread;
2122
pub use pthread::{Mutex, raw};
22-
} else if #[cfg(target_os = "windows")] {
23-
mod windows;
24-
pub use windows::{Mutex, raw};
23+
} else if #[cfg(all(target_os = "windows", target_vendor = "win7"))] {
24+
mod windows7;
25+
pub use windows7::{Mutex, raw};
2526
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
2627
mod sgx;
2728
pub use sgx::Mutex;

library/std/src/sys/locks/rwlock/futex.rs

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ use crate::sync::atomic::{
22
AtomicU32,
33
Ordering::{Acquire, Relaxed, Release},
44
};
5+
#[cfg(windows)]
6+
use crate::sys::api::{
7+
wait_on_address as futex_wait, wake_by_address_all as futex_wake_all,
8+
wake_by_address_single as futex_wake,
9+
};
10+
#[cfg(not(windows))]
511
use crate::sys::futex::{futex_wait, futex_wake, futex_wake_all};
612

713
pub struct RwLock {
@@ -291,7 +297,10 @@ impl RwLock {
291297
}
292298

293299
/// Spin for a while, but stop directly at the given condition.
300+
///
301+
/// We avoid spinning on Windows because the futex implementation spins enough.
294302
#[inline]
303+
#[cfg(not(windows))]
295304
fn spin_until(&self, f: impl Fn(u32) -> bool) -> u32 {
296305
let mut spin = 100; // Chosen by fair dice roll.
297306
loop {
@@ -304,6 +313,12 @@ impl RwLock {
304313
}
305314
}
306315

316+
#[inline]
317+
#[cfg(windows)]
318+
fn spin_until(&self, _f: impl Fn(u32) -> bool) -> u32 {
319+
self.state.load(Relaxed)
320+
}
321+
307322
#[inline]
308323
fn spin_write(&self) -> u32 {
309324
// Stop spinning when it's unlocked or when there's waiting writers, to keep things somewhat fair.

library/std/src/sys/locks/rwlock/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
target_os = "freebsd",
@@ -14,9 +15,9 @@ cfg_if::cfg_if! {
1415
} else if #[cfg(target_family = "unix")] {
1516
mod queue;
1617
pub use queue::RwLock;
17-
} else if #[cfg(target_os = "windows")] {
18-
mod windows;
19-
pub use windows::RwLock;
18+
} else if #[cfg(all(target_os = "windows", target_vendor = "win7"))] {
19+
mod windows7;
20+
pub use windows7::RwLock;
2021
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
2122
mod sgx;
2223
pub use sgx::RwLock;

library/std/src/sys/pal/windows/api.rs

+39
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,42 @@ pub fn get_last_error() -> WinError {
155155
pub struct WinError {
156156
pub code: u32,
157157
}
158+
159+
#[cfg(not(target_vendor = "win7"))]
160+
mod futex {
161+
use super::*;
162+
use crate::sys::dur2timeout;
163+
use core::mem;
164+
use core::time::Duration;
165+
166+
#[inline(always)]
167+
pub fn wait_on_address<T, U>(address: &T, compare: U, timeout: Option<Duration>) -> bool {
168+
assert_eq!(mem::size_of::<T>(), mem::size_of::<U>());
169+
unsafe {
170+
let addr = addr_of!(*address).cast::<c_void>();
171+
let size = mem::size_of::<T>();
172+
let compare_addr = addr_of!(compare).cast::<c_void>();
173+
let timeout = timeout.map(dur2timeout).unwrap_or(c::INFINITE);
174+
c::WaitOnAddress(addr, compare_addr, size, timeout) == c::TRUE
175+
}
176+
}
177+
178+
#[inline(always)]
179+
pub fn wake_by_address_single<T>(address: &T) -> bool {
180+
unsafe {
181+
let addr = addr_of!(*address).cast::<c_void>();
182+
c::WakeByAddressSingle(addr);
183+
false
184+
}
185+
}
186+
187+
#[inline(always)]
188+
pub fn wake_by_address_all<T>(address: &T) {
189+
unsafe {
190+
let addr = addr_of!(*address).cast::<c_void>();
191+
c::WakeByAddressAll(addr);
192+
}
193+
}
194+
}
195+
#[cfg(not(target_vendor = "win7"))]
196+
pub use futex::*;

library/std/src/sys/pal/windows/c.rs

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub type LPVOID = *mut c_void;
3636
pub type LPWCH = *mut WCHAR;
3737
pub type LPWSTR = *mut WCHAR;
3838

39+
#[cfg(target_vendor = "win7")]
3940
pub type PSRWLOCK = *mut SRWLOCK;
4041

4142
pub type socklen_t = c_int;
@@ -50,7 +51,9 @@ pub const INVALID_HANDLE_VALUE: HANDLE = ::core::ptr::without_provenance_mut(-1i
5051
pub const EXIT_SUCCESS: u32 = 0;
5152
pub const EXIT_FAILURE: u32 = 1;
5253

54+
#[cfg(target_vendor = "win7")]
5355
pub const CONDITION_VARIABLE_INIT: CONDITION_VARIABLE = CONDITION_VARIABLE { Ptr: ptr::null_mut() };
56+
#[cfg(target_vendor = "win7")]
5457
pub const SRWLOCK_INIT: SRWLOCK = SRWLOCK { Ptr: ptr::null_mut() };
5558
pub const INIT_ONCE_STATIC_INIT: INIT_ONCE = INIT_ONCE { Ptr: ptr::null_mut() };
5659

@@ -373,6 +376,7 @@ extern "system" {
373376
dwmilliseconds: u32,
374377
) -> BOOL;
375378
pub fn WakeByAddressSingle(address: *const c_void);
379+
pub fn WakeByAddressAll(address: *const c_void);
376380
}
377381

378382
#[cfg(target_vendor = "win7")]

library/std/src/sys/pal/windows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cfg_if::cfg_if! {
3939
}
4040
}
4141

42-
mod api;
42+
pub(in crate::sys) mod api;
4343

4444
/// Map a Result<T, WinError> to io::Result<T>.
4545
trait IoResult<T> {

src/tools/miri/src/shims/windows/foreign_items.rs

+6
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
355355

356356
this.WakeByAddressSingle(ptr_op)?;
357357
}
358+
"WakeByAddressAll" => {
359+
let [ptr_op] =
360+
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
361+
362+
this.WakeByAddressAll(ptr_op)?;
363+
}
358364

359365
// Dynamic symbol loading
360366
"GetProcAddress" => {

src/tools/miri/src/shims/windows/sync.rs

+15
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
384384

385385
Ok(())
386386
}
387+
fn WakeByAddressAll(&mut self, ptr_op: &OpTy<'tcx, Provenance>) -> InterpResult<'tcx> {
388+
let this = self.eval_context_mut();
389+
390+
let ptr = this.read_pointer(ptr_op)?;
391+
392+
// See the Linux futex implementation for why this fence exists.
393+
this.atomic_fence(AtomicFenceOrd::SeqCst)?;
394+
395+
while let Some(thread) = this.futex_wake(ptr.addr().bytes(), u32::MAX) {
396+
this.unblock_thread(thread);
397+
this.unregister_timeout_callback_if_exists(thread);
398+
}
399+
400+
Ok(())
401+
}
387402

388403
fn SleepConditionVariableSRW(
389404
&mut self,

0 commit comments

Comments
 (0)