Skip to content

Commit b2c6d6f

Browse files
committed
rustuv: Implement timeouts for unix networking
This commit implements the set{,_read,_write}_timeout() methods for the libuv-based networking I/O objects. The implementation details are commented thoroughly throughout the implementation.
1 parent 295e0a0 commit b2c6d6f

File tree

8 files changed

+711
-277
lines changed

8 files changed

+711
-277
lines changed

src/librustuv/access.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct Guard<'a> {
3131
}
3232

3333
struct Inner {
34-
queue: Vec<BlockedTask>,
34+
queue: Vec<(BlockedTask, uint)>,
3535
held: bool,
3636
closed: bool,
3737
}
@@ -47,16 +47,17 @@ impl Access {
4747
}
4848
}
4949

50-
pub fn grant<'a>(&'a mut self, missile: HomingMissile) -> Guard<'a> {
50+
pub fn grant<'a>(&'a mut self, token: uint,
51+
missile: HomingMissile) -> Guard<'a> {
5152
// This unsafety is actually OK because the homing missile argument
5253
// guarantees that we're on the same event loop as all the other objects
5354
// attempting to get access granted.
54-
let inner: &mut Inner = unsafe { cast::transmute(self.inner.get()) };
55+
let inner: &mut Inner = unsafe { &mut *self.inner.get() };
5556

5657
if inner.held {
5758
let t: Box<Task> = Local::take();
5859
t.deschedule(1, |task| {
59-
inner.queue.push(task);
60+
inner.queue.push((task, token));
6061
Ok(())
6162
});
6263
assert!(inner.held);
@@ -75,6 +76,17 @@ impl Access {
7576
// necessary synchronization to be running on this thread.
7677
unsafe { (*self.inner.get()).closed = true; }
7778
}
79+
80+
// Dequeue a blocked task with a specified token. This is unsafe because it
81+
// is only safe to invoke while on the home event loop, and there is no
82+
// guarantee that this i being invoked on the home event loop.
83+
pub unsafe fn dequeue(&mut self, token: uint) -> Option<BlockedTask> {
84+
let inner: &mut Inner = &mut *self.inner.get();
85+
match inner.queue.iter().position(|&(_, t)| t == token) {
86+
Some(i) => Some(inner.queue.remove(i).unwrap().val0()),
87+
None => None,
88+
}
89+
}
7890
}
7991

8092
impl Clone for Access {
@@ -111,9 +123,9 @@ impl<'a> Drop for Guard<'a> {
111123
// scheduled on this scheduler. Because we might be woken up on some
112124
// other scheduler, we drop our homing missile before we reawaken
113125
// the task.
114-
Some(task) => {
126+
Some((task, _)) => {
115127
drop(self.missile.take());
116-
let _ = task.wake().map(|t| t.reawaken());
128+
task.reawaken();
117129
}
118130
None => { inner.held = false; }
119131
}

src/librustuv/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ fn start(argc: int, argv: **u8) -> int {
8484
mod macros;
8585

8686
mod access;
87+
mod timeout;
8788
mod homing;
8889
mod queue;
8990
mod rc;

0 commit comments

Comments
 (0)