summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2013-06-19 00:39:10 -0700
committerBrian Anderson <banderson@mozilla.com>2013-06-19 00:39:10 -0700
commit915aaa7f67671186348b1b6c10d765a3d9ab6e37 (patch)
treeb5b6e0c11a29b90ec86dfd8b0042c7f8e67e5d96 /src/libstd
parent29ad8e15a2b7e2024941d74ea4ce261cb501ded9 (diff)
downloadrust-915aaa7f67671186348b1b6c10d765a3d9ab6e37.tar.gz
rust-915aaa7f67671186348b1b6c10d765a3d9ab6e37.zip
std::rt: Set the process exit code
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/rt/mod.rs24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs
index 581e3addff0..899fa171b72 100644
--- a/src/libstd/rt/mod.rs
+++ b/src/libstd/rt/mod.rs
@@ -74,6 +74,8 @@ use rt::task::Task;
 use rt::thread::Thread;
 use rt::work_queue::WorkQueue;
 use rt::uv::uvio::UvEventLoop;
+use unstable::atomics::{AtomicInt, SeqCst};
+use unstable::sync::UnsafeAtomicRcBox;
 use vec::{OwnedVector, MutableVector};
 
 /// The global (exchange) heap.
@@ -174,10 +176,10 @@ pub mod util;
 pub fn start(_argc: int, _argv: **u8, crate_map: *u8, main: ~fn()) -> int {
 
     init(crate_map);
-    run(main);
+    let exit_code = run(main);
     cleanup();
 
-    return 0;
+    return exit_code;
 }
 
 /// One-time runtime initialization. Currently all this does is set up logging
@@ -190,7 +192,9 @@ pub fn cleanup() {
     global_heap::cleanup();
 }
 
-pub fn run(main: ~fn()) {
+pub fn run(main: ~fn()) -> int {
+    static DEFAULT_ERROR_CODE: int = 101;
+
     let nthreads = match os::getenv("RUST_THREADS") {
         Some(nstr) => FromStr::from_str(nstr).get(),
         None => unsafe {
@@ -216,10 +220,13 @@ pub fn run(main: ~fn()) {
         scheds.push(sched);
     }
 
+    let exit_code = UnsafeAtomicRcBox::new(AtomicInt::new(0));
+    let exit_code_clone = exit_code.clone();
+
     let main_cell = Cell::new(main);
     let handles = Cell::new(handles);
     let mut new_task = ~Task::new_root();
-    let on_exit: ~fn(bool) = |exit_status| {
+    let on_exit: ~fn(bool) = |exit_success| {
 
         let mut handles = handles.take();
         // Tell schedulers to exit
@@ -227,7 +234,10 @@ pub fn run(main: ~fn()) {
             handle.send(Shutdown);
         }
 
-        rtassert!(exit_status);
+        unsafe {
+            let exit_code = if exit_success { 0 } else { DEFAULT_ERROR_CODE };
+            (*exit_code_clone.get()).store(exit_code, SeqCst);
+        }
     };
     new_task.on_exit = Some(on_exit);
     let main_task = ~Coroutine::with_task(&mut scheds[0].stack_pool,
@@ -249,6 +259,10 @@ pub fn run(main: ~fn()) {
 
     // Wait for schedulers
     let _threads = threads;
+
+    unsafe {
+        (*exit_code.get()).load(SeqCst)
+    }
 }
 
 /// Possible contexts in which Rust code may be executing.