about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-03-18 09:21:43 -0700
committerbors <bors@rust-lang.org>2016-03-18 09:21:43 -0700
commit24bb607e7d65ebfc487eba62e053ac049f140efc (patch)
tree5488e6cdaba0a034870a1b0b459b9685e7352ed6 /src/libstd
parent235d77457d80b549dad3ac36d94f235208a1eafb (diff)
parent50fda1eead10af900a7b7c9f07983937e66bcc3c (diff)
downloadrust-24bb607e7d65ebfc487eba62e053ac049f140efc.tar.gz
rust-24bb607e7d65ebfc487eba62e053ac049f140efc.zip
Auto merge of #32282 - sfackler:panic-hook, r=alexcrichton
Adjustments to the panic hook API

Rename `set_handler` and `take_handler` to `set_hook` and `take_hook` since we're not actually "handling" (i.e. fixing) anything.

Also alter `set_hook` to take a `Box<Fn(&PanicInfo) + 'static + Sync + Send>` rather than a parameterized closure since there's otherwise no easy way to re-register a hook that came from `take_hook`.

cc #30449

r? @aturon
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/panic.rs16
-rw-r--r--src/libstd/panicking.rs62
2 files changed, 45 insertions, 33 deletions
diff --git a/src/libstd/panic.rs b/src/libstd/panic.rs
index 5c2e36623cb..aff11d036f8 100644
--- a/src/libstd/panic.rs
+++ b/src/libstd/panic.rs
@@ -23,7 +23,21 @@ use sync::{Arc, Mutex, RwLock};
 use sys_common::unwind;
 use thread::Result;
 
-pub use panicking::{take_handler, set_handler, PanicInfo, Location};
+pub use panicking::{take_hook, set_hook, PanicInfo, Location};
+
+///
+#[rustc_deprecated(since = "1.9.0", reason = "renamed to set_hook")]
+#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
+pub fn set_handler<F>(handler: F) where F: Fn(&PanicInfo) + 'static + Sync + Send {
+    set_hook(Box::new(handler))
+}
+
+///
+#[rustc_deprecated(since = "1.9.0", reason = "renamed to take_hook")]
+#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
+pub fn take_handler() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
+    take_hook()
+}
 
 /// A marker trait which represents "panic safe" types in Rust.
 ///
diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs
index 490c5f4b352..fd6a15b0f69 100644
--- a/src/libstd/panicking.rs
+++ b/src/libstd/panicking.rs
@@ -32,73 +32,71 @@ thread_local! {
 }
 
 #[derive(Copy, Clone)]
-enum Handler {
+enum Hook {
     Default,
     Custom(*mut (Fn(&PanicInfo) + 'static + Sync + Send)),
 }
 
-static HANDLER_LOCK: StaticRwLock = StaticRwLock::new();
-static mut HANDLER: Handler = Handler::Default;
+static HOOK_LOCK: StaticRwLock = StaticRwLock::new();
+static mut HOOK: Hook = Hook::Default;
 static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
 
-/// Registers a custom panic handler, replacing any that was previously
-/// registered.
+/// Registers a custom panic hook, replacing any that was previously registered.
 ///
-/// The panic handler is invoked when a thread panics, but before it begins
-/// unwinding the stack. The default handler prints a message to standard error
+/// The panic hook is invoked when a thread panics, but before it begins
+/// unwinding the stack. The default hook prints a message to standard error
 /// and generates a backtrace if requested, but this behavior can be customized
-/// with the `set_handler` and `take_handler` functions.
+/// with the `set_hook` and `take_hook` functions.
 ///
-/// The handler is provided with a `PanicInfo` struct which contains information
+/// The hook is provided with a `PanicInfo` struct which contains information
 /// about the origin of the panic, including the payload passed to `panic!` and
 /// the source code location from which the panic originated.
 ///
-/// The panic handler is a global resource.
+/// The panic hook is a global resource.
 ///
 /// # Panics
 ///
 /// Panics if called from a panicking thread.
 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
-pub fn set_handler<F>(handler: F) where F: Fn(&PanicInfo) + 'static + Sync + Send {
+pub fn set_hook(hook: Box<Fn(&PanicInfo) + 'static + Sync + Send>) {
     if thread::panicking() {
-        panic!("cannot modify the panic handler from a panicking thread");
+        panic!("cannot modify the panic hook from a panicking thread");
     }
 
-    let handler = Box::new(handler);
     unsafe {
-        let lock = HANDLER_LOCK.write();
-        let old_handler = HANDLER;
-        HANDLER = Handler::Custom(Box::into_raw(handler));
+        let lock = HOOK_LOCK.write();
+        let old_hook = HOOK;
+        HOOK = Hook::Custom(Box::into_raw(hook));
         drop(lock);
 
-        if let Handler::Custom(ptr) = old_handler {
+        if let Hook::Custom(ptr) = old_hook {
             Box::from_raw(ptr);
         }
     }
 }
 
-/// Unregisters the current panic handler, returning it.
+/// Unregisters the current panic hook, returning it.
 ///
-/// If no custom handler is registered, the default handler will be returned.
+/// If no custom hook is registered, the default hook will be returned.
 ///
 /// # Panics
 ///
 /// Panics if called from a panicking thread.
 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
-pub fn take_handler() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
+pub fn take_hook() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
     if thread::panicking() {
-        panic!("cannot modify the panic handler from a panicking thread");
+        panic!("cannot modify the panic hook from a panicking thread");
     }
 
     unsafe {
-        let lock = HANDLER_LOCK.write();
-        let handler = HANDLER;
-        HANDLER = Handler::Default;
+        let lock = HOOK_LOCK.write();
+        let hook = HOOK;
+        HOOK = Hook::Default;
         drop(lock);
 
-        match handler {
-            Handler::Default => Box::new(default_handler),
-            Handler::Custom(ptr) => {Box::from_raw(ptr)} // FIXME #30530
+        match hook {
+            Hook::Default => Box::new(default_hook),
+            Hook::Custom(ptr) => {Box::from_raw(ptr)} // FIXME #30530
         }
     }
 }
@@ -151,7 +149,7 @@ impl<'a> Location<'a> {
     }
 }
 
-fn default_handler(info: &PanicInfo) {
+fn default_hook(info: &PanicInfo) {
     let panics = PANIC_COUNT.with(|s| s.get());
 
     // If this is a double panic, make sure that we print a backtrace
@@ -224,10 +222,10 @@ pub fn on_panic(obj: &(Any+Send), file: &'static str, line: u32) {
     };
 
     unsafe {
-        let _lock = HANDLER_LOCK.read();
-        match HANDLER {
-            Handler::Default => default_handler(&info),
-            Handler::Custom(ptr) => (*ptr)(&info),
+        let _lock = HOOK_LOCK.read();
+        match HOOK {
+            Hook::Default => default_hook(&info),
+            Hook::Custom(ptr) => (*ptr)(&info),
         }
     }