Skip to content

Commit 1c4af5e

Browse files
committed
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.
1 parent 3dc38b0 commit 1c4af5e

File tree

6 files changed

+27
-32
lines changed

6 files changed

+27
-32
lines changed

src/libnative/io/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ fn mkerr_winbool(ret: libc::c_int) -> IoResult<()> {
111111
pub struct IoFactory;
112112

113113
impl rtio::IoFactory for IoFactory {
114-
// all native io factories are the same
115-
fn id(&self) -> uint { 0 }
116-
117114
// networking
118115
fn tcp_connect(&mut self, _addr: SocketAddr) -> IoResult<~RtioTcpStream> {
119116
Err(unimpl())

src/librustuv/homing.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
3434
#[allow(dead_code)];
3535

36+
use std::cast;
3637
use std::rt::local::Local;
3738
use std::rt::rtio::LocalIo;
3839
use std::rt::task::{Task, BlockedTask};
@@ -70,6 +71,17 @@ impl Clone for HomeHandle {
7071
}
7172
}
7273

74+
pub fn local_id() -> uint {
75+
let mut io = match LocalIo::borrow() {
76+
Some(io) => io, None => return 0,
77+
};
78+
let io = io.get();
79+
unsafe {
80+
let (_vtable, ptr): (uint, uint) = cast::transmute(io);
81+
return ptr;
82+
}
83+
}
84+
7385
pub trait HomingIO {
7486
fn home<'r>(&'r mut self) -> &'r mut HomeHandle;
7587

@@ -79,35 +91,26 @@ pub trait HomingIO {
7991
fn go_to_IO_home(&mut self) -> uint {
8092
let _f = ForbidUnwind::new("going home");
8193

82-
let mut cur_task: ~Task = Local::take();
83-
let cur_loop_id = {
84-
let mut io = cur_task.local_io().expect("libuv must have I/O");
85-
io.get().id()
86-
};
94+
let cur_loop_id = local_id();
95+
let destination = self.home().id;
8796

8897
// Try at all costs to avoid the homing operation because it is quite
8998
// expensive. Hence, we only deschedule/send if we're not on the correct
9099
// event loop. If we're already on the home event loop, then we're good
91100
// to go (remember we have no preemption, so we're guaranteed to stay on
92101
// this event loop as long as we avoid the scheduler).
93-
if cur_loop_id != self.home().id {
102+
if cur_loop_id != destination {
103+
let cur_task: ~Task = Local::take();
94104
cur_task.deschedule(1, |task| {
95105
self.home().send(task);
96106
Ok(())
97107
});
98108

99109
// Once we wake up, assert that we're in the right location
100-
let cur_loop_id = {
101-
let mut io = LocalIo::borrow().expect("libuv must have I/O");
102-
io.get().id()
103-
};
104-
assert_eq!(cur_loop_id, self.home().id);
105-
106-
cur_loop_id
107-
} else {
108-
Local::put(cur_task);
109-
cur_loop_id
110+
assert_eq!(local_id(), destination);
110111
}
112+
113+
return destination;
111114
}
112115

113116
/// Fires a single homing missile, returning another missile targeted back
@@ -130,8 +133,7 @@ impl HomingMissile {
130133
/// Check at runtime that the task has *not* transplanted itself to a
131134
/// different I/O loop while executing.
132135
pub fn check(&self, msg: &'static str) {
133-
let mut io = LocalIo::borrow().expect("libuv must have I/O");
134-
assert!(io.get().id() == self.io_home, "{}", msg);
136+
assert!(local_id() == self.io_home, "{}", msg);
135137
}
136138
}
137139

src/librustuv/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ use std::ptr::null;
5353
use std::ptr;
5454
use std::rt::local::Local;
5555
use std::rt::task::{BlockedTask, Task};
56-
use std::rt::rtio::LocalIo;
5756
use std::str::raw::from_c_str;
5857
use std::str;
5958
use std::task;
@@ -161,18 +160,16 @@ pub struct ForbidSwitch {
161160

162161
impl ForbidSwitch {
163162
fn new(s: &'static str) -> ForbidSwitch {
164-
let mut io = LocalIo::borrow().expect("libuv must have local I/O");
165163
ForbidSwitch {
166164
msg: s,
167-
io: io.get().id(),
165+
io: homing::local_id(),
168166
}
169167
}
170168
}
171169

172170
impl Drop for ForbidSwitch {
173171
fn drop(&mut self) {
174-
let mut io = LocalIo::borrow().expect("libuv must have local I/O");
175-
assert!(self.io == io.get().id(),
172+
assert!(self.io == homing::local_id(),
176173
"didnt want a scheduler switch: {}",
177174
self.msg);
178175
}

src/librustuv/uvio.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,14 @@ impl UvIoFactory {
132132
pub fn uv_loop<'a>(&mut self) -> *uvll::uv_loop_t { self.loop_.handle }
133133

134134
pub fn make_handle(&mut self) -> HomeHandle {
135-
HomeHandle::new(self.id(), &mut **self.handle_pool.get_mut_ref())
135+
// It's understood by the homing code that the "local id" is just the
136+
// pointer of the local I/O factory cast to a uint.
137+
let id: uint = unsafe { cast::transmute_copy(&self) };
138+
HomeHandle::new(id, &mut **self.handle_pool.get_mut_ref())
136139
}
137140
}
138141

139142
impl IoFactory for UvIoFactory {
140-
fn id(&self) -> uint { unsafe { cast::transmute(self) } }
141-
142143
// Connect to an address and return a new stream
143144
// NB: This blocks the task waiting on the connection.
144145
// It would probably be better to return a future

src/librustuv/uvll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::libc;
3838
use std::libc::uintptr_t;
3939

4040
pub use self::errors::{EACCES, ECONNREFUSED, ECONNRESET, EPIPE, ECONNABORTED,
41-
ECANCELED, EBADF, ENOTCONN};
41+
ECANCELED, EBADF, ENOTCONN, ENOENT};
4242

4343
pub static OK: c_int = 0;
4444
pub static EOF: c_int = -4095;

src/libstd/rt/rtio.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ impl<'a> LocalIo<'a> {
150150
}
151151

152152
pub trait IoFactory {
153-
fn id(&self) -> uint;
154-
155153
// networking
156154
fn tcp_connect(&mut self, addr: SocketAddr) -> Result<~RtioTcpStream, IoError>;
157155
fn tcp_bind(&mut self, addr: SocketAddr) -> Result<~RtioTcpListener, IoError>;

0 commit comments

Comments
 (0)