2012-10-17 18:40:18 -05:00
|
|
|
// helper for transmutation, shown below.
|
|
|
|
type RustClosure = (int,int);
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
struct Condition<T, U:Copy> {
|
|
|
|
key: task::local_data::LocalDataKey<Handler<T,U>>
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
struct Handler<T, U:Copy> {
|
2012-10-18 14:27:09 -05:00
|
|
|
handle: RustClosure,
|
2012-10-18 20:45:33 -05:00
|
|
|
prev: Option<@Handler<T, U>>
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
struct ProtectBlock<T, U:Copy> {
|
|
|
|
cond: &Condition<T, U>,
|
2012-10-17 18:40:18 -05:00
|
|
|
inner: RustClosure
|
|
|
|
}
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
struct PopHandler<T, U:Copy> {
|
|
|
|
cond: &Condition<T,U>,
|
2012-10-17 18:40:18 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
struct HandleBlock<T, U:Copy> {
|
|
|
|
pb: &ProtectBlock<T,U>,
|
|
|
|
handler: @Handler<T,U>,
|
2012-10-17 18:40:18 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
impl<T, U: Copy> ProtectBlock<T,U> {
|
|
|
|
fn handle(&self, h: &self/fn(&T) ->U) -> HandleBlock/&self<T,U> {
|
2012-10-17 18:40:18 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
impl<T, U: Copy> Condition<T,U> {
|
2012-10-17 18:40:18 -05:00
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
fn protect(&self, inner: &self/fn()) -> ProtectBlock/&self<T,U> {
|
2012-10-17 18:40:18 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
fn raise(t:&T) -> U {
|
2012-10-17 18:40:18 -05:00
|
|
|
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-18 20:45:33 -05:00
|
|
|
fn sadness_key(_x: @Handler<int,int>) { }
|
2012-10-17 18:40:18 -05:00
|
|
|
|
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.
|
2012-10-18 20:45:33 -05:00
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
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
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
2012-10-17 18:40:18 -05:00
|
|
|
|
|
|
|
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() {
|
2012-10-18 20:45:33 -05:00
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
2012-10-18 14:27:09 -05:00
|
|
|
|
|
|
|
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() {
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
2012-10-18 14:27:09 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|