From 7171fec13f3a3091f702a8e55f495ad1563dc4cd Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Wed, 21 Apr 2021 16:21:10 +0200 Subject: [PATCH] Remove `Once` from `init` --- library/std/src/rt.rs | 5 ++++- library/std/src/sys_common/rt.rs | 11 ++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/library/std/src/rt.rs b/library/std/src/rt.rs index ce6e318c9de..1e19aff51f8 100644 --- a/library/std/src/rt.rs +++ b/library/std/src/rt.rs @@ -28,8 +28,11 @@ fn lang_start_internal( use crate::panic; use crate::sys_common; - sys_common::rt::init(argc, argv); + // SAFETY: Only called once during runtime initialization. + unsafe { sys_common::rt::init(argc, argv) }; + let exit_code = panic::catch_unwind(main); + sys_common::rt::cleanup(); exit_code.unwrap_or(101) as isize diff --git a/library/std/src/sys_common/rt.rs b/library/std/src/sys_common/rt.rs index c0c4a63cde9..c70f2ecc04e 100644 --- a/library/std/src/sys_common/rt.rs +++ b/library/std/src/sys_common/rt.rs @@ -1,3 +1,5 @@ +#![deny(unsafe_op_in_unsafe_fn)] + use crate::sync::Once; use crate::sys; use crate::sys_common::thread_info; @@ -5,12 +7,11 @@ use crate::thread::Thread; // One-time runtime initialization. // Runs before `main`. +// SAFETY: must be called only once during runtime initialization. // NOTE: this is not guaranteed to run, for example when Rust code is called externally. #[cfg_attr(test, allow(dead_code))] -pub fn init(argc: isize, argv: *const *const u8) { - static INIT: Once = Once::new(); - INIT.call_once(|| unsafe { - // SAFETY: Only called once during runtime initialization. +pub unsafe fn init(argc: isize, argv: *const *const u8) { + unsafe { sys::init(argc, argv); let main_guard = sys::thread::guard::init(); @@ -20,7 +21,7 @@ pub fn init(argc: isize, argv: *const *const u8) { // info about the stack bounds. let thread = Thread::new(Some("main".to_owned())); thread_info::set(main_guard, thread); - }); + } } // One-time runtime cleanup.