From 841c2c15beaa6fe0c2d1298c2d823a47f19b53e3 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Sun, 4 Feb 2024 20:17:58 +0100 Subject: [PATCH] introduce an async lock for stdin and stderr --- src/console.rs | 18 ++++++++++-------- src/fd/stdio.rs | 13 ++++++++----- src/lib.rs | 8 +------- src/macros.rs | 4 ++-- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/console.rs b/src/console.rs index 2d51bcd6e3..f22fe354bf 100644 --- a/src/console.rs +++ b/src/console.rs @@ -4,7 +4,7 @@ use hermit_sync::InterruptTicketMutex; use crate::arch; -pub struct Console(()); +pub(crate) struct Console(()); /// A collection of methods that are required to format /// a message to Hermit's console. @@ -21,14 +21,16 @@ impl fmt::Write for Console { } } -impl Console { - #[inline] - pub fn write_all(&mut self, buf: &[u8]) { - arch::output_message_buf(buf) - } -} - +#[cfg(feature = "newlib")] pub static CONSOLE: InterruptTicketMutex = InterruptTicketMutex::new(Console(())); +#[cfg(not(feature = "newlib"))] +static CONSOLE: InterruptTicketMutex = InterruptTicketMutex::new(Console(())); + +#[doc(hidden)] +pub fn _print(args: ::core::fmt::Arguments<'_>) { + use core::fmt::Write; + CONSOLE.lock().write_fmt(args).unwrap(); +} #[cfg(all(test, not(target_os = "none")))] mod tests { diff --git a/src/fd/stdio.rs b/src/fd/stdio.rs index f2940493ca..ba84177bae 100644 --- a/src/fd/stdio.rs +++ b/src/fd/stdio.rs @@ -2,17 +2,20 @@ use alloc::boxed::Box; #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] use core::ptr; +use async_lock::Mutex; use async_trait::async_trait; #[cfg(target_arch = "x86_64")] use x86::io::*; +use crate::arch; #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] use crate::arch::mm::{paging, VirtAddr}; -use crate::console::CONSOLE; use crate::fd::{IoError, ObjectInterface, PollEvent, STDERR_FILENO, STDOUT_FILENO}; const UHYVE_PORT_WRITE: u16 = 0x400; +static IO_LOCK: Mutex<()> = Mutex::new(()); + #[repr(C, packed)] struct SysWrite { fd: i32, @@ -95,8 +98,8 @@ impl ObjectInterface for GenericStdout { } async fn async_write(&self, buf: &[u8]) -> Result { - // stdin/err/out all go to console - CONSOLE.lock().write_all(buf); + let _guard = IO_LOCK.lock().await; + arch::output_message_buf(buf); Ok(buf.len()) } @@ -128,8 +131,8 @@ impl ObjectInterface for GenericStderr { } async fn async_write(&self, buf: &[u8]) -> Result { - // stdin/err/out all go to console - CONSOLE.lock().write_all(buf); + let _guard = IO_LOCK.lock().await; + arch::output_message_buf(buf); Ok(buf.len()) } diff --git a/src/lib.rs b/src/lib.rs index 9d388fac70..b3190aa5f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,7 +77,7 @@ mod logging; mod arch; mod config; -mod console; +pub mod console; mod drivers; mod entropy; mod env; @@ -95,12 +95,6 @@ pub mod time; #[cfg(target_os = "none")] hermit_entry::define_entry_version!(); -#[doc(hidden)] -pub fn _print(args: ::core::fmt::Arguments<'_>) { - use core::fmt::Write; - crate::console::CONSOLE.lock().write_fmt(args).unwrap(); -} - #[cfg(test)] #[cfg(target_os = "none")] #[no_mangle] diff --git a/src/macros.rs b/src/macros.rs index c127bab229..4dca430372 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -7,7 +7,7 @@ #[macro_export] macro_rules! print { ($($arg:tt)*) => {{ - $crate::_print(::core::format_args!($($arg)*)); + $crate::console::_print(::core::format_args!($($arg)*)); }}; } @@ -23,7 +23,7 @@ macro_rules! println { $crate::print!("\n") }; ($($arg:tt)*) => {{ - $crate::_print(::core::format_args!("{}\n", format_args!($($arg)*))); + $crate::console::_print(::core::format_args!("{}\n", format_args!($($arg)*))); }}; }