Skip to content

Commit

Permalink
Merge pull request rust-lang#34836 from cynicaldevil/panic-counter
Browse files Browse the repository at this point in the history
Refactored code to access TLS only in case of panic
  • Loading branch information
alexcrichton authored Jul 16, 2016
2 parents eb196dc + b28e9dc commit d5b9850
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/libstd/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,9 @@ fn default_hook(info: &PanicInfo) {
pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<Any + Send>> {
let mut slot = None;
let mut f = Some(f);
let ret = PANIC_COUNT.with(|s| {
let prev = s.get();
s.set(0);
let ret;

{
let mut to_run = || {
slot = Some(f.take().unwrap()());
};
Expand All @@ -248,18 +247,21 @@ pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<Any + Send>> {
dataptr,
&mut any_data,
&mut any_vtable);
s.set(prev);

if r == 0 {
Ok(())
ret = Ok(());
} else {
Err(mem::transmute(raw::TraitObject {
PANIC_COUNT.with(|s| {
let prev = s.get();
s.set(prev - 1);
});
ret = Err(mem::transmute(raw::TraitObject {
data: any_data as *mut _,
vtable: any_vtable as *mut _,
}))
}));
}
});
}

debug_assert!(PANIC_COUNT.with(|c| c.get() == 0));
return ret.map(|()| {
slot.take().unwrap()
});
Expand Down

0 comments on commit d5b9850

Please sign in to comment.