2014-01-15 11:34:05 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-04-21 18:28:17 -05:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Language-level runtime services that should reasonably expected
|
|
|
|
//! to be available 'everywhere'. Local heaps, GC, unwinding,
|
|
|
|
//! local storage, and logging. Even a 'freestanding' Rust would likely want
|
|
|
|
//! to implement this.
|
|
|
|
|
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
|
|
|
use core::prelude::*;
|
|
|
|
|
2014-05-19 19:33:40 -05:00
|
|
|
use alloc::arc::Arc;
|
2014-07-10 16:19:17 -05:00
|
|
|
use alloc::boxed::{BoxAny, Box};
|
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
|
|
|
use core::any::Any;
|
2014-06-04 02:01:40 -05:00
|
|
|
use core::atomics::{AtomicUint, 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
|
|
|
use core::iter::Take;
|
2014-06-13 18:03:41 -05:00
|
|
|
use core::kinds::marker;
|
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
|
|
|
use core::mem;
|
2014-06-04 02:01:40 -05:00
|
|
|
use core::raw;
|
2014-05-19 19:33:40 -05:00
|
|
|
|
2013-10-11 16:20:34 -05:00
|
|
|
use local_data;
|
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
|
|
|
use Runtime;
|
|
|
|
use local::Local;
|
|
|
|
use local_heap::LocalHeap;
|
|
|
|
use rtio::LocalIo;
|
2014-06-13 18:03:41 -05:00
|
|
|
use 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
|
|
|
use unwind::Unwinder;
|
|
|
|
use collections::str::SendStr;
|
2013-12-12 20:01:59 -06:00
|
|
|
|
2014-06-13 18:03:41 -05:00
|
|
|
/// State associated with Rust tasks.
|
|
|
|
///
|
|
|
|
/// Rust tasks are primarily built with two separate components. One is this
|
|
|
|
/// structure which handles standard services such as TLD, unwinding support,
|
|
|
|
/// naming of a task, etc. The second component is the runtime of this task, a
|
|
|
|
/// `Runtime` trait object.
|
|
|
|
///
|
|
|
|
/// The `Runtime` object instructs this task how it can perform critical
|
|
|
|
/// operations such as blocking, rescheduling, I/O constructors, etc. The two
|
|
|
|
/// halves are separately owned, but one is often found contained in the other.
|
|
|
|
/// A task's runtime can be reflected upon with the `maybe_take_runtime` method,
|
|
|
|
/// and otherwise its ownership is managed with `take_runtime` and
|
|
|
|
/// `put_runtime`.
|
|
|
|
///
|
|
|
|
/// In general, this structure should not be used. This is meant to be an
|
|
|
|
/// unstable internal detail of the runtime itself. From time-to-time, however,
|
|
|
|
/// it is useful to manage tasks directly. An example of this would be
|
|
|
|
/// interoperating with the Rust runtime from FFI callbacks or such. For this
|
|
|
|
/// reason, there are two methods of note with the `Task` structure.
|
|
|
|
///
|
|
|
|
/// * `run` - This function will execute a closure inside the context of a task.
|
|
|
|
/// Failure is caught and handled via the task's on_exit callback. If
|
|
|
|
/// this fails, the task is still returned, but it can no longer be
|
|
|
|
/// used, it is poisoned.
|
|
|
|
///
|
|
|
|
/// * `destroy` - This is a required function to call to destroy a task. If a
|
|
|
|
/// task falls out of scope without calling `destroy`, its
|
|
|
|
/// destructor bomb will go off, aborting the process.
|
|
|
|
///
|
|
|
|
/// With these two methods, tasks can be re-used to execute code inside of its
|
|
|
|
/// context while having a point in the future where destruction is allowed.
|
|
|
|
/// More information can be found on these specific methods.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// extern crate native;
|
|
|
|
/// use std::uint;
|
|
|
|
/// # fn main() {
|
|
|
|
///
|
|
|
|
/// // Create a task using a native runtime
|
|
|
|
/// let task = native::task::new((0, uint::MAX));
|
|
|
|
///
|
|
|
|
/// // Run some code, catching any possible failures
|
|
|
|
/// let task = task.run(|| {
|
|
|
|
/// // Run some code inside this task
|
|
|
|
/// println!("Hello with a native runtime!");
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// // Run some code again, catching the failure
|
|
|
|
/// let task = task.run(|| {
|
|
|
|
/// fail!("oh no, what to do!");
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// // Now that the task is failed, it can never be used again
|
|
|
|
/// assert!(task.is_destroyed());
|
|
|
|
///
|
|
|
|
/// // Deallocate the resources associated with this task
|
|
|
|
/// task.destroy();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2013-05-19 03:04:01 -05:00
|
|
|
pub struct Task {
|
2014-03-27 17:09:47 -05:00
|
|
|
pub heap: LocalHeap,
|
|
|
|
pub gc: GarbageCollector,
|
|
|
|
pub storage: LocalStorage,
|
|
|
|
pub unwinder: Unwinder,
|
|
|
|
pub death: Death,
|
|
|
|
pub name: Option<SendStr>,
|
2013-12-12 20:01:59 -06:00
|
|
|
|
2014-07-24 00:48:04 -05:00
|
|
|
state: TaskState,
|
2014-06-14 13:03:34 -05:00
|
|
|
imp: Option<Box<Runtime + Send>>,
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
|
2014-07-24 00:48:04 -05:00
|
|
|
// Once a task has entered the `Armed` state it must be destroyed via `drop`,
|
|
|
|
// and no other method. This state is used to track this transition.
|
|
|
|
#[deriving(PartialEq)]
|
|
|
|
enum TaskState {
|
|
|
|
New,
|
|
|
|
Armed,
|
|
|
|
Destroyed,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
pub struct TaskOpts {
|
|
|
|
/// Invoke this procedure with the result of the task when it finishes.
|
2014-06-14 13:03:34 -05:00
|
|
|
pub on_exit: Option<proc(Result): Send>,
|
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
|
|
|
/// A name for the task-to-be, for identification in failure messages
|
|
|
|
pub name: Option<SendStr>,
|
|
|
|
/// The size of the stack for the spawned task
|
|
|
|
pub stack_size: Option<uint>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates the manner in which a task exited.
|
|
|
|
///
|
|
|
|
/// A task that completes without failing is considered to exit successfully.
|
|
|
|
///
|
|
|
|
/// If you wish for this result's delivery to block until all
|
|
|
|
/// children tasks complete, recommend using a result future.
|
2014-06-14 13:03:34 -05:00
|
|
|
pub type Result = ::core::result::Result<(), Box<Any + Send>>;
|
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
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
pub struct GarbageCollector;
|
2014-03-31 21:01:01 -05:00
|
|
|
pub struct LocalStorage(pub Option<local_data::Map>);
|
2013-06-26 18:41:00 -05:00
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
/// A handle to a blocked task. Usually this means having the Box<Task>
|
|
|
|
/// pointer by ownership, but if the task is killable, a killer can steal it
|
|
|
|
/// at any time.
|
2013-12-12 20:01:59 -06:00
|
|
|
pub enum BlockedTask {
|
2014-05-05 20:56:44 -05:00
|
|
|
Owned(Box<Task>),
|
2014-05-19 19:33:40 -05:00
|
|
|
Shared(Arc<AtomicUint>),
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
/// Per-task state related to task death, killing, failure, etc.
|
|
|
|
pub struct Death {
|
2014-06-13 18:03:41 -05:00
|
|
|
pub on_exit: Option<proc(Result):Send>,
|
|
|
|
marker: marker::NoCopy,
|
2013-04-21 18:28:17 -05:00
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
pub struct BlockedTasks {
|
2014-05-19 19:33:40 -05:00
|
|
|
inner: Arc<AtomicUint>,
|
2013-12-12 20:01:59 -06:00
|
|
|
}
|
2013-04-22 19:15:31 -05:00
|
|
|
|
2013-05-19 03:04:01 -05:00
|
|
|
impl Task {
|
2014-06-13 18:03:41 -05:00
|
|
|
/// Creates a new uninitialized task.
|
|
|
|
///
|
|
|
|
/// This method cannot be used to immediately invoke `run` because the task
|
|
|
|
/// itself will likely require a runtime to be inserted via `put_runtime`.
|
|
|
|
///
|
|
|
|
/// Note that you likely don't want to call this function, but rather the
|
|
|
|
/// task creation functions through libnative or libgreen.
|
2013-12-12 20:01:59 -06:00
|
|
|
pub fn new() -> Task {
|
2013-05-19 03:04:01 -05:00
|
|
|
Task {
|
2013-04-21 21:03:52 -05:00
|
|
|
heap: LocalHeap::new(),
|
2013-04-21 18:28:17 -05:00
|
|
|
gc: GarbageCollector,
|
2013-08-10 22:06:39 -05:00
|
|
|
storage: LocalStorage(None),
|
2013-12-12 20:01:59 -06:00
|
|
|
unwinder: Unwinder::new(),
|
2013-07-01 22:24:24 -05:00
|
|
|
death: Death::new(),
|
2014-07-24 00:48:04 -05:00
|
|
|
state: New,
|
2013-07-30 18:20:59 -05:00
|
|
|
name: None,
|
2013-12-12 20:01:59 -06:00
|
|
|
imp: None,
|
2013-04-21 18:28:17 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-22 14:54:03 -05:00
|
|
|
|
2014-06-13 18:03:41 -05:00
|
|
|
/// Consumes ownership of a task, runs some code, and returns the task back.
|
2013-12-12 20:01:59 -06:00
|
|
|
///
|
2014-06-13 18:03:41 -05:00
|
|
|
/// This function can be used as an emulated "try/catch" to interoperate
|
|
|
|
/// with the rust runtime at the outermost boundary. It is not possible to
|
|
|
|
/// use this function in a nested fashion (a try/catch inside of another
|
2014-07-02 20:27:07 -05:00
|
|
|
/// try/catch). Invoking this function is quite cheap.
|
2014-06-13 18:03:41 -05:00
|
|
|
///
|
|
|
|
/// If the closure `f` succeeds, then the returned task can be used again
|
|
|
|
/// for another invocation of `run`. If the closure `f` fails then `self`
|
|
|
|
/// will be internally destroyed along with all of the other associated
|
|
|
|
/// resources of this task. The `on_exit` callback is invoked with the
|
|
|
|
/// cause of failure (not returned here). This can be discovered by querying
|
|
|
|
/// `is_destroyed()`.
|
|
|
|
///
|
|
|
|
/// Note that it is possible to view partial execution of the closure `f`
|
|
|
|
/// because it is not guaranteed to run to completion, but this function is
|
|
|
|
/// guaranteed to return if it fails. Care should be taken to ensure that
|
|
|
|
/// stack references made by `f` are handled appropriately.
|
|
|
|
///
|
|
|
|
/// It is invalid to call this function with a task that has been previously
|
|
|
|
/// destroyed via a failed call to `run`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// extern crate native;
|
|
|
|
/// use std::uint;
|
|
|
|
/// # fn main() {
|
|
|
|
///
|
|
|
|
/// // Create a new native task
|
|
|
|
/// let task = native::task::new((0, uint::MAX));
|
|
|
|
///
|
|
|
|
/// // Run some code once and then destroy this task
|
|
|
|
/// task.run(|| {
|
|
|
|
/// println!("Hello with a native runtime!");
|
|
|
|
/// }).destroy();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2014-07-24 00:48:04 -05:00
|
|
|
pub fn run(mut self: Box<Task>, f: ||) -> Box<Task> {
|
2014-06-13 18:03:41 -05:00
|
|
|
assert!(!self.is_destroyed(), "cannot re-use a destroyed task");
|
|
|
|
|
|
|
|
// First, make sure that no one else is in TLS. This does not allow
|
|
|
|
// recursive invocations of run(). If there's no one else, then
|
|
|
|
// relinquish ownership of ourselves back into TLS.
|
|
|
|
if Local::exists(None::<Task>) {
|
|
|
|
fail!("cannot run a task recursively inside another");
|
|
|
|
}
|
2014-07-24 00:48:04 -05:00
|
|
|
self.state = Armed;
|
2013-12-12 20:01:59 -06:00
|
|
|
Local::put(self);
|
2013-04-22 19:15:31 -05:00
|
|
|
|
2014-06-13 18:03:41 -05:00
|
|
|
// There are two primary reasons that general try/catch is unsafe. The
|
|
|
|
// first is that we do not support nested try/catch. The above check for
|
|
|
|
// an existing task in TLS is sufficient for this invariant to be
|
|
|
|
// upheld. The second is that unwinding while unwinding is not defined.
|
|
|
|
// We take care of that by having an 'unwinding' flag in the task
|
|
|
|
// itself. For these reasons, this unsafety should be ok.
|
|
|
|
let result = unsafe { unwind::try(f) };
|
|
|
|
|
|
|
|
// After running the closure given return the task back out if it ran
|
|
|
|
// successfully, or clean up the task if it failed.
|
|
|
|
let task: Box<Task> = Local::take();
|
|
|
|
match result {
|
|
|
|
Ok(()) => task,
|
|
|
|
Err(cause) => { task.cleanup(Err(cause)) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroy all associated resources of this task.
|
|
|
|
///
|
|
|
|
/// This function will perform any necessary clean up to prepare the task
|
|
|
|
/// for destruction. It is required that this is called before a `Task`
|
|
|
|
/// falls out of scope.
|
|
|
|
///
|
|
|
|
/// The returned task cannot be used for running any more code, but it may
|
|
|
|
/// be used to extract the runtime as necessary.
|
2014-07-23 12:21:50 -05:00
|
|
|
pub fn destroy(self: Box<Task>) -> Box<Task> {
|
2014-06-13 18:03:41 -05:00
|
|
|
if self.is_destroyed() {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
self.cleanup(Ok(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cleans up a task, processing the result of the task as appropriate.
|
|
|
|
///
|
|
|
|
/// This function consumes ownership of the task, deallocating it once it's
|
|
|
|
/// done being processed. It is assumed that TLD and the local heap have
|
|
|
|
/// already been destroyed and/or annihilated.
|
2014-07-23 12:21:50 -05:00
|
|
|
fn cleanup(self: Box<Task>, result: Result) -> Box<Task> {
|
2014-06-13 18:03:41 -05:00
|
|
|
// The first thing to do when cleaning up is to deallocate our local
|
|
|
|
// resources, such as TLD and GC data.
|
|
|
|
//
|
|
|
|
// FIXME: there are a number of problems with this code
|
|
|
|
//
|
|
|
|
// 1. If any TLD object fails destruction, then all of TLD will leak.
|
|
|
|
// This appears to be a consequence of #14875.
|
|
|
|
//
|
|
|
|
// 2. Failing during GC annihilation aborts the runtime #14876.
|
|
|
|
//
|
|
|
|
// 3. Setting a TLD key while destroying TLD or while destroying GC will
|
|
|
|
// abort the runtime #14807.
|
|
|
|
//
|
|
|
|
// 4. Invoking GC in GC destructors will abort the runtime #6996.
|
|
|
|
//
|
|
|
|
// 5. The order of destruction of TLD and GC matters, but either way is
|
|
|
|
// susceptible to leaks (see 3/4) #8302.
|
|
|
|
//
|
|
|
|
// That being said, there are a few upshots to this code
|
|
|
|
//
|
|
|
|
// 1. If TLD destruction fails, heap destruction will be attempted.
|
|
|
|
// There is a test for this at fail-during-tld-destroy.rs. Sadly the
|
|
|
|
// other way can't be tested due to point 2 above. Note that we must
|
2014-07-02 20:27:07 -05:00
|
|
|
// immortalize the heap first because if any deallocations are
|
2014-06-13 18:03:41 -05:00
|
|
|
// attempted while TLD is being dropped it will attempt to free the
|
|
|
|
// allocation from the wrong heap (because the current one has been
|
|
|
|
// replaced).
|
|
|
|
//
|
|
|
|
// 2. One failure in destruction is tolerable, so long as the task
|
|
|
|
// didn't originally fail while it was running.
|
|
|
|
//
|
|
|
|
// And with all that in mind, we attempt to clean things up!
|
|
|
|
let mut task = self.run(|| {
|
|
|
|
let mut task = Local::borrow(None::<Task>);
|
|
|
|
let tld = {
|
|
|
|
let &LocalStorage(ref mut optmap) = &mut task.storage;
|
|
|
|
optmap.take()
|
|
|
|
};
|
|
|
|
let mut heap = mem::replace(&mut task.heap, LocalHeap::new());
|
|
|
|
unsafe { heap.immortalize() }
|
|
|
|
drop(task);
|
|
|
|
|
|
|
|
// First, destroy task-local storage. This may run user dtors.
|
|
|
|
drop(tld);
|
|
|
|
|
|
|
|
// Destroy remaining boxes. Also may run user dtors.
|
|
|
|
drop(heap);
|
|
|
|
});
|
|
|
|
|
|
|
|
// If the above `run` block failed, then it must be the case that the
|
|
|
|
// task had previously succeeded. This also means that the code below
|
|
|
|
// was recursively run via the `run` method invoking this method. In
|
|
|
|
// this case, we just make sure the world is as we thought, and return.
|
|
|
|
if task.is_destroyed() {
|
|
|
|
rtassert!(result.is_ok())
|
|
|
|
return task
|
|
|
|
}
|
2013-12-12 20:01:59 -06:00
|
|
|
|
2014-06-13 18:03:41 -05:00
|
|
|
// After taking care of the data above, we need to transmit the result
|
|
|
|
// of this task.
|
|
|
|
let what_to_do = task.death.on_exit.take();
|
|
|
|
Local::put(task);
|
2013-06-22 03:09:06 -05:00
|
|
|
|
2014-06-13 18:03:41 -05:00
|
|
|
// FIXME: this is running in a seriously constrained context. If this
|
|
|
|
// allocates GC or allocates TLD then it will likely abort the
|
|
|
|
// runtime. Similarly, if this fails, this will also likely abort
|
|
|
|
// the runtime.
|
|
|
|
//
|
|
|
|
// This closure is currently limited to a channel send via the
|
|
|
|
// standard library's task interface, but this needs
|
|
|
|
// reconsideration to whether it's a reasonable thing to let a
|
|
|
|
// task to do or not.
|
|
|
|
match what_to_do {
|
|
|
|
Some(f) => { f(result) }
|
|
|
|
None => { drop(result) }
|
2013-12-12 23:38:57 -06:00
|
|
|
}
|
2014-06-13 18:03:41 -05:00
|
|
|
|
|
|
|
// Now that we're done, we remove the task from TLS and flag it for
|
|
|
|
// destruction.
|
|
|
|
let mut task: Box<Task> = Local::take();
|
2014-07-24 00:48:04 -05:00
|
|
|
task.state = Destroyed;
|
2014-06-13 18:03:41 -05:00
|
|
|
return task;
|
2013-04-22 14:54:03 -05:00
|
|
|
}
|
2013-06-26 18:41:00 -05:00
|
|
|
|
2014-06-13 18:03:41 -05:00
|
|
|
/// Queries whether this can be destroyed or not.
|
2014-07-24 00:48:04 -05:00
|
|
|
pub fn is_destroyed(&self) -> bool { self.state == Destroyed }
|
2014-06-13 18:03:41 -05:00
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
/// Inserts a runtime object into this task, transferring ownership to the
|
|
|
|
/// task. It is illegal to replace a previous runtime object in this task
|
|
|
|
/// with this argument.
|
2014-06-14 13:03:34 -05:00
|
|
|
pub fn put_runtime(&mut self, ops: Box<Runtime + Send>) {
|
2013-12-12 20:01:59 -06:00
|
|
|
assert!(self.imp.is_none());
|
|
|
|
self.imp = Some(ops);
|
|
|
|
}
|
2013-06-26 18:41:00 -05:00
|
|
|
|
2014-06-13 18:03:41 -05:00
|
|
|
/// Removes the runtime from this task, transferring ownership to the
|
|
|
|
/// caller.
|
|
|
|
pub fn take_runtime(&mut self) -> Box<Runtime + Send> {
|
|
|
|
assert!(self.imp.is_some());
|
|
|
|
self.imp.take().unwrap()
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
/// Attempts to extract the runtime as a specific type. If the runtime does
|
|
|
|
/// not have the provided type, then the runtime is not removed. If the
|
|
|
|
/// runtime does have the specified type, then it is removed and returned
|
|
|
|
/// (transfer of ownership).
|
|
|
|
///
|
|
|
|
/// It is recommended to only use this method when *absolutely necessary*.
|
|
|
|
/// This function may not be available in the future.
|
2014-05-05 20:56:44 -05:00
|
|
|
pub fn maybe_take_runtime<T: 'static>(&mut self) -> Option<Box<T>> {
|
2013-12-12 20:01:59 -06:00
|
|
|
// This is a terrible, terrible function. The general idea here is to
|
2014-05-05 20:56:44 -05:00
|
|
|
// take the runtime, cast it to Box<Any>, check if it has the right
|
|
|
|
// type, and then re-cast it back if necessary. The method of doing
|
|
|
|
// this is pretty sketchy and involves shuffling vtables of trait
|
|
|
|
// objects around, but it gets the job done.
|
2013-12-12 20:01:59 -06:00
|
|
|
//
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME: This function is a serious code smell and should be avoided at
|
2013-12-12 20:01:59 -06:00
|
|
|
// all costs. I have yet to think of a method to avoid this
|
|
|
|
// function, and I would be saddened if more usage of the function
|
|
|
|
// crops up.
|
|
|
|
unsafe {
|
|
|
|
let imp = self.imp.take_unwrap();
|
2014-06-04 02:01:40 -05:00
|
|
|
let vtable = mem::transmute::<_, &raw::TraitObject>(&imp).vtable;
|
2014-07-10 16:19:17 -05:00
|
|
|
match imp.wrap().downcast::<T>() {
|
2013-12-12 20:01:59 -06:00
|
|
|
Ok(t) => Some(t),
|
|
|
|
Err(t) => {
|
2014-06-04 02:01:40 -05:00
|
|
|
let data = mem::transmute::<_, raw::TraitObject>(t).data;
|
2014-06-14 13:03:34 -05:00
|
|
|
let obj: Box<Runtime + Send> =
|
2014-06-04 02:01:40 -05:00
|
|
|
mem::transmute(raw::TraitObject {
|
|
|
|
vtable: vtable,
|
|
|
|
data: data,
|
|
|
|
});
|
2013-12-12 20:01:59 -06:00
|
|
|
self.put_runtime(obj);
|
|
|
|
None
|
|
|
|
}
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
/// Spawns a sibling to this task. The newly spawned task is configured with
|
|
|
|
/// the `opts` structure and will run `f` as the body of its code.
|
2014-07-23 12:21:50 -05:00
|
|
|
pub fn spawn_sibling(mut self: Box<Task>,
|
|
|
|
opts: TaskOpts,
|
|
|
|
f: proc(): Send) {
|
2013-12-12 20:01:59 -06:00
|
|
|
let ops = self.imp.take_unwrap();
|
|
|
|
ops.spawn_sibling(self, opts, f)
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
/// Deschedules the current task, invoking `f` `amt` times. It is not
|
|
|
|
/// recommended to use this function directly, but rather communication
|
|
|
|
/// primitives in `std::comm` should be used.
|
2014-07-23 12:21:50 -05:00
|
|
|
pub fn deschedule(mut self: Box<Task>,
|
|
|
|
amt: 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
|
|
|
f: |BlockedTask| -> ::core::result::Result<(), BlockedTask>) {
|
2013-12-12 20:01:59 -06:00
|
|
|
let ops = self.imp.take_unwrap();
|
|
|
|
ops.deschedule(amt, self, f)
|
|
|
|
}
|
|
|
|
|
2014-02-17 05:53:45 -06:00
|
|
|
/// Wakes up a previously blocked task, optionally specifying whether the
|
2013-12-12 20:01:59 -06:00
|
|
|
/// current task can accept a change in scheduling. This function can only
|
|
|
|
/// be called on tasks that were previously blocked in `deschedule`.
|
2014-07-23 12:21:50 -05:00
|
|
|
pub fn reawaken(mut self: Box<Task>) {
|
2013-12-12 20:01:59 -06:00
|
|
|
let ops = self.imp.take_unwrap();
|
2014-01-16 21:58:42 -06:00
|
|
|
ops.reawaken(self);
|
2013-12-12 20:01:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Yields control of this task to another task. This function will
|
|
|
|
/// eventually return, but possibly not immediately. This is used as an
|
|
|
|
/// opportunity to allow other tasks a chance to run.
|
2014-07-23 12:21:50 -05:00
|
|
|
pub fn yield_now(mut self: Box<Task>) {
|
2013-12-12 20:01:59 -06:00
|
|
|
let ops = self.imp.take_unwrap();
|
|
|
|
ops.yield_now(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Similar to `yield_now`, except that this function may immediately return
|
|
|
|
/// without yielding (depending on what the runtime decides to do).
|
2014-07-23 12:21:50 -05:00
|
|
|
pub fn maybe_yield(mut self: Box<Task>) {
|
2013-12-12 20:01:59 -06:00
|
|
|
let ops = self.imp.take_unwrap();
|
|
|
|
ops.maybe_yield(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Acquires a handle to the I/O factory that this task contains, normally
|
|
|
|
/// stored in the task's runtime. This factory may not always be available,
|
|
|
|
/// which is why the return type is `Option`
|
|
|
|
pub fn local_io<'a>(&'a mut self) -> Option<LocalIo<'a>> {
|
|
|
|
self.imp.get_mut_ref().local_io()
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
2014-01-04 02:06:13 -06:00
|
|
|
|
|
|
|
/// Returns the stack bounds for this task in (lo, hi) format. The stack
|
|
|
|
/// bounds may not be known for all tasks, so the return value may be
|
|
|
|
/// `None`.
|
2014-01-06 20:39:55 -06:00
|
|
|
pub fn stack_bounds(&self) -> (uint, uint) {
|
2014-01-04 02:06:13 -06:00
|
|
|
self.imp.get_ref().stack_bounds()
|
|
|
|
}
|
2014-01-16 21:58:42 -06:00
|
|
|
|
|
|
|
/// Returns whether it is legal for this task to block the OS thread that it
|
|
|
|
/// is running on.
|
|
|
|
pub fn can_block(&self) -> bool {
|
|
|
|
self.imp.get_ref().can_block()
|
|
|
|
}
|
2014-07-24 00:48:04 -05:00
|
|
|
|
|
|
|
/// Consume this task, flagging it as a candidate for destruction.
|
|
|
|
///
|
|
|
|
/// This function is required to be invoked to destroy a task. A task
|
|
|
|
/// destroyed through a normal drop will abort.
|
|
|
|
pub fn drop(mut self) {
|
|
|
|
self.state = Destroyed;
|
|
|
|
}
|
2013-04-22 14:54:03 -05:00
|
|
|
}
|
|
|
|
|
2013-05-19 03:04:01 -05:00
|
|
|
impl Drop for Task {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2014-01-28 20:05:57 -06:00
|
|
|
rtdebug!("called drop for a task: {}", self as *mut Task as uint);
|
2014-07-24 00:48:04 -05:00
|
|
|
rtassert!(self.state != Armed);
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
2013-04-21 18:28:17 -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
|
|
|
impl TaskOpts {
|
|
|
|
pub fn new() -> TaskOpts {
|
|
|
|
TaskOpts { on_exit: None, name: None, stack_size: None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl Iterator<BlockedTask> for BlockedTasks {
|
2013-12-12 20:01:59 -06:00
|
|
|
fn next(&mut self) -> Option<BlockedTask> {
|
|
|
|
Some(Shared(self.inner.clone()))
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
2013-12-12 20:01:59 -06:00
|
|
|
}
|
2013-06-26 18:41:00 -05:00
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
impl BlockedTask {
|
|
|
|
/// Returns Some if the task was successfully woken; None if already killed.
|
2014-05-05 20:56:44 -05:00
|
|
|
pub fn wake(self) -> Option<Box<Task>> {
|
2013-12-12 20:01:59 -06:00
|
|
|
match self {
|
|
|
|
Owned(task) => Some(task),
|
2014-05-19 19:33:40 -05:00
|
|
|
Shared(arc) => {
|
|
|
|
match arc.swap(0, SeqCst) {
|
2013-12-12 20:01:59 -06:00
|
|
|
0 => None,
|
2014-05-19 19:33:40 -05:00
|
|
|
n => Some(unsafe { mem::transmute(n) }),
|
2013-12-12 20:01:59 -06:00
|
|
|
}
|
|
|
|
}
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 22:47:49 -05:00
|
|
|
/// Reawakens this task if ownership is acquired. If finer-grained control
|
|
|
|
/// is desired, use `wake` instead.
|
|
|
|
pub fn reawaken(self) {
|
|
|
|
self.wake().map(|t| t.reawaken());
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
// This assertion has two flavours because the wake involves an atomic op.
|
|
|
|
// In the faster version, destructors will fail dramatically instead.
|
|
|
|
#[cfg(not(test))] pub fn trash(self) { }
|
|
|
|
#[cfg(test)] pub fn trash(self) { assert!(self.wake().is_none()); }
|
2013-06-26 18:41:00 -05:00
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
/// Create a blocked task, unless the task was already killed.
|
2014-05-05 20:56:44 -05:00
|
|
|
pub fn block(task: Box<Task>) -> BlockedTask {
|
2013-12-12 20:01:59 -06:00
|
|
|
Owned(task)
|
|
|
|
}
|
2013-07-19 16:25:05 -05:00
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
/// Converts one blocked task handle to a list of many handles to the same.
|
2014-04-13 16:39:04 -05:00
|
|
|
pub fn make_selectable(self, num_handles: uint) -> Take<BlockedTasks> {
|
2013-12-12 20:01:59 -06:00
|
|
|
let arc = match self {
|
|
|
|
Owned(task) => {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
let flag = unsafe { AtomicUint::new(mem::transmute(task)) };
|
2014-05-19 19:33:40 -05:00
|
|
|
Arc::new(flag)
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
2013-12-12 20:01:59 -06:00
|
|
|
Shared(arc) => arc.clone(),
|
2013-06-26 18:41:00 -05:00
|
|
|
};
|
2014-01-14 21:32:24 -06:00
|
|
|
BlockedTasks{ inner: arc }.take(num_handles)
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
/// Convert to an unsafe uint value. Useful for storing in a pipe's state
|
|
|
|
/// flag.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn cast_to_uint(self) -> uint {
|
2013-06-26 18:41:00 -05:00
|
|
|
match self {
|
2013-12-12 20:01:59 -06:00
|
|
|
Owned(task) => {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
let blocked_task_ptr: uint = mem::transmute(task);
|
2013-12-12 20:01:59 -06:00
|
|
|
rtassert!(blocked_task_ptr & 0x1 == 0);
|
|
|
|
blocked_task_ptr
|
|
|
|
}
|
|
|
|
Shared(arc) => {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
let blocked_task_ptr: uint = mem::transmute(box arc);
|
2013-12-12 20:01:59 -06:00
|
|
|
rtassert!(blocked_task_ptr & 0x1 == 0);
|
|
|
|
blocked_task_ptr | 0x1
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
/// Convert from an unsafe uint value. Useful for retrieving a pipe's state
|
|
|
|
/// flag.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn cast_from_uint(blocked_task_ptr: uint) -> BlockedTask {
|
|
|
|
if blocked_task_ptr & 0x1 == 0 {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
Owned(mem::transmute(blocked_task_ptr))
|
2013-10-17 03:40:33 -05:00
|
|
|
} else {
|
2014-05-19 19:33:40 -05:00
|
|
|
let ptr: Box<Arc<AtomicUint>> =
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
mem::transmute(blocked_task_ptr & !1);
|
2013-12-12 20:01:59 -06:00
|
|
|
Shared(*ptr)
|
2013-10-17 03:40:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
impl Death {
|
|
|
|
pub fn new() -> Death {
|
2014-06-13 18:03:41 -05:00
|
|
|
Death { on_exit: None, marker: marker::NoCopy }
|
2013-10-09 12:34:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-21 21:03:52 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2013-10-27 14:12:40 -05:00
|
|
|
use super::*;
|
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
|
|
|
use std::prelude::*;
|
|
|
|
use std::task;
|
2014-06-11 21:33:52 -05:00
|
|
|
use std::gc::{Gc, GC};
|
2013-04-21 21:03:52 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn local_heap() {
|
2014-06-27 14:30:25 -05:00
|
|
|
let a = box(GC) 5i;
|
2013-12-12 23:38:57 -06:00
|
|
|
let b = a;
|
|
|
|
assert!(*a == 5);
|
|
|
|
assert!(*b == 5);
|
2013-04-21 21:03:52 -05:00
|
|
|
}
|
2013-04-22 14:54:03 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tls() {
|
2014-06-11 21:33:52 -05:00
|
|
|
local_data_key!(key: Gc<String>)
|
|
|
|
key.replace(Some(box(GC) "data".to_string()));
|
2014-04-28 22:36:08 -05:00
|
|
|
assert_eq!(key.get().unwrap().as_slice(), "data");
|
2014-06-11 21:33:52 -05:00
|
|
|
local_data_key!(key2: Gc<String>)
|
|
|
|
key2.replace(Some(box(GC) "data".to_string()));
|
2014-04-28 22:36:08 -05:00
|
|
|
assert_eq!(key2.get().unwrap().as_slice(), "data");
|
2013-04-22 14:54:03 -05:00
|
|
|
}
|
2013-04-22 19:15:31 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unwind() {
|
2013-12-12 23:38:57 -06:00
|
|
|
let result = task::try(proc()());
|
|
|
|
rtdebug!("trying first assert");
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let result = task::try::<()>(proc() fail!());
|
|
|
|
rtdebug!("trying second assert");
|
|
|
|
assert!(result.is_err());
|
2013-04-22 19:15:31 -05:00
|
|
|
}
|
2013-05-06 20:24:37 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rng() {
|
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
|
|
|
use std::rand::{StdRng, Rng};
|
2014-05-01 13:12:16 -05:00
|
|
|
let mut r = StdRng::new().ok().unwrap();
|
2013-12-12 23:38:57 -06:00
|
|
|
let _ = r.next_u32();
|
2013-05-06 20:24:37 -05:00
|
|
|
}
|
2013-04-27 20:57:15 -05:00
|
|
|
|
2013-05-17 01:12:22 -05:00
|
|
|
#[test]
|
|
|
|
fn comm_stream() {
|
2014-03-09 16:58:32 -05:00
|
|
|
let (tx, rx) = channel();
|
2014-06-27 14:30:25 -05:00
|
|
|
tx.send(10i);
|
2014-03-09 16:58:32 -05:00
|
|
|
assert!(rx.recv() == 10);
|
2013-05-17 01:12:22 -05:00
|
|
|
}
|
2013-06-14 01:31:19 -05:00
|
|
|
|
2013-06-20 20:26:56 -05:00
|
|
|
#[test]
|
|
|
|
fn comm_shared_chan() {
|
2014-03-09 16:58:32 -05:00
|
|
|
let (tx, rx) = channel();
|
2014-06-27 14:30:25 -05:00
|
|
|
tx.send(10i);
|
2014-03-09 16:58:32 -05:00
|
|
|
assert!(rx.recv() == 10);
|
2013-06-20 20:26:56 -05:00
|
|
|
}
|
|
|
|
|
2013-06-22 03:09:06 -05:00
|
|
|
#[test]
|
|
|
|
fn heap_cycles() {
|
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
|
|
|
use std::cell::RefCell;
|
2013-06-22 03:09:06 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
struct List {
|
2014-06-11 21:33:52 -05:00
|
|
|
next: Option<Gc<RefCell<List>>>,
|
2013-12-12 23:38:57 -06:00
|
|
|
}
|
2013-06-22 03:09:06 -05:00
|
|
|
|
2014-06-11 21:33:52 -05:00
|
|
|
let a = box(GC) RefCell::new(List { next: None });
|
|
|
|
let b = box(GC) RefCell::new(List { next: Some(a) });
|
2013-06-22 03:09:06 -05:00
|
|
|
|
2013-12-30 19:46:48 -06:00
|
|
|
{
|
|
|
|
let mut a = a.borrow_mut();
|
2014-03-20 17:04:55 -05:00
|
|
|
a.next = Some(b);
|
2013-12-30 19:46:48 -06:00
|
|
|
}
|
2013-06-22 03:09:06 -05:00
|
|
|
}
|
2013-10-27 14:12:40 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2013-12-18 11:57:58 -06:00
|
|
|
fn test_begin_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
|
|
|
use std::rt::unwind::begin_unwind;
|
2013-12-18 11:57:58 -06:00
|
|
|
begin_unwind("cause", file!(), line!())
|
|
|
|
}
|
2013-12-12 20:01:59 -06:00
|
|
|
|
2014-07-24 00:48:04 -05:00
|
|
|
#[test]
|
|
|
|
fn drop_new_task_ok() {
|
|
|
|
drop(Task::new());
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:01:59 -06:00
|
|
|
// Task blocking tests
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_and_wake() {
|
2014-04-25 03:08:02 -05:00
|
|
|
let task = box Task::new();
|
2013-12-12 23:38:57 -06:00
|
|
|
let mut task = BlockedTask::block(task).wake().unwrap();
|
2014-07-24 09:32:14 -05:00
|
|
|
task.drop();
|
2013-12-12 20:01:59 -06:00
|
|
|
}
|
2013-05-08 14:26:34 -05:00
|
|
|
}
|