2013-05-19 03:13:53 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2014-07-10 16:19:17 -05:00
|
|
|
use alloc::boxed::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 local_ptr;
|
|
|
|
use task::Task;
|
2013-05-19 17:25:35 -05:00
|
|
|
|
2013-12-03 21:18:58 -06:00
|
|
|
/// Encapsulates some task-local data.
|
|
|
|
pub trait Local<Borrowed> {
|
2014-05-05 20:56:44 -05:00
|
|
|
fn put(value: Box<Self>);
|
|
|
|
fn take() -> Box<Self>;
|
|
|
|
fn try_take() -> Option<Box<Self>>;
|
2013-08-08 13:38:10 -05:00
|
|
|
fn exists(unused_value: Option<Self>) -> bool;
|
2013-12-03 21:18:58 -06:00
|
|
|
fn borrow(unused_value: Option<Self>) -> Borrowed;
|
2014-05-05 20:56:44 -05:00
|
|
|
unsafe fn unsafe_take() -> Box<Self>;
|
2013-05-19 17:45:39 -05:00
|
|
|
unsafe fn unsafe_borrow() -> *mut Self;
|
2013-05-19 18:50:21 -05:00
|
|
|
unsafe fn try_unsafe_borrow() -> Option<*mut Self>;
|
2013-05-19 03:13:53 -05:00
|
|
|
}
|
2013-05-19 17:25:35 -05:00
|
|
|
|
2013-12-03 21:18:58 -06:00
|
|
|
impl Local<local_ptr::Borrowed<Task>> for Task {
|
2013-08-17 03:24:29 -05:00
|
|
|
#[inline]
|
2014-05-05 20:56:44 -05:00
|
|
|
fn put(value: Box<Task>) { unsafe { local_ptr::put(value) } }
|
2013-08-17 03:24:29 -05:00
|
|
|
#[inline]
|
2014-05-05 20:56:44 -05:00
|
|
|
fn take() -> Box<Task> { unsafe { local_ptr::take() } }
|
2013-12-30 02:55:27 -06:00
|
|
|
#[inline]
|
2014-05-05 20:56:44 -05:00
|
|
|
fn try_take() -> Option<Box<Task>> { unsafe { local_ptr::try_take() } }
|
2013-08-08 13:38:10 -05:00
|
|
|
fn exists(_: Option<Task>) -> bool { local_ptr::exists() }
|
2013-12-03 21:18:58 -06:00
|
|
|
#[inline]
|
|
|
|
fn borrow(_: Option<Task>) -> local_ptr::Borrowed<Task> {
|
2013-06-14 14:17:56 -05:00
|
|
|
unsafe {
|
2013-12-03 21:18:58 -06:00
|
|
|
local_ptr::borrow::<Task>()
|
2013-06-14 14:17:56 -05:00
|
|
|
}
|
2013-06-10 17:29:02 -05:00
|
|
|
}
|
2013-08-17 03:24:29 -05:00
|
|
|
#[inline]
|
2014-05-05 20:56:44 -05:00
|
|
|
unsafe fn unsafe_take() -> Box<Task> { local_ptr::unsafe_take() }
|
2013-08-17 19:40:38 -05:00
|
|
|
#[inline]
|
2013-07-19 16:25:05 -05:00
|
|
|
unsafe fn unsafe_borrow() -> *mut Task { local_ptr::unsafe_borrow() }
|
2013-08-17 03:24:29 -05:00
|
|
|
#[inline]
|
2013-08-01 17:08:51 -05:00
|
|
|
unsafe fn try_unsafe_borrow() -> Option<*mut Task> {
|
2013-08-12 21:09:46 -05:00
|
|
|
local_ptr::try_unsafe_borrow()
|
2013-08-01 17:08:51 -05:00
|
|
|
}
|
2013-05-19 18:50:21 -05:00
|
|
|
}
|
|
|
|
|
2013-05-19 17:45:39 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
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::*;
|
2014-11-15 01:05:37 -06:00
|
|
|
use thread::Thread;
|
2013-05-19 17:45:39 -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 task::Task;
|
2013-05-19 17:45:39 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-07-19 16:25:05 -05:00
|
|
|
fn thread_local_task_smoke_test() {
|
2014-05-14 03:13:29 -05:00
|
|
|
Thread::start(proc() {
|
2014-11-15 01:05:37 -06:00
|
|
|
let task = box Task::new(None, None);
|
2013-08-03 19:40:29 -05:00
|
|
|
Local::put(task);
|
2014-05-05 20:56:44 -05:00
|
|
|
let task: Box<Task> = Local::take();
|
2013-08-03 19:40:29 -05:00
|
|
|
cleanup_task(task);
|
2014-05-14 03:13:29 -05:00
|
|
|
}).join();
|
2013-05-19 17:45:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-07-19 16:25:05 -05:00
|
|
|
fn thread_local_task_two_instances() {
|
2014-05-14 03:13:29 -05:00
|
|
|
Thread::start(proc() {
|
2014-11-15 01:05:37 -06:00
|
|
|
let task = box Task::new(None, None);
|
2013-08-03 19:40:29 -05:00
|
|
|
Local::put(task);
|
2014-05-05 20:56:44 -05:00
|
|
|
let task: Box<Task> = Local::take();
|
2013-08-03 19:40:29 -05:00
|
|
|
cleanup_task(task);
|
2014-11-15 01:05:37 -06:00
|
|
|
let task = box Task::new(None, None);
|
2013-08-03 19:40:29 -05:00
|
|
|
Local::put(task);
|
2014-05-05 20:56:44 -05:00
|
|
|
let task: Box<Task> = Local::take();
|
2013-08-03 19:40:29 -05:00
|
|
|
cleanup_task(task);
|
2014-05-14 03:13:29 -05:00
|
|
|
}).join();
|
2013-05-19 17:45:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn borrow_smoke_test() {
|
2014-05-14 03:13:29 -05:00
|
|
|
Thread::start(proc() {
|
2014-11-15 01:05:37 -06:00
|
|
|
let task = box Task::new(None, None);
|
2013-08-03 19:40:29 -05:00
|
|
|
Local::put(task);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let _task: *mut Task = Local::unsafe_borrow();
|
|
|
|
}
|
2014-05-05 20:56:44 -05:00
|
|
|
let task: Box<Task> = Local::take();
|
2013-08-03 19:40:29 -05:00
|
|
|
cleanup_task(task);
|
2014-05-14 03:13:29 -05:00
|
|
|
}).join();
|
2013-05-19 17:45:39 -05:00
|
|
|
}
|
2013-06-10 17:29:02 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn borrow_with_return() {
|
2014-05-14 03:13:29 -05:00
|
|
|
Thread::start(proc() {
|
2014-11-15 01:05:37 -06:00
|
|
|
let task = box Task::new(None, None);
|
2013-08-03 19:40:29 -05:00
|
|
|
Local::put(task);
|
|
|
|
|
2013-12-03 21:18:58 -06:00
|
|
|
{
|
|
|
|
let _ = Local::borrow(None::<Task>);
|
|
|
|
}
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
let task: Box<Task> = Local::take();
|
2013-08-03 19:40:29 -05:00
|
|
|
cleanup_task(task);
|
2014-05-14 03:13:29 -05:00
|
|
|
}).join();
|
2013-06-10 17:29:02 -05:00
|
|
|
}
|
2013-06-14 14:17:56 -05:00
|
|
|
|
2013-12-30 02:55:27 -06:00
|
|
|
#[test]
|
|
|
|
fn try_take() {
|
2014-05-14 03:13:29 -05:00
|
|
|
Thread::start(proc() {
|
2014-11-15 01:05:37 -06:00
|
|
|
let task = box Task::new(None, None);
|
2013-12-30 02:55:27 -06:00
|
|
|
Local::put(task);
|
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
let t: Box<Task> = Local::try_take().unwrap();
|
|
|
|
let u: Option<Box<Task>> = Local::try_take();
|
2013-12-30 02:55:27 -06:00
|
|
|
assert!(u.is_none());
|
|
|
|
|
|
|
|
cleanup_task(t);
|
2014-05-14 03:13:29 -05:00
|
|
|
}).join();
|
2013-12-30 02:55:27 -06:00
|
|
|
}
|
|
|
|
|
2014-07-24 00:48:04 -05:00
|
|
|
fn cleanup_task(t: Box<Task>) {
|
|
|
|
t.drop();
|
2013-12-12 23:38:57 -06:00
|
|
|
}
|
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
}
|