]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys_common/rt.rs
Remove `sys_common::util::abort`
[rust.git] / library / std / src / sys_common / rt.rs
1 #![deny(unsafe_op_in_unsafe_fn)]
2 #![allow(unused_macros)]
3
4 use crate::sync::Once;
5 use crate::sys;
6 use crate::sys_common::thread_info;
7 use crate::thread::Thread;
8
9 // One-time runtime initialization.
10 // Runs before `main`.
11 // SAFETY: must be called only once during runtime initialization.
12 // NOTE: this is not guaranteed to run, for example when Rust code is called externally.
13 #[cfg_attr(test, allow(dead_code))]
14 pub unsafe fn init(argc: isize, argv: *const *const u8) {
15     unsafe {
16         sys::init(argc, argv);
17
18         let main_guard = sys::thread::guard::init();
19         // Next, set up the current Thread with the guard information we just
20         // created. Note that this isn't necessary in general for new threads,
21         // but we just do this to name the main thread and to give it correct
22         // info about the stack bounds.
23         let thread = Thread::new(Some("main".to_owned()));
24         thread_info::set(main_guard, thread);
25     }
26 }
27
28 // One-time runtime cleanup.
29 // Runs after `main` or at program exit.
30 // NOTE: this is not guaranteed to run, for example when the program aborts.
31 #[cfg_attr(test, allow(dead_code))]
32 pub fn cleanup() {
33     static CLEANUP: Once = Once::new();
34     CLEANUP.call_once(|| unsafe {
35         // Flush stdout and disable buffering.
36         crate::io::cleanup();
37         // SAFETY: Only called once during runtime cleanup.
38         sys::cleanup();
39     });
40 }
41
42 macro_rules! rterr {
43     ($($t:tt)*) => {
44         if let Some(mut out) = crate::sys::stdio::panic_output() {
45             let _ = crate::io::Write::write_fmt(&mut out, format_args!($($t)*));
46         }
47     }
48 }
49
50 macro_rules! rtabort {
51     ($($t:tt)*) => {
52         {
53             rterr!("fatal runtime error: {}\n", format_args!($($t)*));
54             crate::sys::abort_internal();
55         }
56     }
57 }
58
59 macro_rules! rtassert {
60     ($e:expr) => {
61         if !$e {
62             rtabort!(concat!("assertion failed: ", stringify!($e)));
63         }
64     };
65 }
66
67 macro_rules! rtunwrap {
68     ($ok:ident, $e:expr) => {
69         match $e {
70             $ok(v) => v,
71             ref err => {
72                 let err = err.as_ref().map(drop); // map Ok/Some which might not be Debug
73                 rtabort!(concat!("unwrap failed: ", stringify!($e), " = {:?}"), err)
74             }
75         }
76     };
77 }