2013-02-03 20:15:43 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-04-27 21:05:41 -05:00
|
|
|
//! The EventLoop and internal synchronous I/O interface.
|
|
|
|
|
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 collections::string::String;
|
|
|
|
use collections::vec::Vec;
|
|
|
|
use core::fmt;
|
|
|
|
use core::mem;
|
2013-11-11 00:46:32 -06:00
|
|
|
use libc::c_int;
|
2013-09-16 17:28:56 -05:00
|
|
|
use libc;
|
2013-04-17 19:55:21 -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
|
|
|
use c_str::CString;
|
|
|
|
use local::Local;
|
|
|
|
use task::Task;
|
2013-11-04 14:45:05 -06:00
|
|
|
|
2013-02-03 20:15:43 -06:00
|
|
|
pub trait EventLoop {
|
|
|
|
fn run(&mut self);
|
2014-06-14 13:03:34 -05:00
|
|
|
fn callback(&mut self, arg: proc(): Send);
|
|
|
|
fn pausable_idle_callback(&mut self, Box<Callback + Send>)
|
|
|
|
-> Box<PausableIdleCallback + Send>;
|
|
|
|
fn remote_callback(&mut self, Box<Callback + Send>)
|
|
|
|
-> Box<RemoteCallback + Send>;
|
2013-10-16 19:05:28 -05:00
|
|
|
|
2013-12-05 19:25:48 -06:00
|
|
|
/// The asynchronous I/O services. Not all event loops may provide one.
|
2013-12-05 19:37:02 -06:00
|
|
|
fn io<'a>(&'a mut self) -> Option<&'a mut IoFactory>;
|
2014-02-10 21:59:35 -06:00
|
|
|
fn has_active_io(&self) -> bool;
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
pub trait Callback {
|
|
|
|
fn call(&mut self);
|
|
|
|
}
|
|
|
|
|
2013-05-20 20:23:56 -05:00
|
|
|
pub trait RemoteCallback {
|
2013-08-16 15:41:30 -05:00
|
|
|
/// Trigger the remote callback. Note that the number of times the
|
|
|
|
/// callback is run is not guaranteed. All that is guaranteed is
|
|
|
|
/// that, after calling 'fire', the callback will be called at
|
|
|
|
/// least once, but multiple callbacks may be coalesced and
|
|
|
|
/// callbacks may be called more often requested. Destruction also
|
|
|
|
/// triggers the callback.
|
2013-05-20 20:23:56 -05:00
|
|
|
fn fire(&mut self);
|
|
|
|
}
|
|
|
|
|
2013-10-22 11:36:30 -05:00
|
|
|
/// Description of what to do when a file handle is closed
|
|
|
|
pub enum CloseBehavior {
|
|
|
|
/// Do not close this handle when the object is destroyed
|
|
|
|
DontClose,
|
|
|
|
/// Synchronously close the handle, meaning that the task will block when
|
|
|
|
/// the handle is destroyed until it has been fully closed.
|
|
|
|
CloseSynchronously,
|
|
|
|
/// Asynchronously closes a handle, meaning that the task will *not* block
|
|
|
|
/// when the handle is destroyed, but the handle will still get deallocated
|
|
|
|
/// and cleaned up (but this will happen asynchronously on the local event
|
|
|
|
/// loop).
|
|
|
|
CloseAsynchronously,
|
|
|
|
}
|
|
|
|
|
2014-05-05 16:33:55 -05:00
|
|
|
/// Data needed to spawn a process. Serializes the `std::io::process::Command`
|
|
|
|
/// builder.
|
|
|
|
pub struct ProcessConfig<'a> {
|
|
|
|
/// Path to the program to run.
|
|
|
|
pub program: &'a CString,
|
|
|
|
|
|
|
|
/// Arguments to pass to the program (doesn't include the program itself).
|
|
|
|
pub args: &'a [CString],
|
|
|
|
|
|
|
|
/// Optional environment to specify for the program. If this is None, then
|
|
|
|
/// it will inherit the current process's environment.
|
2014-07-02 15:50:45 -05:00
|
|
|
pub env: Option<&'a [(&'a CString, &'a CString)]>,
|
2014-05-05 16:33:55 -05:00
|
|
|
|
|
|
|
/// Optional working directory for the new process. If this is None, then
|
|
|
|
/// the current directory of the running process is inherited.
|
|
|
|
pub cwd: Option<&'a CString>,
|
|
|
|
|
|
|
|
/// Configuration for the child process's stdin handle (file descriptor 0).
|
|
|
|
/// This field defaults to `CreatePipe(true, false)` so the input can be
|
|
|
|
/// written to.
|
|
|
|
pub stdin: StdioContainer,
|
|
|
|
|
|
|
|
/// Configuration for the child process's stdout handle (file descriptor 1).
|
|
|
|
/// This field defaults to `CreatePipe(false, true)` so the output can be
|
|
|
|
/// collected.
|
|
|
|
pub stdout: StdioContainer,
|
|
|
|
|
|
|
|
/// Configuration for the child process's stdout handle (file descriptor 2).
|
|
|
|
/// This field defaults to `CreatePipe(false, true)` so the output can be
|
|
|
|
/// collected.
|
|
|
|
pub stderr: StdioContainer,
|
|
|
|
|
|
|
|
/// Any number of streams/file descriptors/pipes may be attached to this
|
|
|
|
/// process. This list enumerates the file descriptors and such for the
|
|
|
|
/// process to be spawned, and the file descriptors inherited will start at
|
|
|
|
/// 3 and go to the length of this array. The first three file descriptors
|
|
|
|
/// (stdin/stdout/stderr) are configured with the `stdin`, `stdout`, and
|
|
|
|
/// `stderr` fields.
|
|
|
|
pub extra_io: &'a [StdioContainer],
|
|
|
|
|
|
|
|
/// Sets the child process's user id. This translates to a `setuid` call in
|
|
|
|
/// the child process. Setting this value on windows will cause the spawn to
|
|
|
|
/// fail. Failure in the `setuid` call on unix will also cause the spawn to
|
|
|
|
/// fail.
|
|
|
|
pub uid: Option<uint>,
|
|
|
|
|
|
|
|
/// Similar to `uid`, but sets the group id of the child process. This has
|
|
|
|
/// the same semantics as the `uid` field.
|
|
|
|
pub gid: Option<uint>,
|
|
|
|
|
|
|
|
/// If true, the child process is spawned in a detached state. On unix, this
|
|
|
|
/// means that the child is the leader of a new process group.
|
|
|
|
pub detach: bool,
|
|
|
|
}
|
|
|
|
|
2013-12-05 19:37:02 -06:00
|
|
|
pub struct LocalIo<'a> {
|
2014-08-27 20:46:52 -05:00
|
|
|
factory: &'a mut IoFactory+'a,
|
2013-12-05 19:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[unsafe_destructor]
|
2013-12-05 19:37:02 -06:00
|
|
|
impl<'a> Drop for LocalIo<'a> {
|
2013-12-05 19:25:48 -06:00
|
|
|
fn drop(&mut self) {
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME(pcwalton): Do nothing here for now, but eventually we may want
|
2013-12-05 19:25:48 -06:00
|
|
|
// something. For now this serves to make `LocalIo` noncopyable.
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 19:05:28 -05:00
|
|
|
|
2013-12-05 19:37:02 -06:00
|
|
|
impl<'a> LocalIo<'a> {
|
2013-12-05 19:25:48 -06:00
|
|
|
/// Returns the local I/O: either the local scheduler's I/O services or
|
|
|
|
/// the native I/O services.
|
2014-07-17 23:44:59 -05:00
|
|
|
pub fn borrow() -> Option<LocalIo<'a>> {
|
2013-12-18 11:57:58 -06:00
|
|
|
// FIXME(#11053): bad
|
|
|
|
//
|
|
|
|
// This is currently very unsafely implemented. We don't actually
|
|
|
|
// *take* the local I/O so there's a very real possibility that we
|
|
|
|
// can have two borrows at once. Currently there is not a clear way
|
|
|
|
// to actually borrow the local I/O factory safely because even if
|
|
|
|
// ownership were transferred down to the functions that the I/O
|
|
|
|
// factory implements it's just too much of a pain to know when to
|
|
|
|
// relinquish ownership back into the local task (but that would be
|
|
|
|
// the safe way of implementing this function).
|
2013-12-12 19:30:41 -06:00
|
|
|
//
|
|
|
|
// In order to get around this, we just transmute a copy out of the task
|
|
|
|
// in order to have what is likely a static lifetime (bad).
|
2014-06-04 12:54:35 -05:00
|
|
|
let mut t: Box<Task> = match Local::try_take() {
|
|
|
|
Some(t) => t,
|
|
|
|
None => return None,
|
|
|
|
};
|
2013-12-12 19:30:41 -06:00
|
|
|
let ret = t.local_io().map(|t| {
|
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
|
|
|
unsafe { mem::transmute_copy(&t) }
|
2013-12-12 19:30:41 -06:00
|
|
|
});
|
|
|
|
Local::put(t);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-12-05 19:25:48 -06:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn maybe_raise<T>(f: |io: &mut IoFactory| -> IoResult<T>)
|
|
|
|
-> IoResult<T>
|
2013-12-12 19:30:41 -06:00
|
|
|
{
|
2014-08-18 10:29:44 -05:00
|
|
|
#[cfg(unix)] use libc::EINVAL as ERROR;
|
|
|
|
#[cfg(windows)] use libc::ERROR_CALL_NOT_IMPLEMENTED as ERROR;
|
2013-12-12 19:30:41 -06:00
|
|
|
match LocalIo::borrow() {
|
2014-01-29 18:33:57 -06:00
|
|
|
Some(mut io) => f(io.get()),
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
None => Err(IoError {
|
|
|
|
code: ERROR as uint,
|
|
|
|
extra: 0,
|
|
|
|
detail: None,
|
|
|
|
}),
|
2013-10-16 19:05:28 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-12 16:38:28 -06:00
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
pub fn new<'a>(io: &'a mut IoFactory+'a) -> LocalIo<'a> {
|
2013-12-12 19:30:41 -06:00
|
|
|
LocalIo { factory: io }
|
|
|
|
}
|
|
|
|
|
2013-12-05 19:25:48 -06:00
|
|
|
/// Returns the underlying I/O factory as a trait reference.
|
|
|
|
#[inline]
|
2013-12-05 19:37:02 -06:00
|
|
|
pub fn get<'a>(&'a mut self) -> &'a mut IoFactory {
|
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
|
|
|
let f: &'a mut IoFactory = self.factory;
|
|
|
|
f
|
2013-12-05 19:25:48 -06:00
|
|
|
}
|
2013-10-16 19:05:28 -05:00
|
|
|
}
|
|
|
|
|
2013-02-03 20:15:43 -06:00
|
|
|
pub trait IoFactory {
|
2013-10-30 01:31:07 -05:00
|
|
|
// networking
|
2014-04-18 15:23:56 -05:00
|
|
|
fn tcp_connect(&mut self, addr: SocketAddr,
|
2014-06-14 13:03:34 -05:00
|
|
|
timeout: Option<u64>) -> IoResult<Box<RtioTcpStream + Send>>;
|
2014-05-05 20:56:44 -05:00
|
|
|
fn tcp_bind(&mut self, addr: SocketAddr)
|
2014-06-14 13:03:34 -05:00
|
|
|
-> IoResult<Box<RtioTcpListener + Send>>;
|
2014-05-05 20:56:44 -05:00
|
|
|
fn udp_bind(&mut self, addr: SocketAddr)
|
2014-06-14 13:03:34 -05:00
|
|
|
-> IoResult<Box<RtioUdpSocket + Send>>;
|
2014-03-08 20:21:49 -06:00
|
|
|
fn unix_bind(&mut self, path: &CString)
|
2014-06-14 13:03:34 -05:00
|
|
|
-> IoResult<Box<RtioUnixListener + Send>>;
|
2014-04-22 20:38:59 -05:00
|
|
|
fn unix_connect(&mut self, path: &CString,
|
2014-06-14 13:03:34 -05:00
|
|
|
timeout: Option<u64>) -> IoResult<Box<RtioPipe + Send>>;
|
2013-10-16 19:05:28 -05:00
|
|
|
fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
hint: Option<AddrinfoHint>)
|
|
|
|
-> IoResult<Vec<AddrinfoInfo>>;
|
2013-10-30 01:31:07 -05:00
|
|
|
|
|
|
|
// filesystem operations
|
2014-03-08 20:21:49 -06:00
|
|
|
fn fs_from_raw_fd(&mut self, fd: c_int, close: CloseBehavior)
|
2014-06-14 13:03:34 -05:00
|
|
|
-> Box<RtioFileStream + Send>;
|
2013-10-16 18:48:30 -05:00
|
|
|
fn fs_open(&mut self, path: &CString, fm: FileMode, fa: FileAccess)
|
2014-06-14 13:03:34 -05:00
|
|
|
-> IoResult<Box<RtioFileStream + Send>>;
|
2014-03-08 20:21:49 -06:00
|
|
|
fn fs_unlink(&mut self, path: &CString) -> IoResult<()>;
|
|
|
|
fn fs_stat(&mut self, path: &CString) -> IoResult<FileStat>;
|
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
|
|
|
fn fs_mkdir(&mut self, path: &CString, mode: uint) -> IoResult<()>;
|
|
|
|
fn fs_chmod(&mut self, path: &CString, mode: uint) -> IoResult<()>;
|
2014-03-08 20:21:49 -06:00
|
|
|
fn fs_rmdir(&mut self, path: &CString) -> IoResult<()>;
|
|
|
|
fn fs_rename(&mut self, path: &CString, to: &CString) -> IoResult<()>;
|
2013-10-16 18:48:30 -05:00
|
|
|
fn fs_readdir(&mut self, path: &CString, flags: c_int) ->
|
2014-06-03 00:11:19 -05:00
|
|
|
IoResult<Vec<CString>>;
|
2014-03-08 20:21:49 -06:00
|
|
|
fn fs_lstat(&mut self, path: &CString) -> IoResult<FileStat>;
|
2013-10-30 01:31:07 -05:00
|
|
|
fn fs_chown(&mut self, path: &CString, uid: int, gid: int) ->
|
2014-03-08 20:21:49 -06:00
|
|
|
IoResult<()>;
|
2014-06-03 00:11:19 -05:00
|
|
|
fn fs_readlink(&mut self, path: &CString) -> IoResult<CString>;
|
2014-03-08 20:21:49 -06:00
|
|
|
fn fs_symlink(&mut self, src: &CString, dst: &CString) -> IoResult<()>;
|
|
|
|
fn fs_link(&mut self, src: &CString, dst: &CString) -> IoResult<()>;
|
2013-11-05 17:48:27 -06:00
|
|
|
fn fs_utime(&mut self, src: &CString, atime: u64, mtime: u64) ->
|
2014-03-08 20:21:49 -06:00
|
|
|
IoResult<()>;
|
2013-10-30 01:31:07 -05:00
|
|
|
|
|
|
|
// misc
|
2014-06-14 13:03:34 -05:00
|
|
|
fn timer_init(&mut self) -> IoResult<Box<RtioTimer + Send>>;
|
2014-05-05 16:33:55 -05:00
|
|
|
fn spawn(&mut self, cfg: ProcessConfig)
|
2014-06-14 13:03:34 -05:00
|
|
|
-> IoResult<(Box<RtioProcess + Send>,
|
|
|
|
Vec<Option<Box<RtioPipe + Send>>>)>;
|
2014-03-08 20:21:49 -06:00
|
|
|
fn kill(&mut self, pid: libc::pid_t, signal: int) -> IoResult<()>;
|
2014-06-14 13:03:34 -05:00
|
|
|
fn pipe_open(&mut self, fd: c_int) -> IoResult<Box<RtioPipe + Send>>;
|
2013-10-17 19:04:51 -05:00
|
|
|
fn tty_open(&mut self, fd: c_int, readable: bool)
|
2014-06-14 13:03:34 -05:00
|
|
|
-> IoResult<Box<RtioTTY + Send>>;
|
|
|
|
fn signal(&mut self, signal: int, cb: Box<Callback + Send>)
|
|
|
|
-> IoResult<Box<RtioSignal + Send>>;
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
|
|
|
|
2013-07-02 18:40:57 -05:00
|
|
|
pub trait RtioTcpListener : RtioSocket {
|
2014-07-23 12:21:50 -05:00
|
|
|
fn listen(self: Box<Self>) -> IoResult<Box<RtioTcpAcceptor + Send>>;
|
2013-08-27 12:01:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RtioTcpAcceptor : RtioSocket {
|
2014-06-14 13:03:34 -05:00
|
|
|
fn accept(&mut self) -> IoResult<Box<RtioTcpStream + Send>>;
|
2014-03-08 20:21:49 -06:00
|
|
|
fn accept_simultaneously(&mut self) -> IoResult<()>;
|
|
|
|
fn dont_accept_simultaneously(&mut self) -> IoResult<()>;
|
2014-04-21 22:30:07 -05:00
|
|
|
fn set_timeout(&mut self, timeout: Option<u64>);
|
2014-07-11 16:29:15 -05:00
|
|
|
fn clone(&self) -> Box<RtioTcpAcceptor + Send>;
|
|
|
|
fn close_accept(&mut self) -> IoResult<()>;
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
|
2013-08-29 16:20:48 -05:00
|
|
|
pub trait RtioTcpStream : RtioSocket {
|
2014-03-08 20:21:49 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
|
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
|
|
|
|
fn peer_name(&mut self) -> IoResult<SocketAddr>;
|
|
|
|
fn control_congestion(&mut self) -> IoResult<()>;
|
|
|
|
fn nodelay(&mut self) -> IoResult<()>;
|
|
|
|
fn keepalive(&mut self, delay_in_seconds: uint) -> IoResult<()>;
|
|
|
|
fn letdie(&mut self) -> IoResult<()>;
|
2014-06-14 13:03:34 -05:00
|
|
|
fn clone(&self) -> Box<RtioTcpStream + Send>;
|
2014-03-08 20:21:49 -06:00
|
|
|
fn close_write(&mut self) -> IoResult<()>;
|
2014-04-24 20:48:21 -05:00
|
|
|
fn close_read(&mut self) -> IoResult<()>;
|
2014-04-25 22:47:49 -05:00
|
|
|
fn set_timeout(&mut self, timeout_ms: Option<u64>);
|
|
|
|
fn set_read_timeout(&mut self, timeout_ms: Option<u64>);
|
|
|
|
fn set_write_timeout(&mut self, timeout_ms: Option<u64>);
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
2013-06-17 14:32:21 -05:00
|
|
|
|
2013-07-02 18:40:57 -05:00
|
|
|
pub trait RtioSocket {
|
2014-03-08 20:21:49 -06:00
|
|
|
fn socket_name(&mut self) -> IoResult<SocketAddr>;
|
2013-07-02 18:40:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RtioUdpSocket : RtioSocket {
|
2014-06-29 22:08:27 -05:00
|
|
|
fn recv_from(&mut self, buf: &mut [u8]) -> IoResult<(uint, SocketAddr)>;
|
|
|
|
fn send_to(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()>;
|
2013-07-02 18:40:57 -05:00
|
|
|
|
2014-03-08 20:21:49 -06:00
|
|
|
fn join_multicast(&mut self, multi: IpAddr) -> IoResult<()>;
|
|
|
|
fn leave_multicast(&mut self, multi: IpAddr) -> IoResult<()>;
|
2013-07-02 18:40:57 -05:00
|
|
|
|
2014-03-08 20:21:49 -06:00
|
|
|
fn loop_multicast_locally(&mut self) -> IoResult<()>;
|
|
|
|
fn dont_loop_multicast_locally(&mut self) -> IoResult<()>;
|
2013-07-02 18:40:57 -05:00
|
|
|
|
2014-03-08 20:21:49 -06:00
|
|
|
fn multicast_time_to_live(&mut self, ttl: int) -> IoResult<()>;
|
|
|
|
fn time_to_live(&mut self, ttl: int) -> IoResult<()>;
|
2013-07-02 18:40:57 -05:00
|
|
|
|
2014-03-08 20:21:49 -06:00
|
|
|
fn hear_broadcasts(&mut self) -> IoResult<()>;
|
|
|
|
fn ignore_broadcasts(&mut self) -> IoResult<()>;
|
2014-01-22 21:32:16 -06:00
|
|
|
|
2014-06-14 13:03:34 -05:00
|
|
|
fn clone(&self) -> Box<RtioUdpSocket + Send>;
|
2014-04-25 22:47:49 -05:00
|
|
|
fn set_timeout(&mut self, timeout_ms: Option<u64>);
|
|
|
|
fn set_read_timeout(&mut self, timeout_ms: Option<u64>);
|
|
|
|
fn set_write_timeout(&mut self, timeout_ms: Option<u64>);
|
2013-06-17 14:32:21 -05:00
|
|
|
}
|
2013-07-19 18:02:38 -05:00
|
|
|
|
|
|
|
pub trait RtioTimer {
|
2013-08-08 20:58:18 -05:00
|
|
|
fn sleep(&mut self, msecs: u64);
|
2014-06-14 13:03:34 -05:00
|
|
|
fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>);
|
|
|
|
fn period(&mut self, msecs: u64, cb: Box<Callback + Send>);
|
2013-07-19 18:02:38 -05:00
|
|
|
}
|
2013-08-19 18:10:43 -05:00
|
|
|
|
2013-08-20 17:38:41 -05:00
|
|
|
pub trait RtioFileStream {
|
2014-03-08 20:21:49 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<int>;
|
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
|
|
|
|
fn pread(&mut self, buf: &mut [u8], offset: u64) -> IoResult<int>;
|
|
|
|
fn pwrite(&mut self, buf: &[u8], offset: u64) -> IoResult<()>;
|
|
|
|
fn seek(&mut self, pos: i64, whence: SeekStyle) -> IoResult<u64>;
|
|
|
|
fn tell(&self) -> IoResult<u64>;
|
|
|
|
fn fsync(&mut self) -> IoResult<()>;
|
|
|
|
fn datasync(&mut self) -> IoResult<()>;
|
|
|
|
fn truncate(&mut self, offset: i64) -> IoResult<()>;
|
2014-05-12 00:31:22 -05:00
|
|
|
fn fstat(&mut self) -> IoResult<FileStat>;
|
2013-08-19 18:10:43 -05:00
|
|
|
}
|
2013-09-16 17:28:56 -05:00
|
|
|
|
|
|
|
pub trait RtioProcess {
|
|
|
|
fn id(&self) -> libc::pid_t;
|
2014-03-08 20:21:49 -06:00
|
|
|
fn kill(&mut self, signal: int) -> IoResult<()>;
|
2014-05-05 18:58:42 -05:00
|
|
|
fn wait(&mut self) -> IoResult<ProcessExit>;
|
|
|
|
fn set_timeout(&mut self, timeout: Option<u64>);
|
2013-09-16 17:28:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RtioPipe {
|
2014-03-08 20:21:49 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
|
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
|
2014-06-14 13:03:34 -05:00
|
|
|
fn clone(&self) -> Box<RtioPipe + Send>;
|
2014-04-24 20:48:21 -05:00
|
|
|
|
|
|
|
fn close_write(&mut self) -> IoResult<()>;
|
|
|
|
fn close_read(&mut self) -> IoResult<()>;
|
2014-04-25 22:47:49 -05:00
|
|
|
fn set_timeout(&mut self, timeout_ms: Option<u64>);
|
|
|
|
fn set_read_timeout(&mut self, timeout_ms: Option<u64>);
|
|
|
|
fn set_write_timeout(&mut self, timeout_ms: Option<u64>);
|
2013-09-16 17:28:56 -05:00
|
|
|
}
|
2013-10-15 21:44:08 -05:00
|
|
|
|
|
|
|
pub trait RtioUnixListener {
|
2014-07-23 12:21:50 -05:00
|
|
|
fn listen(self: Box<Self>) -> IoResult<Box<RtioUnixAcceptor + Send>>;
|
2013-10-15 21:44:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RtioUnixAcceptor {
|
2014-06-14 13:03:34 -05:00
|
|
|
fn accept(&mut self) -> IoResult<Box<RtioPipe + Send>>;
|
2014-04-22 20:38:59 -05:00
|
|
|
fn set_timeout(&mut self, timeout: Option<u64>);
|
2014-07-11 16:29:15 -05:00
|
|
|
fn clone(&self) -> Box<RtioUnixAcceptor + Send>;
|
|
|
|
fn close_accept(&mut self) -> IoResult<()>;
|
2013-10-15 21:44:08 -05:00
|
|
|
}
|
2013-10-16 13:47:12 -05:00
|
|
|
|
|
|
|
pub trait RtioTTY {
|
2014-03-08 20:21:49 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
|
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
|
|
|
|
fn set_raw(&mut self, raw: bool) -> IoResult<()>;
|
|
|
|
fn get_winsize(&mut self) -> IoResult<(int, int)>;
|
2013-11-18 18:26:03 -06:00
|
|
|
fn isatty(&self) -> bool;
|
2013-10-16 13:47:12 -05:00
|
|
|
}
|
2013-10-16 16:48:05 -05:00
|
|
|
|
2013-12-14 23:29:17 -06:00
|
|
|
pub trait PausableIdleCallback {
|
2013-10-16 16:48:05 -05:00
|
|
|
fn pause(&mut self);
|
|
|
|
fn resume(&mut self);
|
|
|
|
}
|
2013-09-18 23:03:50 -05:00
|
|
|
|
|
|
|
pub trait RtioSignal {}
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 21:11:49 -05:00
|
|
|
|
2014-10-15 02:24:37 -05:00
|
|
|
#[deriving(Show)]
|
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 IoError {
|
|
|
|
pub code: uint,
|
|
|
|
pub extra: uint,
|
|
|
|
pub detail: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type IoResult<T> = Result<T, IoError>;
|
|
|
|
|
|
|
|
#[deriving(PartialEq, Eq)]
|
|
|
|
pub enum IpAddr {
|
|
|
|
Ipv4Addr(u8, u8, u8, u8),
|
|
|
|
Ipv6Addr(u16, u16, u16, u16, u16, u16, u16, u16),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Show for IpAddr {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Ipv4Addr(a, b, c, d) => write!(fmt, "{}.{}.{}.{}", a, b, c, d),
|
|
|
|
Ipv6Addr(a, b, c, d, e, f, g, h) => {
|
|
|
|
write!(fmt,
|
|
|
|
"{:04x}:{:04x}:{:04x}:{:04x}:{:04x}:{:04x}:{:04x}:{:04x}",
|
|
|
|
a, b, c, d, e, f, g, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(PartialEq, Eq)]
|
|
|
|
pub struct SocketAddr {
|
|
|
|
pub ip: IpAddr,
|
|
|
|
pub port: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum StdioContainer {
|
|
|
|
Ignored,
|
|
|
|
InheritFd(i32),
|
|
|
|
CreatePipe(bool, bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ProcessExit {
|
|
|
|
ExitStatus(int),
|
|
|
|
ExitSignal(int),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum FileMode {
|
|
|
|
Open,
|
|
|
|
Append,
|
|
|
|
Truncate,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum FileAccess {
|
|
|
|
Read,
|
|
|
|
Write,
|
|
|
|
ReadWrite,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FileStat {
|
|
|
|
pub size: u64,
|
|
|
|
pub kind: u64,
|
|
|
|
pub perm: u64,
|
|
|
|
pub created: u64,
|
|
|
|
pub modified: u64,
|
|
|
|
pub accessed: u64,
|
|
|
|
pub device: u64,
|
|
|
|
pub inode: u64,
|
|
|
|
pub rdev: u64,
|
|
|
|
pub nlink: u64,
|
|
|
|
pub uid: u64,
|
|
|
|
pub gid: u64,
|
|
|
|
pub blksize: u64,
|
|
|
|
pub blocks: u64,
|
|
|
|
pub flags: u64,
|
|
|
|
pub gen: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum SeekStyle {
|
|
|
|
SeekSet,
|
|
|
|
SeekEnd,
|
|
|
|
SeekCur,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AddrinfoHint {
|
|
|
|
pub family: uint,
|
|
|
|
pub socktype: uint,
|
|
|
|
pub protocol: uint,
|
|
|
|
pub flags: uint,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AddrinfoInfo {
|
|
|
|
pub address: SocketAddr,
|
|
|
|
pub family: uint,
|
|
|
|
pub socktype: uint,
|
|
|
|
pub protocol: uint,
|
|
|
|
pub flags: uint,
|
|
|
|
}
|