rustuv: Remove the id() function from IoFactory

The only user of this was the homing code in librustuv, and it just manually
does the cast from a pointer to a uint now.
This commit is contained in:
Alex Crichton 2013-12-18 10:14:44 -08:00
parent 3dc38b0c51
commit 1c4af5e3d9
6 changed files with 27 additions and 32 deletions

View file

@ -111,9 +111,6 @@ fn mkerr_winbool(ret: libc::c_int) -> IoResult<()> {
pub struct IoFactory;
impl rtio::IoFactory for IoFactory {
// all native io factories are the same
fn id(&self) -> uint { 0 }
// networking
fn tcp_connect(&mut self, _addr: SocketAddr) -> IoResult<~RtioTcpStream> {
Err(unimpl())

View file

@ -33,6 +33,7 @@
#[allow(dead_code)];
use std::cast;
use std::rt::local::Local;
use std::rt::rtio::LocalIo;
use std::rt::task::{Task, BlockedTask};
@ -70,6 +71,17 @@ impl Clone for HomeHandle {
}
}
pub fn local_id() -> uint {
let mut io = match LocalIo::borrow() {
Some(io) => io, None => return 0,
};
let io = io.get();
unsafe {
let (_vtable, ptr): (uint, uint) = cast::transmute(io);
return ptr;
}
}
pub trait HomingIO {
fn home<'r>(&'r mut self) -> &'r mut HomeHandle;
@ -79,35 +91,26 @@ pub trait HomingIO {
fn go_to_IO_home(&mut self) -> uint {
let _f = ForbidUnwind::new("going home");
let mut cur_task: ~Task = Local::take();
let cur_loop_id = {
let mut io = cur_task.local_io().expect("libuv must have I/O");
io.get().id()
};
let cur_loop_id = local_id();
let destination = self.home().id;
// Try at all costs to avoid the homing operation because it is quite
// expensive. Hence, we only deschedule/send if we're not on the correct
// event loop. If we're already on the home event loop, then we're good
// to go (remember we have no preemption, so we're guaranteed to stay on
// this event loop as long as we avoid the scheduler).
if cur_loop_id != self.home().id {
if cur_loop_id != destination {
let cur_task: ~Task = Local::take();
cur_task.deschedule(1, |task| {
self.home().send(task);
Ok(())
});
// Once we wake up, assert that we're in the right location
let cur_loop_id = {
let mut io = LocalIo::borrow().expect("libuv must have I/O");
io.get().id()
};
assert_eq!(cur_loop_id, self.home().id);
cur_loop_id
} else {
Local::put(cur_task);
cur_loop_id
assert_eq!(local_id(), destination);
}
return destination;
}
/// Fires a single homing missile, returning another missile targeted back
@ -130,8 +133,7 @@ impl HomingMissile {
/// Check at runtime that the task has *not* transplanted itself to a
/// different I/O loop while executing.
pub fn check(&self, msg: &'static str) {
let mut io = LocalIo::borrow().expect("libuv must have I/O");
assert!(io.get().id() == self.io_home, "{}", msg);
assert!(local_id() == self.io_home, "{}", msg);
}
}

View file

@ -53,7 +53,6 @@ use std::ptr::null;
use std::ptr;
use std::rt::local::Local;
use std::rt::task::{BlockedTask, Task};
use std::rt::rtio::LocalIo;
use std::str::raw::from_c_str;
use std::str;
use std::task;
@ -161,18 +160,16 @@ pub struct ForbidSwitch {
impl ForbidSwitch {
fn new(s: &'static str) -> ForbidSwitch {
let mut io = LocalIo::borrow().expect("libuv must have local I/O");
ForbidSwitch {
msg: s,
io: io.get().id(),
io: homing::local_id(),
}
}
}
impl Drop for ForbidSwitch {
fn drop(&mut self) {
let mut io = LocalIo::borrow().expect("libuv must have local I/O");
assert!(self.io == io.get().id(),
assert!(self.io == homing::local_id(),
"didnt want a scheduler switch: {}",
self.msg);
}

View file

@ -132,13 +132,14 @@ impl UvIoFactory {
pub fn uv_loop<'a>(&mut self) -> *uvll::uv_loop_t { self.loop_.handle }
pub fn make_handle(&mut self) -> HomeHandle {
HomeHandle::new(self.id(), &mut **self.handle_pool.get_mut_ref())
// It's understood by the homing code that the "local id" is just the
// pointer of the local I/O factory cast to a uint.
let id: uint = unsafe { cast::transmute_copy(&self) };
HomeHandle::new(id, &mut **self.handle_pool.get_mut_ref())
}
}
impl IoFactory for UvIoFactory {
fn id(&self) -> uint { unsafe { cast::transmute(self) } }
// Connect to an address and return a new stream
// NB: This blocks the task waiting on the connection.
// It would probably be better to return a future

View file

@ -38,7 +38,7 @@ use std::libc;
use std::libc::uintptr_t;
pub use self::errors::{EACCES, ECONNREFUSED, ECONNRESET, EPIPE, ECONNABORTED,
ECANCELED, EBADF, ENOTCONN};
ECANCELED, EBADF, ENOTCONN, ENOENT};
pub static OK: c_int = 0;
pub static EOF: c_int = -4095;

View file

@ -150,8 +150,6 @@ impl<'a> LocalIo<'a> {
}
pub trait IoFactory {
fn id(&self) -> uint;
// networking
fn tcp_connect(&mut self, addr: SocketAddr) -> Result<~RtioTcpStream, IoError>;
fn tcp_bind(&mut self, addr: SocketAddr) -> Result<~RtioTcpListener, IoError>;