forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#121956 - ChrisDenton:srwlock, r=joboet
Windows: Implement condvar, mutex and rwlock using futex Well, the Windows equivalent: [`WaitOnAddress`,](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitonaddress) [`WakeByAddressSingle`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-wakebyaddresssingle) and [`WakeByAddressAll`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-wakebyaddressall). Note that Windows flavoured futexes can be different sizes (1, 2, 4 or 8 bytes). I took advantage of that in the `Mutex` implementation. I also edited the Mutex implementation a bit more than necessary. I was having trouble keeping in my head what 0, 1 and 2 meant so I replaced them with consts. I *think* we're maybe spinning a bit much. `WaitOnAddress` seems to be looping quite a bit too. But for now I've keep the implementations the same. I do wonder if it'd be worth reducing or removing our spinning on Windows. This also adds a new shim to miri, because of course it does. Fixes rust-lang#121949
- Loading branch information
Showing
14 changed files
with
159 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use super::api; | ||
use crate::sys::c; | ||
use crate::sys::dur2timeout; | ||
use core::ffi::c_void; | ||
use core::mem; | ||
use core::ptr; | ||
use core::sync::atomic::{ | ||
AtomicBool, AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, AtomicPtr, AtomicU16, | ||
AtomicU32, AtomicU64, AtomicU8, AtomicUsize, | ||
}; | ||
use core::time::Duration; | ||
|
||
pub unsafe trait Waitable { | ||
type Atomic; | ||
} | ||
macro_rules! unsafe_waitable_int { | ||
($(($int:ty, $atomic:ty)),*$(,)?) => { | ||
$( | ||
unsafe impl Waitable for $int { | ||
type Atomic = $atomic; | ||
} | ||
)* | ||
}; | ||
} | ||
unsafe_waitable_int! { | ||
(bool, AtomicBool), | ||
(i8, AtomicI8), | ||
(i16, AtomicI16), | ||
(i32, AtomicI32), | ||
(i64, AtomicI64), | ||
(isize, AtomicIsize), | ||
(u8, AtomicU8), | ||
(u16, AtomicU16), | ||
(u32, AtomicU32), | ||
(u64, AtomicU64), | ||
(usize, AtomicUsize), | ||
} | ||
unsafe impl<T> Waitable for *const T { | ||
type Atomic = AtomicPtr<T>; | ||
} | ||
unsafe impl<T> Waitable for *mut T { | ||
type Atomic = AtomicPtr<T>; | ||
} | ||
|
||
pub fn wait_on_address<W: Waitable>( | ||
address: &W::Atomic, | ||
compare: W, | ||
timeout: Option<Duration>, | ||
) -> bool { | ||
unsafe { | ||
let addr = ptr::from_ref(address).cast::<c_void>(); | ||
let size = mem::size_of::<W>(); | ||
let compare_addr = ptr::addr_of!(compare).cast::<c_void>(); | ||
let timeout = timeout.map(dur2timeout).unwrap_or(c::INFINITE); | ||
c::WaitOnAddress(addr, compare_addr, size, timeout) == c::TRUE | ||
} | ||
} | ||
|
||
pub fn wake_by_address_single<T>(address: &T) { | ||
unsafe { | ||
let addr = ptr::from_ref(address).cast::<c_void>(); | ||
c::WakeByAddressSingle(addr); | ||
} | ||
} | ||
|
||
pub fn wake_by_address_all<T>(address: &T) { | ||
unsafe { | ||
let addr = ptr::from_ref(address).cast::<c_void>(); | ||
c::WakeByAddressAll(addr); | ||
} | ||
} | ||
|
||
pub fn futex_wait<W: Waitable>(futex: &W::Atomic, expected: W, timeout: Option<Duration>) -> bool { | ||
// return false only on timeout | ||
wait_on_address(futex, expected, timeout) || api::get_last_error().code != c::ERROR_TIMEOUT | ||
} | ||
|
||
pub fn futex_wake<T>(futex: &T) -> bool { | ||
wake_by_address_single(futex); | ||
false | ||
} | ||
|
||
pub fn futex_wake_all<T>(futex: &T) { | ||
wake_by_address_all(futex) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters