Skip to content

Commit 07e52eb

Browse files
committed
std: Make os::set_exit_status work with newsched
1 parent ec6d4a1 commit 07e52eb

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

src/libstd/os.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,15 @@ pub fn last_os_error() -> ~str {
11341134
* ignored and the process exits with the default failure status
11351135
*/
11361136
pub fn set_exit_status(code: int) {
1137-
unsafe {
1138-
rustrt::rust_set_exit_status(code as libc::intptr_t);
1137+
use rt;
1138+
use rt::OldTaskContext;
1139+
1140+
if rt::context() == OldTaskContext {
1141+
unsafe {
1142+
rustrt::rust_set_exit_status(code as libc::intptr_t);
1143+
}
1144+
} else {
1145+
rt::util::set_exit_status(code);
11391146
}
11401147
}
11411148

src/libstd/rt/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,15 @@ pub fn run(main: ~fn()) -> int {
260260
}
261261

262262
unsafe {
263-
let exit_code = if exit_success { 0 } else { DEFAULT_ERROR_CODE };
263+
let exit_code = if exit_success {
264+
use rt::util;
265+
266+
// If we're exiting successfully, then return the global
267+
// exit status, which can be set programmatically.
268+
util::get_exit_status()
269+
} else {
270+
DEFAULT_ERROR_CODE
271+
};
264272
(*exit_code_clone.get()).store(exit_code, SeqCst);
265273
}
266274
};

src/libstd/rt/util.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,25 @@ memory and partly incapable of presentation to others.",
9797

9898
unsafe { libc::abort(); }
9999
}
100+
101+
pub fn set_exit_status(code: int) {
102+
103+
unsafe {
104+
return rust_set_exit_status_newrt(code as libc::uintptr_t);
105+
}
106+
107+
extern {
108+
fn rust_set_exit_status_newrt(code: libc::uintptr_t);
109+
}
110+
}
111+
112+
pub fn get_exit_status() -> int {
113+
114+
unsafe {
115+
return rust_get_exit_status_newrt() as int;
116+
}
117+
118+
extern {
119+
fn rust_get_exit_status_newrt() -> libc::uintptr_t;
120+
}
121+
}

src/rt/rust_builtin.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,21 @@ rust_get_global_args_ptr() {
960960
return &global_args_ptr;
961961
}
962962

963+
static lock_and_signal exit_status_lock;
964+
static uintptr_t exit_status = 0;
965+
966+
extern "C" CDECL void
967+
rust_set_exit_status_newrt(uintptr_t code) {
968+
scoped_lock with(exit_status_lock);
969+
exit_status = code;
970+
}
971+
972+
extern "C" CDECL uintptr_t
973+
rust_get_exit_status_newrt() {
974+
scoped_lock with(exit_status_lock);
975+
return exit_status;
976+
}
977+
963978
//
964979
// Local Variables:
965980
// mode: C++

src/rt/rustrt.def.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,5 @@ rust_get_global_args_ptr
270270
rust_current_boxed_region
271271
rust_take_global_args_lock
272272
rust_drop_global_args_lock
273+
rust_set_exit_status_newrt
274+
rust_get_exit_status_newrt

0 commit comments

Comments
 (0)