]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
1 | //! Runtime services |
2 | //! | |
3 | //! The `rt` module provides a narrow set of runtime services, | |
4 | //! including the global heap (exported in `heap`) and unwinding and | |
5 | //! backtrace support. The APIs in this module are highly unstable, | |
6 | //! and should be considered as private implementation details for the | |
7 | //! time being. | |
8 | ||
60c5eb7d XL |
9 | #![unstable( |
10 | feature = "rt", | |
11 | reason = "this public module should not exist and is highly likely \ | |
12 | to disappear", | |
13 | issue = "0" | |
14 | )] | |
e9174d1e SL |
15 | #![doc(hidden)] |
16 | ||
2c00a5a8 | 17 | // Re-export some of our utilities which are expected by other crates. |
532ac7d7 | 18 | pub use crate::panicking::{begin_panic, begin_panic_fmt, update_panic_count}; |
e9174d1e | 19 | |
ff7c6d11 XL |
20 | // To reduce the generated code of the new `lang_start`, this function is doing |
21 | // the real work. | |
2c00a5a8 | 22 | #[cfg(not(test))] |
60c5eb7d XL |
23 | fn lang_start_internal( |
24 | main: &(dyn Fn() -> i32 + Sync + crate::panic::RefUnwindSafe), | |
25 | argc: isize, | |
26 | argv: *const *const u8, | |
27 | ) -> isize { | |
532ac7d7 XL |
28 | use crate::panic; |
29 | use crate::sys; | |
30 | use crate::sys_common; | |
31 | use crate::sys_common::thread_info; | |
32 | use crate::thread::Thread; | |
ff7c6d11 XL |
33 | |
34 | sys::init(); | |
35 | ||
36 | unsafe { | |
37 | let main_guard = sys::thread::guard::init(); | |
38 | sys::stack_overflow::init(); | |
39 | ||
40 | // Next, set up the current Thread with the guard information we just | |
41 | // created. Note that this isn't necessary in general for new threads, | |
42 | // but we just do this to name the main thread and to give it correct | |
43 | // info about the stack bounds. | |
44 | let thread = Thread::new(Some("main".to_owned())); | |
45 | thread_info::set(main_guard, thread); | |
46 | ||
47 | // Store our args if necessary in a squirreled away location | |
48 | sys::args::init(argc, argv); | |
49 | ||
50 | // Let's run some code! | |
ff7c6d11 | 51 | let exit_code = panic::catch_unwind(|| { |
532ac7d7 | 52 | sys_common::backtrace::__rust_begin_short_backtrace(move || main()) |
ff7c6d11 | 53 | }); |
ff7c6d11 XL |
54 | |
55 | sys_common::cleanup(); | |
56 | exit_code.unwrap_or(101) as isize | |
57 | } | |
58 | } | |
59 | ||
2c00a5a8 | 60 | #[cfg(not(test))] |
ff7c6d11 | 61 | #[lang = "start"] |
60c5eb7d XL |
62 | fn lang_start<T: crate::process::Termination + 'static>( |
63 | main: fn() -> T, | |
64 | argc: isize, | |
65 | argv: *const *const u8, | |
66 | ) -> isize { | |
ff7c6d11 XL |
67 | lang_start_internal(&move || main().report(), argc, argv) |
68 | } |