2015-09-08 15:53:46 -07:00
|
|
|
//! Runtime services
|
|
|
|
//!
|
|
|
|
//! The `rt` module provides a narrow set of runtime services,
|
|
|
|
//! including the global heap (exported in `heap`) and unwinding and
|
|
|
|
//! backtrace support. The APIs in this module are highly unstable,
|
|
|
|
//! and should be considered as private implementation details for the
|
|
|
|
//! time being.
|
|
|
|
|
|
|
|
#![unstable(feature = "rt",
|
|
|
|
reason = "this public module should not exist and is highly likely \
|
|
|
|
to disappear",
|
|
|
|
issue = "0")]
|
|
|
|
#![doc(hidden)]
|
|
|
|
|
2015-12-18 13:29:49 +01:00
|
|
|
|
2018-01-12 16:41:25 -05:00
|
|
|
// Re-export some of our utilities which are expected by other crates.
|
2019-02-11 04:23:21 +09:00
|
|
|
pub use crate::panicking::{begin_panic, begin_panic_fmt, update_panic_count};
|
2015-09-08 15:53:46 -07:00
|
|
|
|
2017-12-24 00:15:38 +01:00
|
|
|
// To reduce the generated code of the new `lang_start`, this function is doing
|
|
|
|
// the real work.
|
2018-01-01 17:53:47 -08:00
|
|
|
#[cfg(not(test))]
|
2019-02-11 04:23:21 +09:00
|
|
|
fn lang_start_internal(main: &(dyn Fn() -> i32 + Sync + crate::panic::RefUnwindSafe),
|
2017-12-26 23:52:20 +01:00
|
|
|
argc: isize, argv: *const *const u8) -> isize {
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::panic;
|
|
|
|
use crate::sys;
|
|
|
|
use crate::sys_common;
|
|
|
|
use crate::sys_common::thread_info;
|
|
|
|
use crate::thread::Thread;
|
2017-12-03 22:16:24 +01:00
|
|
|
|
|
|
|
sys::init();
|
|
|
|
|
2017-12-26 23:52:20 +01:00
|
|
|
unsafe {
|
2017-12-03 22:16:24 +01:00
|
|
|
let main_guard = sys::thread::guard::init();
|
|
|
|
sys::stack_overflow::init();
|
|
|
|
|
|
|
|
// Next, set up the current Thread with the guard information we just
|
|
|
|
// created. Note that this isn't necessary in general for new threads,
|
|
|
|
// but we just do this to name the main thread and to give it correct
|
|
|
|
// info about the stack bounds.
|
|
|
|
let thread = Thread::new(Some("main".to_owned()));
|
|
|
|
thread_info::set(main_guard, thread);
|
|
|
|
|
|
|
|
// Store our args if necessary in a squirreled away location
|
|
|
|
sys::args::init(argc, argv);
|
|
|
|
|
|
|
|
// Let's run some code!
|
|
|
|
let exit_code = panic::catch_unwind(|| {
|
2019-02-11 04:23:21 +09:00
|
|
|
sys_common::backtrace::__rust_begin_short_backtrace(move || main())
|
2017-12-03 22:16:24 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
sys_common::cleanup();
|
2017-12-26 23:52:20 +01:00
|
|
|
exit_code.unwrap_or(101) as isize
|
|
|
|
}
|
2017-12-03 22:16:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-01 17:53:47 -08:00
|
|
|
#[cfg(not(test))]
|
2017-12-24 00:15:38 +01:00
|
|
|
#[lang = "start"]
|
2019-02-11 04:23:21 +09:00
|
|
|
fn lang_start<T: crate::process::Termination + 'static>
|
2017-12-26 23:52:20 +01:00
|
|
|
(main: fn() -> T, argc: isize, argv: *const *const u8) -> isize
|
2017-12-24 00:15:38 +01:00
|
|
|
{
|
2017-12-24 00:35:38 +01:00
|
|
|
lang_start_internal(&move || main().report(), argc, argv)
|
2017-12-24 00:15:38 +01:00
|
|
|
}
|