Skip to content

Commit

Permalink
Touch up style a bit in fibers implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Dec 10, 2018
1 parent 70cbe92 commit 2c3befa
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ cfg_if! {

cfg_if! {
if #[cfg(windows)] {
use std::ptr;
use std::io;

use winapi::shared::basetsd::*;
use winapi::shared::minwindef::{LPVOID, BOOL};
use winapi::shared::ntdef::*;
Expand Down Expand Up @@ -264,18 +267,22 @@ cfg_if! {
// to the current stack. Threads coverted to fibers still act like
// regular threads, but they have associated fiber data. We later
// convert it back to a regular thread and free the fiber data.
ConvertThreadToFiber(0i32 as _)
ConvertThreadToFiber(ptr::null_mut())
}
},
};
if info.parent_fiber == 0i32 as _ {
if info.parent_fiber.is_null() {
// We don't have a handle to the fiber, so we can't switch back
panic!("unable to convert thread to fiber");
panic!("unable to convert thread to fiber: {}", io::Error::last_os_error());
}

let fiber = CreateFiber(stack_size as _, Some(fiber_proc), &mut info as *mut FiberInfo as *mut _);
if fiber == 0i32 as _ {
panic!("unable to allocate fiber");
let fiber = CreateFiber(
stack_size as SIZE_T,
Some(fiber_proc),
&mut info as *mut FiberInfo as *mut _,
);
if fiber.is_null() {
panic!("unable to allocate fiber: {}", io::Error::last_os_error());
}

// Switch to the fiber we created. This changes stacks and starts executing
Expand All @@ -288,7 +295,9 @@ cfg_if! {
// If we started out on a non-fiber thread, we converted that thread to a fiber.
// Here we convert back.
if !was_fiber {
ConvertFiberToThread();
if ConvertFiberToThread() == 0 {
panic!("unable to convert back to thread: {}", io::Error::last_os_error());
}
}

if let Err(payload) = info.result.unwrap() {
Expand Down

0 comments on commit 2c3befa

Please sign in to comment.