Skip to content

Commit a149871

Browse files
committed
---
yaml --- r: 33406 b: refs/heads/snap-stage3 c: 89de49c h: refs/heads/master v: v3
1 parent 9ce4e14 commit a149871

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 0243d86e19295d9e25e6fa83005f0aa03eef519e
4+
refs/heads/snap-stage3: 89de49cecdf5e48497e5770eba13240091627320
55
refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libcore/condition.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ struct HandleBlock<T, U:Copy> {
5151
}
5252
}
5353

54+
struct Trap<T, U:Copy> {
55+
cond: &Condition<T,U>,
56+
handler: @Handler<T, U>
57+
}
58+
5459
impl<T, U: Copy> ProtectBlock<T,U> {
5560
fn handle(&self, h: &self/fn(&T) ->U) -> HandleBlock/&self<T,U> {
5661
unsafe {
@@ -65,6 +70,20 @@ impl<T, U: Copy> ProtectBlock<T,U> {
6570
}
6671

6772

73+
74+
impl<T, U: Copy> Trap<T,U> {
75+
fn in<V: Copy>(&self, inner: &self/fn() -> V) -> V {
76+
unsafe {
77+
let prev = task::local_data::local_data_get(self.cond.key);
78+
let _g = Guard { cond: self.cond,
79+
prev: prev };
80+
debug!("Trap: pushing handler to TLS");
81+
task::local_data::local_data_set(self.cond.key, self.handler);
82+
inner()
83+
}
84+
}
85+
}
86+
6887
impl<T, U: Copy> Condition<T,U> {
6988

7089
fn guard(&self, h: &self/fn(&T) ->U) -> Guard/&self<T,U> {
@@ -79,6 +98,14 @@ impl<T, U: Copy> Condition<T,U> {
7998
}
8099
}
81100

101+
fn trap(&self, h: &self/fn(&T) ->U) -> Trap/&self<T,U> {
102+
unsafe {
103+
let p : *RustClosure = ::cast::transmute(&h);
104+
let h = @Handler{handle: *p};
105+
move Trap { cond: self, handler: h }
106+
}
107+
}
108+
82109
fn protect(&self, inner: &self/fn()) -> ProtectBlock/&self<T,U> {
83110
unsafe {
84111
// transmutation to avoid copying non-copyable, should
@@ -229,3 +256,45 @@ fn nested_guard_test_outer() {
229256

230257
assert outer_trapped;
231258
}
259+
260+
261+
262+
#[cfg(test)]
263+
fn nested_trap_test_inner() {
264+
let sadness_condition : Condition<int,int> =
265+
Condition { key: sadness_key };
266+
267+
let mut inner_trapped = false;
268+
269+
do sadness_condition.trap(|_j| {
270+
debug!("nested_trap_test_inner: in handler");
271+
inner_trapped = true;
272+
0
273+
}).in {
274+
debug!("nested_trap_test_inner: in protected block");
275+
trouble(1);
276+
}
277+
278+
assert inner_trapped;
279+
}
280+
281+
#[test]
282+
fn nested_trap_test_outer() {
283+
284+
let sadness_condition : Condition<int,int> =
285+
Condition { key: sadness_key };
286+
287+
let mut outer_trapped = false;
288+
289+
do sadness_condition.trap(|_j| {
290+
debug!("nested_trap_test_outer: in handler");
291+
outer_trapped = true; 0
292+
}).in {
293+
debug!("nested_guard_test_outer: in protected block");
294+
nested_trap_test_inner();
295+
trouble(1);
296+
}
297+
298+
299+
assert outer_trapped;
300+
}

0 commit comments

Comments
 (0)