Move most init to sys::init

This commit is contained in:
Christiaan Dirkx 2021-04-11 23:48:10 +02:00
parent cf470197ad
commit 11445c10ab
16 changed files with 31 additions and 57 deletions

View File

@ -37,7 +37,6 @@
#[path = "../unsupported/process.rs"] #[path = "../unsupported/process.rs"]
pub mod process; pub mod process;
pub mod rwlock; pub mod rwlock;
pub mod stack_overflow;
pub mod stdio; pub mod stdio;
pub mod thread; pub mod thread;
pub mod thread_local_dtor; pub mod thread_local_dtor;
@ -97,8 +96,9 @@ pub extern "C" fn __rust_abort() {
} }
// SAFETY: must be called only once during runtime initialization. // SAFETY: must be called only once during runtime initialization.
pub unsafe fn init() { pub unsafe fn init(argc: isize, argv: *const *const u8) {
let _ = net::init(); let _ = net::init();
args::init(argc, argv);
} }
// SAFETY: must be called only once during runtime cleanup. // SAFETY: must be called only once during runtime cleanup.

View File

@ -1,2 +0,0 @@
#[inline]
pub unsafe fn init() {}

View File

@ -32,7 +32,6 @@
#[path = "../unsupported/process.rs"] #[path = "../unsupported/process.rs"]
pub mod process; pub mod process;
pub mod rwlock; pub mod rwlock;
pub mod stack_overflow;
pub mod stdio; pub mod stdio;
pub mod thread; pub mod thread;
pub mod thread_local_key; pub mod thread_local_key;
@ -41,7 +40,11 @@
pub use crate::sys_common::os_str_bytes as os_str; pub use crate::sys_common::os_str_bytes as os_str;
// SAFETY: must be called only once during runtime initialization. // SAFETY: must be called only once during runtime initialization.
pub unsafe fn init() {} pub unsafe fn init(argc: isize, argv: *const *const u8) {
unsafe {
args::init(argc, argv);
}
}
// SAFETY: must be called only once during runtime cleanup. // SAFETY: must be called only once during runtime cleanup.
pub unsafe fn cleanup() {} pub unsafe fn cleanup() {}

View File

@ -1,2 +0,0 @@
#[cfg_attr(test, allow(dead_code))]
pub unsafe fn init() {}

View File

@ -45,7 +45,7 @@
pub use crate::sys_common::os_str_bytes as os_str; pub use crate::sys_common::os_str_bytes as os_str;
// SAFETY: must be called only once during runtime initialization. // SAFETY: must be called only once during runtime initialization.
pub unsafe fn init() { pub unsafe fn init(argc: isize, argv: *const *const u8) {
// The standard streams might be closed on application startup. To prevent // The standard streams might be closed on application startup. To prevent
// std::io::{stdin, stdout,stderr} objects from using other unrelated file // std::io::{stdin, stdout,stderr} objects from using other unrelated file
// resources opened later, we reopen standards streams when they are closed. // resources opened later, we reopen standards streams when they are closed.
@ -60,22 +60,22 @@ pub unsafe fn init() {
// to prevent this problem. // to prevent this problem.
reset_sigpipe(); reset_sigpipe();
cfg_if::cfg_if! { stack_overflow::init();
if #[cfg(miri)] { args::init(argc, argv);
// The standard fds are always available in Miri.
unsafe fn sanitize_standard_fds() {} unsafe fn sanitize_standard_fds() {
} else if #[cfg(not(any( cfg_if::cfg_if! {
target_os = "emscripten", if #[cfg(not(any(
target_os = "fuchsia", // The standard fds are always available in Miri.
target_os = "vxworks", miri,
// The poll on Darwin doesn't set POLLNVAL for closed fds. target_os = "emscripten",
target_os = "macos", target_os = "fuchsia",
target_os = "ios", target_os = "vxworks",
target_os = "redox", // The poll on Darwin doesn't set POLLNVAL for closed fds.
)))] { target_os = "macos",
// In the case when all file descriptors are open, the poll has been target_os = "ios",
// observed to perform better than fcntl (on GNU/Linux). target_os = "redox",
unsafe fn sanitize_standard_fds() { )))] {
use crate::sys::os::errno; use crate::sys::os::errno;
let pfds: &mut [_] = &mut [ let pfds: &mut [_] = &mut [
libc::pollfd { fd: 0, events: 0, revents: 0 }, libc::pollfd { fd: 0, events: 0, revents: 0 },
@ -100,9 +100,7 @@ unsafe fn sanitize_standard_fds() {
libc::abort(); libc::abort();
} }
} }
} } else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "redox"))] {
} else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "redox"))] {
unsafe fn sanitize_standard_fds() {
use crate::sys::os::errno; use crate::sys::os::errno;
for fd in 0..3 { for fd in 0..3 {
if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF { if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
@ -112,17 +110,13 @@ unsafe fn sanitize_standard_fds() {
} }
} }
} }
} else {
unsafe fn sanitize_standard_fds() {}
} }
} }
#[cfg(not(any(target_os = "emscripten", target_os = "fuchsia")))]
unsafe fn reset_sigpipe() { unsafe fn reset_sigpipe() {
#[cfg(not(any(target_os = "emscripten", target_os = "fuchsia")))]
assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR); assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR);
} }
#[cfg(any(target_os = "emscripten", target_os = "fuchsia"))]
unsafe fn reset_sigpipe() {}
} }
// SAFETY: must be called only once during runtime cleanup. // SAFETY: must be called only once during runtime cleanup.

