2012-10-17 18:40:18 -05:00
|
|
|
// helper for transmutation, shown below.
|
|
|
|
type RustClosure = (int,int);
|
|
|
|
|
|
|
|
struct Condition {
|
|
|
|
key: task::local_data::LocalDataKey<Handler>
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Handler {
|
2012-10-18 14:27:09 -05:00
|
|
|
handle: RustClosure,
|
|
|
|
prev: Option<@Handler>
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ProtectBlock {
|
|
|
|
cond: &Condition,
|
|
|
|
inner: RustClosure
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PopHandler {
|
|
|
|
cond: &Condition,
|
|
|
|
drop {
|
|
|
|
unsafe {
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("PopHandler: popping handler from TLS");
|
|
|
|
match task::local_data::local_data_pop(self.cond.key) {
|
|
|
|
None => (),
|
|
|
|
Some(h) => {
|
|
|
|
match h.prev {
|
|
|
|
None => (),
|
|
|
|
Some(p) =>
|
|
|
|
task::local_data::local_data_set(self.cond.key, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct HandleBlock {
|
|
|
|
pb: &ProtectBlock,
|
|
|
|
handler: @Handler,
|
|
|
|
drop {
|
|
|
|
unsafe {
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("HandleBlock: pushing handler to TLS");
|
2012-10-17 18:40:18 -05:00
|
|
|
task::local_data::local_data_set(self.pb.cond.key,
|
|
|
|
self.handler);
|
|
|
|
let _pop = PopHandler { cond: self.pb.cond };
|
|
|
|
// transmutation to avoid copying non-copyable, should
|
|
|
|
// be fixable by tracking closure pointees in regionck.
|
|
|
|
let f : &fn() = ::cast::transmute(self.pb.inner);
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("HandleBlock: invoking protected code");
|
2012-10-17 18:40:18 -05:00
|
|
|
f();
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("HandleBlock: returned from protected code");
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProtectBlock {
|
|
|
|
fn handle<T, U: Copy>(&self, h: &self/fn(&T) ->U) -> HandleBlock/&self {
|
|
|
|
unsafe {
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("ProtectBlock.handle: setting up handler block");
|
2012-10-17 18:40:18 -05:00
|
|
|
let p : *RustClosure = ::cast::transmute(&h);
|
2012-10-18 14:27:09 -05:00
|
|
|
let prev = task::local_data::local_data_get(self.cond.key);
|
2012-10-17 18:40:18 -05:00
|
|
|
HandleBlock { pb: self,
|
2012-10-18 14:27:09 -05:00
|
|
|
handler: @Handler{handle: *p, prev: prev} }
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Condition {
|
|
|
|
|
|
|
|
fn protect(&self, inner: &self/fn()) -> ProtectBlock/&self {
|
|
|
|
unsafe {
|
|
|
|
// transmutation to avoid copying non-copyable, should
|
|
|
|
// be fixable by tracking closure pointees in regionck.
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("Condition.protect: setting up protected block");
|
2012-10-17 18:40:18 -05:00
|
|
|
let p : *RustClosure = ::cast::transmute(&inner);
|
|
|
|
ProtectBlock { cond: self,
|
2012-10-18 14:27:09 -05:00
|
|
|
inner: *p }
|
|
|
|
}
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn raise<T, U: Copy>(t:&T) -> U {
|
|
|
|
unsafe {
|
|
|
|
match task::local_data::local_data_get(self.key) {
|
2012-10-18 14:27:09 -05:00
|
|
|
None => {
|
|
|
|
debug!("Condition.raise: found no handler");
|
|
|
|
fail
|
|
|
|
}
|
|
|
|
|
2012-10-17 18:40:18 -05:00
|
|
|
Some(handler) => {
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("Condition.raise: found handler");
|
2012-10-17 18:40:18 -05:00
|
|
|
let f : &fn(&T) -> U = ::cast::transmute(handler.handle);
|
|
|
|
f(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-18 14:27:09 -05:00
|
|
|
#[cfg(test)]
|
2012-10-17 18:40:18 -05:00
|
|
|
fn happiness_key(_x: @Handler) { }
|
|
|
|
|
2012-10-18 14:27:09 -05:00
|
|
|
#[cfg(test)]
|
2012-10-17 18:40:18 -05:00
|
|
|
fn sadness_key(_x: @Handler) { }
|
|
|
|
|
2012-10-18 14:27:09 -05:00
|
|
|
#[cfg(test)]
|
2012-10-17 18:40:18 -05:00
|
|
|
fn trouble(i: int) {
|
|
|
|
// Condition should work as a const, just limitations in consts.
|
|
|
|
let sadness_condition : Condition = Condition { key: sadness_key };
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("trouble: raising conition");
|
2012-10-17 18:40:18 -05:00
|
|
|
let j = sadness_condition.raise(&i);
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("trouble: handler recovered with %d", j);
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-10-18 14:27:09 -05:00
|
|
|
fn test1() {
|
2012-10-17 18:40:18 -05:00
|
|
|
|
|
|
|
let sadness_condition : Condition = Condition { key: sadness_key };
|
|
|
|
|
|
|
|
let mut i = 10;
|
|
|
|
|
|
|
|
let b = do sadness_condition.protect {
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("test1: in protected block");
|
2012-10-17 18:40:18 -05:00
|
|
|
trouble(1);
|
|
|
|
trouble(2);
|
|
|
|
trouble(3);
|
|
|
|
};
|
|
|
|
|
|
|
|
do b.handle |j| {
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("test1: in handler");
|
2012-10-17 18:40:18 -05:00
|
|
|
i += *j;
|
|
|
|
i
|
|
|
|
};
|
|
|
|
|
|
|
|
assert i == 16;
|
2012-10-18 14:27:09 -05:00
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
fn nested_test_inner() {
|
|
|
|
let sadness_condition : Condition = Condition { key: sadness_key };
|
|
|
|
|
|
|
|
let mut inner_trapped = false;
|
|
|
|
|
|
|
|
let b = do sadness_condition.protect {
|
|
|
|
debug!("nested_test_inner: in protected block");
|
|
|
|
trouble(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
do b.handle |_j:&int| {
|
|
|
|
debug!("nested_test_inner: in handler");
|
|
|
|
inner_trapped = true;
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
assert inner_trapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nested_test_outer() {
|
|
|
|
|
|
|
|
let sadness_condition : Condition = Condition { key: sadness_key };
|
|
|
|
|
|
|
|
let mut outer_trapped = false;
|
|
|
|
|
|
|
|
let b = do sadness_condition.protect {
|
|
|
|
debug!("nested_test_outer: in protected block");
|
|
|
|
nested_test_inner();
|
|
|
|
trouble(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
do b.handle |_j:&int| {
|
|
|
|
debug!("nested_test_outer: in handler");
|
|
|
|
outer_trapped = true;
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
assert outer_trapped;
|
|
|
|
}
|