From 9775b480fd3a5cd383e457816be100145028ea64 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sun, 8 Jan 2012 15:32:13 -0800 Subject: [PATCH] add spawn_connected --- src/libcore/task.rs | 60 +++++++++++++++++++++++++++++++--------- src/test/stdtest/task.rs | 19 +++++++++++++ 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/libcore/task.rs b/src/libcore/task.rs index 4a966d91fbb..afc9d783712 100644 --- a/src/libcore/task.rs +++ b/src/libcore/task.rs @@ -46,6 +46,9 @@ export get_task; export spawn; export spawn_joinable; +export spawn_connected; +export connected_fn; +export connected_task; #[abi = "rust-intrinsic"] native mod rusti { @@ -190,27 +193,58 @@ fn spawn_joinable(+f: sendfn()) -> joinable_task { } /* -type connected_fn = sendfn(comm::chan, comm::port); +Type: connected_fn + +The prototype for a connected child task function. Such a function will be +supplied with a channel to send messages to the parent and a port to receive +messages from the parent. The type parameter `ToCh` is the type for messages +sent from the parent to the child and `FrCh` is the type for messages sent +from the child to the parent. */ +type connected_fn = sendfn(comm::port, comm::chan); + +/* +Type: connected_fn + +The result type of +*/ type connected_task = { - port: comm::port, - chan: comm::chan, + from_child: comm::port, + to_child: comm::chan, task: task }; + +/* +Function: spawn_connected + +Spawns a child task along with a port/channel for exchanging messages +with the parent task. The type `ToCh` represents messages sent to the child +and `FrCh` messages received from the child. + +Parameters: + +f - the child function to execute + +Returns: + +The new child task along with the port to receive messages and the channel +to send messages. +*/ fn spawn_connected(f: connected_fn) - -> connected_fn { - let from_child_port = comm::port(); + -> connected_task { + let from_child_port = comm::port::(); let from_child_chan = comm::chan(from_child_port); - let get_to_child_port = comm::port>(); - let get_to_child_chan = comm::chan(to_child_port); + let get_to_child_port = comm::port::>(); + let get_to_child_chan = comm::chan(get_to_child_port); let child_task = spawn(sendfn[move f]() { - let to_child_port = comm::port(); - comm::send(get_to_child_chan, to_child_port); - f(from_child_chan, to_child_port); + let to_child_port = comm::port::(); + comm::send(get_to_child_chan, comm::chan(to_child_port)); + f(to_child_port, from_child_chan); }); - let to_child_chan = comm::recv(get_out); - ret {port: from_child_port, chan: to_child_chan, task: child_task}; + let to_child_chan = comm::recv(get_to_child_port); + ret {from_child: from_child_port, + to_child: to_child_chan, + task: child_task}; } -*/ /* Section: Operations */ diff --git a/src/test/stdtest/task.rs b/src/test/stdtest/task.rs index d1408d183d5..021dee4bc98 100644 --- a/src/test/stdtest/task.rs +++ b/src/test/stdtest/task.rs @@ -57,3 +57,22 @@ fn spawn_polymorphic() { task::spawn {|| foo(true);}; task::spawn {|| foo(42);}; } + +#[test] +fn spawn_connected_stringifier() { + fn stringifer(p: comm::port, ch: comm::chan) { + let u = 1u; + while u != 0u { + u = comm::recv(p); + comm::send(ch, uint::to_str(u, 10u)); + } + } + + let ch = task::spawn_connected(stringifer); + comm::send(ch.to_child, 22u); + assert "22" == comm::recv(ch.from_child); + comm::send(ch.to_child, 44u); + assert "44" == comm::recv(ch.from_child); + comm::send(ch.to_child, 0u); + assert "0" == comm::recv(ch.from_child); +} \ No newline at end of file -- 2.44.0