From 530909f2d8595447ef95e15326549ab7a51874e7 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 30 Dec 2013 09:36:26 -0800 Subject: Implement std::rt::at_exit This routine is currently only used to clean up the timer helper thread in the libnative implementation, but there are possibly other uses for this. The documentation is clear that the procedures are *not* run with any task context and hence have very little available to them. I also opted to disallow at_exit inside of at_exit and just abort the process at that point. --- src/libstd/rt/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/libstd/rt/mod.rs') diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs index 40e9a3ec5b2..7aa966802f2 100644 --- a/src/libstd/rt/mod.rs +++ b/src/libstd/rt/mod.rs @@ -127,6 +127,9 @@ mod util; // Global command line argument storage pub mod args; +// Support for running procedures when a program has exited. +mod at_exit_imp; + /// The default error code of the rust runtime if the main task fails instead /// of exiting cleanly. pub static DEFAULT_ERROR_CODE: int = 101; @@ -171,9 +174,27 @@ pub fn init(argc: int, argv: **u8) { env::init(); logging::init(); local_ptr::init(); + at_exit_imp::init(); } } +/// Enqueues a procedure to run when the runtime is cleaned up +/// +/// The procedure passed to this function will be executed as part of the +/// runtime cleanup phase. For normal rust programs, this means that it will run +/// after all other tasks have exited. +/// +/// The procedure is *not* executed with a local `Task` available to it, so +/// primitives like logging, I/O, channels, spawning, etc, are *not* available. +/// This is meant for "bare bones" usage to clean up runtime details, this is +/// not meant as a general-purpose "let's clean everything up" function. +/// +/// It is forbidden for procedures to register more `at_exit` handlers when they +/// are running, and doing so will lead to a process abort. +pub fn at_exit(f: proc()) { + at_exit_imp::push(f); +} + /// One-time runtime cleanup. /// /// This function is unsafe because it performs no checks to ensure that the @@ -184,6 +205,7 @@ pub fn init(argc: int, argv: **u8) { /// Invoking cleanup while portions of the runtime are still in use may cause /// undefined behavior. pub unsafe fn cleanup() { + at_exit_imp::run(); args::cleanup(); local_ptr::cleanup(); } -- cgit 1.4.1-3-g733a5