Convenience methods for spawning and joining tasks.

This commit is contained in:
Eric Holk 2011-08-17 15:07:19 -07:00
parent ae89ea223d
commit 9e020b8b8f
2 changed files with 25 additions and 2 deletions

View File

@ -73,8 +73,17 @@ tag task_notification {
exit(task, task_result);
}
fn join(t: task) -> task_result {
join_id(cast(t))
fn join(task_port : (task_id, comm::port<task_notification>))
-> task_result {
let (id, port) = task_port;
while true {
alt comm::recv::<task_notification>(port) {
exit(_id, res) {
if _id == id { ret res }
}
}
}
fail
}
fn join_id(t : task_id) -> task_result {
@ -104,6 +113,12 @@ fn spawn_notify(thunk : -fn() -> (), notify : _chan<task_notification>)
spawn_inner(thunk, some(notify))
}
fn spawn_joinable(thunk : -fn()) -> (task_id, comm::port<task_notification>) {
let p = comm::port::<task_notification>();
let id = spawn_notify(thunk, comm::chan::<task_notification>(p));
ret (id, p);
}
// FIXME: make this a fn~ once those are supported.
fn spawn_inner(thunk : -fn() -> (),
notify : option<_chan<task_notification>>)

View File

@ -72,3 +72,11 @@ fn test_join_chan_fail() {
_ { fail "invalid task status received" }
}
}
#[test]
fn test_join_convenient() {
fn winner() { }
let f = winner;
let handle = task::spawn_joinable(f);
assert(task::tr_success == task::join(handle));
}