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 21:47:19 -05:00
|
|
|
handle: RustClosure
|
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 21:47:19 -05:00
|
|
|
struct Guard<T, U:Copy> {
|
2012-10-18 20:45:33 -05:00
|
|
|
cond: &Condition<T,U>,
|
2012-10-18 21:47:19 -05:00
|
|
|
prev: Option<@Handler<T, U>>,
|
2012-10-17 18:40:18 -05:00
|
|
|
drop {
|
2012-10-18 21:47:19 -05:00
|
|
|
match self.prev {
|
|
|
|
None => (),
|
|
|
|
Some(p) =>
|
|
|
|
unsafe {
|
|
|
|
debug!("Guard: popping handler from TLS");
|
|
|
|
task::local_data::local_data_set(self.cond.key, p)
|
2012-10-18 14:27:09 -05:00
|
|
|
}
|
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>,
|
2012-10-18 21:47:19 -05:00
|
|
|
prev: Option<@Handler<T,U>>,
|
2012-10-18 20:45:33 -05:00
|
|
|
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-18 21:47:19 -05:00
|
|
|
let _g = Guard { cond: self.pb.cond,
|
|
|
|
prev: self.prev };
|
2012-10-17 18:40:18 -05:00
|
|
|
task::local_data::local_data_set(self.pb.cond.key,
|
|
|
|
self.handler);
|
|
|
|
// 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-19 16:46:32 -05:00
|
|
|
struct Trap<T, U:Copy> {
|
|
|
|
cond: &Condition<T,U>,
|
|
|
|
handler: @Handler<T, U>
|
|
|
|
}
|
|
|
|
|
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 21:47:19 -05:00
|
|
|
prev: prev,
|
|
|
|
handler: @Handler{handle: *p} }
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-19 16:46:32 -05:00
|
|
|
|
|
|
|
impl<T, U: Copy> Trap<T,U> {
|
|
|
|
fn in<V: Copy>(&self, inner: &self/fn() -> V) -> V {
|
|
|
|
unsafe {
|
|
|
|
let prev = task::local_data::local_data_get(self.cond.key);
|
|
|
|
let _g = Guard { cond: self.cond,
|
|
|
|
prev: prev };
|
|
|
|
debug!("Trap: pushing handler to TLS");
|
|
|
|
task::local_data::local_data_set(self.cond.key, self.handler);
|
|
|
|
inner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 21:47:19 -05:00
|
|
|
fn guard(&self, h: &self/fn(&T) ->U) -> Guard/&self<T,U> {
|
|
|
|
unsafe {
|
|
|
|
let prev = task::local_data::local_data_get(self.key);
|
|
|
|
let g = Guard { cond: self, prev: prev };
|
|
|
|
debug!("Guard: pushing handler to TLS");
|
|
|
|
let p : *RustClosure = ::cast::transmute(&h);
|
|
|
|
let h = @Handler{handle: *p};
|
|
|
|
task::local_data::local_data_set(self.key, h);
|
|
|
|
move g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 16:46:32 -05:00
|
|
|
fn trap(&self, h: &self/fn(&T) ->U) -> Trap/&self<T,U> {
|
|
|
|
unsafe {
|
|
|
|
let p : *RustClosure = ::cast::transmute(&h);
|
|
|
|
let h = @Handler{handle: *p};
|
|
|
|
move Trap { cond: self, handler: h }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2012-10-18 21:47:19 -05:00
|
|
|
do b.handle |_j| {
|
2012-10-18 14:27:09 -05:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2012-10-18 21:47:19 -05:00
|
|
|
do b.handle |_j| {
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("nested_test_outer: in handler");
|
|
|
|
outer_trapped = true;
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
assert outer_trapped;
|
|
|
|
}
|
2012-10-18 21:47:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn nested_guard_test_inner() {
|
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
|
|
|
|
|
|
|
let mut inner_trapped = false;
|
|
|
|
|
|
|
|
let _g = do sadness_condition.guard |_j| {
|
|
|
|
debug!("nested_guard_test_inner: in handler");
|
|
|
|
inner_trapped = true;
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("nested_guard_test_inner: in protected block");
|
|
|
|
trouble(1);
|
|
|
|
|
|
|
|
assert inner_trapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nested_guard_test_outer() {
|
|
|
|
|
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
|
|
|
|
|
|
|
let mut outer_trapped = false;
|
|
|
|
|
|
|
|
let _g = do sadness_condition.guard |_j| {
|
|
|
|
debug!("nested_guard_test_outer: in handler");
|
|
|
|
outer_trapped = true;
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("nested_guard_test_outer: in protected block");
|
|
|
|
nested_guard_test_inner();
|
|
|
|
trouble(1);
|
|
|
|
|
|
|
|
assert outer_trapped;
|
|
|
|
}
|
2012-10-19 16:46:32 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn nested_trap_test_inner() {
|
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
|
|
|
|
|
|
|
let mut inner_trapped = false;
|
|
|
|
|
|
|
|
do sadness_condition.trap(|_j| {
|
|
|
|
debug!("nested_trap_test_inner: in handler");
|
|
|
|
inner_trapped = true;
|
|
|
|
0
|
|
|
|
}).in {
|
|
|
|
debug!("nested_trap_test_inner: in protected block");
|
|
|
|
trouble(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert inner_trapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nested_trap_test_outer() {
|
|
|
|
|
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
|
|
|
|
|
|
|
let mut outer_trapped = false;
|
|
|
|
|
|
|
|
do sadness_condition.trap(|_j| {
|
|
|
|
debug!("nested_trap_test_outer: in handler");
|
|
|
|
outer_trapped = true; 0
|
|
|
|
}).in {
|
|
|
|
debug!("nested_guard_test_outer: in protected block");
|
|
|
|
nested_trap_test_inner();
|
|
|
|
trouble(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
assert outer_trapped;
|
|
|
|
}
|