Skip to content

Commit

Permalink
Runtime removal: fully remove rtio
Browse files Browse the repository at this point in the history
This patch cleans up the remnants of the runtime IO interface.

Because this eliminates APIs in `libnative` and `librustrt`, it is a:

[breaking-change]

This functionality is likely to be available publicly, in some form,
from `std` in the future.
  • Loading branch information
aturon committed Nov 8, 2014
1 parent 67b123b commit 2c9bb3f
Show file tree
Hide file tree
Showing 11 changed files with 11 additions and 276 deletions.
4 changes: 1 addition & 3 deletions src/libgreen/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use alloc::arc::Arc;
use std::sync::atomic;
use std::mem;
use std::rt::rtio::{EventLoop, IoFactory, RemoteCallback};
use std::rt::rtio::{EventLoop, RemoteCallback};
use std::rt::rtio::{PausableIdleCallback, Callback};
use std::rt::exclusive::Exclusive;

Expand Down Expand Up @@ -150,8 +150,6 @@ impl EventLoop for BasicLoop {
Box<RemoteCallback + Send>
}

fn io<'a>(&'a mut self) -> Option<&'a mut IoFactory> { None }

fn has_active_io(&self) -> bool { false }
}

Expand Down
4 changes: 2 additions & 2 deletions src/libgreen/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::mem;
use std::rt::Runtime;
use std::rt::local::Local;
use std::rt::mutex::NativeMutex;
use std::rt::rtio;
use std::rt::task::{Task, BlockedTask, TaskOpts};

struct SimpleTask {
Expand Down Expand Up @@ -79,9 +78,10 @@ impl Runtime for SimpleTask {
_f: proc():Send) {
panic!()
}
fn local_io<'a>(&'a mut self) -> Option<rtio::LocalIo<'a>> { None }

fn stack_bounds(&self) -> (uint, uint) { panic!() }
fn stack_guard(&self) -> Option<uint> { panic!() }

fn can_block(&self) -> bool { true }
fn wrap(self: Box<SimpleTask>) -> Box<Any+'static> { panic!() }
}
Expand Down
9 changes: 0 additions & 9 deletions src/libgreen/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::raw;
use std::rt::Runtime;
use std::rt::local::Local;
use std::rt::mutex::NativeMutex;
use std::rt::rtio;
use std::rt::stack;
use std::rt::task::{Task, BlockedTask, TaskOpts};
use std::rt;
Expand Down Expand Up @@ -468,14 +467,6 @@ impl Runtime for GreenTask {
sched.run_task(me, sibling)
}

// Local I/O is provided by the scheduler's event loop
fn local_io<'a>(&'a mut self) -> Option<rtio::LocalIo<'a>> {
match self.sched.as_mut().unwrap().event_loop.io() {
Some(io) => Some(rtio::LocalIo::new(io)),
None => None,
}
}

fn stack_bounds(&self) -> (uint, uint) {
let c = self.coroutine.as_ref()
.expect("GreenTask.stack_bounds called without a coroutine");
Expand Down
102 changes: 0 additions & 102 deletions src/libnative/io/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/libnative/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ use std::str;

pub use task::NativeTaskBuilder;

pub mod io;
pub mod task;

#[cfg(any(windows, android))]
Expand Down
8 changes: 0 additions & 8 deletions src/libnative/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ use std::mem;
use std::rt::bookkeeping;
use std::rt::local::Local;
use std::rt::mutex::NativeMutex;
use std::rt::rtio;
use std::rt::stack;
use std::rt::task::{Task, BlockedTask, TaskOpts};
use std::rt::thread::Thread;
use std::rt;

use io;
use std::task::{TaskBuilder, Spawner};

/// Creates a new Task which is ready to execute as a 1:1 task.
Expand All @@ -42,7 +40,6 @@ fn ops() -> Box<Ops> {
box Ops {
lock: unsafe { NativeMutex::new() },
awoken: false,
io: io::IoFactory::new(),
// these *should* get overwritten
stack_bounds: (0, 0),
stack_guard: 0
Expand Down Expand Up @@ -112,7 +109,6 @@ impl<S: Spawner> NativeTaskBuilder for TaskBuilder<S> {
struct Ops {
lock: NativeMutex, // native synchronization
awoken: bool, // used to prevent spurious wakeups
io: io::IoFactory, // local I/O factory

// This field holds the known bounds of the stack in (lo, hi) form. Not all
// native tasks necessarily know their precise bounds, hence this is
Expand Down Expand Up @@ -272,10 +268,6 @@ impl rt::Runtime for Ops {

NativeSpawner.spawn(opts, f);
}

fn local_io<'a>(&'a mut self) -> Option<rtio::LocalIo<'a>> {
Some(rtio::LocalIo::new(&mut self.io as &mut rtio::IoFactory))
}
}

#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion src/librustrt/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ pub trait Runtime {
cur_task: Box<Task>,
opts: TaskOpts,
f: proc():Send);
fn local_io<'a>(&'a mut self) -> Option<rtio::LocalIo<'a>>;
/// The (low, high) edges of the current stack.
fn stack_bounds(&self) -> (uint, uint); // (lo, hi)
/// The last writable byte of the stack next to the guard page
Expand Down
130 changes: 1 addition & 129 deletions src/librustrt/rtio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
use core::prelude::*;
use alloc::boxed::Box;
use collections::string::String;
use core::mem;
use libc::c_int;

