Skip to content

Commit 9775b48

Browse files
committed
add spawn_connected
1 parent 91cd57e commit 9775b48

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

src/libcore/task.rs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export tr_failure;
4646
export get_task;
4747
export spawn;
4848
export spawn_joinable;
49+
export spawn_connected;
50+
export connected_fn;
51+
export connected_task;
4952

5053
#[abi = "rust-intrinsic"]
5154
native mod rusti {
@@ -190,27 +193,58 @@ tag task_notification {
190193
}
191194

192195
/*
193-
type connected_fn<ToCh, FrCh> = sendfn(comm::chan<FrCh>, comm::port<ToCh>);
196+
Type: connected_fn
197+
198+
The prototype for a connected child task function. Such a function will be
199+
supplied with a channel to send messages to the parent and a port to receive
200+
messages from the parent. The type parameter `ToCh` is the type for messages
201+
sent from the parent to the child and `FrCh` is the type for messages sent
202+
from the child to the parent. */
203+
type connected_fn<ToCh, FrCh> = sendfn(comm::port<ToCh>, comm::chan<FrCh>);
204+
205+
/*
206+
Type: connected_fn
207+
208+
The result type of <spawn_connected>
209+
*/
194210
type connected_task<ToCh, FrCh> = {
195-
port: comm::port<FrCh>,
196-
chan: comm::chan<ToCh>,
211+
from_child: comm::port<FrCh>,
212+
to_child: comm::chan<ToCh>,
197213
task: task
198214
};
215+
216+
/*
217+
Function: spawn_connected
218+
219+
Spawns a child task along with a port/channel for exchanging messages
220+
with the parent task. The type `ToCh` represents messages sent to the child
221+
and `FrCh` messages received from the child.
222+
223+
Parameters:
224+
225+
f - the child function to execute
226+
227+
Returns:
228+
229+
The new child task along with the port to receive messages and the channel
230+
to send messages.
231+
*/
199232
fn spawn_connected<ToCh:send, FrCh:send>(f: connected_fn<ToCh, FrCh>)
200-
-> connected_fn {
201-
let from_child_port = comm::port<FrCh>();
233+
-> connected_task<ToCh,FrCh> {
234+
let from_child_port = comm::port::<FrCh>();
202235
let from_child_chan = comm::chan(from_child_port);
203-
let get_to_child_port = comm::port<comm::chan<ToCh>>();
204-
let get_to_child_chan = comm::chan(to_child_port);
236+
let get_to_child_port = comm::port::<comm::chan<ToCh>>();
237+
let get_to_child_chan = comm::chan(get_to_child_port);
205238
let child_task = spawn(sendfn[move f]() {
206-
let to_child_port = comm::port<ToCh>();
207-
comm::send(get_to_child_chan, to_child_port);
208-
f(from_child_chan, to_child_port);
239+
let to_child_port = comm::port::<ToCh>();
240+
comm::send(get_to_child_chan, comm::chan(to_child_port));
241+
f(to_child_port, from_child_chan);
209242
});
210-
let to_child_chan = comm::recv(get_out);
211-
ret {port: from_child_port, chan: to_child_chan, task: child_task};
243+
let to_child_chan = comm::recv(get_to_child_port);
244+
ret {from_child: from_child_port,
245+
to_child: to_child_chan,
246+
task: child_task};
212247
}
213-
*/
214248

215249
/* Section: Operations */
216250

src/test/stdtest/task.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,22 @@ fn spawn_polymorphic() {
5757
task::spawn {|| foo(true);};
5858
task::spawn {|| foo(42);};
5959
}
60+
61+
#[test]
62+
fn spawn_connected_stringifier() {
63+
fn stringifer(p: comm::port<uint>, ch: comm::chan<str>) {
64+
let u = 1u;
65+
while u != 0u {
66+
u = comm::recv(p);
67+
comm::send(ch, uint::to_str(u, 10u));
68+
}
69+
}
70+
71+
let ch = task::spawn_connected(stringifer);
72+
comm::send(ch.to_child, 22u);
73+
assert "22" == comm::recv(ch.from_child);
74+
comm::send(ch.to_child, 44u);
75+
assert "44" == comm::recv(ch.from_child);
76+
comm::send(ch.to_child, 0u);
77+
assert "0" == comm::recv(ch.from_child);
78+
}

0 commit comments

Comments
 (0)