rust/src/test/run-pass/task-comm.rs

171 lines
3.1 KiB
Rust
Raw Normal View History

// xfail-stage0
// xfail-stage1
// xfail-stage2
2010-06-23 23:03:09 -05:00
2011-04-19 15:35:49 -05:00
fn main() -> () {
2010-08-09 08:53:37 -05:00
test00(true);
2010-06-23 23:03:09 -05:00
// test01();
2010-08-09 08:53:37 -05:00
test02();
test03();
test04();
test05();
test06();
2010-06-23 23:03:09 -05:00
}
2011-04-19 15:35:49 -05:00
fn test00_start(chan[int] ch, int message, int count) {
2010-06-23 23:03:09 -05:00
log "Starting test00_start";
let int i = 0;
while (i < count) {
log "Sending Message";
2010-06-23 23:03:09 -05:00
ch <| message;
i = i + 1;
}
log "Ending test00_start";
}
2011-04-19 15:35:49 -05:00
fn test00(bool is_multithreaded) {
let int number_of_tasks = 1;
2010-08-09 08:53:37 -05:00
let int number_of_messages = 4;
2010-06-23 23:03:09 -05:00
log "Creating tasks";
let port[int] po = port();
let chan[int] ch = chan(po);
let int i = 0;
let vec[task] tasks = [];
2010-06-23 23:03:09 -05:00
while (i < number_of_tasks) {
i = i + 1;
if (is_multithreaded) {
tasks += [
spawn thread test00_start(ch, i, number_of_messages)];
2010-06-23 23:03:09 -05:00
} else {
tasks += [spawn test00_start(ch, i, number_of_messages)];
2010-06-23 23:03:09 -05:00
}
}
let int sum = 0;
for (task t in tasks) {
i = 0;
while (i < number_of_messages) {
let int value; po |> value;
2010-06-23 23:03:09 -05:00
sum += value;
i = i + 1;
}
}
for (task t in tasks) {
join t;
}
log "Completed: Final number is: ";
assert (sum == number_of_messages *
2010-06-23 23:03:09 -05:00
(number_of_tasks * number_of_tasks + number_of_tasks) / 2);
}
2011-04-19 15:35:49 -05:00
fn test01() {
2010-06-23 23:03:09 -05:00
let port[int] p = port();
log "Reading from a port that is never written to.";
let int value; p |> value;
2010-06-23 23:03:09 -05:00
log value;
}
2011-04-19 15:35:49 -05:00
fn test02() {
2010-06-23 23:03:09 -05:00
let port[int] p = port();
let chan[int] c = chan(p);
log "Writing to a local task channel.";
c <| 42;
log "Reading from a local task port.";
let int value; p |> value;
2010-06-23 23:03:09 -05:00
log value;
}
obj vector(mutable int x, int y) {
fn length() -> int {
x = x + 2;
ret x + y;
}
}
fn test03() {
log "Creating object ...";
let mutable vector v = vector(1, 2);
log "created object ...";
let mutable vector t = v;
log v.length();
}
fn test04_start() {
log "Started task";
2010-06-23 23:03:09 -05:00
let int i = 1024 * 1024 * 64;
while (i > 0) {
i = i - 1;
}
log "Finished task";
2010-06-23 23:03:09 -05:00
}
fn test04() {
log "Spawning lots of tasks.";
2010-08-09 08:53:37 -05:00
let int i = 4;
2010-06-23 23:03:09 -05:00
while (i > 0) {
i = i - 1;
spawn thread test04_start();
}
log "Finishing up.";
}
2011-04-19 15:35:49 -05:00
fn test05_start(chan[int] ch) {
ch <| 10;
ch <| 20;
ch <| 30;
ch <| 30;
ch <| 30;
}
2010-06-23 23:03:09 -05:00
2011-04-19 15:35:49 -05:00
fn test05() {
let port[int] po = port();
let chan[int] ch = chan(po);
spawn thread test05_start(ch);
let int value; po |> value;
po |> value;
po |> value;
log value;
}
2010-06-23 23:03:09 -05:00
fn test06_start(int task_number) {
log "Started task.";
let int i = 0;
while (i < 100000000) {
i = i + 1;
}
log "Finished task.";
}
fn test06() {
2010-08-09 08:53:37 -05:00
let int number_of_tasks = 4;
log "Creating tasks";
let int i = 0;
let vec[task] tasks = [];
while (i < number_of_tasks) {
i = i + 1;
tasks += [spawn thread test06_start(i)];
// tasks += [spawn test06_start(i)];
}
for (task t in tasks) {
join t;
}
}
2010-06-23 23:03:09 -05:00