View File

@ -1,7 +1,5 @@
use crate::ffi::OsString; use crate::ffi::OsString;
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
pub struct Args {} pub struct Args {}
pub fn args() -> Args { pub fn args() -> Args {

View File

@ -11,7 +11,7 @@ pub mod memchr {
use crate::os::raw::c_char; use crate::os::raw::c_char;
// SAFETY: must be called only once during runtime initialization. // SAFETY: must be called only once during runtime initialization.
pub unsafe fn init() {} pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
// SAFETY: must be called only once during runtime cleanup. // SAFETY: must be called only once during runtime cleanup.
pub unsafe fn cleanup() {} pub unsafe fn cleanup() {}

View File

@ -15,7 +15,6 @@
pub mod pipe; pub mod pipe;
pub mod process; pub mod process;
pub mod rwlock; pub mod rwlock;
pub mod stack_overflow;
pub mod stdio; pub mod stdio;
pub mod thread; pub mod thread;
#[cfg(target_thread_local)] #[cfg(target_thread_local)]

View File

@ -1 +0,0 @@
pub unsafe fn init() {}

View File

@ -5,8 +5,6 @@
use crate::os::wasi::ffi::OsStrExt; use crate::os::wasi::ffi::OsStrExt;
use crate::vec; use crate::vec;
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
pub struct Args { pub struct Args {
iter: vec::IntoIter<OsString>, iter: vec::IntoIter<OsString>,
_dont_send_or_sync_me: PhantomData<*mut ()>, _dont_send_or_sync_me: PhantomData<*mut ()>,

View File

@ -42,8 +42,6 @@
pub mod process; pub mod process;
#[path = "../unsupported/rwlock.rs"] #[path = "../unsupported/rwlock.rs"]
pub mod rwlock; pub mod rwlock;
#[path = "../unsupported/stack_overflow.rs"]
pub mod stack_overflow;
pub mod stdio; pub mod stdio;
pub mod thread; pub mod thread;
#[path = "../unsupported/thread_local_dtor.rs"] #[path = "../unsupported/thread_local_dtor.rs"]

View File

@ -2,10 +2,6 @@
use crate::marker::PhantomData; use crate::marker::PhantomData;
use crate::vec; use crate::vec;
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
// On wasm these should always be null, so there's nothing for us to do here
}
pub fn args() -> Args { pub fn args() -> Args {
Args { iter: Vec::new().into_iter(), _dont_send_or_sync_me: PhantomData } Args { iter: Vec::new().into_iter(), _dont_send_or_sync_me: PhantomData }
} }

View File

@ -35,8 +35,6 @@
pub mod pipe; pub mod pipe;
#[path = "../unsupported/process.rs"] #[path = "../unsupported/process.rs"]
pub mod process; pub mod process;
#[path = "../unsupported/stack_overflow.rs"]
pub mod stack_overflow;
#[path = "../unsupported/stdio.rs"] #[path = "../unsupported/stdio.rs"]
pub mod stdio; pub mod stdio;
pub mod thread; pub mod thread;

View File

@ -14,8 +14,6 @@
use core::iter; use core::iter;
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
pub fn args() -> Args { pub fn args() -> Args {
unsafe { unsafe {
let lp_cmd_line = c::GetCommandLineW(); let lp_cmd_line = c::GetCommandLineW();

View File

@ -50,7 +50,9 @@
} }
// SAFETY: must be called only once during runtime initialization. // SAFETY: must be called only once during runtime initialization.
pub unsafe fn init() {} pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
stack_overflow::init();
}
// SAFETY: must be called only once during runtime cleanup. // SAFETY: must be called only once during runtime cleanup.
pub unsafe fn cleanup() { pub unsafe fn cleanup() {

View File

@ -10,20 +10,15 @@ pub fn init(argc: isize, argv: *const *const u8) {
static INIT: Once = Once::new(); static INIT: Once = Once::new();
INIT.call_once(|| unsafe { INIT.call_once(|| unsafe {
// SAFETY: Only called once during runtime initialization. // SAFETY: Only called once during runtime initialization.
sys::init(); sys::init(argc, argv);
let main_guard = sys::thread::guard::init(); let main_guard = sys::thread::guard::init();
sys::stack_overflow::init();
// Next, set up the current Thread with the guard information we just // Next, set up the current Thread with the guard information we just
// created. Note that this isn't necessary in general for new threads, // 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 // but we just do this to name the main thread and to give it correct
// info about the stack bounds. // info about the stack bounds.
let thread = Thread::new(Some("main".to_owned())); let thread = Thread::new(Some("main".to_owned()));
thread_info::set(main_guard, thread); thread_info::set(main_guard, thread);
// Store our args if necessary in a squirreled away location
sys::args::init(argc, argv);
}); });
} }