2013-12-15 19:17:07 -06:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
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
|
|
|
//! Implementation of Rust stack unwinding
|
|
|
|
//!
|
|
|
|
//! For background on exception handling and stack unwinding please see
|
|
|
|
//! "Exception Handling in LLVM" (llvm.org/docs/ExceptionHandling.html) and
|
|
|
|
//! documents linked from it.
|
|
|
|
//! These are also good reads:
|
|
|
|
//! http://theofilos.cs.columbia.edu/blog/2013/09/22/base_abi/
|
|
|
|
//! http://monoinfinito.wordpress.com/series/exception-handling-in-c/
|
|
|
|
//! http://www.airs.com/blog/index.php?s=exception+frames
|
|
|
|
//!
|
|
|
|
//! ## A brief summary
|
|
|
|
//!
|
|
|
|
//! Exception handling happens in two phases: a search phase and a cleanup phase.
|
|
|
|
//!
|
|
|
|
//! In both phases the unwinder walks stack frames from top to bottom using
|
|
|
|
//! information from the stack frame unwind sections of the current process's
|
|
|
|
//! modules ("module" here refers to an OS module, i.e. an executable or a
|
|
|
|
//! dynamic library).
|
|
|
|
//!
|
|
|
|
//! For each stack frame, it invokes the associated "personality routine", whose
|
|
|
|
//! address is also stored in the unwind info section.
|
|
|
|
//!
|
|
|
|
//! In the search phase, the job of a personality routine is to examine exception
|
|
|
|
//! object being thrown, and to decide whether it should be caught at that stack
|
|
|
|
//! frame. Once the handler frame has been identified, cleanup phase begins.
|
|
|
|
//!
|
|
|
|
//! In the cleanup phase, personality routines invoke cleanup code associated
|
|
|
|
//! with their stack frames (i.e. destructors). Once stack has been unwound down
|
|
|
|
//! to the handler frame level, unwinding stops and the last personality routine
|
2014-09-26 12:51:33 -05:00
|
|
|
//! transfers control to its catch block.
|
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
|
|
|
//!
|
|
|
|
//! ## Frame unwind info registration
|
|
|
|
//!
|
2014-09-26 12:51:33 -05:00
|
|
|
//! Each module has its own frame unwind info section (usually ".eh_frame"), and
|
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
|
|
|
//! unwinder needs to know about all of them in order for unwinding to be able to
|
|
|
|
//! cross module boundaries.
|
|
|
|
//!
|
|
|
|
//! On some platforms, like Linux, this is achieved by dynamically enumerating
|
|
|
|
//! currently loaded modules via the dl_iterate_phdr() API and finding all
|
|
|
|
//! .eh_frame sections.
|
|
|
|
//!
|
|
|
|
//! Others, like Windows, require modules to actively register their unwind info
|
|
|
|
//! sections by calling __register_frame_info() API at startup. In the latter
|
|
|
|
//! case it is essential that there is only one copy of the unwinder runtime in
|
|
|
|
//! the process. This is usually achieved by linking to the dynamic version of
|
|
|
|
//! the unwind runtime.
|
|
|
|
//!
|
|
|
|
//! Currently Rust uses unwind runtime provided by libgcc.
|
|
|
|
|
2014-12-22 11:04:23 -06:00
|
|
|
use prelude::v1::*;
|
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-23 21:21:17 -06:00
|
|
|
use any::Any;
|
2014-12-17 16:59:20 -06:00
|
|
|
use cell::Cell;
|
2014-11-23 21:21:17 -06:00
|
|
|
use cmp;
|
2014-12-10 09:37:56 -06:00
|
|
|
use failure;
|
2014-11-23 21:21:17 -06:00
|
|
|
use fmt;
|
|
|
|
use intrinsics;
|
2014-12-10 09:37:56 -06:00
|
|
|
use libc::c_void;
|
2014-11-23 21:21:17 -06:00
|
|
|
use mem;
|
2015-01-02 01:53:35 -06:00
|
|
|
use sync::atomic::{mod, Ordering};
|
2014-12-31 12:20:31 -06:00
|
|
|
use sync::{Once, ONCE_INIT};
|
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-23 21:21:17 -06:00
|
|
|
use rt::libunwind as uw;
|
2013-12-15 19:17:07 -06:00
|
|
|
|
2014-06-04 12:54:35 -05:00
|
|
|
struct Exception {
|
|
|
|
uwe: uw::_Unwind_Exception,
|
2014-06-14 13:03:34 -05:00
|
|
|
cause: Option<Box<Any + Send>>,
|
2014-06-04 12:54:35 -05:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:08:02 -06:00
|
|
|
pub type Callback = fn(msg: &(Any + Send), file: &'static str, line: uint);
|
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-12-26 15:04:27 -06:00
|
|
|
// Variables used for invoking callbacks when a thread starts to unwind.
|
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
|
|
|
//
|
|
|
|
// For more information, see below.
|
2014-10-06 18:35:11 -05:00
|
|
|
const MAX_CALLBACKS: uint = 16;
|
2014-12-31 22:40:24 -06:00
|
|
|
static CALLBACKS: [atomic::AtomicUint; MAX_CALLBACKS] =
|
2014-12-29 17:03:01 -06:00
|
|
|
[atomic::ATOMIC_UINT_INIT, atomic::ATOMIC_UINT_INIT,
|
|
|
|
atomic::ATOMIC_UINT_INIT, atomic::ATOMIC_UINT_INIT,
|
|
|
|
atomic::ATOMIC_UINT_INIT, atomic::ATOMIC_UINT_INIT,
|
|
|
|
atomic::ATOMIC_UINT_INIT, atomic::ATOMIC_UINT_INIT,
|
|
|
|
atomic::ATOMIC_UINT_INIT, atomic::ATOMIC_UINT_INIT,
|
|
|
|
atomic::ATOMIC_UINT_INIT, atomic::ATOMIC_UINT_INIT,
|
|
|
|
atomic::ATOMIC_UINT_INIT, atomic::ATOMIC_UINT_INIT,
|
|
|
|
atomic::ATOMIC_UINT_INIT, atomic::ATOMIC_UINT_INIT];
|
|
|
|
static CALLBACK_CNT: atomic::AtomicUint = atomic::ATOMIC_UINT_INIT;
|
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-12-18 21:41:20 -06:00
|
|
|
thread_local! { static PANICKING: Cell<bool> = Cell::new(false) }
|
2014-12-17 16:59:20 -06:00
|
|
|
|
2014-10-09 14:17:22 -05:00
|
|
|
/// Invoke a closure, capturing the cause of panic if one occurs.
|
2014-06-04 12:54:35 -05:00
|
|
|
///
|
2014-10-09 14:17:22 -05:00
|
|
|
/// This function will return `None` if the closure did not panic, and will
|
|
|
|
/// return `Some(cause)` if the closure panics. The `cause` returned is the
|
|
|
|
/// object with which panic was originally invoked.
|
2014-06-04 12:54:35 -05:00
|
|
|
///
|
|
|
|
/// This function also is unsafe for a variety of reasons:
|
|
|
|
///
|
|
|
|
/// * This is not safe to call in a nested fashion. The unwinding
|
|
|
|
/// interface for Rust is designed to have at most one try/catch block per
|
2014-12-26 15:04:27 -06:00
|
|
|
/// thread, not multiple. No runtime checking is currently performed to uphold
|
2014-06-04 12:54:35 -05:00
|
|
|
/// this invariant, so this function is not safe. A nested try/catch block
|
|
|
|
/// may result in corruption of the outer try/catch block's state, especially
|
2014-12-26 15:04:27 -06:00
|
|
|
/// if this is used within a thread itself.
|
2014-06-04 12:54:35 -05:00
|
|
|
///
|
2014-12-26 15:04:27 -06:00
|
|
|
/// * It is not sound to trigger unwinding while already unwinding. Rust threads
|
2014-06-04 12:54:35 -05:00
|
|
|
/// have runtime checks in place to ensure this invariant, but it is not
|
2014-12-26 15:04:27 -06:00
|
|
|
/// guaranteed that a rust thread is in place when invoking this function.
|
2014-06-04 12:54:35 -05:00
|
|
|
/// Unwinding twice can lead to resource leaks where some destructors are not
|
|
|
|
/// run.
|
2014-12-10 09:49:45 -06:00
|
|
|
pub unsafe fn try<F: FnOnce()>(f: F) -> Result<(), Box<Any + Send>> {
|
|
|
|
let mut f = Some(f);
|
2014-12-17 16:59:20 -06:00
|
|
|
|
|
|
|
let prev = PANICKING.with(|s| s.get());
|
|
|
|
PANICKING.with(|s| s.set(false));
|
2014-12-10 09:49:45 -06:00
|
|
|
let ep = rust_try(try_fn::<F>, &mut f as *mut _ as *mut c_void);
|
2014-12-17 16:59:20 -06:00
|
|
|
PANICKING.with(|s| s.set(prev));
|
2014-06-04 12:54:35 -05:00
|
|
|
return if ep.is_null() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
let my_ep = ep as *mut Exception;
|
|
|
|
rtdebug!("caught {}", (*my_ep).uwe.exception_class);
|
|
|
|
let cause = (*my_ep).cause.take();
|
|
|
|
uw::_Unwind_DeleteException(ep);
|
|
|
|
Err(cause.unwrap())
|
|
|
|
};
|
|
|
|
|
2014-12-10 09:49:45 -06:00
|
|
|
extern fn try_fn<F: FnOnce()>(opt_closure: *mut c_void) {
|
|
|
|
let opt_closure = opt_closure as *mut Option<F>;
|
|
|
|
unsafe { (*opt_closure).take().unwrap()(); }
|
2013-12-15 19:17:07 -06: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
|
|
|
#[link(name = "rustrt_native", kind = "static")]
|
2014-07-30 09:44:20 -05:00
|
|
|
#[cfg(not(test))]
|
|
|
|
extern {}
|
|
|
|
|
2014-06-04 12:54:35 -05:00
|
|
|
extern {
|
|
|
|
// Rust's try-catch
|
|
|
|
// When f(...) returns normally, the return value is null.
|
|
|
|
// When f(...) throws, the return value is a pointer to the caught
|
|
|
|
// exception object.
|
2014-12-10 09:49:45 -06:00
|
|
|
fn rust_try(f: extern fn(*mut c_void),
|
2014-06-25 14:47:34 -05:00
|
|
|
data: *mut c_void) -> *mut uw::_Unwind_Exception;
|
2014-06-04 12:54:35 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-15 19:17:07 -06:00
|
|
|
|
2014-12-17 16:59:20 -06:00
|
|
|
/// Test if the current thread is currently panicking.
|
|
|
|
pub fn panicking() -> bool {
|
|
|
|
PANICKING.with(|s| s.get())
|
|
|
|
}
|
|
|
|
|
2014-06-04 12:54:35 -05:00
|
|
|
// An uninlined, unmangled function upon which to slap yer breakpoints
|
|
|
|
#[inline(never)]
|
|
|
|
#[no_mangle]
|
2014-10-29 18:28:34 -05:00
|
|
|
fn rust_panic(cause: Box<Any + Send>) -> ! {
|
2014-06-04 12:54:35 -05:00
|
|
|
rtdebug!("begin_unwind()");
|
2013-12-30 20:43:03 -06:00
|
|
|
|
2014-06-04 12:54:35 -05:00
|
|
|
unsafe {
|
|
|
|
let exception = box Exception {
|
|
|
|
uwe: uw::_Unwind_Exception {
|
|
|
|
exception_class: rust_exception_class(),
|
|
|
|
exception_cleanup: exception_cleanup,
|
2014-12-31 22:40:24 -06:00
|
|
|
private: [0; uw::unwinder_private_data_size],
|
2014-06-04 12:54:35 -05:00
|
|
|
},
|
|
|
|
cause: Some(cause),
|
|
|
|
};
|
|
|
|
let error = uw::_Unwind_RaiseException(mem::transmute(exception));
|
|
|
|
rtabort!("Could not unwind stack, error = {}", error as int)
|
2013-12-15 19:17:07 -06:00
|
|
|
}
|
|
|
|
|
2014-06-04 12:54:35 -05:00
|
|
|
extern fn exception_cleanup(_unwind_code: uw::_Unwind_Reason_Code,
|
2014-06-25 14:47:34 -05:00
|
|
|
exception: *mut uw::_Unwind_Exception) {
|
2014-06-04 12:54:35 -05:00
|
|
|
rtdebug!("exception_cleanup()");
|
|
|
|
unsafe {
|
|
|
|
let _: Box<Exception> = mem::transmute(exception);
|
2013-12-15 19:17:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rust's exception class identifier. This is used by personality routines to
|
|
|
|
// determine whether the exception was thrown by their own runtime.
|
2013-12-18 11:57:58 -06:00
|
|
|
fn rust_exception_class() -> uw::_Unwind_Exception_Class {
|
|
|
|
// M O Z \0 R U S T -- vendor, language
|
|
|
|
0x4d4f5a_00_52555354
|
2013-12-15 19:17:07 -06:00
|
|
|
}
|
|
|
|
|
2013-12-18 11:57:58 -06:00
|
|
|
// We could implement our personality routine in pure Rust, however exception
|
|
|
|
// info decoding is tedious. More importantly, personality routines have to
|
|
|
|
// handle various platform quirks, which are not fun to maintain. For this
|
|
|
|
// reason, we attempt to reuse personality routine of the C language:
|
|
|
|
// __gcc_personality_v0.
|
2013-12-15 19:17:07 -06:00
|
|
|
//
|
2013-12-18 11:57:58 -06:00
|
|
|
// Since C does not support exception catching, __gcc_personality_v0 simply
|
|
|
|
// always returns _URC_CONTINUE_UNWIND in search phase, and always returns
|
|
|
|
// _URC_INSTALL_CONTEXT (i.e. "invoke cleanup code") in cleanup phase.
|
2013-12-15 19:17:07 -06:00
|
|
|
//
|
2013-12-18 11:57:58 -06:00
|
|
|
// This is pretty close to Rust's exception handling approach, except that Rust
|
2014-12-26 15:04:27 -06:00
|
|
|
// does have a single "catch-all" handler at the bottom of each thread's stack.
|
2014-08-05 21:07:38 -05:00
|
|
|
// So we have two versions of the personality routine:
|
2013-12-18 11:57:58 -06:00
|
|
|
// - rust_eh_personality, used by all cleanup landing pads, which never catches,
|
|
|
|
// so the behavior of __gcc_personality_v0 is perfectly adequate there, and
|
|
|
|
// - rust_eh_personality_catch, used only by rust_try(), which always catches.
|
2014-08-05 21:07:38 -05:00
|
|
|
//
|
|
|
|
// Note, however, that for implementation simplicity, rust_eh_personality_catch
|
|
|
|
// lacks code to install a landing pad, so in order to obtain exception object
|
|
|
|
// pointer (which it needs to return upstream), rust_try() employs another trick:
|
|
|
|
// it calls into the nested rust_try_inner(), whose landing pad does not resume
|
|
|
|
// unwinds. Instead, it extracts the exception pointer and performs a "normal"
|
|
|
|
// return.
|
|
|
|
//
|
|
|
|
// See also: rt/rust_try.ll
|
2013-12-15 19:17:07 -06:00
|
|
|
|
2014-09-29 01:38:24 -05:00
|
|
|
#[cfg(all(not(target_arch = "arm"),
|
|
|
|
not(all(windows, target_arch = "x86_64")),
|
|
|
|
not(test)))]
|
2014-01-07 00:33:37 -06:00
|
|
|
#[doc(hidden)]
|
2014-01-04 01:34:15 -06:00
|
|
|
pub mod eabi {
|
2014-11-23 21:21:17 -06:00
|
|
|
use rt::libunwind as uw;
|
2014-01-04 01:34:15 -06:00
|
|
|
use libc::c_int;
|
2013-12-15 19:17:07 -06:00
|
|
|
|
2014-01-04 01:34:15 -06:00
|
|
|
extern "C" {
|
|
|
|
fn __gcc_personality_v0(version: c_int,
|
|
|
|
actions: uw::_Unwind_Action,
|
|
|
|
exception_class: uw::_Unwind_Exception_Class,
|
2014-06-25 14:47:34 -05:00
|
|
|
ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
context: *mut uw::_Unwind_Context)
|
2014-01-04 01:34:15 -06:00
|
|
|
-> uw::_Unwind_Reason_Code;
|
|
|
|
}
|
|
|
|
|
2014-05-19 11:30:09 -05:00
|
|
|
#[lang="eh_personality"]
|
2014-04-10 12:48:38 -05:00
|
|
|
#[no_mangle] // referenced from rust_try.ll
|
|
|
|
extern fn rust_eh_personality(
|
2014-05-19 11:30:09 -05:00
|
|
|
version: c_int,
|
|
|
|
actions: uw::_Unwind_Action,
|
|
|
|
exception_class: uw::_Unwind_Exception_Class,
|
2014-06-25 14:47:34 -05:00
|
|
|
ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
context: *mut uw::_Unwind_Context
|
2014-05-19 11:30:09 -05:00
|
|
|
) -> uw::_Unwind_Reason_Code
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
__gcc_personality_v0(version, actions, exception_class, ue_header,
|
|
|
|
context)
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 01:34:15 -06:00
|
|
|
|
|
|
|
#[no_mangle] // referenced from rust_try.ll
|
|
|
|
pub extern "C" fn rust_eh_personality_catch(
|
2014-04-10 12:48:38 -05:00
|
|
|
_version: c_int,
|
2014-01-04 01:34:15 -06:00
|
|
|
actions: uw::_Unwind_Action,
|
2014-04-10 12:48:38 -05:00
|
|
|
_exception_class: uw::_Unwind_Exception_Class,
|
|
|
|
_ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
_context: *mut uw::_Unwind_Context
|
2014-01-04 01:34:15 -06:00
|
|
|
) -> uw::_Unwind_Reason_Code
|
|
|
|
{
|
2014-04-10 12:48:38 -05:00
|
|
|
|
2014-01-04 01:34:15 -06:00
|
|
|
if (actions as c_int & uw::_UA_SEARCH_PHASE as c_int) != 0 { // search phase
|
|
|
|
uw::_URC_HANDLER_FOUND // catch!
|
|
|
|
}
|
|
|
|
else { // cleanup phase
|
2014-04-10 12:48:38 -05:00
|
|
|
uw::_URC_INSTALL_CONTEXT
|
2014-05-05 02:07:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iOS on armv7 is using SjLj exceptions and therefore requires to use
|
|
|
|
// a specialized personality routine: __gcc_personality_sj0
|
|
|
|
|
2014-09-29 01:38:24 -05:00
|
|
|
#[cfg(all(target_os = "ios", target_arch = "arm", not(test)))]
|
2014-05-05 02:07:49 -05:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod eabi {
|
2014-11-23 21:21:17 -06:00
|
|
|
use rt::libunwind as uw;
|
2014-05-05 02:07:49 -05:00
|
|
|
use libc::c_int;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn __gcc_personality_sj0(version: c_int,
|
|
|
|
actions: uw::_Unwind_Action,
|
|
|
|
exception_class: uw::_Unwind_Exception_Class,
|
2014-06-25 14:47:34 -05:00
|
|
|
ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
context: *mut uw::_Unwind_Context)
|
2014-05-05 02:07:49 -05:00
|
|
|
-> uw::_Unwind_Reason_Code;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[lang="eh_personality"]
|
2014-04-10 12:48:38 -05:00
|
|
|
#[no_mangle] // referenced from rust_try.ll
|
2014-05-05 02:07:49 -05:00
|
|
|
pub extern "C" fn rust_eh_personality(
|
|
|
|
version: c_int,
|
|
|
|
actions: uw::_Unwind_Action,
|
|
|
|
exception_class: uw::_Unwind_Exception_Class,
|
2014-06-25 14:47:34 -05:00
|
|
|
ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
context: *mut uw::_Unwind_Context
|
2014-05-05 02:07:49 -05:00
|
|
|
) -> uw::_Unwind_Reason_Code
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
__gcc_personality_sj0(version, actions, exception_class, ue_header,
|
|
|
|
context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle] // referenced from rust_try.ll
|
|
|
|
pub extern "C" fn rust_eh_personality_catch(
|
2014-04-10 12:48:38 -05:00
|
|
|
_version: c_int,
|
2014-05-05 02:07:49 -05:00
|
|
|
actions: uw::_Unwind_Action,
|
2014-04-10 12:48:38 -05:00
|
|
|
_exception_class: uw::_Unwind_Exception_Class,
|
|
|
|
_ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
_context: *mut uw::_Unwind_Context
|
2014-05-05 02:07:49 -05:00
|
|
|
) -> uw::_Unwind_Reason_Code
|
|
|
|
{
|
|
|
|
if (actions as c_int & uw::_UA_SEARCH_PHASE as c_int) != 0 { // search phase
|
|
|
|
uw::_URC_HANDLER_FOUND // catch!
|
|
|
|
}
|
|
|
|
else { // cleanup phase
|
2014-08-21 03:10:03 -05:00
|
|
|
unsafe {
|
|
|
|
__gcc_personality_sj0(_version, actions, _exception_class, _ue_header,
|
|
|
|
_context)
|
|
|
|
}
|
2014-01-04 01:34:15 -06:00
|
|
|
}
|
2013-12-15 19:17:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 02:07:49 -05:00
|
|
|
|
2014-01-04 01:34:15 -06:00
|
|
|
// ARM EHABI uses a slightly different personality routine signature,
|
|
|
|
// but otherwise works the same.
|
2014-09-29 01:38:24 -05:00
|
|
|
#[cfg(all(target_arch = "arm", not(target_os = "ios"), not(test)))]
|
2014-08-05 21:07:38 -05:00
|
|
|
#[doc(hidden)]
|
2014-01-04 01:34:15 -06:00
|
|
|
pub mod eabi {
|
2014-11-23 21:21:17 -06:00
|
|
|
use rt::libunwind as uw;
|
2014-01-04 01:34:15 -06:00
|
|
|
use libc::c_int;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn __gcc_personality_v0(state: uw::_Unwind_State,
|
2014-06-25 14:47:34 -05:00
|
|
|
ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
context: *mut uw::_Unwind_Context)
|
2014-01-04 01:34:15 -06:00
|
|
|
-> uw::_Unwind_Reason_Code;
|
2013-12-15 19:17:07 -06:00
|
|
|
}
|
2014-01-04 01:34:15 -06:00
|
|
|
|
2014-05-19 11:30:09 -05:00
|
|
|
#[lang="eh_personality"]
|
2014-04-10 12:48:38 -05:00
|
|
|
#[no_mangle] // referenced from rust_try.ll
|
|
|
|
extern "C" fn rust_eh_personality(
|
2014-05-19 11:30:09 -05:00
|
|
|
state: uw::_Unwind_State,
|
2014-06-25 14:47:34 -05:00
|
|
|
ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
context: *mut uw::_Unwind_Context
|
2014-05-19 11:30:09 -05:00
|
|
|
) -> uw::_Unwind_Reason_Code
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
__gcc_personality_v0(state, ue_header, context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 01:34:15 -06:00
|
|
|
#[no_mangle] // referenced from rust_try.ll
|
|
|
|
pub extern "C" fn rust_eh_personality_catch(
|
|
|
|
state: uw::_Unwind_State,
|
2014-04-10 12:48:38 -05:00
|
|
|
_ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
_context: *mut uw::_Unwind_Context
|
2014-01-04 01:34:15 -06:00
|
|
|
) -> uw::_Unwind_Reason_Code
|
|
|
|
{
|
|
|
|
if (state as c_int & uw::_US_ACTION_MASK as c_int)
|
|
|
|
== uw::_US_VIRTUAL_UNWIND_FRAME as c_int { // search phase
|
|
|
|
uw::_URC_HANDLER_FOUND // catch!
|
|
|
|
}
|
|
|
|
else { // cleanup phase
|
2014-04-10 12:48:38 -05:00
|
|
|
uw::_URC_INSTALL_CONTEXT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Win64 SEH (see http://msdn.microsoft.com/en-us/library/1eyas8tf.aspx)
|
|
|
|
//
|
|
|
|
// This looks a bit convoluted because rather than implementing a native SEH handler,
|
|
|
|
// GCC reuses the same personality routine as for the other architectures by wrapping it
|
|
|
|
// with an "API translator" layer (_GCC_specific_handler).
|
|
|
|
|
2014-09-29 01:38:24 -05:00
|
|
|
#[cfg(all(windows, target_arch = "x86_64", not(test)))]
|
2014-08-05 21:07:38 -05:00
|
|
|
#[doc(hidden)]
|
2014-08-30 00:24:32 -05:00
|
|
|
#[allow(non_camel_case_types, non_snake_case)]
|
2014-04-10 12:48:38 -05:00
|
|
|
pub mod eabi {
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::EXCEPTION_DISPOSITION::*;
|
2014-11-23 21:21:17 -06:00
|
|
|
use rt::libunwind as uw;
|
2014-04-10 12:48:38 -05:00
|
|
|
use libc::{c_void, c_int};
|
|
|
|
|
2014-08-21 23:51:16 -05:00
|
|
|
#[repr(C)]
|
2014-12-08 16:07:33 -06:00
|
|
|
#[allow(missing_copy_implementations)]
|
2014-09-19 14:30:07 -05:00
|
|
|
pub struct EXCEPTION_RECORD;
|
2014-08-21 23:51:16 -05:00
|
|
|
#[repr(C)]
|
2014-12-08 16:07:33 -06:00
|
|
|
#[allow(missing_copy_implementations)]
|
2014-09-19 14:30:07 -05:00
|
|
|
pub struct CONTEXT;
|
2014-08-21 23:51:16 -05:00
|
|
|
#[repr(C)]
|
2014-12-08 16:07:33 -06:00
|
|
|
#[allow(missing_copy_implementations)]
|
2014-09-19 14:30:07 -05:00
|
|
|
pub struct DISPATCHER_CONTEXT;
|
2014-04-10 12:48:38 -05:00
|
|
|
|
|
|
|
#[repr(C)]
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Copy)]
|
2014-09-19 14:30:07 -05:00
|
|
|
pub enum EXCEPTION_DISPOSITION {
|
2014-04-10 12:48:38 -05:00
|
|
|
ExceptionContinueExecution,
|
|
|
|
ExceptionContinueSearch,
|
|
|
|
ExceptionNestedException,
|
|
|
|
ExceptionCollidedUnwind
|
|
|
|
}
|
|
|
|
|
|
|
|
type _Unwind_Personality_Fn =
|
|
|
|
extern "C" fn(
|
|
|
|
version: c_int,
|
|
|
|
actions: uw::_Unwind_Action,
|
|
|
|
exception_class: uw::_Unwind_Exception_Class,
|
|
|
|
ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
context: *mut uw::_Unwind_Context
|
|
|
|
) -> uw::_Unwind_Reason_Code;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn __gcc_personality_seh0(
|
|
|
|
exceptionRecord: *mut EXCEPTION_RECORD,
|
|
|
|
establisherFrame: *mut c_void,
|
|
|
|
contextRecord: *mut CONTEXT,
|
|
|
|
dispatcherContext: *mut DISPATCHER_CONTEXT
|
|
|
|
) -> EXCEPTION_DISPOSITION;
|
|
|
|
|
|
|
|
fn _GCC_specific_handler(
|
|
|
|
exceptionRecord: *mut EXCEPTION_RECORD,
|
|
|
|
establisherFrame: *mut c_void,
|
|
|
|
contextRecord: *mut CONTEXT,
|
|
|
|
dispatcherContext: *mut DISPATCHER_CONTEXT,
|
|
|
|
personality: _Unwind_Personality_Fn
|
|
|
|
) -> EXCEPTION_DISPOSITION;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[lang="eh_personality"]
|
|
|
|
#[no_mangle] // referenced from rust_try.ll
|
|
|
|
extern "C" fn rust_eh_personality(
|
|
|
|
exceptionRecord: *mut EXCEPTION_RECORD,
|
|
|
|
establisherFrame: *mut c_void,
|
|
|
|
contextRecord: *mut CONTEXT,
|
|
|
|
dispatcherContext: *mut DISPATCHER_CONTEXT
|
|
|
|
) -> EXCEPTION_DISPOSITION
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
__gcc_personality_seh0(exceptionRecord, establisherFrame,
|
|
|
|
contextRecord, dispatcherContext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle] // referenced from rust_try.ll
|
|
|
|
pub extern "C" fn rust_eh_personality_catch(
|
|
|
|
exceptionRecord: *mut EXCEPTION_RECORD,
|
|
|
|
establisherFrame: *mut c_void,
|
|
|
|
contextRecord: *mut CONTEXT,
|
|
|
|
dispatcherContext: *mut DISPATCHER_CONTEXT
|
|
|
|
) -> EXCEPTION_DISPOSITION
|
|
|
|
{
|
|
|
|
extern "C" fn inner(
|
|
|
|
_version: c_int,
|
|
|
|
actions: uw::_Unwind_Action,
|
|
|
|
_exception_class: uw::_Unwind_Exception_Class,
|
|
|
|
_ue_header: *mut uw::_Unwind_Exception,
|
|
|
|
_context: *mut uw::_Unwind_Context
|
|
|
|
) -> uw::_Unwind_Reason_Code
|
|
|
|
{
|
|
|
|
if (actions as c_int & uw::_UA_SEARCH_PHASE as c_int) != 0 { // search phase
|
|
|
|
uw::_URC_HANDLER_FOUND // catch!
|
|
|
|
}
|
|
|
|
else { // cleanup phase
|
|
|
|
uw::_URC_INSTALL_CONTEXT
|
2014-01-04 01:34:15 -06:00
|
|
|
}
|
2013-12-15 19:17:07 -06:00
|
|
|
}
|
2014-04-10 12:48:38 -05:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
_GCC_specific_handler(exceptionRecord, establisherFrame,
|
|
|
|
contextRecord, dispatcherContext,
|
|
|
|
inner)
|
|
|
|
}
|
2013-12-15 19:17:07 -06:00
|
|
|
}
|
|
|
|
}
|
2013-12-12 20:01:59 -06:00
|
|
|
|
2014-09-28 21:28:48 -05:00
|
|
|
#[cfg(not(test))]
|
2014-12-27 15:57:43 -06:00
|
|
|
/// Entry point of panic from the libcore crate.
|
|
|
|
#[lang = "panic_fmt"]
|
|
|
|
pub extern fn rust_begin_unwind(msg: fmt::Arguments,
|
|
|
|
file: &'static str, line: uint) -> ! {
|
|
|
|
begin_unwind_fmt(msg, &(file, line))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The entry point for unwinding with a formatted message.
|
|
|
|
///
|
|
|
|
/// This is designed to reduce the amount of code required at the call
|
|
|
|
/// site as much as possible (so that `panic!()` has as low an impact
|
|
|
|
/// on (e.g.) the inlining of other functions as possible), by moving
|
|
|
|
/// the actual formatting into this shared place.
|
|
|
|
#[inline(never)] #[cold]
|
|
|
|
pub fn begin_unwind_fmt(msg: fmt::Arguments, file_line: &(&'static str, uint)) -> ! {
|
2014-12-12 12:59:41 -06:00
|
|
|
use fmt::Writer;
|
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-01-27 19:19:17 -06:00
|
|
|
// We do two allocations here, unfortunately. But (a) they're
|
|
|
|
// required with the current scheme, and (b) we don't handle
|
2014-10-09 14:17:22 -05:00
|
|
|
// panic + OOM properly anyway (see comment in begin_unwind
|
2014-01-27 19:19:17 -06:00
|
|
|
// below).
|
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-12-12 12:59:41 -06:00
|
|
|
let mut s = String::new();
|
|
|
|
let _ = write!(&mut s, "{}", msg);
|
|
|
|
begin_unwind_inner(box s, file_line)
|
2014-01-27 05:37:55 -06:00
|
|
|
}
|
|
|
|
|
2014-10-09 14:17:22 -05:00
|
|
|
/// This is the entry point of unwinding for panic!() and assert!().
|
2014-07-28 23:26:21 -05:00
|
|
|
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
|
|
|
|
pub fn begin_unwind<M: Any + Send>(msg: M, file_line: &(&'static str, uint)) -> ! {
|
|
|
|
// Note that this should be the only allocation performed in this code path.
|
2014-10-09 14:17:22 -05:00
|
|
|
// Currently this means that panic!() on OOM will invoke this code path,
|
|
|
|
// but then again we're not really ready for panic on OOM anyway. If
|
2014-07-28 23:26:21 -05:00
|
|
|
// we do start doing this, then we should propagate this allocation to
|
2014-12-26 15:04:27 -06:00
|
|
|
// be performed in the parent of this thread instead of the thread that's
|
2014-10-09 14:17:22 -05:00
|
|
|
// panicking.
|
2014-07-28 23:26:21 -05:00
|
|
|
|
|
|
|
// see below for why we do the `Any` coercion here.
|
|
|
|
begin_unwind_inner(box msg, file_line)
|
|
|
|
}
|
|
|
|
|
2014-01-27 01:03:37 -06:00
|
|
|
/// The core of the unwinding.
|
|
|
|
///
|
|
|
|
/// This is non-generic to avoid instantiation bloat in other crates
|
2014-06-08 23:00:52 -05:00
|
|
|
/// (which makes compilation of small crates noticeably slower). (Note:
|
2014-01-27 01:03:37 -06:00
|
|
|
/// we need the `Any` object anyway, we're not just creating it to
|
|
|
|
/// avoid being generic.)
|
|
|
|
///
|
2014-12-07 02:32:50 -06:00
|
|
|
/// Doing this split took the LLVM IR line counts of `fn main() { panic!()
|
2014-01-27 01:03:37 -06:00
|
|
|
/// }` from ~1900/3700 (-O/no opts) to 180/590.
|
|
|
|
#[inline(never)] #[cold] // this is the slow path, please never inline this
|
2014-12-31 12:20:31 -06:00
|
|
|
fn begin_unwind_inner(msg: Box<Any + Send>, file_line: &(&'static str, uint)) -> ! {
|
2014-12-10 09:37:56 -06:00
|
|
|
// Make sure the default failure handler is registered before we look at the
|
|
|
|
// callbacks.
|
2014-12-31 12:20:31 -06:00
|
|
|
static INIT: Once = ONCE_INIT;
|
2014-12-29 17:03:01 -06:00
|
|
|
INIT.call_once(|| unsafe { register(failure::on_fail); });
|
2014-12-10 09:37:56 -06:00
|
|
|
|
2014-12-26 15:04:27 -06:00
|
|
|
// First, invoke call the user-defined callbacks triggered on thread panic.
|
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
|
|
|
//
|
|
|
|
// By the time that we see a callback has been registered (by reading
|
2014-06-08 23:00:52 -05:00
|
|
|
// MAX_CALLBACKS), the actual callback itself may have not been stored yet,
|
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
|
|
|
// so we just chalk it up to a race condition and move on to the next
|
|
|
|
// callback. Additionally, CALLBACK_CNT may briefly be higher than
|
|
|
|
// MAX_CALLBACKS, so we're sure to clamp it as necessary.
|
2014-10-10 23:59:10 -05:00
|
|
|
let callbacks = {
|
2015-01-02 01:53:35 -06:00
|
|
|
let amt = CALLBACK_CNT.load(Ordering::SeqCst);
|
2014-09-24 06:41:09 -05:00
|
|
|
CALLBACKS[..cmp::min(amt, MAX_CALLBACKS)]
|
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
|
|
|
};
|
|
|
|
for cb in callbacks.iter() {
|
2015-01-02 01:53:35 -06:00
|
|
|
match cb.load(Ordering::SeqCst) {
|
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
|
|
|
0 => {}
|
|
|
|
n => {
|
|
|
|
let f: Callback = unsafe { mem::transmute(n) };
|
2014-07-24 23:33:46 -05:00
|
|
|
let (file, line) = *file_line;
|
2014-08-28 22:21:28 -05:00
|
|
|
f(&*msg, file, line);
|
2014-01-06 12:26:11 -06:00
|
|
|
}
|
2013-12-12 20:01:59 -06: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
|
|
|
};
|
|
|
|
|
|
|
|
// Now that we've run all the necessary unwind callbacks, we actually
|
2014-12-07 02:32:50 -06:00
|
|
|
// perform the unwinding.
|
2014-12-17 16:59:20 -06:00
|
|
|
if panicking() {
|
2014-12-07 02:32:50 -06:00
|
|
|
// If a thread panics while it's already unwinding then we
|
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
|
|
|
// have limited options. Currently our preference is to
|
|
|
|
// just abort. In the future we may consider resuming
|
2014-12-26 15:04:27 -06:00
|
|
|
// unwinding or otherwise exiting the thread cleanly.
|
2014-12-17 16:59:20 -06:00
|
|
|
rterrln!("thread panicked while panicking. aborting.");
|
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
|
|
|
unsafe { intrinsics::abort() }
|
2014-06-04 12:54:35 -05:00
|
|
|
}
|
2014-12-17 16:59:20 -06:00
|
|
|
PANICKING.with(|s| s.set(true));
|
2014-10-29 18:28:34 -05:00
|
|
|
rust_panic(msg);
|
2014-06-04 12:54:35 -05:00
|
|
|
}
|
2014-02-05 17:19:40 -06:00
|
|
|
|
2014-12-26 15:04:27 -06:00
|
|
|
/// Register a callback to be invoked when a thread unwinds.
|
2014-06-04 12:54:35 -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
|
|
|
/// This is an unsafe and experimental API which allows for an arbitrary
|
2014-12-26 15:04:27 -06:00
|
|
|
/// callback to be invoked when a thread panics. This callback is invoked on both
|
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
|
|
|
/// the initial unwinding and a double unwinding if one occurs. Additionally,
|
|
|
|
/// the local `Task` will be in place for the duration of the callback, and
|
|
|
|
/// the callback must ensure that it remains in place once the callback returns.
|
2014-06-04 12:54:35 -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
|
|
|
/// Only a limited number of callbacks can be registered, and this function
|
|
|
|
/// returns whether the callback was successfully registered or not. It is not
|
|
|
|
/// currently possible to unregister a callback once it has been registered.
|
|
|
|
#[experimental]
|
|
|
|
pub unsafe fn register(f: Callback) -> bool {
|
2015-01-02 01:53:35 -06:00
|
|
|
match CALLBACK_CNT.fetch_add(1, Ordering::SeqCst) {
|
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
|
|
|
// The invocation code has knowledge of this window where the count has
|
|
|
|
// been incremented, but the callback has not been stored. We're
|
|
|
|
// guaranteed that the slot we're storing into is 0.
|
|
|
|
n if n < MAX_CALLBACKS => {
|
2015-01-02 01:53:35 -06:00
|
|
|
let prev = CALLBACKS[n].swap(mem::transmute(f), Ordering::SeqCst);
|
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
|
|
|
rtassert!(prev == 0);
|
|
|
|
true
|
2014-06-04 12:54:35 -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
|
|
|
// If we accidentally bumped the count too high, pull it back.
|
|
|
|
_ => {
|
2015-01-02 01:53:35 -06:00
|
|
|
CALLBACK_CNT.store(MAX_CALLBACKS, Ordering::SeqCst);
|
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
|
|
|
false
|
2014-06-04 12:54:35 -05:00
|
|
|
}
|
2013-12-12 20:01:59 -06:00
|
|
|
}
|
|
|
|
}
|