Skip to content

Refactored code to access TLS only in case of panic (II) #34866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 11, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactored code to access TLS only in case of panic
  • Loading branch information
nikhilshagri committed Aug 10, 2016
commit 221000abbeb233edafff2a00b9b10c2d13f4ad05
20 changes: 11 additions & 9 deletions src/libstd/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,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 @@ -258,18 +257,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