Skip to content

Commit 8019473

Browse files
authoredJan 8, 2022
Merge pull request #311 from clemenswasser/adopt-windows-rs
2 parents c73dd43 + 1f94288 commit 8019473

File tree

4 files changed

+62
-85
lines changed

4 files changed

+62
-85
lines changed
 

‎core/Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ libc = "0.2.95"
2323
redox_syscall = "0.2.8"
2424

2525
[target.'cfg(windows)'.dependencies]
26-
winapi = { version = "0.3.9", features = ["winnt", "ntstatus", "minwindef",
27-
"winerror", "winbase", "errhandlingapi", "handleapi"] }
26+
windows-sys = { version = "0.29", features = [
27+
"Win32_Foundation",
28+
"Win32_System_LibraryLoader",
29+
"Win32_System_SystemServices",
30+
"Win32_System_WindowsProgramming",
31+
] }
2832

2933
[features]
3034
nightly = []

‎core/src/thread_parker/windows/keyed_event.rs

+30-46
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,18 @@
66
// copied, modified, or distributed except according to those terms.
77

88
use core::{
9+
ffi,
910
mem::{self, MaybeUninit},
1011
ptr,
1112
};
1213
use std::sync::atomic::{AtomicUsize, Ordering};
1314
use std::time::Instant;
14-
use winapi::{
15-
shared::{
16-
minwindef::{TRUE, ULONG},
17-
ntdef::NTSTATUS,
18-
ntstatus::{STATUS_SUCCESS, STATUS_TIMEOUT},
19-
},
20-
um::{
21-
handleapi::CloseHandle,
22-
libloaderapi::{GetModuleHandleA, GetProcAddress},
23-
winnt::{
24-
ACCESS_MASK, BOOLEAN, GENERIC_READ, GENERIC_WRITE, HANDLE, LARGE_INTEGER, LPCSTR,
25-
PHANDLE, PLARGE_INTEGER, PVOID,
26-
},
15+
16+
use windows_sys::Win32::{
17+
Foundation::{CloseHandle, BOOLEAN, HANDLE, NTSTATUS, STATUS_SUCCESS, STATUS_TIMEOUT},
18+
System::{
19+
LibraryLoader::{GetModuleHandleA, GetProcAddress},
20+
SystemServices::{GENERIC_READ, GENERIC_WRITE},
2721
},
2822
};
2923

@@ -36,58 +30,49 @@ pub struct KeyedEvent {
3630
handle: HANDLE,
3731
NtReleaseKeyedEvent: extern "system" fn(
3832
EventHandle: HANDLE,
39-
Key: PVOID,
33+
Key: *mut ffi::c_void,
4034
Alertable: BOOLEAN,
41-
Timeout: PLARGE_INTEGER,
35+
Timeout: *mut i64,
4236
) -> NTSTATUS,
4337
NtWaitForKeyedEvent: extern "system" fn(
4438
EventHandle: HANDLE,
45-
Key: PVOID,
39+
Key: *mut ffi::c_void,
4640
Alertable: BOOLEAN,
47-
Timeout: PLARGE_INTEGER,
41+
Timeout: *mut i64,
4842
) -> NTSTATUS,
4943
}
5044

5145
impl KeyedEvent {
5246
#[inline]
53-
unsafe fn wait_for(&self, key: PVOID, timeout: PLARGE_INTEGER) -> NTSTATUS {
54-
(self.NtWaitForKeyedEvent)(self.handle, key, 0, timeout)
47+
unsafe fn wait_for(&self, key: *mut ffi::c_void, timeout: *mut i64) -> NTSTATUS {
48+
(self.NtWaitForKeyedEvent)(self.handle, key, false.into(), timeout)
5549
}
5650

5751
#[inline]
58-
unsafe fn release(&self, key: PVOID) -> NTSTATUS {
59-
(self.NtReleaseKeyedEvent)(self.handle, key, 0, ptr::null_mut())
52+
unsafe fn release(&self, key: *mut ffi::c_void) -> NTSTATUS {
53+
(self.NtReleaseKeyedEvent)(self.handle, key, false.into(), ptr::null_mut())
6054
}
6155

6256
#[allow(non_snake_case)]
6357
pub fn create() -> Option<KeyedEvent> {
6458
unsafe {
65-
let ntdll = GetModuleHandleA(b"ntdll.dll\0".as_ptr() as LPCSTR);
66-
if ntdll.is_null() {
59+
let ntdll = GetModuleHandleA(b"ntdll.dll\0".as_ptr() as *mut u8);
60+
if ntdll == 0 {
6761
return None;
6862
}
6963

7064
let NtCreateKeyedEvent =
71-
GetProcAddress(ntdll, b"NtCreateKeyedEvent\0".as_ptr() as LPCSTR);
72-
if NtCreateKeyedEvent.is_null() {
73-
return None;
74-
}
65+
GetProcAddress(ntdll, b"NtCreateKeyedEvent\0".as_ptr() as *mut u8)?;
7566
let NtReleaseKeyedEvent =
76-
GetProcAddress(ntdll, b"NtReleaseKeyedEvent\0".as_ptr() as LPCSTR);
77-
if NtReleaseKeyedEvent.is_null() {
78-
return None;
79-
}
67+
GetProcAddress(ntdll, b"NtReleaseKeyedEvent\0".as_ptr() as *mut u8)?;
8068
let NtWaitForKeyedEvent =
81-
GetProcAddress(ntdll, b"NtWaitForKeyedEvent\0".as_ptr() as LPCSTR);
82-
if NtWaitForKeyedEvent.is_null() {
83-
return None;
84-
}
69+
GetProcAddress(ntdll, b"NtWaitForKeyedEvent\0".as_ptr() as *mut u8)?;
8570

8671
let NtCreateKeyedEvent: extern "system" fn(
87-
KeyedEventHandle: PHANDLE,
88-
DesiredAccess: ACCESS_MASK,
89-
ObjectAttributes: PVOID,
90-
Flags: ULONG,
72+
KeyedEventHandle: *mut HANDLE,
73+
DesiredAccess: u32,
74+
ObjectAttributes: *mut ffi::c_void,
75+
Flags: u32,
9176
) -> NTSTATUS = mem::transmute(NtCreateKeyedEvent);
9277
let mut handle = MaybeUninit::uninit();
9378
let status = NtCreateKeyedEvent(
@@ -120,7 +105,7 @@ impl KeyedEvent {
120105

121106
#[inline]
122107
pub unsafe fn park(&'static self, key: &AtomicUsize) {
123-
let status = self.wait_for(key as *const _ as PVOID, ptr::null_mut());
108+
let status = self.wait_for(key as *const _ as *mut ffi::c_void, ptr::null_mut());
124109
debug_assert_eq!(status, STATUS_SUCCESS);
125110
}
126111

@@ -140,22 +125,21 @@ impl KeyedEvent {
140125

141126
// NT uses a timeout in units of 100ns. We use a negative value to
142127
// indicate a relative timeout based on a monotonic clock.
143-
let mut nt_timeout: LARGE_INTEGER = mem::zeroed();
144128
let diff = timeout - now;
145129
let value = (diff.as_secs() as i64)
146130
.checked_mul(-10000000)
147131
.and_then(|x| x.checked_sub((diff.subsec_nanos() as i64 + 99) / 100));
148132

149-
match value {
150-
Some(x) => *nt_timeout.QuadPart_mut() = x,
133+
let mut nt_timeout = match value {
134+
Some(x) => x,
151135
None => {
152136
// Timeout overflowed, just sleep indefinitely
153137
self.park(key);
154138
return true;
155139
}
156140
};
157141

158-
let status = self.wait_for(key as *const _ as PVOID, &mut nt_timeout);
142+
let status = self.wait_for(key as *const _ as *mut ffi::c_void, &mut nt_timeout);
159143
if status == STATUS_SUCCESS {
160144
return true;
161145
}
@@ -192,7 +176,7 @@ impl Drop for KeyedEvent {
192176
fn drop(&mut self) {
193177
unsafe {
194178
let ok = CloseHandle(self.handle);
195-
debug_assert_eq!(ok, TRUE);
179+
debug_assert_eq!(ok, true.into());
196180
}
197181
}
198182
}
@@ -211,7 +195,7 @@ impl UnparkHandle {
211195
#[inline]
212196
pub unsafe fn unpark(self) {
213197
if !self.key.is_null() {
214-
let status = self.keyed_event.release(self.key as PVOID);
198+
let status = self.keyed_event.release(self.key as *mut ffi::c_void);
215199
debug_assert_eq!(status, STATUS_SUCCESS);
216200
}
217201
}

‎core/src/thread_parker/windows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ pub fn thread_yield() {
177177
// libraries, but that'll probably take a lot longer than patching this here
178178
// and avoiding the `synchapi` feature entirely.
179179
extern "system" {
180-
fn Sleep(a: winapi::shared::minwindef::DWORD);
180+
fn Sleep(a: u32);
181181
}
182182
unsafe {
183183
// We don't use SwitchToThread here because it doesn't consider all

‎core/src/thread_parker/windows/waitaddress.rs

+25-36
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,24 @@ use core::{
99
mem,
1010
sync::atomic::{AtomicUsize, Ordering},
1111
};
12-
use std::time::Instant;
13-
use winapi::{
14-
shared::{
15-
basetsd::SIZE_T,
16-
minwindef::{BOOL, DWORD, FALSE, TRUE},
17-
winerror::ERROR_TIMEOUT,
18-
},
19-
um::{
20-
errhandlingapi::GetLastError,
21-
libloaderapi::{GetModuleHandleA, GetProcAddress},
22-
winbase::INFINITE,
23-
winnt::{LPCSTR, PVOID},
12+
use std::{ffi, time::Instant};
13+
use windows_sys::Win32::{
14+
Foundation::{GetLastError, BOOL, ERROR_TIMEOUT},
15+
System::{
16+
LibraryLoader::{GetModuleHandleA, GetProcAddress},
17+
WindowsProgramming::INFINITE,
2418
},
2519
};
2620

2721
#[allow(non_snake_case)]
2822
pub struct WaitAddress {
2923
WaitOnAddress: extern "system" fn(
30-
Address: PVOID,
31-
CompareAddress: PVOID,
32-
AddressSize: SIZE_T,
33-
dwMilliseconds: DWORD,
24+
Address: *mut ffi::c_void,
25+
CompareAddress: *mut ffi::c_void,
26+
AddressSize: usize,
27+
dwMilliseconds: u32,
3428
) -> BOOL,
35-
WakeByAddressSingle: extern "system" fn(Address: PVOID),
29+
WakeByAddressSingle: extern "system" fn(Address: *mut ffi::c_void),
3630
}
3731

3832
impl WaitAddress {
@@ -42,20 +36,15 @@ impl WaitAddress {
4236
// MSDN claims that that WaitOnAddress and WakeByAddressSingle are
4337
// located in kernel32.dll, but they are lying...
4438
let synch_dll =
45-
GetModuleHandleA(b"api-ms-win-core-synch-l1-2-0.dll\0".as_ptr() as LPCSTR);
46-
if synch_dll.is_null() {
39+
GetModuleHandleA(b"api-ms-win-core-synch-l1-2-0.dll\0".as_ptr() as *mut u8);
40+
if synch_dll == 0 {
4741
return None;
4842
}
4943

50-
let WaitOnAddress = GetProcAddress(synch_dll, b"WaitOnAddress\0".as_ptr() as LPCSTR);
51-
if WaitOnAddress.is_null() {
52-
return None;
53-
}
44+
let WaitOnAddress = GetProcAddress(synch_dll, b"WaitOnAddress\0".as_ptr() as *mut u8)?;
5445
let WakeByAddressSingle =
55-
GetProcAddress(synch_dll, b"WakeByAddressSingle\0".as_ptr() as LPCSTR);
56-
if WakeByAddressSingle.is_null() {
57-
return None;
58-
}
46+
GetProcAddress(synch_dll, b"WakeByAddressSingle\0".as_ptr() as *mut u8)?;
47+
5948
Some(WaitAddress {
6049
WaitOnAddress: mem::transmute(WaitOnAddress),
6150
WakeByAddressSingle: mem::transmute(WakeByAddressSingle),
@@ -77,7 +66,7 @@ impl WaitAddress {
7766
pub fn park(&'static self, key: &AtomicUsize) {
7867
while key.load(Ordering::Acquire) != 0 {
7968
let r = self.wait_on_address(key, INFINITE);
80-
debug_assert!(r == TRUE);
69+
debug_assert!(r == true.into());
8170
}
8271
}
8372

@@ -94,14 +83,14 @@ impl WaitAddress {
9483
.checked_mul(1000)
9584
.and_then(|x| x.checked_add((diff.subsec_nanos() as u64 + 999999) / 1000000))
9685
.map(|ms| {
97-
if ms > <DWORD>::max_value() as u64 {
86+
if ms > std::u32::MAX as u64 {
9887
INFINITE
9988
} else {
100-
ms as DWORD
89+
ms as u32
10190
}
10291
})
10392
.unwrap_or(INFINITE);
104-
if self.wait_on_address(key, timeout) == FALSE {
93+
if self.wait_on_address(key, timeout) == false.into() {
10594
debug_assert_eq!(unsafe { GetLastError() }, ERROR_TIMEOUT);
10695
}
10796
}
@@ -120,12 +109,12 @@ impl WaitAddress {
120109
}
121110

122111
#[inline]
123-
fn wait_on_address(&'static self, key: &AtomicUsize, timeout: DWORD) -> BOOL {
112+
fn wait_on_address(&'static self, key: &AtomicUsize, timeout: u32) -> BOOL {
124113
let cmp = 1usize;
125114
(self.WaitOnAddress)(
126-
key as *const _ as PVOID,
127-
&cmp as *const _ as PVOID,
128-
mem::size_of::<usize>() as SIZE_T,
115+
key as *const _ as *mut ffi::c_void,
116+
&cmp as *const _ as *mut ffi::c_void,
117+
mem::size_of::<usize>(),
129118
timeout,
130119
)
131120
}
@@ -144,6 +133,6 @@ impl UnparkHandle {
144133
// released to avoid blocking the queue for too long.
145134
#[inline]
146135
pub fn unpark(self) {
147-
(self.waitaddress.WakeByAddressSingle)(self.key as PVOID);
136+
(self.waitaddress.WakeByAddressSingle)(self.key as *mut ffi::c_void);
148137
}
149138
}

0 commit comments

Comments
 (0)
Please sign in to comment.