diff --git a/doc/tutorial-tasks.md b/doc/tutorial-tasks.md index 799fcb99fd0f..d29eb8a98fd2 100644 --- a/doc/tutorial-tasks.md +++ b/doc/tutorial-tasks.md @@ -461,7 +461,7 @@ and child both need to exchange messages with each other. The function `std::comm::DuplexStream()` supports this pattern. We'll look briefly at how to use it. -To see how `spawn_conversation()` works, we will create a child task +To see how `DuplexStream()` works, we will create a child task that repeatedly receives a `uint` message, converts it to a string, and sends the string in response. The child terminates when it receives `0`. Here is the function that implements the child task: diff --git a/src/libcore/task/mod.rs b/src/libcore/task/mod.rs index 6db7aae16b03..c024781c4301 100644 --- a/src/libcore/task/mod.rs +++ b/src/libcore/task/mod.rs @@ -433,43 +433,6 @@ fn spawn_with(arg: A, f: fn~(v: A)) { } } - /** - * Runs a new task while providing a channel from the parent to the child - * - * Sets up a communication channel from the current task to the new - * child task, passes the port to child's body, and returns a channel - * linked to the port to the parent. - * - * This encapsulates some boilerplate handshaking logic that would - * otherwise be required to establish communication from the parent - * to the child. - */ - fn spawn_listener(f: fn~(comm::Port)) -> comm::Chan { - let setup_po = comm::Port(); - let setup_ch = comm::Chan(&setup_po); - do self.spawn |move f| { - let po = comm::Port(); - let ch = comm::Chan(&po); - comm::send(setup_ch, ch); - f(move po); - } - comm::recv(setup_po) - } - - /** - * Runs a new task, setting up communication in both directions - */ - fn spawn_conversation - (f: fn~(comm::Port, comm::Chan)) - -> (comm::Port, comm::Chan) { - let from_child = comm::Port(); - let to_parent = comm::Chan(&from_child); - let to_child = do self.spawn_listener |move f, from_parent| { - f(from_parent, to_parent) - }; - (from_child, to_child) - } - /** * Execute a function in another task and return either the return value * of the function or result::err. @@ -567,28 +530,6 @@ pub fn spawn_with(arg: A, f: fn~(v: A)) { task().spawn_with(move arg, move f) } -pub fn spawn_listener(f: fn~(comm::Port)) -> comm::Chan { - /*! - * Runs a new task while providing a channel from the parent to the child - * - * This function is equivalent to `task().spawn_listener(f)`. - */ - - task().spawn_listener(move f) -} - -pub fn spawn_conversation - (f: fn~(comm::Port, comm::Chan)) - -> (comm::Port, comm::Chan) { - /*! - * Runs a new task, setting up communication in both directions - * - * This function is equivalent to `task().spawn_conversation(f)`. - */ - - task().spawn_conversation(move f) -} - pub fn spawn_sched(mode: SchedMode, f: fn~()) { /*! * Creates a new scheduler and executes a task on it @@ -926,34 +867,6 @@ fn test_back_to_the_future_result() { let _ = task().future_result(util::ignore).future_result(util::ignore); } -#[test] -fn test_spawn_listiner_bidi() { - let po = comm::Port(); - let ch = comm::Chan(&po); - let ch = do spawn_listener |po| { - // Now the child has a port called 'po' to read from and - // an environment-captured channel called 'ch'. - let res: ~str = comm::recv(po); - assert res == ~"ping"; - comm::send(ch, ~"pong"); - }; - // Likewise, the parent has both a 'po' and 'ch' - comm::send(ch, ~"ping"); - let res: ~str = comm::recv(po); - assert res == ~"pong"; -} - -#[test] -fn test_spawn_conversation() { - let (recv_str, send_int) = do spawn_conversation |recv_int, send_str| { - let input = comm::recv(recv_int); - let output = int::str(input); - comm::send(send_str, move output); - }; - comm::send(send_int, 1); - assert comm::recv(recv_str) == ~"1"; -} - #[test] fn test_try_success() { match do try { @@ -1115,15 +1028,6 @@ fn test_avoid_copying_the_body_spawn() { avoid_copying_the_body(spawn); } -#[test] -fn test_avoid_copying_the_body_spawn_listener() { - do avoid_copying_the_body |f| { - spawn_listener(fn~(move f, _po: comm::Port) { - f(); - }); - } -} - #[test] fn test_avoid_copying_the_body_task_spawn() { do avoid_copying_the_body |f| { @@ -1133,15 +1037,6 @@ fn test_avoid_copying_the_body_task_spawn() { } } -#[test] -fn test_avoid_copying_the_body_spawn_listener_1() { - do avoid_copying_the_body |f| { - task().spawn_listener(fn~(move f, _po: comm::Port) { - f(); - }); - } -} - #[test] fn test_avoid_copying_the_body_try() { do avoid_copying_the_body |f| { diff --git a/src/librustdoc/astsrv.rs b/src/librustdoc/astsrv.rs index 0c3154bf8f81..827db4211c49 100644 --- a/src/librustdoc/astsrv.rs +++ b/src/librustdoc/astsrv.rs @@ -64,7 +64,7 @@ pub fn from_file(file: ~str, owner: SrvOwner) -> T { fn run(owner: SrvOwner, source: ~str, +parse: Parser) -> T { let srv_ = Srv({ - ch: do task::spawn_listener |move parse, po| { + ch: do util::spawn_listener |move parse, po| { act(po, source, parse); } }); diff --git a/src/librustdoc/page_pass.rs b/src/librustdoc/page_pass.rs index 5125f7c1da60..4bcb7c64ad0c 100644 --- a/src/librustdoc/page_pass.rs +++ b/src/librustdoc/page_pass.rs @@ -39,7 +39,7 @@ fn run( return doc; } - let (result_port, page_chan) = do task::spawn_conversation + let (result_port, page_chan) = do util::spawn_conversation |page_port, result_chan| { comm::send(result_chan, make_doc_from_pages(page_port)); }; diff --git a/src/librustdoc/util.rs b/src/librustdoc/util.rs index 7fdd3c80a98a..e1c810b19a44 100644 --- a/src/librustdoc/util.rs +++ b/src/librustdoc/util.rs @@ -17,3 +17,26 @@ impl NominalOp: Clone { fn clone(&self) -> NominalOp { copy *self } } +pub fn spawn_listener( + +f: fn~(comm::Port)) -> comm::Chan { + let setup_po = comm::Port(); + let setup_ch = comm::Chan(&setup_po); + do task::spawn |move f| { + let po = comm::Port(); + let ch = comm::Chan(&po); + comm::send(setup_ch, ch); + f(move po); + } + comm::recv(setup_po) +} + +pub fn spawn_conversation + (+f: fn~(comm::Port, comm::Chan)) + -> (comm::Port, comm::Chan) { + let from_child = comm::Port(); + let to_parent = comm::Chan(&from_child); + let to_child = do spawn_listener |move f, from_parent| { + f(from_parent, to_parent) + }; + (from_child, to_child) +} diff --git a/src/test/bench/shootout-chameneos-redux.rs b/src/test/bench/shootout-chameneos-redux.rs index 651a4cc30486..d36e8daf376a 100644 --- a/src/test/bench/shootout-chameneos-redux.rs +++ b/src/test/bench/shootout-chameneos-redux.rs @@ -131,6 +131,19 @@ fn creature( } fn rendezvous(nn: uint, set: ~[color]) { + + pub fn spawn_listener(+f: fn~(comm::Port)) -> comm::Chan { + let setup_po = comm::Port(); + let setup_ch = comm::Chan(&setup_po); + do task::spawn |move f| { + let po = comm::Port(); + let ch = comm::Chan(&po); + comm::send(setup_ch, ch); + f(move po); + } + comm::recv(setup_po) + } + // these ports will allow us to hear from the creatures let from_creatures: comm::Port = comm::Port(); let from_creatures_log: comm::Port<~str> = comm::Port(); @@ -146,7 +159,7 @@ fn rendezvous(nn: uint, set: ~[color]) { // give us a channel to talk to each let ii = ii; let col = *col; - do task::spawn_listener |from_rendezvous, move ii, move col| { + do spawn_listener |from_rendezvous, move ii, move col| { creature(ii, col, from_rendezvous, to_rendezvous, to_rendezvous_log); }