Skip to content

Commit 7a31b4a

Browse files
committed
Always use WaitOnAddress on Win10+
1 parent 3246e79 commit 7a31b4a

File tree

3 files changed

+154
-106
lines changed

3 files changed

+154
-106
lines changed

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

+21-7
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ pub type SIZE_T = usize;
2828
pub type WORD = u16;
2929
pub type CHAR = c_char;
3030
pub type ULONG = c_ulong;
31-
pub type ACCESS_MASK = DWORD;
3231

3332
pub type LPCVOID = *const c_void;
34-
pub type LPHANDLE = *mut HANDLE;
3533
pub type LPOVERLAPPED = *mut OVERLAPPED;
3634
pub type LPSECURITY_ATTRIBUTES = *mut SECURITY_ATTRIBUTES;
3735
pub type LPVOID = *mut c_void;
3836
pub type LPWCH = *mut WCHAR;
3937
pub type LPWSTR = *mut WCHAR;
4038

41-
pub type PLARGE_INTEGER = *mut c_longlong;
4239
pub type PSRWLOCK = *mut SRWLOCK;
4340

4441
pub type socklen_t = c_int;
@@ -345,6 +342,19 @@ compat_fn_with_fallback! {
345342
}
346343
}
347344

345+
#[cfg(not(target_vendor = "win7"))]
346+
#[link(name = "synchronization")]
347+
extern "system" {
348+
pub fn WaitOnAddress(
349+
address: *const c_void,
350+
compareaddress: *const c_void,
351+
addresssize: usize,
352+
dwmilliseconds: u32,
353+
) -> BOOL;
354+
pub fn WakeByAddressSingle(address: *const c_void);
355+
}
356+
357+
#[cfg(target_vendor = "win7")]
348358
compat_fn_optional! {
349359
crate::sys::compat::load_synch_functions();
350360
pub fn WaitOnAddress(
@@ -356,30 +366,34 @@ compat_fn_optional! {
356366
pub fn WakeByAddressSingle(address: *const ::core::ffi::c_void);
357367
}
358368

369+
#[cfg(any(target_vendor = "win7", target_vendor = "uwp"))]
359370
compat_fn_with_fallback! {
360371
pub static NTDLL: &CStr = c"ntdll";
361372

373+
#[cfg(target_vendor = "win7")]
362374
pub fn NtCreateKeyedEvent(
363-
KeyedEventHandle: LPHANDLE,
364-
DesiredAccess: ACCESS_MASK,
375+
KeyedEventHandle: *mut HANDLE,
376+
DesiredAccess: DWORD,
365377
ObjectAttributes: LPVOID,
366378
Flags: ULONG
367379
) -> NTSTATUS {
368380
panic!("keyed events not available")
369381
}
382+
#[cfg(target_vendor = "win7")]
370383
pub fn NtReleaseKeyedEvent(
371384
EventHandle: HANDLE,
372385
Key: LPVOID,
373386
Alertable: BOOLEAN,
374-
Timeout: PLARGE_INTEGER
387+
Timeout: *mut c_longlong
375388
) -> NTSTATUS {
376389
panic!("keyed events not available")
377390
}
391+
#[cfg(target_vendor = "win7")]
378392
pub fn NtWaitForKeyedEvent(
379393
EventHandle: HANDLE,
380394
Key: LPVOID,
381395
Alertable: BOOLEAN,
382-
Timeout: PLARGE_INTEGER
396+
Timeout: *mut c_longlong
383397
) -> NTSTATUS {
384398
panic!("keyed events not available")
385399
}

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
2222
use crate::ffi::{c_void, CStr};
2323
use crate::ptr::NonNull;
24-
use crate::sync::atomic::Ordering;
2524
use crate::sys::c;
2625

2726
// This uses a static initializer to preload some imported functions.
@@ -38,6 +37,7 @@ use crate::sys::c;
3837
// file an issue for discussion; currently we don't guarantee any functionality
3938
// before main.
4039
// See https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170
40+
#[cfg(target_vendor = "win7")]
4141
#[used]
4242
#[link_section = ".CRT$XCT"]
4343
static INIT_TABLE_ENTRY: unsafe extern "C" fn() = init;
@@ -52,6 +52,7 @@ static INIT_TABLE_ENTRY: unsafe extern "C" fn() = init;
5252
/// negative performance impact in practical situations.
5353
///
5454
/// Currently we only preload `WaitOnAddress` and `WakeByAddressSingle`.
55+
#[cfg(target_vendor = "win7")]
5556
unsafe extern "C" fn init() {
5657
// In an exe this code is executed before main() so is single threaded.
5758
// In a DLL the system's loader lock will be held thereby synchronizing
@@ -183,6 +184,7 @@ macro_rules! compat_fn_with_fallback {
183184
func($($argname),*)
184185
}
185186
}
187+
#[allow(unused)]
186188
$(#[$meta])*
187189
$vis use $symbol::call as $symbol;
188190
)*)
@@ -191,6 +193,7 @@ macro_rules! compat_fn_with_fallback {
191193
/// Optionally loaded functions.
192194
///
193195
/// Actual loading of the function defers to $load_functions.
196+
#[cfg(target_vendor = "win7")]
194197
macro_rules! compat_fn_optional {
195198
($load_functions:expr;
196199
$(
@@ -218,13 +221,19 @@ macro_rules! compat_fn_optional {
218221
NonNull::new(PTR.load(Ordering::Relaxed)).map(|f| unsafe { mem::transmute(f) })
219222
}
220223
}
224+
#[inline]
225+
pub unsafe extern "system" fn $symbol($($argname: $argtype),*) $(-> $rettype)? {
226+
$symbol::option().unwrap()($($argname),*)
227+
}
221228
)+
222229
)
223230
}
224231

225232
/// Load all needed functions from "api-ms-win-core-synch-l1-2-0".
233+
#[cfg(target_vendor = "win7")]
226234
pub(super) fn load_synch_functions() {
227235
fn try_load() -> Option<()> {
236+
use crate::sync::atomic::Ordering;
228237
const MODULE_NAME: &CStr = c"api-ms-win-core-synch-l1-2-0";
229238
const WAIT_ON_ADDRESS: &CStr = c"WaitOnAddress";
230239
const WAKE_BY_ADDRESS_SINGLE: &CStr = c"WakeByAddressSingle";

0 commit comments

Comments
 (0)