-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
188 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This file is part of libfringe, a low-level green threading library. | ||
// Copyright (c) Amanieu d'Antras <amanieu@gmail.com>, | ||
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or | ||
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or | ||
// http://opensource.org/licenses/MIT>, at your option. This file may not be | ||
// copied, modified, or distributed except according to those terms. | ||
extern crate std; | ||
|
||
use self::std::panic; | ||
use self::std::boxed::Box; | ||
use core::any::Any; | ||
use arch::StackPointer; | ||
|
||
// Marker object that is passed through the stack unwinding | ||
struct UnwindMarker { | ||
// We use the CFA address as an identifier so that nested generators are handled | ||
// correctly. When unwinding, we will want to continue through any number of | ||
// nested generators until we reach the one identified with a matching CFA address. | ||
cfa_addr: *mut usize, | ||
} | ||
unsafe impl Send for UnwindMarker {} | ||
|
||
// Whether the current platform support unwinding across multiple stacks. | ||
fn have_cross_stack_unwind() -> bool { | ||
// The only platform which doesn't support cross-stack unwinding is Windows | ||
// since it uses SEH for unwinding instead of libunwind. | ||
!cfg!(windows) | ||
} | ||
|
||
pub unsafe extern "C" fn unwind_wrapper(arg: usize, sp: StackPointer, cfa_addr: *mut usize, | ||
f: unsafe extern "C" fn(usize, StackPointer)) -> Option<Box<Box<Any + Send>>> { | ||
// Catch any attempts to unwind out of the context. | ||
match panic::catch_unwind(move || f(arg, sp)) { | ||
Ok(_) => None, | ||
Err(err) => { | ||
// If the unwinding is due to an UnwindMarker, check whether it is intended | ||
// for us by comparing the CFA of the caller with ours. If it is the same | ||
// then we can swallow the exception and return to the caller normally. | ||
if let Some(marker) = err.downcast_ref::<UnwindMarker>() { | ||
if marker.cfa_addr == cfa_addr { | ||
return None; | ||
} | ||
} | ||
|
||
// Otherwise, propagate the panic to the parent context. | ||
if have_cross_stack_unwind() { | ||
panic::resume_unwind(err) | ||
} else { | ||
// The assembly code will call start_unwind in the parent context and | ||
// pass it this Box as parameter. | ||
Some(Box::new(err)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub unsafe extern "C" fn start_unwind(panic: Box<Box<Any + Send>>) -> ! { | ||
// Use resume_unwind instead of panic! to avoid printing a message. | ||
panic::resume_unwind(*panic) | ||
} | ||
|
||
pub fn unwind_arg(cfa_addr: *mut usize) -> usize { | ||
let marker = UnwindMarker { | ||
cfa_addr: cfa_addr | ||
}; | ||
Box::into_raw(Box::new(Box::new(marker) as Box<Any + Send>)) as usize | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters