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:
|
2015-07-13 20:11:44 -05:00
|
|
|
//! http://mentorembedded.github.io/cxx-abi/abi-eh.html
|
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
|
|
|
//! 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.
|
|
|
|
//!
|
2015-10-21 11:59:24 -05:00
|
|
|
//! In the cleanup phase, the unwinder invokes each personality routine again.
|
|
|
|
//! This time it decides which (if any) cleanup code needs to be run for
|
|
|
|
//! the current stack frame. If so, the control is transferred to a special branch
|
|
|
|
//! in the function body, the "landing pad", which invokes destructors, frees memory,
|
|
|
|
//! etc. At the end of the landing pad, control is transferred back to the unwinder
|
|
|
|
//! and unwinding resumes.
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
//!
|
2015-10-21 11:59:24 -05:00
|
|
|
//! Once stack has been unwound down to the handler frame level, unwinding stops
|
|
|
|
//! and the last personality routine transfers control to the 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
|
|
|
//!
|
2015-10-21 11:59:24 -05:00
|
|
|
//! ## `eh_personality` and `eh_unwind_resume`
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
//!
|
2015-10-21 11:59:24 -05:00
|
|
|
//! These language items are used by the compiler when generating unwind info.
|
|
|
|
//! The first one is the personality routine described above. The second one
|
|
|
|
//! allows compilation target to customize the process of resuming unwind at the
|
|
|
|
//! end of the landing pads. `eh_unwind_resume` is used only if `custom_unwind_resume`
|
|
|
|
//! flag in the target options is set.
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
//!
|
2015-10-21 11:59:24 -05:00
|
|
|
//! ## Frame unwind info registration
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
//!
|
2015-10-21 11:59:24 -05:00
|
|
|
//! Each module's image contains a frame unwind info section (usually ".eh_frame").
|
|
|
|
//! When a module is loaded/unloaded into the process, the unwinder must be informed
|
|
|
|
//! about the location of this section in memory. The methods of achieving that vary
|
|
|
|
//! by the platform.
|
|
|
|
//! On some (e.g. Linux), the unwinder can discover unwind info sections on its own
|
|
|
|
//! (by dynamically enumerating currently loaded modules via the dl_iterate_phdr() API
|
|
|
|
//! and finding their ".eh_frame" sections);
|
|
|
|
//! Others, like Windows, require modules to actively register their unwind info
|
|
|
|
//! sections via unwinder API (see `rust_eh_register_frames`/`rust_eh_unregister_frames`).
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
|
2015-05-11 23:09:07 -05:00
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_imports)]
|
|
|
|
|
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;
|
2015-02-22 17:58:22 -06:00
|
|
|
use boxed;
|
2014-11-23 21:21:17 -06:00
|
|
|
use cmp;
|
2015-09-24 16:49:38 -05:00
|
|
|
use panicking::{self,PANIC_COUNT};
|
2014-11-23 21:21:17 -06:00
|
|
|
use fmt;
|
|
|
|
use intrinsics;
|
|
|
|
use mem;
|
2014-11-25 15:28:35 -06:00
|
|
|
use sync::atomic::{self, Ordering};
|
2015-05-27 03:18:36 -05:00
|
|
|
use sys_common::mutex::Mutex;
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
|
2015-05-11 23:09:07 -05:00
|
|
|
// The actual unwinding implementation is cfg'd here, and we've got two current
|
|
|
|
// implementations. One goes through SEH on Windows and the other goes through
|
|
|
|
// libgcc via the libunwind-like API.
|
2015-07-13 20:11:44 -05:00
|
|
|
|
2015-08-11 13:48:43 -05:00
|
|
|
// i686-pc-windows-msvc
|
|
|
|
#[cfg(all(windows, target_arch = "x86", target_env = "msvc"))]
|
2015-07-13 20:11:44 -05:00
|
|
|
#[path = "seh.rs"] #[doc(hidden)]
|
|
|
|
pub mod imp;
|
|
|
|
|
2015-08-11 13:48:43 -05:00
|
|
|
// x86_64-pc-windows-*
|
2015-12-11 15:07:11 -06:00
|
|
|
#[cfg(all(windows, target_arch = "x86_64"))]
|
2015-07-13 20:11:44 -05:00
|
|
|
#[path = "seh64_gnu.rs"] #[doc(hidden)]
|
2015-05-11 23:09:07 -05:00
|
|
|
pub mod imp;
|
2015-07-13 20:11:44 -05:00
|
|
|
|
|
|
|
// i686-pc-windows-gnu and all others
|
2015-08-11 13:48:43 -05:00
|
|
|
#[cfg(any(unix, all(windows, target_arch = "x86", target_env = "gnu")))]
|
2015-07-13 20:11:44 -05:00
|
|
|
#[path = "gcc.rs"] #[doc(hidden)]
|
2015-05-11 23:09:07 -05:00
|
|
|
pub mod imp;
|
2014-06-04 12:54:35 -05: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
|
|
|
///
|
2015-02-04 01:19:54 -06:00
|
|
|
/// This function will return `Ok(())` if the closure did not panic, and will
|
|
|
|
/// return `Err(cause)` if the closure panics. The `cause` returned is the
|
2014-10-09 14:17:22 -05:00
|
|
|
/// 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);
|
2015-07-20 15:27:38 -05:00
|
|
|
return inner_try(try_fn::<F>, &mut f as *mut _ as *mut u8);
|
2014-12-17 16:59:20 -06:00
|
|
|
|
2015-05-12 17:52:33 -05:00
|
|
|
// If an inner function were not used here, then this generic function `try`
|
|
|
|
// uses the native symbol `rust_try`, for which the code is statically
|
|
|
|
// linked into the standard library. This means that the DLL for the
|
|
|
|
// standard library must have `rust_try` as an exposed symbol that
|
|
|
|
// downstream crates can link against (because monomorphizations of `try` in
|
|
|
|
// downstream crates will have a reference to the `rust_try` symbol).
|
|
|
|
//
|
|
|
|
// On MSVC this requires the symbol `rust_try` to be tagged with
|
|
|
|
// `dllexport`, but it's easier to not have conditional `src/rt/rust_try.ll`
|
|
|
|
// files and instead just have this non-generic shim the compiler can take
|
|
|
|
// care of exposing correctly.
|
2015-07-20 15:27:38 -05:00
|
|
|
unsafe fn inner_try(f: fn(*mut u8), data: *mut u8)
|
2015-05-12 17:52:33 -05:00
|
|
|
-> Result<(), Box<Any + Send>> {
|
2015-09-24 16:49:38 -05:00
|
|
|
PANIC_COUNT.with(|s| {
|
2015-09-22 01:58:47 -05:00
|
|
|
let prev = s.get();
|
2015-09-24 16:49:38 -05:00
|
|
|
s.set(0);
|
2015-09-22 01:58:47 -05:00
|
|
|
let ep = intrinsics::try(f, data);
|
|
|
|
s.set(prev);
|
|
|
|
if ep.is_null() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(imp::cleanup(ep))
|
|
|
|
}
|
|
|
|
})
|
2015-05-12 17:52:33 -05:00
|
|
|
}
|
2014-06-04 12:54:35 -05:00
|
|
|
|
2015-07-20 15:27:38 -05:00
|
|
|
fn try_fn<F: FnOnce()>(opt_closure: *mut u8) {
|
2014-12-10 09:49:45 -06:00
|
|
|
let opt_closure = opt_closure as *mut Option<F>;
|
|
|
|
unsafe { (*opt_closure).take().unwrap()(); }
|
2013-12-15 19:17:07 -06:00
|
|
|
}
|
|
|
|
|
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.
|
2015-07-20 15:27:38 -05:00
|
|
|
fn rust_try(f: extern fn(*mut u8),
|
|
|
|
data: *mut u8) -> *mut u8;
|
2014-06-04 12:54:35 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-15 19:17:07 -06:00
|
|
|
|
2015-01-17 15:44:04 -06:00
|
|
|
/// Determines whether the current thread is unwinding because of panic.
|
2014-12-17 16:59:20 -06:00
|
|
|
pub fn panicking() -> bool {
|
2015-09-24 16:49:38 -05:00
|
|
|
PANIC_COUNT.with(|s| s.get() != 0)
|
2014-12-17 16:59:20 -06:00
|
|
|
}
|
|
|
|
|
2014-06-04 12:54:35 -05:00
|
|
|
// An uninlined, unmangled function upon which to slap yer breakpoints
|
|
|
|
#[inline(never)]
|
|
|
|
#[no_mangle]
|
2015-01-31 08:16:56 -06:00
|
|
|
#[allow(private_no_mangle_fns)]
|
2015-12-25 13:00:40 -06:00
|
|
|
pub fn rust_panic(cause: Box<Any + Send + 'static>) -> ! {
|
2014-06-04 12:54:35 -05:00
|
|
|
unsafe {
|
2015-05-11 23:09:07 -05:00
|
|
|
imp::panic(cause)
|
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"]
|
2015-09-11 13:10:43 -05:00
|
|
|
#[unwind]
|
2014-12-27 15:57:43 -06:00
|
|
|
pub extern fn rust_begin_unwind(msg: fmt::Arguments,
|
2015-04-11 04:46:57 -05:00
|
|
|
file: &'static str, line: u32) -> ! {
|
2014-12-27 15:57:43 -06:00
|
|
|
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.
|
2015-11-16 10:54:28 -06:00
|
|
|
#[unstable(feature = "libstd_sys_internals",
|
|
|
|
reason = "used by the panic! macro",
|
|
|
|
issue = "0")]
|
2014-12-27 15:57:43 -06:00
|
|
|
#[inline(never)] #[cold]
|
2015-04-11 04:46:57 -05:00
|
|
|
pub fn begin_unwind_fmt(msg: fmt::Arguments, file_line: &(&'static str, u32)) -> ! {
|
2015-02-13 17:56:32 -06:00
|
|
|
use fmt::Write;
|
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();
|
2015-04-16 23:49:53 -05:00
|
|
|
let _ = s.write_fmt(msg);
|
2015-02-15 02:52:21 -06:00
|
|
|
begin_unwind_inner(Box::new(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!().
|
2015-11-16 10:54:28 -06:00
|
|
|
#[unstable(feature = "libstd_sys_internals",
|
|
|
|
reason = "used by the panic! macro",
|
|
|
|
issue = "0")]
|
2014-07-28 23:26:21 -05:00
|
|
|
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
|
2015-04-11 04:46:57 -05:00
|
|
|
pub fn begin_unwind<M: Any + Send>(msg: M, file_line: &(&'static str, u32)) -> ! {
|
|
|
|
// Note that this should be the only allocation performed in this code path.
|
|
|
|
// 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
|
|
|
|
// we do start doing this, then we should propagate this allocation to
|
|
|
|
// be performed in the parent of this thread instead of the thread that's
|
|
|
|
// panicking.
|
|
|
|
|
2014-07-28 23:26:21 -05:00
|
|
|
// see below for why we do the `Any` coercion here.
|
2015-02-15 02:52:21 -06:00
|
|
|
begin_unwind_inner(Box::new(msg), file_line)
|
2014-07-28 23:26:21 -05:00
|
|
|
}
|
|
|
|
|
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
|
2015-01-01 12:19:42 -06:00
|
|
|
fn begin_unwind_inner(msg: Box<Any + Send>,
|
2015-04-11 04:46:57 -05:00
|
|
|
file_line: &(&'static str, u32)) -> ! {
|
2015-09-22 01:40:15 -05:00
|
|
|
let (file, line) = *file_line;
|
2014-12-10 09:37:56 -06:00
|
|
|
|
2015-09-22 01:40:15 -05:00
|
|
|
// First, invoke the default panic handler.
|
|
|
|
panicking::on_panic(&*msg, file, line);
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
|
2015-09-22 11:51:04 -05:00
|
|
|
// Finally, perform the unwinding.
|
|
|
|
rust_panic(msg);
|
2013-12-12 20:01:59 -06:00
|
|
|
}
|