diff --git a/api/arceos_posix_api/src/imp/fd_ops.rs b/api/arceos_posix_api/src/imp/fd_ops.rs index 1b75583b04..cabfd27791 100644 --- a/api/arceos_posix_api/src/imp/fd_ops.rs +++ b/api/arceos_posix_api/src/imp/fd_ops.rs @@ -3,7 +3,7 @@ use core::ffi::c_int; use axerrno::{LinuxError, LinuxResult}; use axio::PollState; -use axns::{AxResource, def_resource}; +use axns::{ResArc, def_resource}; use flatten_objects::FlattenObjects; use spin::RwLock; @@ -23,7 +23,7 @@ pub trait FileLike: Send + Sync { def_resource! { #[allow(non_camel_case_types)] - pub(crate) static FD_TABLE: AxResource, AX_FILE_LIMIT>>> = AxResource::new(); + pub(crate) static FD_TABLE: ResArc, AX_FILE_LIMIT>>> = ResArc::new(); } pub fn get_file_like(fd: c_int) -> LinuxResult> { @@ -122,3 +122,22 @@ pub fn sys_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int { } }) } + +#[ctor_bare::register_ctor] +#[cfg(feature = "fd")] +fn init_stdio() { + use crate::imp::fd_ops::FD_TABLE; + use crate::imp::stdio::{stdin, stdout}; + use alloc::sync::Arc; + let mut fd_table = flatten_objects::FlattenObjects::new(); + fd_table + .add_at(0, Arc::new(stdin()) as _) + .unwrap_or_else(|_| panic!()); // stdin + fd_table + .add_at(1, Arc::new(stdout()) as _) + .unwrap_or_else(|_| panic!()); // stdout + fd_table + .add_at(2, Arc::new(stdout()) as _) + .unwrap_or_else(|_| panic!()); // stderr + FD_TABLE.init_new(spin::RwLock::new(fd_table)); +} diff --git a/api/arceos_posix_api/src/imp/mod.rs b/api/arceos_posix_api/src/imp/mod.rs index 0202f5c0e1..603f934baa 100644 --- a/api/arceos_posix_api/src/imp/mod.rs +++ b/api/arceos_posix_api/src/imp/mod.rs @@ -18,22 +18,3 @@ pub mod net; pub mod pipe; #[cfg(feature = "multitask")] pub mod pthread; - -#[ctor_bare::register_ctor] -#[cfg(feature = "fd")] -fn init_stdio() { - use crate::imp::fd_ops::FD_TABLE; - use alloc::sync::Arc; - use stdio::{stdin, stdout}; - let mut fd_table = flatten_objects::FlattenObjects::new(); - fd_table - .add_at(0, Arc::new(stdin()) as _) - .unwrap_or_else(|_| panic!()); // stdin - fd_table - .add_at(1, Arc::new(stdout()) as _) - .unwrap_or_else(|_| panic!()); // stdout - fd_table - .add_at(2, Arc::new(stdout()) as _) - .unwrap_or_else(|_| panic!()); // stderr - FD_TABLE.init_new(spin::RwLock::new(fd_table)); -} diff --git a/modules/axfs/src/root.rs b/modules/axfs/src/root.rs index 4bcebb5307..8dda269cd4 100644 --- a/modules/axfs/src/root.rs +++ b/modules/axfs/src/root.rs @@ -5,7 +5,7 @@ use alloc::{string::String, sync::Arc, vec::Vec}; use axerrno::{AxError, AxResult, ax_err}; use axfs_vfs::{VfsNodeAttr, VfsNodeOps, VfsNodeRef, VfsNodeType, VfsOps, VfsResult}; -use axns::{AxResource, def_resource}; +use axns::{ResArc, def_resource}; use axsync::Mutex; use lazyinit::LazyInit; @@ -13,9 +13,9 @@ use crate::{api::FileType, fs, mounts}; def_resource! { #[allow(non_camel_case_types)] - static CURRENT_DIR_PATH: AxResource> = AxResource::new(); + static CURRENT_DIR_PATH: ResArc> = ResArc::new(); #[allow(non_camel_case_types)] - static CURRENT_DIR: AxResource> = AxResource::new(); + static CURRENT_DIR: ResArc> = ResArc::new(); } struct MountPoint { diff --git a/modules/axns/src/lib.rs b/modules/axns/src/lib.rs index 641d17b22f..95ce32ea6d 100644 --- a/modules/axns/src/lib.rs +++ b/modules/axns/src/lib.rs @@ -113,9 +113,9 @@ impl Drop for AxNamespace { /// /// It provides methods to lazily initialize the resource of the current thread, /// or to share the resource with other threads. -pub struct AxResource(LazyInit>); +pub struct ResArc(LazyInit>); -impl AxResource { +impl ResArc { /// Creates a new uninitialized resource. pub const fn new() -> Self { Self(LazyInit::new()) @@ -142,7 +142,7 @@ impl AxResource { } } -impl Deref for AxResource { +impl Deref for ResArc { type Target = T; fn deref(&self) -> &Self::Target { @@ -150,7 +150,7 @@ impl Deref for AxResource { } } -impl fmt::Debug for AxResource { +impl fmt::Debug for ResArc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(f) } @@ -195,11 +195,11 @@ pub unsafe fn current_namespace_base() -> *mut u8 { /// # Example /// /// ``` -/// use axns::AxResource; +/// use axns::ResArc; /// /// axns::def_resource! { /// static FOO: u32 = 42; -/// static BAR: AxResource = AxResource::new(); +/// static BAR: ResArc = ResArc::new(); /// } /// /// BAR.init_new("hello world".to_string()); @@ -209,10 +209,10 @@ pub unsafe fn current_namespace_base() -> *mut u8 { /// mod imp { /// use axns::{AxNamespace, AxNamespaceIf}; /// -/// struct AxResourceImpl; +/// struct ResArcImpl; /// /// #[crate_interface::impl_interface] -/// impl AxNamespaceIf for AxResourceImpl { +/// impl AxNamespaceIf for ResArcImpl { /// fn current_namespace_base() -> *mut u8 { /// AxNamespace::global().base() /// } diff --git a/modules/axns/tests/test_global.rs b/modules/axns/tests/test_global.rs index 020341d8c8..00aaf4e430 100644 --- a/modules/axns/tests/test_global.rs +++ b/modules/axns/tests/test_global.rs @@ -2,13 +2,13 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Barrier, Mutex}; use std::thread; -use axns::{AxResource, def_resource}; +use axns::{ResArc, def_resource}; use self::imp::thread_init_namespace; def_resource! { - static FOO: AxResource = AxResource::new(); - static BAR: AxResource> = AxResource::new(); + static FOO: ResArc = ResArc::new(); + static BAR: ResArc> = ResArc::new(); } static BARRIER: Barrier = Barrier::new(3); diff --git a/modules/axns/tests/test_thread_local.rs b/modules/axns/tests/test_thread_local.rs index dc4a99901c..0b8eff465b 100644 --- a/modules/axns/tests/test_thread_local.rs +++ b/modules/axns/tests/test_thread_local.rs @@ -4,13 +4,13 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Barrier, Mutex}; use std::thread; -use axns::{AxResource, def_resource}; +use axns::{ResArc, def_resource}; use self::imp::thread_init_namespace; def_resource! { - static FOO: AxResource = AxResource::new(); - static BAR: AxResource> = AxResource::new(); + static FOO: ResArc = ResArc::new(); + static BAR: ResArc> = ResArc::new(); } static BARRIER: Barrier = Barrier::new(3); diff --git a/modules/axruntime/src/lib.rs b/modules/axruntime/src/lib.rs index f1d0844edc..1261f95116 100644 --- a/modules/axruntime/src/lib.rs +++ b/modules/axruntime/src/lib.rs @@ -183,11 +183,11 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! { init_tls(); } + ctor_bare::call_ctors(); + info!("Primary CPU {} init OK.", cpu_id); INITED_CPUS.fetch_add(1, Ordering::Relaxed); - ctor_bare::call_ctors(); - while !is_init_ok() { core::hint::spin_loop(); }