Skip to content

Commit

Permalink
introduce an async lock for stdin and stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Feb 4, 2024
1 parent 24a3b22 commit 841c2c1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
18 changes: 10 additions & 8 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Console> = InterruptTicketMutex::new(Console(()));
#[cfg(not(feature = "newlib"))]
static CONSOLE: InterruptTicketMutex<Console> = 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 {
Expand Down
13 changes: 8 additions & 5 deletions src/fd/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -95,8 +98,8 @@ impl ObjectInterface for GenericStdout {
}

async fn async_write(&self, buf: &[u8]) -> Result<usize, IoError> {
// 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())
}
Expand Down Expand Up @@ -128,8 +131,8 @@ impl ObjectInterface for GenericStderr {
}

async fn async_write(&self, buf: &[u8]) -> Result<usize, IoError> {
// 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())
}
Expand Down
8 changes: 1 addition & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod logging;

mod arch;
mod config;
mod console;
pub mod console;
mod drivers;
mod entropy;
mod env;
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{
$crate::_print(::core::format_args!($($arg)*));
$crate::console::_print(::core::format_args!($($arg)*));
}};
}

Expand All @@ -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)*)));
}};
}

Expand Down

0 comments on commit 841c2c1

Please sign in to comment.