Kernel: Add try_print! and try_println!

This commit is contained in:
apio 2022-11-01 10:55:42 +01:00
parent 6a41c4fc4e
commit 523130a69e

View File

@ -1,19 +1,48 @@
use crate::arch::io::SERIAL; use crate::arch::io::SERIAL;
use core::fmt; use core::fmt;
/// Locks the serial port and writes formatted output.
#[macro_export] #[macro_export]
macro_rules! print { macro_rules! print {
($($arg:tt)*) => ($crate::log::_print(format_args!($($arg)*))); ($($arg:tt)*) => ($crate::log::_print(format_args!($($arg)*)));
} }
/// Tries to lock the serial port and writes formatted output.
/// Should be used in interrupt handlers, as it does not block but silently fails.
/// (If we are blocked waiting for interrupted code to unlock a lock, that's an instant deadlock unless we are on SMP and another CPU schedules that code)
#[macro_export]
macro_rules! try_print {
($($arg:tt)*) => ($crate::log::_try_print(format_args!($($arg)*)));
}
/// Locks the serial port and writes formatted output with a trailing newline.
#[macro_export] #[macro_export]
macro_rules! println { macro_rules! println {
() => ($crate::print!("\n")); () => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*))); ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
} }
/// Tries to lock the serial port and writes formatted output with a trailing newline.
/// Should be used in interrupt handlers, as it does not block but silently fails.
/// (If we are blocked waiting for interrupted code to unlock a lock, that's an instant deadlock unless we are on SMP and another CPU schedules that code)
#[macro_export]
macro_rules! try_println {
() => ($crate::try_print!("\n"));
($($arg:tt)*) => ($crate::try_print!("{}\n", format_args!($($arg)*)));
}
#[doc(hidden)] #[doc(hidden)]
pub fn _print(args: fmt::Arguments) { pub fn _print(args: fmt::Arguments) {
use core::fmt::Write; use core::fmt::Write;
SERIAL.lock().write_fmt(args).unwrap(); SERIAL.lock().write_fmt(args).unwrap();
}
#[doc(hidden)]
pub fn _try_print(args: fmt::Arguments) {
use core::fmt::Write;
let serial = SERIAL.try_lock();
if serial.is_some()
{
serial.unwrap().write_fmt(args).unwrap();
}
} }