1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Implementation of running at_exit routines
13 //! Documentation can be found on the `rt::at_exit` function.
19 use option::{Some, None};
22 use unstable::sync::Exclusive;
23 use slice::OwnedVector;
26 type Queue = Exclusive<Vec<proc():Send>>;
28 // You'll note that these variables are *not* atomic, and this is done on
29 // purpose. This module is designed to have init() called *once* in a
30 // single-task context, and then run() is called only once in another
31 // single-task context. As a result of this, only the `push` function is
32 // thread-safe, and it assumes that the `init` function has run previously.
33 static mut QUEUE: *mut Queue = 0 as *mut Queue;
34 static mut RUNNING: bool = false;
39 rtassert!(QUEUE.is_null());
40 let state: Box<Queue> = box Exclusive::new(vec!());
41 QUEUE = cast::transmute(state);
45 pub fn push(f: proc():Send) {
48 rtassert!(!QUEUE.is_null());
49 let state: &mut Queue = cast::transmute(QUEUE);
52 arr.push(f.take_unwrap());
60 rtassert!(!QUEUE.is_null());
62 let state: Box<Queue> = cast::transmute(QUEUE);
63 QUEUE = 0 as *mut Queue;
66 vec = Some(mem::replace(arr, vec!()));
72 for f in vec.move_iter() {