2012-01-06 23:14:20 -06:00
|
|
|
// xfail-test
|
|
|
|
|
|
|
|
// A port of task-killjoin to use a resource to manage
|
2012-01-07 00:07:04 -06:00
|
|
|
// the join.
|
2012-01-06 23:14:20 -06:00
|
|
|
|
|
|
|
use std;
|
|
|
|
import task;
|
|
|
|
|
|
|
|
fn joinable(f: fn()) -> (task::task, comm::port<bool>) {
|
|
|
|
resource notify(data: (comm::chan<bool>,
|
|
|
|
@mutable bool)) {
|
|
|
|
let (c, v) = data;
|
2012-01-09 15:47:37 -06:00
|
|
|
#error["notify: task=%d v=%x unwinding=%b b=%b",
|
|
|
|
task::get_task(),
|
|
|
|
ptr::addr_of(*v) as uint,
|
|
|
|
task::currently_unwinding(),
|
|
|
|
*v];
|
2012-01-06 23:14:20 -06:00
|
|
|
comm::send(c, *v);
|
|
|
|
}
|
|
|
|
fn wrapper(pair: (comm::chan<bool>, fn())) {
|
|
|
|
let (c, f) = pair;
|
|
|
|
let b = @mutable false;
|
2012-01-09 15:47:37 -06:00
|
|
|
#error["wrapper: task=%d allocated v=%x",
|
|
|
|
task::get_task(),
|
|
|
|
ptr::addr_of(*b) as uint];
|
2012-01-06 23:14:20 -06:00
|
|
|
let _r = notify((c, b));
|
|
|
|
f();
|
|
|
|
*b = true;
|
|
|
|
}
|
|
|
|
let p = comm::port();
|
|
|
|
let c = comm::chan(p);
|
2012-01-09 15:47:37 -06:00
|
|
|
let t = task::spawn {|| wrapper((c, f)) };
|
2012-01-06 23:14:20 -06:00
|
|
|
ret (t, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn join(pair: (task::task, comm::port<bool>)) -> bool {
|
|
|
|
let (_, port) = pair;
|
|
|
|
comm::recv(port)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn supervised() {
|
|
|
|
// Yield to make sure the supervisor joins before we
|
|
|
|
// fail. This is currently not needed because the supervisor
|
|
|
|
// runs first, but I can imagine that changing.
|
2012-01-09 15:47:37 -06:00
|
|
|
#error["supervised task=%d", task::get_task()];
|
2012-01-06 23:14:20 -06:00
|
|
|
task::yield();
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn supervisor() {
|
|
|
|
// Unsupervise this task so the process doesn't return a failure status as
|
|
|
|
// a result of the main task being killed.
|
|
|
|
task::unsupervise();
|
2012-01-09 15:47:37 -06:00
|
|
|
#error["supervisor task=%d", task::get_task()];
|
|
|
|
let t = joinable(supervised);
|
|
|
|
join(t);
|
2012-01-06 23:14:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
join(joinable(supervisor));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|