std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-12-22 11:04:23 -06:00
|
|
|
use prelude::v1::*;
|
2015-03-11 17:24:14 -05:00
|
|
|
use io::prelude::*;
|
2014-12-17 16:59:20 -06:00
|
|
|
|
2015-01-01 00:13:08 -06:00
|
|
|
use any::Any;
|
2015-09-24 16:49:38 -05:00
|
|
|
use cell::Cell;
|
2014-11-14 16:20:57 -06:00
|
|
|
use cell::RefCell;
|
2015-09-24 16:49:38 -05:00
|
|
|
use intrinsics;
|
2015-12-18 01:51:55 -06:00
|
|
|
use sync::StaticRwLock;
|
2016-01-25 11:16:43 -06:00
|
|
|
use sync::atomic::{AtomicBool, Ordering};
|
2015-03-11 17:24:14 -05:00
|
|
|
use sys::stdio::Stderr;
|
2015-09-08 17:53:46 -05:00
|
|
|
use sys_common::backtrace;
|
2015-04-15 14:27:05 -05:00
|
|
|
use sys_common::thread_info;
|
2015-09-24 16:49:38 -05:00
|
|
|
use sys_common::util;
|
2015-12-18 01:51:55 -06:00
|
|
|
use thread;
|
2015-09-24 16:49:38 -05:00
|
|
|
|
|
|
|
thread_local! { pub static PANIC_COUNT: Cell<usize> = Cell::new(0) }
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
thread_local! {
|
2015-03-11 17:24:14 -05:00
|
|
|
pub static LOCAL_STDERR: RefCell<Option<Box<Write + Send>>> = {
|
2014-11-14 11:18:10 -06:00
|
|
|
RefCell::new(None)
|
|
|
|
}
|
|
|
|
}
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
|
2015-12-18 01:51:55 -06:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Handler {
|
|
|
|
Default,
|
|
|
|
Custom(*mut (Fn(&PanicInfo) + 'static + Sync + Send)),
|
|
|
|
}
|
|
|
|
|
|
|
|
static HANDLER_LOCK: StaticRwLock = StaticRwLock::new();
|
|
|
|
static mut HANDLER: Handler = Handler::Default;
|
2016-01-25 11:16:43 -06:00
|
|
|
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
|
2015-12-18 01:51:55 -06:00
|
|
|
|
|
|
|
/// Registers a custom panic handler, 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
|
|
|
|
/// and generates a backtrace if requested, but this behavior can be customized
|
|
|
|
/// with the `set_handler` and `take_handler` functions.
|
|
|
|
///
|
|
|
|
/// The handler 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.
|
|
|
|
///
|
|
|
|
/// # 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 {
|
|
|
|
if thread::panicking() {
|
|
|
|
panic!("cannot modify the panic handler 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));
|
|
|
|
drop(lock);
|
|
|
|
|
|
|
|
if let Handler::Custom(ptr) = old_handler {
|
|
|
|
Box::from_raw(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unregisters the current panic handler, returning it.
|
|
|
|
///
|
|
|
|
/// If no custom handler is registered, the default handler 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> {
|
|
|
|
if thread::panicking() {
|
|
|
|
panic!("cannot modify the panic handler from a panicking thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let lock = HANDLER_LOCK.write();
|
|
|
|
let handler = HANDLER;
|
|
|
|
HANDLER = Handler::Default;
|
|
|
|
drop(lock);
|
|
|
|
|
|
|
|
match handler {
|
|
|
|
Handler::Default => Box::new(default_handler),
|
|
|
|
Handler::Custom(ptr) => {Box::from_raw(ptr)} // FIXME #30530
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A struct providing information about a panic.
|
|
|
|
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
|
|
|
|
pub struct PanicInfo<'a> {
|
|
|
|
payload: &'a (Any + Send),
|
|
|
|
location: Location<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PanicInfo<'a> {
|
|
|
|
/// Returns the payload associated with the panic.
|
|
|
|
///
|
|
|
|
/// This will commonly, but not always, be a `&'static str` or `String`.
|
|
|
|
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
|
|
|
|
pub fn payload(&self) -> &(Any + Send) {
|
|
|
|
self.payload
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns information about the location from which the panic originated,
|
|
|
|
/// if available.
|
|
|
|
///
|
|
|
|
/// This method will currently always return `Some`, but this may change
|
|
|
|
/// in future versions.
|
|
|
|
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
|
|
|
|
pub fn location(&self) -> Option<&Location> {
|
|
|
|
Some(&self.location)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A struct containing information about the location of a panic.
|
|
|
|
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
|
|
|
|
pub struct Location<'a> {
|
|
|
|
file: &'a str,
|
|
|
|
line: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Location<'a> {
|
|
|
|
/// Returns the name of the source file from which the panic originated.
|
|
|
|
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
|
|
|
|
pub fn file(&self) -> &str {
|
|
|
|
self.file
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the line number from which the panic originated.
|
|
|
|
#[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
|
|
|
|
pub fn line(&self) -> u32 {
|
|
|
|
self.line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_handler(info: &PanicInfo) {
|
|
|
|
let panics = PANIC_COUNT.with(|s| s.get());
|
|
|
|
|
|
|
|
// If this is a double panic, make sure that we print a backtrace
|
|
|
|
// for this panic. Otherwise only print it if logging is enabled.
|
|
|
|
let log_backtrace = panics >= 2 || backtrace::log_enabled();
|
|
|
|
|
|
|
|
let file = info.location.file;
|
|
|
|
let line = info.location.line;
|
|
|
|
|
|
|
|
let msg = match info.payload.downcast_ref::<&'static str>() {
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
Some(s) => *s,
|
2015-12-18 01:51:55 -06:00
|
|
|
None => match info.payload.downcast_ref::<String>() {
|
2015-02-18 13:48:57 -06:00
|
|
|
Some(s) => &s[..],
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
None => "Box<Any>",
|
|
|
|
}
|
|
|
|
};
|
2015-06-20 21:54:15 -05:00
|
|
|
let mut err = Stderr::new().ok();
|
2015-04-15 14:27:05 -05:00
|
|
|
let thread = thread_info::current_thread();
|
|
|
|
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
|
2015-09-23 12:47:15 -05:00
|
|
|
|
|
|
|
let write = |err: &mut ::io::Write| {
|
|
|
|
let _ = writeln!(err, "thread '{}' panicked at '{}', {}:{}",
|
|
|
|
name, msg, file, line);
|
2016-01-25 11:16:43 -06:00
|
|
|
|
2015-09-23 12:47:15 -05:00
|
|
|
if log_backtrace {
|
|
|
|
let _ = backtrace::write(err);
|
2016-01-25 11:16:43 -06:00
|
|
|
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
|
|
|
|
let _ = writeln!(err, "note: Run with `RUST_BACKTRACE=1` for a backtrace.");
|
2015-09-23 12:47:15 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-06 20:34:37 -06:00
|
|
|
let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
|
2015-06-20 21:54:15 -05:00
|
|
|
match (prev, err.as_mut()) {
|
|
|
|
(Some(mut stderr), _) => {
|
2015-09-23 12:47:15 -05:00
|
|
|
write(&mut *stderr);
|
2014-12-06 20:34:37 -06:00
|
|
|
let mut s = Some(stderr);
|
|
|
|
LOCAL_STDERR.with(|slot| {
|
|
|
|
*slot.borrow_mut() = s.take();
|
|
|
|
});
|
|
|
|
}
|
2015-09-23 12:47:15 -05:00
|
|
|
(None, Some(ref mut err)) => { write(err) }
|
2015-06-20 21:54:15 -05:00
|
|
|
_ => {}
|
2014-12-06 20:34:37 -06:00
|
|
|
}
|
2015-09-23 12:47:15 -05:00
|
|
|
}
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
|
2015-09-23 12:47:15 -05:00
|
|
|
pub fn on_panic(obj: &(Any+Send), file: &'static str, line: u32) {
|
2015-09-24 16:49:38 -05:00
|
|
|
let panics = PANIC_COUNT.with(|s| {
|
|
|
|
let count = s.get() + 1;
|
|
|
|
s.set(count);
|
|
|
|
count
|
|
|
|
});
|
|
|
|
|
2015-09-24 10:21:29 -05:00
|
|
|
// If this is the third nested call, on_panic triggered the last panic,
|
|
|
|
// otherwise the double-panic check would have aborted the process.
|
|
|
|
// Even if it is likely that on_panic was unable to log the backtrace,
|
|
|
|
// abort immediately to avoid infinite recursion, so that attaching a
|
|
|
|
// debugger provides a useable stacktrace.
|
|
|
|
if panics >= 3 {
|
|
|
|
util::dumb_print(format_args!("thread panicked while processing \
|
2016-01-09 13:19:56 -06:00
|
|
|
panic. aborting.\n"));
|
2015-09-24 10:21:29 -05:00
|
|
|
unsafe { intrinsics::abort() }
|
|
|
|
}
|
|
|
|
|
2015-12-18 01:51:55 -06:00
|
|
|
let info = PanicInfo {
|
|
|
|
payload: obj,
|
|
|
|
location: Location {
|
|
|
|
file: file,
|
|
|
|
line: line,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let _lock = HANDLER_LOCK.read();
|
|
|
|
match HANDLER {
|
|
|
|
Handler::Default => default_handler(&info),
|
|
|
|
Handler::Custom(ptr) => (*ptr)(&info),
|
|
|
|
}
|
|
|
|
}
|
2015-09-24 16:49:38 -05:00
|
|
|
|
|
|
|
if panics >= 2 {
|
|
|
|
// If a thread panics while it's already unwinding then we
|
|
|
|
// have limited options. Currently our preference is to
|
|
|
|
// just abort. In the future we may consider resuming
|
|
|
|
// unwinding or otherwise exiting the thread cleanly.
|
|
|
|
util::dumb_print(format_args!("thread panicked while panicking. \
|
2016-01-09 13:19:56 -06:00
|
|
|
aborting.\n"));
|
2015-09-24 16:49:38 -05:00
|
|
|
unsafe { intrinsics::abort() }
|
|
|
|
}
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
}
|