rust/src/test/run-pass/task-comm-6.rs
Alex Crichton 0a6b9219d1 Rewrite channels yet again for upgradeability
This, the Nth rewrite of channels, is not a rewrite of the core logic behind
channels, but rather their API usage. In the past, we had the distinction
between oneshot, stream, and shared channels, but the most recent rewrite
dropped oneshots in favor of streams and shared channels.

This distinction of stream vs shared has shown that it's not quite what we'd
like either, and this moves the `std::comm` module in the direction of "one
channel to rule them all". There now remains only one Chan and one Port.

This new channel is actually a hybrid oneshot/stream/shared channel under the
hood in order to optimize for the use cases in question. Additionally, this also
reduces the cognitive burden of having to choose between a Chan or a SharedChan
in an API.

My simple benchmarks show no reduction in efficiency over the existing channels
today, and a 3x improvement in the oneshot case. I sadly don't have a
pre-last-rewrite compiler to test out the old old oneshots, but I would imagine
that the performance is comparable, but slightly slower (due to atomic reference
counting).

This commit also brings the bonus bugfix to channels that the pending queue of
messages are all dropped when a Port disappears rather then when both the Port
and the Chan disappear.
2014-02-11 16:32:00 -08:00

49 lines
1.3 KiB
Rust

// Copyright 2012 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.
#[allow(dead_assignment)];
pub fn main() { test00(); }
fn test00() {
let mut r: int = 0;
let mut sum: int = 0;
let (p, ch) = Chan::new();
let mut c0 = ch.clone();
let mut c1 = ch.clone();
let mut c2 = ch.clone();
let mut c3 = ch.clone();
let number_of_messages: int = 1000;
let mut i: int = 0;
while i < number_of_messages {
c0.send(i + 0);
c1.send(i + 0);
c2.send(i + 0);
c3.send(i + 0);
i += 1;
}
i = 0;
while i < number_of_messages {
r = p.recv();
sum += r;
r = p.recv();
sum += r;
r = p.recv();
sum += r;
r = p.recv();
sum += r;
i += 1;
}
assert_eq!(sum, 1998000);
// assert (sum == 4 * ((number_of_messages *
// (number_of_messages - 1)) / 2));
}