Update the rock-paper-scissors example in the tutorial, and rename some types in core::pipes
This commit is contained in:
parent
b592d57311
commit
5f57588887
12 changed files with 35 additions and 36 deletions
|
@ -74,7 +74,7 @@ Here's a parallel game of rock, paper, scissors to whet your appetite.
|
||||||
~~~~
|
~~~~
|
||||||
use std;
|
use std;
|
||||||
|
|
||||||
import comm::listen;
|
import pipes::PortSet;
|
||||||
import task::spawn;
|
import task::spawn;
|
||||||
import iter::repeat;
|
import iter::repeat;
|
||||||
import rand::{seeded_rng, seed};
|
import rand::{seeded_rng, seed};
|
||||||
|
@ -83,17 +83,17 @@ import io::println;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Open a channel to receive game results
|
// Open a channel to receive game results
|
||||||
do listen |result_from_game| {
|
let result_from_game = PortSet();
|
||||||
|
|
||||||
let times = 10;
|
let times = 10;
|
||||||
let player1 = ~"graydon";
|
let player1 = ~"graydon";
|
||||||
let player2 = ~"patrick";
|
let player2 = ~"patrick";
|
||||||
|
|
||||||
for repeat(times) {
|
for repeat(times) {
|
||||||
// Start another task to play the game
|
// Start another task to play the game
|
||||||
|
let result = result_from_game.chan();
|
||||||
do spawn |copy player1, copy player2| {
|
do spawn |copy player1, copy player2| {
|
||||||
let outcome = play_game(player1, player2);
|
let outcome = play_game(player1, player2);
|
||||||
result_from_game.send(outcome);
|
result.send(outcome);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +102,6 @@ fn main() {
|
||||||
let winner = result_from_game.recv();
|
let winner = result_from_game.recv();
|
||||||
println(#fmt("%s wins round #%u", winner, round));
|
println(#fmt("%s wins round #%u", winner, round));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn play_game(player1: ~str, player2: ~str) -> ~str {
|
fn play_game(player1: ~str, player2: ~str) -> ~str {
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ fn run(lib_path: ~str,
|
||||||
|
|
||||||
|
|
||||||
writeclose(pipe_in.out, input);
|
writeclose(pipe_in.out, input);
|
||||||
let p = pipes::port_set();
|
let p = pipes::PortSet();
|
||||||
let ch = p.chan();
|
let ch = p.chan();
|
||||||
do task::spawn_sched(task::SingleThreaded) {
|
do task::spawn_sched(task::SingleThreaded) {
|
||||||
let errput = readclose(pipe_err.in);
|
let errput = readclose(pipe_err.in);
|
||||||
|
|
|
@ -93,7 +93,7 @@ export atomic_add_acq, atomic_sub_rel;
|
||||||
export send_packet, recv_packet, send, recv, try_recv, peek;
|
export send_packet, recv_packet, send, recv, try_recv, peek;
|
||||||
export select, select2, selecti, select2i, selectable;
|
export select, select2, selecti, select2i, selectable;
|
||||||
export spawn_service, spawn_service_recv;
|
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 oneshot, chan_one, port_one;
|
||||||
export recv_one, try_recv_one, send_one, try_send_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.
|
/// Treat many ports as one.
|
||||||
struct port_set<T: send> : recv<T> {
|
struct PortSet<T: send> : recv<T> {
|
||||||
let mut ports: ~[pipes::port<T>];
|
let mut ports: ~[pipes::port<T>];
|
||||||
|
|
||||||
new() { self.ports = ~[]; }
|
new() { self.ports = ~[]; }
|
||||||
|
@ -1096,9 +1096,9 @@ impl<T: send> port<T>: selectable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A channel that can be shared between many senders.
|
/// 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) {
|
fn send(+x: T) {
|
||||||
let mut xx = some(x);
|
let mut xx = some(x);
|
||||||
do self.with |chan| {
|
do self.with |chan| {
|
||||||
|
@ -1119,7 +1119,7 @@ impl<T: send> shared_chan<T>: channel<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a `chan` into a `shared_chan`.
|
/// 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)
|
unsafe::exclusive(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -423,7 +423,7 @@ mod tests {
|
||||||
let (c, p) = pipes::stream();
|
let (c, p) = pipes::stream();
|
||||||
|
|
||||||
do task::spawn() {
|
do task::spawn() {
|
||||||
let p = pipes::port_set();
|
let p = pipes::PortSet();
|
||||||
c.send(p.chan());
|
c.send(p.chan());
|
||||||
|
|
||||||
let arc_v = p.recv();
|
let arc_v = p.recv();
|
||||||
|
|
|
@ -14,7 +14,7 @@ use std;
|
||||||
import io::Writer;
|
import io::Writer;
|
||||||
import io::WriterUtil;
|
import io::WriterUtil;
|
||||||
|
|
||||||
import pipes::{port, chan, shared_chan};
|
import pipes::{port, chan, SharedChan};
|
||||||
|
|
||||||
macro_rules! move_out {
|
macro_rules! move_out {
|
||||||
{ $x:expr } => { unsafe { let y <- *ptr::addr_of($x); y } }
|
{ $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_parent, from_child) = pipes::stream();
|
||||||
let (to_child, from_parent) = 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 size = option::get(uint::from_str(args[1]));
|
||||||
let workers = option::get(uint::from_str(args[2]));
|
let workers = option::get(uint::from_str(args[2]));
|
||||||
|
|
|
@ -10,7 +10,7 @@ use std;
|
||||||
import io::Writer;
|
import io::Writer;
|
||||||
import io::WriterUtil;
|
import io::WriterUtil;
|
||||||
|
|
||||||
import pipes::{port, port_set, chan};
|
import pipes::{port, PortSet, chan};
|
||||||
|
|
||||||
macro_rules! move_out {
|
macro_rules! move_out {
|
||||||
{ $x:expr } => { unsafe { let y <- *ptr::addr_of($x); y } }
|
{ $x:expr } => { unsafe { let y <- *ptr::addr_of($x); y } }
|
||||||
|
@ -22,7 +22,7 @@ enum request {
|
||||||
stop
|
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 count = 0u;
|
||||||
let mut done = false;
|
let mut done = false;
|
||||||
while !done {
|
while !done {
|
||||||
|
@ -43,7 +43,7 @@ fn server(requests: port_set<request>, responses: pipes::chan<uint>) {
|
||||||
fn run(args: &[~str]) {
|
fn run(args: &[~str]) {
|
||||||
let (to_parent, from_child) = pipes::stream();
|
let (to_parent, from_child) = pipes::stream();
|
||||||
let (to_child, from_parent_) = pipes::stream();
|
let (to_child, from_parent_) = pipes::stream();
|
||||||
let from_parent = port_set();
|
let from_parent = PortSet();
|
||||||
from_parent.add(from_parent_);
|
from_parent.add(from_parent_);
|
||||||
|
|
||||||
let size = option::get(uint::from_str(args[1]));
|
let size = option::get(uint::from_str(args[1]));
|
||||||
|
|
|
@ -30,7 +30,7 @@ fn fib(n: int) -> int {
|
||||||
} else if n <= 2 {
|
} else if n <= 2 {
|
||||||
c.send(1);
|
c.send(1);
|
||||||
} else {
|
} else {
|
||||||
let p = pipes::port_set();
|
let p = pipes::PortSet();
|
||||||
let ch = p.chan();
|
let ch = p.chan();
|
||||||
task::spawn(|| pfib(ch, n - 1) );
|
task::spawn(|| pfib(ch, n - 1) );
|
||||||
let ch = p.chan();
|
let ch = p.chan();
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import task;
|
import task;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let po = pipes::port_set();
|
let po = pipes::PortSet();
|
||||||
|
|
||||||
// Spawn 10 tasks each sending us back one int.
|
// Spawn 10 tasks each sending us back one int.
|
||||||
let mut i = 10;
|
let mut i = 10;
|
||||||
|
|
|
@ -24,7 +24,7 @@ fn test00() {
|
||||||
|
|
||||||
debug!{"Creating tasks"};
|
debug!{"Creating tasks"};
|
||||||
|
|
||||||
let po = pipes::port_set();
|
let po = pipes::PortSet();
|
||||||
|
|
||||||
let mut i: int = 0;
|
let mut i: int = 0;
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ fn main() { test00(); }
|
||||||
fn test00() {
|
fn test00() {
|
||||||
let mut r: int = 0;
|
let mut r: int = 0;
|
||||||
let mut sum: int = 0;
|
let mut sum: int = 0;
|
||||||
let p = pipes::port_set();
|
let p = pipes::PortSet();
|
||||||
let c0 = p.chan();
|
let c0 = p.chan();
|
||||||
let c1 = p.chan();
|
let c1 = p.chan();
|
||||||
let c2 = p.chan();
|
let c2 = p.chan();
|
||||||
|
|
|
@ -11,7 +11,7 @@ fn test00_start(c: pipes::chan<int>, start: int, number_of_messages: int) {
|
||||||
fn test00() {
|
fn test00() {
|
||||||
let mut r: int = 0;
|
let mut r: int = 0;
|
||||||
let mut sum: int = 0;
|
let mut sum: int = 0;
|
||||||
let p = pipes::port_set();
|
let p = pipes::PortSet();
|
||||||
let number_of_messages: int = 10;
|
let number_of_messages: int = 10;
|
||||||
|
|
||||||
let c = p.chan();
|
let c = p.chan();
|
||||||
|
|
|
@ -11,7 +11,7 @@ fn test00_start(c: pipes::chan<int>, number_of_messages: int) {
|
||||||
fn test00() {
|
fn test00() {
|
||||||
let r: int = 0;
|
let r: int = 0;
|
||||||
let mut sum: int = 0;
|
let mut sum: int = 0;
|
||||||
let p = pipes::port_set();
|
let p = pipes::PortSet();
|
||||||
let number_of_messages: int = 10;
|
let number_of_messages: int = 10;
|
||||||
let ch = p.chan();
|
let ch = p.chan();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue