Skip to content

Commit 91cf50a

Browse files
authored
migrate from winapi to windows_sys (#76)
1 parent 071bab3 commit 91cf50a

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

Cargo.toml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ cfg-if = "1.0.0"
2323
libc = "0.2.45"
2424
psm = { path = "psm", version = "0.1.7" }
2525

26-
[target.'cfg(windows)'.dependencies.winapi]
27-
version = "0.3.6"
26+
[target.'cfg(windows)'.dependencies.windows-sys]
27+
version = ">=0.34.0, <0.42.0"
2828
features = [
29-
'memoryapi',
30-
'winbase',
31-
'fibersapi',
32-
'processthreadsapi',
33-
'minwindef',
29+
"Win32_System_Memory",
30+
"Win32_System_Threading",
31+
"Win32_Foundation",
3432
]
3533

34+
3635
[build-dependencies]
3736
cc = "1.0.2"

src/lib.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
extern crate cfg_if;
2929
extern crate libc;
3030
#[cfg(windows)]
31-
extern crate winapi;
31+
extern crate windows_sys;
3232
#[macro_use]
3333
extern crate psm;
3434

@@ -92,7 +92,7 @@ pub fn remaining_stack() -> Option<usize> {
9292
get_stack_limit().map(|limit| current_ptr - limit)
9393
}
9494

95-
psm_stack_information! (
95+
psm_stack_information!(
9696
yes {
9797
fn current_stack_ptr() -> usize {
9898
psm::stack_pointer() as usize
@@ -278,29 +278,27 @@ cfg_if! {
278278
if #[cfg(windows)] {
279279
use std::ptr;
280280
use std::io;
281-
282-
use winapi::shared::basetsd::*;
283-
use winapi::shared::minwindef::{LPVOID, BOOL};
284-
use winapi::shared::ntdef::*;
285-
use winapi::um::fibersapi::*;
286-
use winapi::um::memoryapi::*;
287-
use winapi::um::processthreadsapi::*;
288-
use winapi::um::winbase::*;
281+
use libc::c_void;
282+
use windows_sys::Win32::System::Threading::{SwitchToFiber, IsThreadAFiber, ConvertThreadToFiber,
283+
CreateFiber, DeleteFiber, ConvertFiberToThread, SetThreadStackGuarantee
284+
};
285+
use windows_sys::Win32::Foundation::BOOL;
286+
use windows_sys::Win32::System::Memory::VirtualQuery;
289287

290288
// Make sure the libstacker.a (implemented in C) is linked.
291289
// See https://github.com/rust-lang/rust/issues/65610
292290
#[link(name="stacker")]
293291
extern {
294-
fn __stacker_get_current_fiber() -> PVOID;
292+
fn __stacker_get_current_fiber() -> *mut c_void;
295293
}
296294

297295
struct FiberInfo<F> {
298296
callback: std::mem::MaybeUninit<F>,
299297
panic: Option<Box<dyn std::any::Any + Send + 'static>>,
300-
parent_fiber: LPVOID,
298+
parent_fiber: *mut c_void,
301299
}
302300

303-
unsafe extern "system" fn fiber_proc<F: FnOnce()>(data: LPVOID) {
301+
unsafe extern "system" fn fiber_proc<F: FnOnce()>(data: *mut c_void) {
304302
// This function is the entry point to our inner fiber, and as argument we get an
305303
// instance of `FiberInfo`. We will set-up the "runtime" for the callback and execute
306304
// it.
@@ -313,7 +311,6 @@ cfg_if! {
313311
// Restore to the previous Fiber
314312
set_stack_limit(old_stack_limit);
315313
SwitchToFiber(data.parent_fiber);
316-
return;
317314
}
318315

319316
fn _grow(stack_size: usize, callback: &mut dyn FnMut()) {
@@ -322,7 +319,7 @@ cfg_if! {
322319
// to it so we can use it's stack. After running `callback` within our fiber, we switch
323320
// back to the current stack and destroy the fiber and its associated stack.
324321
unsafe {
325-
let was_fiber = IsThreadAFiber() == TRUE as BOOL;
322+
let was_fiber = IsThreadAFiber() == 1 as BOOL;
326323
let mut data = FiberInfo {
327324
callback: std::mem::MaybeUninit::new(callback),
328325
panic: None,
@@ -346,7 +343,7 @@ cfg_if! {
346343
}
347344

348345
let fiber = CreateFiber(
349-
stack_size as SIZE_T,
346+
stack_size as usize,
350347
Some(fiber_proc::<&mut dyn FnMut()>),
351348
&mut data as *mut FiberInfo<&mut dyn FnMut()> as *mut _,
352349
);
@@ -361,12 +358,11 @@ cfg_if! {
361358
DeleteFiber(fiber);
362359

363360
// Clean-up.
364-
if !was_fiber {
365-
if ConvertFiberToThread() == 0 {
361+
if !was_fiber && ConvertFiberToThread() == 0 {
366362
// FIXME: Perhaps should not panic here?
367363
panic!("unable to convert back to thread: {}", io::Error::last_os_error());
368-
}
369364
}
365+
370366
if let Some(p) = data.panic {
371367
std::panic::resume_unwind(p);
372368
}
@@ -400,12 +396,12 @@ cfg_if! {
400396
// to discover the size of the stack
401397
//
402398
// FIXME: we could read stack base from the TIB, specifically the 3rd element of it.
403-
type QueryT = winapi::um::winnt::MEMORY_BASIC_INFORMATION;
399+
type QueryT = windows_sys::Win32::System::Memory::MEMORY_BASIC_INFORMATION;
404400
let mut mi = std::mem::MaybeUninit::<QueryT>::uninit();
405401
VirtualQuery(
406402
psm::stack_pointer() as *const _,
407403
mi.as_mut_ptr(),
408-
std::mem::size_of::<QueryT>() as SIZE_T,
404+
std::mem::size_of::<QueryT>() as usize,
409405
);
410406
Some(mi.assume_init().AllocationBase as usize + get_thread_stack_guarantee() + 0x1000)
411407
}

0 commit comments

Comments
 (0)