From daaec28c6f71f5d6e2f5bc716ffc2780ef56fa7b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 12 Dec 2013 17:51:54 -0800 Subject: [PATCH] std: Move management of the exit code to std::os Previously this functionality was located in std::rt::util, but there's no real reason for it to be located in there. --- src/libstd/os.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 8da7c0340f7..43a9390bfb6 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -28,8 +28,6 @@ #[allow(missing_doc)]; -#[cfg(unix)] -use c_str::CString; use clone::Clone; use container::Container; #[cfg(target_os = "macos")] @@ -43,6 +41,7 @@ use str; use to_str; use unstable::finally::Finally; +use sync::atomics::{AtomicInt, INIT_ATOMIC_INT, SeqCst}; pub use os::consts::*; @@ -58,6 +57,8 @@ pub fn close(fd: c_int) -> c_int { #[cfg(unix)] pub fn getcwd() -> Path { + use c_str::CString; + let mut buf = [0 as libc::c_char, ..BUF_BYTES]; unsafe { if libc::getcwd(buf.as_mut_ptr(), buf.len() as size_t).is_null() { @@ -675,17 +676,26 @@ fn FormatMessageW(flags: DWORD, strerror() } +static mut EXIT_STATUS: AtomicInt = INIT_ATOMIC_INT; + /** * Sets the process exit code * * Sets the exit code returned by the process if all supervised tasks * terminate successfully (without failing). If the current root task fails * and is supervised by the scheduler then any user-specified exit status is - * ignored and the process exits with the default failure status + * ignored and the process exits with the default failure status. + * + * Note that this is not synchronized against modifications of other threads. */ pub fn set_exit_status(code: int) { - use rt; - rt::set_exit_status(code); + unsafe { EXIT_STATUS.store(code, SeqCst) } +} + +/// Fetches the process's current exit code. This defaults to 0 and can change +/// by calling `set_exit_status`. +pub fn get_exit_status() -> int { + unsafe { EXIT_STATUS.load(SeqCst) } } #[cfg(target_os = "macos")]