use local::Local;
use task::Task;

pub trait EventLoop {
fn run(&mut self);
Expand All @@ -27,8 +21,7 @@ pub trait EventLoop {
fn remote_callback(&mut self, Box<Callback + Send>)
-> Box<RemoteCallback + Send>;

/// The asynchronous I/O services. Not all event loops may provide one.
fn io<'a>(&'a mut self) -> Option<&'a mut IoFactory>;
// last vestige of IoFactory
fn has_active_io(&self) -> bool;
}

Expand All @@ -46,128 +39,7 @@ pub trait RemoteCallback {
fn fire(&mut self);
}

pub struct LocalIo<'a> {
factory: &'a mut IoFactory+'a,
}

#[unsafe_destructor]
impl<'a> Drop for LocalIo<'a> {
fn drop(&mut self) {
// FIXME(pcwalton): Do nothing here for now, but eventually we may want
// something. For now this serves to make `LocalIo` noncopyable.
}
}

impl<'a> LocalIo<'a> {
/// Returns the local I/O: either the local scheduler's I/O services or
/// the native I/O services.
pub fn borrow() -> Option<LocalIo<'a>> {
// 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).
//
// 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).
let mut t: Box<Task> = match Local::try_take() {
Some(t) => t,
None => return None,
};
let ret = t.local_io().map(|t| {
unsafe { mem::transmute_copy(&t) }
});
Local::put(t);
return ret;
}

pub fn maybe_raise<T>(f: |io: &mut IoFactory| -> IoResult<T>)
-> IoResult<T>
{
#[cfg(unix)] use libc::EINVAL as ERROR;
#[cfg(windows)] use libc::ERROR_CALL_NOT_IMPLEMENTED as ERROR;
match LocalIo::borrow() {
Some(mut io) => f(io.get()),
None => Err(IoError {
code: ERROR as uint,
extra: 0,
detail: None,
}),
}
}

pub fn new<'a>(io: &'a mut IoFactory+'a) -> LocalIo<'a> {
LocalIo { factory: io }
}

/// Returns the underlying I/O factory as a trait reference.
#[inline]
pub fn get<'a>(&'a mut self) -> &'a mut IoFactory {
let f: &'a mut IoFactory = self.factory;
f
}
}

pub trait IoFactory {
fn timer_init(&mut self) -> IoResult<Box<RtioTimer + Send>>;
fn tty_open(&mut self, fd: c_int, readable: bool)
-> IoResult<Box<RtioTTY + Send>>;
}

pub trait RtioTimer {
fn sleep(&mut self, msecs: u64);
fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>);
fn period(&mut self, msecs: u64, cb: Box<Callback + Send>);
}

pub trait RtioPipe {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
fn write(&mut self, buf: &[u8]) -> IoResult<()>;
fn clone(&self) -> Box<RtioPipe + Send>;

fn close_write(&mut self) -> IoResult<()>;
fn close_read(&mut self) -> IoResult<()>;
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>);
}

pub trait RtioUnixListener {
fn listen(self: Box<Self>) -> IoResult<Box<RtioUnixAcceptor + Send>>;
}

pub trait RtioUnixAcceptor {
fn accept(&mut self) -> IoResult<Box<RtioPipe + Send>>;
fn set_timeout(&mut self, timeout: Option<u64>);
fn clone(&self) -> Box<RtioUnixAcceptor + Send>;
fn close_accept(&mut self) -> IoResult<()>;
}

pub trait RtioTTY {
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)>;
fn isatty(&self) -> bool;
}

pub trait PausableIdleCallback {
fn pause(&mut self);
fn resume(&mut self);
}

pub trait RtioSignal {}

#[deriving(Show)]
pub struct IoError {
pub code: uint,
pub extra: uint,
pub detail: Option<String>,
}

pub type IoResult<T> = Result<T, IoError>;
8 changes: 0 additions & 8 deletions src/librustrt/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use core::raw;
use local_data;
use Runtime;
use local::Local;
use rtio::LocalIo;
use unwind;
use unwind::Unwinder;
use collections::str::SendStr;
Expand Down Expand Up @@ -421,13 +420,6 @@ impl Task {
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.as_mut().unwrap().local_io()
}

/// 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`.
Expand Down
Loading

4 comments on commit 2c9bb3f

@bors
Copy link
Contributor

@bors bors commented on 2c9bb3f Nov 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at aturon@2c9bb3f

@bors
Copy link
Contributor

@bors bors commented on 2c9bb3f Nov 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging aturon/rust/io-removal = 2c9bb3f into auto

@bors
Copy link
Contributor

@bors bors commented on 2c9bb3f Nov 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aturon/rust/io-removal = 2c9bb3f merged ok, testing candidate = 62ff64b

Please sign in to comment.