rust/src/test/run-pass/fail-during-tld-destroy.rs
Alex Crichton 7d756e44a9 rustrt: Reorganize task usage
Most of the comments are available on the Task structure itself, but this commit
is aimed at making FFI-style usage of Rust tasks a little nicer.

Primarily, this commit enables re-use of tasks across multiple invocations. The
method `run` will no longer unconditionally destroy the task itself. Rather, the
task will be internally re-usable if the closure specified did not fail. Once a
task has failed once it is considered poisoned and it can never be used again.

Along the way I tried to document shortcomings of the current method of tearing
down a task, opening a few issues as well. For now none of the behavior is a
showstopper, but it's useful to acknowledge it. Also along the way I attempted
to remove as much `unsafe` code as possible, opting for safer abstractions.
2014-06-26 12:40:27 -07:00

54 lines
1.2 KiB
Rust

// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::task;
use std::gc::{GC, Gc};
use std::cell::RefCell;
static mut DROPS: uint = 0;
struct Foo(bool);
impl Drop for Foo {
fn drop(&mut self) {
let Foo(fail) = *self;
unsafe { DROPS += 1; }
if fail { fail!() }
}
}
fn tld_fail(fail: bool) {
local_data_key!(foo: Foo);
foo.replace(Some(Foo(fail)));
}
fn gc_fail(fail: bool) {
struct A {
inner: RefCell<Option<Gc<A>>>,
other: Foo,
}
let a = box(GC) A {
inner: RefCell::new(None),
other: Foo(fail),
};
*a.inner.borrow_mut() = Some(a.clone());
}
fn main() {
let _ = task::try(proc() {
tld_fail(true);
gc_fail(false);
});
unsafe {
assert_eq!(DROPS, 2);
}
}