Skip to content

Commit f15b4f7

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

File tree

16 files changed

+266
-104
lines changed

16 files changed

+266
-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,96 @@
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);
95+
}
96+
}
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;

0 commit comments

Comments
 (0)