Skip to content

Commit 54d1ce2

Browse files
committed
Prevent double reference in generic futex
1 parent a91f7d7 commit 54d1ce2

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use core::sync::atomic::{
1010
};
1111
use core::time::Duration;
1212

13+
pub unsafe trait Futex {}
1314
pub unsafe trait Waitable {
1415
type Atomic;
1516
}
@@ -19,6 +20,7 @@ macro_rules! unsafe_waitable_int {
1920
unsafe impl Waitable for $int {
2021
type Atomic = $atomic;
2122
}
23+
unsafe impl Futex for $atomic {}
2224
)*
2325
};
2426
}
@@ -41,6 +43,7 @@ unsafe impl<T> Waitable for *const T {
4143
unsafe impl<T> Waitable for *mut T {
4244
type Atomic = AtomicPtr<T>;
4345
}
46+
unsafe impl<T> Futex for AtomicPtr<T> {}
4447

4548
pub fn wait_on_address<W: Waitable>(
4649
address: &W::Atomic,
@@ -56,14 +59,14 @@ pub fn wait_on_address<W: Waitable>(
5659
}
5760
}
5861

59-
pub fn wake_by_address_single<T>(address: &T) {
62+
pub fn wake_by_address_single<T: Futex>(address: &T) {
6063
unsafe {
6164
let addr = ptr::from_ref(address).cast::<c_void>();
6265
c::WakeByAddressSingle(addr);
6366
}
6467
}
6568

66-
pub fn wake_by_address_all<T>(address: &T) {
69+
pub fn wake_by_address_all<T: Futex>(address: &T) {
6770
unsafe {
6871
let addr = ptr::from_ref(address).cast::<c_void>();
6972
c::WakeByAddressAll(addr);
@@ -75,11 +78,11 @@ pub fn futex_wait<W: Waitable>(futex: &W::Atomic, expected: W, timeout: Option<D
7578
wait_on_address(futex, expected, timeout) || api::get_last_error() != WinError::TIMEOUT
7679
}
7780

78-
pub fn futex_wake<T>(futex: &T) -> bool {
81+
pub fn futex_wake<T: Futex>(futex: &T) -> bool {
7982
wake_by_address_single(futex);
8083
false
8184
}
8285

83-
pub fn futex_wake_all<T>(futex: &T) {
86+
pub fn futex_wake_all<T: Futex>(futex: &T) {
8487
wake_by_address_all(futex)
8588
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'a> Drop for CompletionGuard<'a> {
5757
// up on the Once. `futex_wake_all` does its own synchronization, hence
5858
// we do not need `AcqRel`.
5959
if self.state.swap(self.set_state_on_drop_to, Release) == QUEUED {
60-
futex_wake_all(&self.state);
60+
futex_wake_all(self.state);
6161
}
6262
}
6363
}

0 commit comments

Comments
 (0)