Skip to content

Commit 70cbe92

Browse files
committed
Switch to winapi 0.3
1 parent 1d37dc0 commit 70cbe92

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

Cargo.toml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ doctest = false
1919
test = false
2020

2121
[dependencies]
22-
cfg-if = "0.1"
23-
libc = "0.2"
22+
cfg-if = "0.1.6"
23+
libc = "0.2.45"
2424

25-
[target.'cfg(windows)'.dependencies]
26-
kernel32-sys = "0.2.2"
27-
winapi = "0.2.8"
25+
[target.'cfg(windows)'.dependencies.winapi]
26+
version = "0.3.6"
27+
features = [
28+
'memoryapi',
29+
'winbase',
30+
'fibersapi',
31+
'processthreadsapi',
32+
'minwindef',
33+
]
2834

2935
[build-dependencies]
3036
cc = "1.0"

src/lib.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
extern crate cfg_if;
2929
extern crate libc;
3030
#[cfg(windows)]
31-
extern crate kernel32;
32-
#[cfg(windows)]
3331
extern crate winapi;
3432

3533
use std::cell::Cell;
@@ -202,17 +200,25 @@ cfg_if! {
202200

203201
cfg_if! {
204202
if #[cfg(windows)] {
203+
use winapi::shared::basetsd::*;
204+
use winapi::shared::minwindef::{LPVOID, BOOL};
205+
use winapi::shared::ntdef::*;
206+
use winapi::um::fibersapi::*;
207+
use winapi::um::memoryapi::*;
208+
use winapi::um::processthreadsapi::*;
209+
use winapi::um::winbase::*;
210+
205211
extern {
206-
fn __stacker_get_current_fiber() -> winapi::PVOID;
212+
fn __stacker_get_current_fiber() -> PVOID;
207213
}
208214

209215
struct FiberInfo<'a> {
210216
callback: &'a mut FnMut(),
211217
result: Option<std::thread::Result<()>>,
212-
parent_fiber: winapi::LPVOID,
218+
parent_fiber: LPVOID,
213219
}
214220

215-
unsafe extern "system" fn fiber_proc(info: winapi::LPVOID) {
221+
unsafe extern "system" fn fiber_proc(info: LPVOID) {
216222
let info = &mut *(info as *mut FiberInfo);
217223

218224
// Remember the old stack limit
@@ -227,7 +233,7 @@ cfg_if! {
227233
// Restore the stack limit of the previous fiber
228234
set_stack_limit(old_stack_limit);
229235

230-
kernel32::SwitchToFiber(info.parent_fiber);
236+
SwitchToFiber(info.parent_fiber);
231237
return;
232238
}
233239

@@ -239,7 +245,7 @@ cfg_if! {
239245
// `callback` we switch back to the current stack and destroy
240246
// the fiber and its associated stack.
241247

242-
let was_fiber = kernel32::IsThreadAFiber() == winapi::TRUE;
248+
let was_fiber = IsThreadAFiber() == TRUE as BOOL;
243249

244250
let mut info = FiberInfo {
245251
callback,
@@ -258,7 +264,7 @@ cfg_if! {
258264
// to the current stack. Threads coverted to fibers still act like
259265
// regular threads, but they have associated fiber data. We later
260266
// convert it back to a regular thread and free the fiber data.
261-
kernel32::ConvertThreadToFiber(0i32 as _)
267+
ConvertThreadToFiber(0i32 as _)
262268
}
263269
},
264270
};
@@ -267,22 +273,22 @@ cfg_if! {
267273
panic!("unable to convert thread to fiber");
268274
}
269275

270-
let fiber = kernel32::CreateFiber(stack_size as _, Some(fiber_proc), &mut info as *mut FiberInfo as *mut _);
276+
let fiber = CreateFiber(stack_size as _, Some(fiber_proc), &mut info as *mut FiberInfo as *mut _);
271277
if fiber == 0i32 as _ {
272278
panic!("unable to allocate fiber");
273279
}
274280

275281
// Switch to the fiber we created. This changes stacks and starts executing
276282
// fiber_proc on it. fiber_proc will run `callback` and then switch back
277-
kernel32::SwitchToFiber(fiber);
283+
SwitchToFiber(fiber);
278284

279285
// We are back on the old stack and now we have destroy the fiber and its stack
280-
kernel32::DeleteFiber(fiber);
286+
DeleteFiber(fiber);
281287

282288
// If we started out on a non-fiber thread, we converted that thread to a fiber.
283289
// Here we convert back.
284290
if !was_fiber {
285-
kernel32::ConvertFiberToThread();
291+
ConvertFiberToThread();
286292
}
287293

288294
if let Err(payload) = info.result.unwrap() {
@@ -307,7 +313,7 @@ cfg_if! {
307313
// some further logic to calculate the real stack
308314
// guarantee. This logic is what is used on x86-32 and
309315
// x86-64 Windows 10. Other versions and platforms may differ
310-
kernel32::SetThreadStackGuarantee(&mut stack_guarantee)
316+
SetThreadStackGuarantee(&mut stack_guarantee)
311317
};
312318
std::cmp::max(stack_guarantee, min_guarantee) as usize + 0x1000
313319
}
@@ -317,10 +323,10 @@ cfg_if! {
317323
let mut mi = std::mem::zeroed();
318324
// Query the allocation which contains our stack pointer in order
319325
// to discover the size of the stack
320-
kernel32::VirtualQuery(
326+
VirtualQuery(
321327
__stacker_stack_pointer() as *const _,
322328
&mut mi,
323-
std::mem::size_of_val(&mi) as winapi::SIZE_T,
329+
std::mem::size_of_val(&mi) as SIZE_T,
324330
);
325331
Some(mi.AllocationBase as usize + get_thread_stack_guarantee() + 0x1000)
326332
}

0 commit comments

Comments
 (0)