This is part of the overall strategy I would like to take when approaching issue #11165. The only two I/O objects that reasonably want to be "split" are the network stream objects. Everything else can be "split" by just creating another version. The initial idea I had was the literally split the object into a reader and a writer half, but that would just introduce lots of clutter with extra interfaces that were a little unnnecssary, or it would return a ~Reader and a ~Writer which means you couldn't access things like the remote peer name or local socket name. The solution I found to be nicer was to just clone the stream itself. The clone is just a clone of the handle, nothing fancy going on at the kernel level. Conceptually I found this very easy to wrap my head around (everything else supports clone()), and it solved the "split" problem at the same time. The cloning support is pretty specific per platform/lib combination: * native/win32 - uses some specific WSA apis to clone the SOCKET handle * native/unix - uses dup() to get another file descriptor * green/all - This is where things get interesting. When we support full clones of a handle, this implies that we're allowing simultaneous writes and reads to happen. It turns out that libuv doesn't support two simultaneous reads or writes of the same object. It does support *one* read and *one* write at the same time, however. Some extra infrastructure was added to just block concurrent writers/readers until the previous read/write operation was completed. I've added tests to the tcp/unix modules to make sure that this functionality is supported everywhere.
110 lines
3.3 KiB
Rust
110 lines
3.3 KiB
Rust
// Copyright 2014 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.
|
|
|
|
/// An exclusive access primitive
|
|
///
|
|
/// This primitive is used to gain exclusive access to read() and write() in uv.
|
|
/// It is assumed that all invocations of this struct happen on the same thread
|
|
/// (the uv event loop).
|
|
|
|
use std::cast;
|
|
use std::sync::arc::UnsafeArc;
|
|
use std::rt::task::{BlockedTask, Task};
|
|
use std::rt::local::Local;
|
|
|
|
use homing::HomingMissile;
|
|
|
|
pub struct Access {
|
|
priv inner: UnsafeArc<Inner>,
|
|
}
|
|
|
|
pub struct Guard<'a> {
|
|
priv access: &'a mut Access,
|
|
priv missile: Option<HomingMissile>,
|
|
}
|
|
|
|
struct Inner {
|
|
queue: ~[BlockedTask],
|
|
held: bool,
|
|
}
|
|
|
|
impl Access {
|
|
pub fn new() -> Access {
|
|
Access {
|
|
inner: UnsafeArc::new(Inner {
|
|
queue: ~[],
|
|
held: false,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub fn grant<'a>(&'a mut self, missile: HomingMissile) -> Guard<'a> {
|
|
// This unsafety is actually OK because the homing missile argument
|
|
// guarantees that we're on the same event loop as all the other objects
|
|
// attempting to get access granted.
|
|
let inner: &mut Inner = unsafe { cast::transmute(self.inner.get()) };
|
|
|
|
if inner.held {
|
|
let t: ~Task = Local::take();
|
|
t.deschedule(1, |task| {
|
|
inner.queue.push(task);
|
|
Ok(())
|
|
});
|
|
assert!(inner.held);
|
|
} else {
|
|
inner.held = true;
|
|
}
|
|
|
|
Guard { access: self, missile: Some(missile) }
|
|
}
|
|
}
|
|
|
|
impl Clone for Access {
|
|
fn clone(&self) -> Access {
|
|
Access { inner: self.inner.clone() }
|
|
}
|
|
}
|
|
|
|
#[unsafe_destructor]
|
|
impl<'a> Drop for Guard<'a> {
|
|
fn drop(&mut self) {
|
|
// This guard's homing missile is still armed, so we're guaranteed to be
|
|
// on the same I/O event loop, so this unsafety should be ok.
|
|
assert!(self.missile.is_some());
|
|
let inner: &mut Inner = unsafe {
|
|
cast::transmute(self.access.inner.get())
|
|
};
|
|
|
|
match inner.queue.shift() {
|
|
// Here we have found a task that was waiting for access, and we
|
|
// current have the "access lock" we need to relinquish access to
|
|
// this sleeping task.
|
|
//
|
|
// To do so, we first drop out homing missile and we then reawaken
|
|
// the task. In reawakening the task, it will be immediately
|
|
// scheduled on this scheduler. Because we might be woken up on some
|
|
// other scheduler, we drop our homing missile before we reawaken
|
|
// the task.
|
|
Some(task) => {
|
|
drop(self.missile.take());
|
|
let _ = task.wake().map(|t| t.reawaken());
|
|
}
|
|
None => { inner.held = false; }
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for Inner {
|
|
fn drop(&mut self) {
|
|
assert!(!self.held);
|
|
assert_eq!(self.queue.len(), 0);
|
|
}
|
|
}
|