Skip to content

Commit cb4232d

Browse files
committed
Make unwinding an optional feature
1 parent af1f450 commit cb4232d

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ version = "1.0.0"
1919
simd = "0.1"
2020

2121
[features]
22-
default = ["alloc", "valgrind"]
22+
default = ["alloc", "valgrind", "unwind"]
2323
alloc = []
2424
valgrind = ["valgrind_request"]
25+
unwind = []
2526

2627
# These apply only to tests within this library; assembly at -O0 is completely
2728
# unreadable, so use -O1.

src/generator.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ impl<'a, Input, Output, Stack> Generator<'a, Input, Output, Stack>
218218
/// Extracts the stack from a generator without checking if the generator function has returned.
219219
/// This will leave any pointers into the generator stack dangling, and won't run destructors.
220220
pub unsafe fn unsafe_unwrap(mut self) -> Stack {
221-
self.stack_ptr.map(|stack_ptr| arch::unwind(stack_ptr, self.stack.inner.base()));
221+
if cfg!(feature = "unwind") {
222+
self.stack_ptr.map(|stack_ptr| arch::unwind(stack_ptr, self.stack.inner.base()));
223+
}
222224

223225
// We can't just return self.stack since Generator has a Drop impl
224226
let stack = ptr::read(&self.stack.inner);
@@ -232,8 +234,11 @@ impl<'a, Input, Output, Stack> Drop for Generator<'a, Input, Output, Stack>
232234
where Input: 'a, Output: 'a, Stack: stack::Stack {
233235
fn drop(&mut self) {
234236
unsafe {
235-
self.stack_ptr.map(|stack_ptr| arch::unwind(stack_ptr, self.stack.inner.base()));
236-
ptr::drop_in_place(&mut self.stack.inner);
237+
// If unwinding is not available then we have to leak the stack.
238+
if cfg!(feature = "unwind") {
239+
self.stack_ptr.map(|stack_ptr| arch::unwind(stack_ptr, self.stack.inner.base()));
240+
ptr::drop_in_place(&mut self.stack.inner);
241+
}
237242
}
238243
}
239244
}

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ pub const STACK_ALIGNMENT: usize = arch::STACK_ALIGNMENT;
4343

4444
mod debug;
4545

46+
#[cfg(feature = "unwind")]
47+
#[path = "unwind.rs"]
48+
mod unwind;
49+
#[cfg(not(feature = "unwind"))]
50+
#[path = "no_unwind.rs"]
4651
mod unwind;
4752

4853
pub mod generator;

src/no_unwind.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file is part of libfringe, a low-level green threading library.
2+
// Copyright (c) Amanieu d'Antras <[email protected]>,
3+
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4+
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5+
// http://opensource.org/licenses/MIT>, at your option. This file may not be
6+
// copied, modified, or distributed except according to those terms.
7+
8+
use arch::StackPointer;
9+
10+
pub unsafe extern "C" fn unwind_wrapper(arg: usize, sp: StackPointer, _stack_base: *mut u8,
11+
f: unsafe fn(usize, StackPointer)) -> usize {
12+
f(arg, sp);
13+
0
14+
}
15+
16+
pub unsafe extern "C" fn start_unwind(_panic: usize) -> ! {
17+
unreachable!();
18+
}
19+
20+
#[inline]
21+
pub fn unwind_arg(_stack_base: *mut u8) -> usize {
22+
unreachable!();
23+
}

0 commit comments

Comments
 (0)