Skip to content

Commit dc00e8c

Browse files
committed
Auto merge of #121317 - ChrisDenton:win10-sync, r=Mark-Simulacrum
Always use WaitOnAddress on Win10+ `WaitOnAddress` and `WakeByAddressSingle` are always available since Windows 8 so they can now be used without needing to delay load. I've also moved the Windows 7 thread parking fallbacks into a separate sub-module.
2 parents 0250ef2 + 35421c7 commit dc00e8c

File tree

4 files changed

+156
-108
lines changed

4 files changed

+156
-108
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;
@@ -360,6 +357,19 @@ compat_fn_with_fallback! {
360357
}
361358
}
362359

360+
#[cfg(not(target_vendor = "win7"))]
361+
#[link(name = "synchronization")]
362+
extern "system" {
363+
pub fn WaitOnAddress(
364+
address: *const c_void,
365+
compareaddress: *const c_void,
366+
addresssize: usize,
367+
dwmilliseconds: u32,
368+
) -> BOOL;
369+
pub fn WakeByAddressSingle(address: *const c_void);
370+
}
371+
372+
#[cfg(target_vendor = "win7")]
363373
compat_fn_optional! {
364374
crate::sys::compat::load_synch_functions();
365375
pub fn WaitOnAddress(
@@ -371,30 +381,34 @@ compat_fn_optional! {
371381
pub fn WakeByAddressSingle(address: *const ::core::ffi::c_void);
372382
}
373383

384+
#[cfg(any(target_vendor = "win7", target_vendor = "uwp"))]
374385
compat_fn_with_fallback! {
375386
pub static NTDLL: &CStr = c"ntdll";
376387

388+
#[cfg(target_vendor = "win7")]
377389
pub fn NtCreateKeyedEvent(
378-
KeyedEventHandle: LPHANDLE,
379-
DesiredAccess: ACCESS_MASK,
390+
KeyedEventHandle: *mut HANDLE,
391+
DesiredAccess: DWORD,
380392
ObjectAttributes: LPVOID,
381393
Flags: ULONG
382394
) -> NTSTATUS {
383395
panic!("keyed events not available")
384396
}
397+
#[cfg(target_vendor = "win7")]
385398
pub fn NtReleaseKeyedEvent(
386399
EventHandle: HANDLE,
387400
Key: LPVOID,
388401
Alertable: BOOLEAN,
389-
Timeout: PLARGE_INTEGER
402+
Timeout: *mut c_longlong
390403
) -> NTSTATUS {
391404
panic!("keyed events not available")
392405
}
406+
#[cfg(target_vendor = "win7")]
393407
pub fn NtWaitForKeyedEvent(
394408
EventHandle: HANDLE,
395409
Key: LPVOID,
396410
Alertable: BOOLEAN,
397-
Timeout: PLARGE_INTEGER
411+
Timeout: *mut c_longlong
398412
) -> NTSTATUS {
399413
panic!("keyed events not available")
400414
}

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)