]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt.rs
Format libstd/sys with rustfmt
[rust.git] / src / libstd / rt.rs
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
9 #![unstable(feature = "rt",
10             reason = "this public module should not exist and is highly likely \
11                       to disappear",
12             issue = "0")]
13 #![doc(hidden)]
14
15
16 // Re-export some of our utilities which are expected by other crates.
17 pub use crate::panicking::{begin_panic, begin_panic_fmt, update_panic_count};
18
19 // To reduce the generated code of the new `lang_start`, this function is doing
20 // the real work.
21 #[cfg(not(test))]
22 fn lang_start_internal(main: &(dyn Fn() -> i32 + Sync + crate::panic::RefUnwindSafe),
23                        argc: isize, argv: *const *const u8) -> isize {
24     use crate::panic;
25     use crate::sys;
26     use crate::sys_common;
27     use crate::sys_common::thread_info;
28     use crate::thread::Thread;
29
30     sys::init();
31
32     unsafe {
33         let main_guard = sys::thread::guard::init();
34         sys::stack_overflow::init();
35
36         // Next, set up the current Thread with the guard information we just
37         // created. Note that this isn't necessary in general for new threads,
38         // but we just do this to name the main thread and to give it correct
39         // info about the stack bounds.
40         let thread = Thread::new(Some("main".to_owned()));
41         thread_info::set(main_guard, thread);
42
43         // Store our args if necessary in a squirreled away location
44         sys::args::init(argc, argv);
45
46         // Let's run some code!
47         let exit_code = panic::catch_unwind(|| {
48             sys_common::backtrace::__rust_begin_short_backtrace(move || main())
49         });
50
51         sys_common::cleanup();
52         exit_code.unwrap_or(101) as isize
53     }
54 }
55
56 #[cfg(not(test))]
57 #[lang = "start"]
58 fn lang_start<T: crate::process::Termination + 'static>
59     (main: fn() -> T, argc: isize, argv: *const *const u8) -> isize
60 {
61     lang_start_internal(&move || main().report(), argc, argv)
62 }