Skip to content

Commit dd40636

Browse files
committed
Add assert_once_ever macro. Close #7748. (fixme cf #8472)
1 parent 7f26812 commit dd40636

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Diff for: src/libstd/macros.rs

+36
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,39 @@ macro_rules! rtabort(
4040
} )
4141
)
4242

43+
macro_rules! assert_once_ever(
44+
($( $msg:expr),+) => ( {
45+
// FIXME(#8472) extra function should not be needed to hide unsafe
46+
fn assert_once_ever() {
47+
unsafe {
48+
static mut already_happened: int = 0;
49+
// Double-check lock to avoid a swap in the common case.
50+
if already_happened != 0 ||
51+
::unstable::intrinsics::atomic_xchg_relaxed(&mut already_happened, 1) != 0 {
52+
fail!(fmt!("assert_once_ever happened twice: %s", fmt!($($msg),+)));
53+
}
54+
}
55+
}
56+
assert_once_ever();
57+
} )
58+
)
59+
60+
#[cfg(test)]
61+
mod tests {
62+
#[test]
63+
fn test_assert_once_ever_ok() {
64+
assert_once_ever!("help i'm stuck in an");
65+
assert_once_ever!("assertion error message");
66+
}
67+
68+
#[test] #[ignore(cfg(windows))] #[should_fail]
69+
fn test_assert_once_ever_fail() {
70+
use task;
71+
72+
fn f() { assert_once_ever!("if you're seeing this... good!") }
73+
74+
// linked & watched, naturally
75+
task::spawn(f);
76+
task::spawn(f);
77+
}
78+
}

Diff for: src/libstd/rt/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ fn run_(main: ~fn(), use_main_sched: bool) -> int {
323323
// task tree, shut down the schedulers and set the exit code.
324324
let handles = Cell::new(handles);
325325
let on_exit: ~fn(bool) = |exit_success| {
326+
assert_once_ever!("last task exiting");
326327

327328
let mut handles = handles.take();
328329
for handle in handles.mut_iter() {

0 commit comments

Comments
 (0)