28
28
extern crate cfg_if;
29
29
extern crate libc;
30
30
#[ cfg( windows) ]
31
- extern crate kernel32;
32
- #[ cfg( windows) ]
33
31
extern crate winapi;
34
32
35
33
use std:: cell:: Cell ;
@@ -202,17 +200,25 @@ cfg_if! {
202
200
203
201
cfg_if ! {
204
202
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
+
205
211
extern {
206
- fn __stacker_get_current_fiber( ) -> winapi :: PVOID ;
212
+ fn __stacker_get_current_fiber( ) -> PVOID ;
207
213
}
208
214
209
215
struct FiberInfo <' a> {
210
216
callback: & ' a mut FnMut ( ) ,
211
217
result: Option <std:: thread:: Result <( ) >>,
212
- parent_fiber: winapi :: LPVOID ,
218
+ parent_fiber: LPVOID ,
213
219
}
214
220
215
- unsafe extern "system" fn fiber_proc( info: winapi :: LPVOID ) {
221
+ unsafe extern "system" fn fiber_proc( info: LPVOID ) {
216
222
let info = & mut * ( info as * mut FiberInfo ) ;
217
223
218
224
// Remember the old stack limit
@@ -227,7 +233,7 @@ cfg_if! {
227
233
// Restore the stack limit of the previous fiber
228
234
set_stack_limit( old_stack_limit) ;
229
235
230
- kernel32 :: SwitchToFiber ( info. parent_fiber) ;
236
+ SwitchToFiber ( info. parent_fiber) ;
231
237
return ;
232
238
}
233
239
@@ -239,7 +245,7 @@ cfg_if! {
239
245
// `callback` we switch back to the current stack and destroy
240
246
// the fiber and its associated stack.
241
247
242
- let was_fiber = kernel32 :: IsThreadAFiber ( ) == winapi :: TRUE ;
248
+ let was_fiber = IsThreadAFiber ( ) == TRUE as BOOL ;
243
249
244
250
let mut info = FiberInfo {
245
251
callback,
@@ -258,7 +264,7 @@ cfg_if! {
258
264
// to the current stack. Threads coverted to fibers still act like
259
265
// regular threads, but they have associated fiber data. We later
260
266
// convert it back to a regular thread and free the fiber data.
261
- kernel32 :: ConvertThreadToFiber ( 0i32 as _)
267
+ ConvertThreadToFiber ( 0i32 as _)
262
268
}
263
269
} ,
264
270
} ;
@@ -267,22 +273,22 @@ cfg_if! {
267
273
panic!( "unable to convert thread to fiber" ) ;
268
274
}
269
275
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 _) ;
271
277
if fiber == 0i32 as _ {
272
278
panic!( "unable to allocate fiber" ) ;
273
279
}
274
280
275
281
// Switch to the fiber we created. This changes stacks and starts executing
276
282
// fiber_proc on it. fiber_proc will run `callback` and then switch back
277
- kernel32 :: SwitchToFiber ( fiber) ;
283
+ SwitchToFiber ( fiber) ;
278
284
279
285
// We are back on the old stack and now we have destroy the fiber and its stack
280
- kernel32 :: DeleteFiber ( fiber) ;
286
+ DeleteFiber ( fiber) ;
281
287
282
288
// If we started out on a non-fiber thread, we converted that thread to a fiber.
283
289
// Here we convert back.
284
290
if !was_fiber {
285
- kernel32 :: ConvertFiberToThread ( ) ;
291
+ ConvertFiberToThread ( ) ;
286
292
}
287
293
288
294
if let Err ( payload) = info. result. unwrap( ) {
@@ -307,7 +313,7 @@ cfg_if! {
307
313
// some further logic to calculate the real stack
308
314
// guarantee. This logic is what is used on x86-32 and
309
315
// x86-64 Windows 10. Other versions and platforms may differ
310
- kernel32 :: SetThreadStackGuarantee ( & mut stack_guarantee)
316
+ SetThreadStackGuarantee ( & mut stack_guarantee)
311
317
} ;
312
318
std:: cmp:: max( stack_guarantee, min_guarantee) as usize + 0x1000
313
319
}
@@ -317,10 +323,10 @@ cfg_if! {
317
323
let mut mi = std:: mem:: zeroed( ) ;
318
324
// Query the allocation which contains our stack pointer in order
319
325
// to discover the size of the stack
320
- kernel32 :: VirtualQuery (
326
+ VirtualQuery (
321
327
__stacker_stack_pointer( ) as * const _,
322
328
& mut mi,
323
- std:: mem:: size_of_val( & mi) as winapi :: SIZE_T ,
329
+ std:: mem:: size_of_val( & mi) as SIZE_T ,
324
330
) ;
325
331
Some ( mi. AllocationBase as usize + get_thread_stack_guarantee( ) + 0x1000 )
326
332
}
0 commit comments