Update the rock-paper-scissors example in the tutorial, and rename some types in core::pipes

This commit is contained in:
Eric Holk 2012-08-16 17:50:21 -07:00
parent b592d57311
commit 5f57588887
12 changed files with 35 additions and 36 deletions

View File

@ -74,7 +74,7 @@ Here's a parallel game of rock, paper, scissors to whet your appetite.
~~~~
use std;
import comm::listen;
import pipes::PortSet;
import task::spawn;
import iter::repeat;
import rand::{seeded_rng, seed};
@ -83,25 +83,24 @@ import io::println;
fn main() {
// Open a channel to receive game results
do listen |result_from_game| {
let result_from_game = PortSet();
let times = 10;
let player1 = ~"graydon";
let player2 = ~"patrick";
let times = 10;
let player1 = ~"graydon";
let player2 = ~"patrick";
for repeat(times) {
// Start another task to play the game
do spawn |copy player1, copy player2| {
let outcome = play_game(player1, player2);
result_from_game.send(outcome);
}
for repeat(times) {
// Start another task to play the game
let result = result_from_game.chan();
do spawn |copy player1, copy player2| {
let outcome = play_game(player1, player2);
result.send(outcome);
}
}
// Report the results as the games complete
for range(0, times) |round| {
let winner = result_from_game.recv();
println(#fmt("%s wins round #%u", winner, round));
}
// Report the results as the games complete
for range(0, times) |round| {
let winner = result_from_game.recv();
println(#fmt("%s wins round #%u", winner, round));
}
fn play_game(player1: ~str, player2: ~str) -> ~str {

View File

@ -60,7 +60,7 @@ fn run(lib_path: ~str,
writeclose(pipe_in.out, input);
let p = pipes::port_set();
let p = pipes::PortSet();
let ch = p.chan();
do task::spawn_sched(task::SingleThreaded) {
let errput = readclose(pipe_err.in);

View File

@ -93,7 +93,7 @@ export atomic_add_acq, atomic_sub_rel;
export send_packet, recv_packet, send, recv, try_recv, peek;
export select, select2, selecti, select2i, selectable;
export spawn_service, spawn_service_recv;
export stream, port, chan, shared_chan, port_set, channel;
export stream, port, chan, SharedChan, PortSet, channel;
export oneshot, chan_one, port_one;
export recv_one, try_recv_one, send_one, try_send_one;
@ -1020,8 +1020,8 @@ impl<T: send> port<T>: recv<T> {
}
}
// Treat a whole bunch of ports as one.
struct port_set<T: send> : recv<T> {
/// Treat many ports as one.
struct PortSet<T: send> : recv<T> {
let mut ports: ~[pipes::port<T>];
new() { self.ports = ~[]; }
@ -1096,9 +1096,9 @@ impl<T: send> port<T>: selectable {
}
/// A channel that can be shared between many senders.
type shared_chan<T: send> = unsafe::Exclusive<chan<T>>;
type SharedChan<T: send> = unsafe::Exclusive<chan<T>>;
impl<T: send> shared_chan<T>: channel<T> {
impl<T: send> SharedChan<T>: channel<T> {
fn send(+x: T) {
let mut xx = some(x);
do self.with |chan| {
@ -1119,7 +1119,7 @@ impl<T: send> shared_chan<T>: channel<T> {
}
/// Converts a `chan` into a `shared_chan`.
fn shared_chan<T:send>(+c: chan<T>) -> shared_chan<T> {
fn SharedChan<T:send>(+c: chan<T>) -> SharedChan<T> {
unsafe::exclusive(c)
}

View File

@ -423,7 +423,7 @@ mod tests {
let (c, p) = pipes::stream();
do task::spawn() {
let p = pipes::port_set();
let p = pipes::PortSet();
c.send(p.chan());
let arc_v = p.recv();

View File

@ -14,7 +14,7 @@ use std;
import io::Writer;
import io::WriterUtil;
import pipes::{port, chan, shared_chan};
import pipes::{port, chan, SharedChan};
macro_rules! move_out {
{ $x:expr } => { unsafe { let y <- *ptr::addr_of($x); y } }
@ -48,7 +48,7 @@ fn run(args: &[~str]) {
let (to_parent, from_child) = pipes::stream();
let (to_child, from_parent) = pipes::stream();
let to_child = shared_chan(to_child);
let to_child = SharedChan(to_child);
let size = option::get(uint::from_str(args[1]));
let workers = option::get(uint::from_str(args[2]));

View File

@ -10,7 +10,7 @@ use std;
import io::Writer;
import io::WriterUtil;
import pipes::{port, port_set, chan};
import pipes::{port, PortSet, chan};
macro_rules! move_out {
{ $x:expr } => { unsafe { let y <- *ptr::addr_of($x); y } }
@ -22,7 +22,7 @@ enum request {
stop
}
fn server(requests: port_set<request>, responses: pipes::chan<uint>) {
fn server(requests: PortSet<request>, responses: pipes::chan<uint>) {
let mut count = 0u;
let mut done = false;
while !done {
@ -43,7 +43,7 @@ fn server(requests: port_set<request>, responses: pipes::chan<uint>) {
fn run(args: &[~str]) {
let (to_parent, from_child) = pipes::stream();
let (to_child, from_parent_) = pipes::stream();
let from_parent = port_set();
let from_parent = PortSet();
from_parent.add(from_parent_);
let size = option::get(uint::from_str(args[1]));

View File

@ -30,7 +30,7 @@ fn fib(n: int) -> int {
} else if n <= 2 {
c.send(1);
} else {
let p = pipes::port_set();
let p = pipes::PortSet();
let ch = p.chan();
task::spawn(|| pfib(ch, n - 1) );
let ch = p.chan();

View File

@ -1,7 +1,7 @@
import task;
fn main() {
let po = pipes::port_set();
let po = pipes::PortSet();
// Spawn 10 tasks each sending us back one int.
let mut i = 10;

View File

@ -24,7 +24,7 @@ fn test00() {
debug!{"Creating tasks"};
let po = pipes::port_set();
let po = pipes::PortSet();
let mut i: int = 0;

View File

@ -9,7 +9,7 @@ fn main() { test00(); }
fn test00() {
let mut r: int = 0;
let mut sum: int = 0;
let p = pipes::port_set();
let p = pipes::PortSet();
let c0 = p.chan();
let c1 = p.chan();
let c2 = p.chan();

View File

@ -11,7 +11,7 @@ fn test00_start(c: pipes::chan<int>, start: int, number_of_messages: int) {
fn test00() {
let mut r: int = 0;
let mut sum: int = 0;
let p = pipes::port_set();
let p = pipes::PortSet();
let number_of_messages: int = 10;
let c = p.chan();

View File

@ -11,7 +11,7 @@ fn test00_start(c: pipes::chan<int>, number_of_messages: int) {
fn test00() {
let r: int = 0;
let mut sum: int = 0;
let p = pipes::port_set();
let p = pipes::PortSet();
let number_of_messages: int = 10;
let ch = p.chan();