Skip to content

Commit 915aaa7

Browse files
committed
std::rt: Set the process exit code
1 parent 29ad8e1 commit 915aaa7

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/libstd/rt/mod.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ use rt::task::Task;
7474
use rt::thread::Thread;
7575
use rt::work_queue::WorkQueue;
7676
use rt::uv::uvio::UvEventLoop;
77+
use unstable::atomics::{AtomicInt, SeqCst};
78+
use unstable::sync::UnsafeAtomicRcBox;
7779
use vec::{OwnedVector, MutableVector};
7880

7981
/// The global (exchange) heap.
@@ -174,10 +176,10 @@ pub mod util;
174176
pub fn start(_argc: int, _argv: **u8, crate_map: *u8, main: ~fn()) -> int {
175177

176178
init(crate_map);
177-
run(main);
179+
let exit_code = run(main);
178180
cleanup();
179181

180-
return 0;
182+
return exit_code;
181183
}
182184

183185
/// One-time runtime initialization. Currently all this does is set up logging
@@ -190,7 +192,9 @@ pub fn cleanup() {
190192
global_heap::cleanup();
191193
}
192194

193-
pub fn run(main: ~fn()) {
195+
pub fn run(main: ~fn()) -> int {
196+
static DEFAULT_ERROR_CODE: int = 101;
197+
194198
let nthreads = match os::getenv("RUST_THREADS") {
195199
Some(nstr) => FromStr::from_str(nstr).get(),
196200
None => unsafe {
@@ -216,18 +220,24 @@ pub fn run(main: ~fn()) {
216220
scheds.push(sched);
217221
}
218222

223+
let exit_code = UnsafeAtomicRcBox::new(AtomicInt::new(0));
224+
let exit_code_clone = exit_code.clone();
225+
219226
let main_cell = Cell::new(main);
220227
let handles = Cell::new(handles);
221228
let mut new_task = ~Task::new_root();
222-
let on_exit: ~fn(bool) = |exit_status| {
229+
let on_exit: ~fn(bool) = |exit_success| {
223230

224231
let mut handles = handles.take();
225232
// Tell schedulers to exit
226233
for handles.mut_iter().advance |handle| {
227234
handle.send(Shutdown);
228235
}
229236

230-
rtassert!(exit_status);
237+
unsafe {
238+
let exit_code = if exit_success { 0 } else { DEFAULT_ERROR_CODE };
239+
(*exit_code_clone.get()).store(exit_code, SeqCst);
240+
}
231241
};
232242
new_task.on_exit = Some(on_exit);
233243
let main_task = ~Coroutine::with_task(&mut scheds[0].stack_pool,
@@ -249,6 +259,10 @@ pub fn run(main: ~fn()) {
249259

250260
// Wait for schedulers
251261
let _threads = threads;
262+
263+
unsafe {
264+
(*exit_code.get()).load(SeqCst)
265+
}
252266
}
253267

254268
/// Possible contexts in which Rust code may be executing.

0 commit comments

Comments
 (0)