Skip to content

Commit 9e020b8

Browse files
author
Eric Holk
committed
Convenience methods for spawning and joining tasks.
1 parent ae89ea2 commit 9e020b8

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/lib/task.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,17 @@ tag task_notification {
7373
exit(task, task_result);
7474
}
7575

76-
fn join(t: task) -> task_result {
77-
join_id(cast(t))
76+
fn join(task_port : (task_id, comm::port<task_notification>))
77+
-> task_result {
78+
let (id, port) = task_port;
79+
while true {
80+
alt comm::recv::<task_notification>(port) {
81+
exit(_id, res) {
82+
if _id == id { ret res }
83+
}
84+
}
85+
}
86+
fail
7887
}
7988

8089
fn join_id(t : task_id) -> task_result {
@@ -104,6 +113,12 @@ fn spawn_notify(thunk : -fn() -> (), notify : _chan<task_notification>)
104113
spawn_inner(thunk, some(notify))
105114
}
106115

116+
fn spawn_joinable(thunk : -fn()) -> (task_id, comm::port<task_notification>) {
117+
let p = comm::port::<task_notification>();
118+
let id = spawn_notify(thunk, comm::chan::<task_notification>(p));
119+
ret (id, p);
120+
}
121+
107122
// FIXME: make this a fn~ once those are supported.
108123
fn spawn_inner(thunk : -fn() -> (),
109124
notify : option<_chan<task_notification>>)

src/test/stdtest/task.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,11 @@ fn test_join_chan_fail() {
7272
_ { fail "invalid task status received" }
7373
}
7474
}
75+
76+
#[test]
77+
fn test_join_convenient() {
78+
fn winner() { }
79+
let f = winner;
80+
let handle = task::spawn_joinable(f);
81+
assert(task::tr_success == task::join(handle));
82+
}

0 commit comments

Comments
 (0)