2014-02-07 19:08:32 +00:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-11 01:32:48 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-02-07 19:08:32 +00:00
|
|
|
// ignore-fast
|
2012-09-18 22:52:21 +00:00
|
|
|
|
2014-02-14 18:10:06 +00:00
|
|
|
extern crate extra;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
|
|
|
use std::task;
|
2011-07-12 22:27:17 +00:00
|
|
|
|
2013-10-21 20:08:31 +00:00
|
|
|
pub fn main() { info!("===== WITHOUT THREADS ====="); test00(); }
|
2010-07-19 21:05:18 +00:00
|
|
|
|
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-01-09 02:31:48 +00:00
|
|
|
fn test00_start(ch: &Chan<int>, message: int, count: int) {
|
2013-10-21 20:08:31 +00:00
|
|
|
info!("Starting test00_start");
|
2012-03-22 15:39:41 +00:00
|
|
|
let mut i: int = 0;
|
2011-08-10 21:38:49 +00:00
|
|
|
while i < count {
|
2013-10-21 20:08:31 +00:00
|
|
|
info!("Sending Message");
|
2012-07-25 21:05:06 +00:00
|
|
|
ch.send(message + 0);
|
2011-08-10 21:38:49 +00:00
|
|
|
i = i + 1;
|
|
|
|
}
|
2013-10-21 20:08:31 +00:00
|
|
|
info!("Ending test00_start");
|
2010-07-19 21:05:18 +00:00
|
|
|
}
|
|
|
|
|
2011-07-12 22:27:17 +00:00
|
|
|
fn test00() {
|
2011-07-27 12:19:39 +00:00
|
|
|
let number_of_tasks: int = 16;
|
|
|
|
let number_of_messages: int = 4;
|
2011-07-13 22:44:09 +00:00
|
|
|
|
2013-10-21 20:08:31 +00:00
|
|
|
info!("Creating tasks");
|
2011-07-13 22:44:09 +00:00
|
|
|
|
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-01-09 02:31:48 +00:00
|
|
|
let (po, ch) = Chan::new();
|
2011-07-13 22:44:09 +00:00
|
|
|
|
2012-03-22 15:39:41 +00:00
|
|
|
let mut i: int = 0;
|
2011-07-13 22:44:09 +00:00
|
|
|
|
2010-07-19 21:05:18 +00:00
|
|
|
// Create and spawn tasks...
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut results = ~[];
|
2011-07-27 12:19:39 +00:00
|
|
|
while i < number_of_tasks {
|
2013-07-31 06:39:58 +00:00
|
|
|
let ch = ch.clone();
|
2013-05-07 02:29:04 +00:00
|
|
|
let mut builder = task::task();
|
2013-10-18 08:38:46 +00:00
|
|
|
results.push(builder.future_result());
|
2013-05-07 02:29:04 +00:00
|
|
|
builder.spawn({
|
2013-12-06 02:19:06 +00:00
|
|
|
let ch = ch;
|
2013-01-10 18:59:58 +00:00
|
|
|
let i = i;
|
2013-12-06 02:19:06 +00:00
|
|
|
proc() {
|
|
|
|
test00_start(&ch, i, number_of_messages)
|
|
|
|
}
|
2013-01-10 18:59:58 +00:00
|
|
|
});
|
2010-08-09 13:53:37 +00:00
|
|
|
i = i + 1;
|
2010-07-19 21:05:18 +00:00
|
|
|
}
|
2011-07-13 22:44:09 +00:00
|
|
|
|
2010-07-19 21:05:18 +00:00
|
|
|
// Read from spawned tasks...
|
2012-03-22 15:39:41 +00:00
|
|
|
let mut sum = 0;
|
2013-08-17 15:37:42 +00:00
|
|
|
for _r in results.iter() {
|
2010-07-19 21:05:18 +00:00
|
|
|
i = 0;
|
2011-07-27 12:19:39 +00:00
|
|
|
while i < number_of_messages {
|
2012-07-25 21:05:06 +00:00
|
|
|
let value = po.recv();
|
2010-07-19 21:05:18 +00:00
|
|
|
sum += value;
|
|
|
|
i = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join spawned tasks...
|
2013-08-03 16:45:23 +00:00
|
|
|
for r in results.iter() { r.recv(); }
|
2011-07-13 22:44:09 +00:00
|
|
|
|
2013-10-21 20:08:31 +00:00
|
|
|
info!("Completed: Final number is: ");
|
|
|
|
error!("{:?}", sum);
|
2011-07-13 22:44:09 +00:00
|
|
|
// assert (sum == (((number_of_tasks * (number_of_tasks - 1)) / 2) *
|
2010-08-09 13:53:37 +00:00
|
|
|
// number_of_messages));
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(sum, 480);
|
2011-08-06 17:07:39 +00:00
|
|
|
}
|