thread_tree/
unwind.rs

1
2
3// Package up unwind recovery. Note that if you are in some sensitive
4// place, you can use the `AbortIfPanic` helper to protect against
5// accidental panics in the rayon code itself.
6
7use std::any::Any;
8use std::thread;
9use std::io::prelude::*;
10use std::io::stderr;
11use std::panic::{self, AssertUnwindSafe};
12
13/// Executes `f` and captures any panic, translating that panic into a
14/// `Err` result. The assumption is that any panic will be propagated
15/// later with `resume_unwinding`, and hence `f` can be treated as
16/// exception safe.
17pub fn halt_unwinding<F, R>(func: F) -> thread::Result<R>
18where
19    F: FnOnce() -> R,
20{
21    panic::catch_unwind(AssertUnwindSafe(func))
22}
23
24pub fn resume_unwinding(payload: Box<dyn Any + Send>) -> ! {
25    panic::resume_unwind(payload)
26}
27
28pub struct AbortIfPanic;
29
30fn aborting() {
31    let _ = writeln!(&mut stderr(), "Rayon: detected unexpected panic; aborting");
32}
33
34impl Drop for AbortIfPanic {
35    fn drop(&mut self) {
36        aborting();
37        ::std::process::abort(); // stable in rust 1.17
38    }
39}