2013-05-04 19:30:31 -05:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
|
|
|
//! A very simple unsynchronized channel type for sending buffered data from
|
|
|
|
//! scheduler context to task context.
|
|
|
|
//!
|
|
|
|
//! XXX: This would be safer to use if split into two types like Port/Chan
|
|
|
|
|
|
|
|
use option::*;
|
|
|
|
use clone::Clone;
|
|
|
|
use super::rc::RC;
|
2013-06-26 18:41:00 -05:00
|
|
|
use rt::sched::Scheduler;
|
2013-05-04 19:30:31 -05:00
|
|
|
use rt::{context, TaskContext, SchedulerContext};
|
2013-05-19 17:45:39 -05:00
|
|
|
use rt::local::Local;
|
2013-06-26 18:41:00 -05:00
|
|
|
use rt::task::Task;
|
2013-05-14 17:30:01 -05:00
|
|
|
use vec::OwnedVector;
|
|
|
|
use container::Container;
|
2013-05-04 19:30:31 -05:00
|
|
|
|
|
|
|
struct TubeState<T> {
|
2013-06-26 18:41:00 -05:00
|
|
|
blocked_task: Option<~Task>,
|
2013-05-04 19:30:31 -05:00
|
|
|
buf: ~[T]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Tube<T> {
|
|
|
|
p: RC<TubeState<T>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Tube<T> {
|
|
|
|
pub fn new() -> Tube<T> {
|
|
|
|
Tube {
|
|
|
|
p: RC::new(TubeState {
|
|
|
|
blocked_task: None,
|
|
|
|
buf: ~[]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send(&mut self, val: T) {
|
|
|
|
rtdebug!("tube send");
|
|
|
|
assert!(context() == SchedulerContext);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let state = self.p.unsafe_borrow_mut();
|
|
|
|
(*state).buf.push(val);
|
|
|
|
|
|
|
|
if (*state).blocked_task.is_some() {
|
|
|
|
// There's a waiting task. Wake it up
|
|
|
|
rtdebug!("waking blocked tube");
|
|
|
|
let task = (*state).blocked_task.swap_unwrap();
|
2013-05-19 17:45:39 -05:00
|
|
|
let sched = Local::take::<Scheduler>();
|
2013-05-04 19:30:31 -05:00
|
|
|
sched.resume_task_immediately(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recv(&mut self) -> T {
|
|
|
|
assert!(context() == TaskContext);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let state = self.p.unsafe_borrow_mut();
|
|
|
|
if !(*state).buf.is_empty() {
|
|
|
|
return (*state).buf.shift();
|
|
|
|
} else {
|
|
|
|
// Block and wait for the next message
|
|
|
|
rtdebug!("blocking on tube recv");
|
|
|
|
assert!(self.p.refcount() > 1); // There better be somebody to wake us up
|
|
|
|
assert!((*state).blocked_task.is_none());
|
2013-05-19 17:45:39 -05:00
|
|
|
let sched = Local::take::<Scheduler>();
|
2013-05-29 19:52:00 -05:00
|
|
|
do sched.deschedule_running_task_and_then |_, task| {
|
2013-05-04 19:30:31 -05:00
|
|
|
(*state).blocked_task = Some(task);
|
|
|
|
}
|
|
|
|
rtdebug!("waking after tube recv");
|
|
|
|
let buf = &mut (*state).buf;
|
|
|
|
assert!(!buf.is_empty());
|
|
|
|
return buf.shift();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for Tube<T> {
|
|
|
|
fn clone(&self) -> Tube<T> {
|
|
|
|
Tube { p: self.p.clone() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use int;
|
|
|
|
use cell::Cell;
|
|
|
|
use rt::test::*;
|
|
|
|
use rt::rtio::EventLoop;
|
2013-05-19 17:45:39 -05:00
|
|
|
use rt::sched::Scheduler;
|
|
|
|
use rt::local::Local;
|
2013-05-04 19:30:31 -05:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_test() {
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let mut tube: Tube<int> = Tube::new();
|
|
|
|
let tube_clone = tube.clone();
|
2013-06-04 05:03:58 -05:00
|
|
|
let tube_clone_cell = Cell::new(tube_clone);
|
2013-05-19 17:45:39 -05:00
|
|
|
let sched = Local::take::<Scheduler>();
|
2013-05-29 19:52:00 -05:00
|
|
|
do sched.deschedule_running_task_and_then |sched, task| {
|
2013-05-04 19:30:31 -05:00
|
|
|
let mut tube_clone = tube_clone_cell.take();
|
|
|
|
tube_clone.send(1);
|
2013-05-29 19:52:00 -05:00
|
|
|
sched.enqueue_task(task);
|
2013-05-04 19:30:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
assert!(tube.recv() == 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn blocking_test() {
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let mut tube: Tube<int> = Tube::new();
|
|
|
|
let tube_clone = tube.clone();
|
2013-06-16 04:03:37 -05:00
|
|
|
let tube_clone = Cell::new(tube_clone);
|
2013-05-19 17:45:39 -05:00
|
|
|
let sched = Local::take::<Scheduler>();
|
2013-05-29 19:52:00 -05:00
|
|
|
do sched.deschedule_running_task_and_then |sched, task| {
|
2013-06-16 04:03:37 -05:00
|
|
|
let tube_clone = Cell::new(tube_clone.take());
|
2013-05-29 19:52:00 -05:00
|
|
|
do sched.event_loop.callback {
|
|
|
|
let mut tube_clone = tube_clone.take();
|
|
|
|
// The task should be blocked on this now and
|
|
|
|
// sending will wake it up.
|
|
|
|
tube_clone.send(1);
|
2013-05-04 19:30:31 -05:00
|
|
|
}
|
2013-05-29 19:52:00 -05:00
|
|
|
sched.enqueue_task(task);
|
2013-05-04 19:30:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
assert!(tube.recv() == 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn many_blocking_test() {
|
|
|
|
static MAX: int = 100;
|
|
|
|
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let mut tube: Tube<int> = Tube::new();
|
|
|
|
let tube_clone = tube.clone();
|
2013-06-04 05:03:58 -05:00
|
|
|
let tube_clone = Cell::new(tube_clone);
|
2013-05-19 17:45:39 -05:00
|
|
|
let sched = Local::take::<Scheduler>();
|
2013-05-29 19:52:00 -05:00
|
|
|
do sched.deschedule_running_task_and_then |sched, task| {
|
2013-05-04 19:30:31 -05:00
|
|
|
callback_send(tube_clone.take(), 0);
|
|
|
|
|
|
|
|
fn callback_send(tube: Tube<int>, i: int) {
|
|
|
|
if i == 100 { return; }
|
|
|
|
|
2013-06-04 05:03:58 -05:00
|
|
|
let tube = Cell::new(Cell::new(tube));
|
2013-06-10 17:29:02 -05:00
|
|
|
do Local::borrow::<Scheduler, ()> |sched| {
|
2013-05-04 19:30:31 -05:00
|
|
|
let tube = tube.take();
|
|
|
|
do sched.event_loop.callback {
|
|
|
|
let mut tube = tube.take();
|
|
|
|
// The task should be blocked on this now and
|
|
|
|
// sending will wake it up.
|
|
|
|
tube.send(i);
|
|
|
|
callback_send(tube, i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 19:52:00 -05:00
|
|
|
sched.enqueue_task(task);
|
2013-05-04 19:30:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for int::range(0, MAX) |i| {
|
|
|
|
let j = tube.recv();
|
|
|
|
assert!(j == i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|