Skip to content

Commit

Permalink
rust: support for printk_once
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Guo <[email protected]>
  • Loading branch information
nbdd0121 committed May 26, 2021
1 parent 1173c1f commit 765221c
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions rust/kernel/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ pub fn call_printk_cont(lvl: LogLevel, args: fmt::Arguments<'_>) {
/// ```
#[macro_export]
macro_rules! printk (
(once, $($arg:tt)+) => {{
use ::core::sync::atomic::{AtomicBool, Ordering};

#[link_section = ".data.once"]
static ONCE: AtomicBool = AtomicBool::new(false);

if ONCE
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
.is_ok()
{
$crate::printk!($($arg)+);
}
}};

(target: $target:expr, $lvl:expr, $($arg:tt)+) => {{
$crate::print::call_printk(
$lvl,
Expand Down Expand Up @@ -176,6 +190,13 @@ macro_rules! printk (
/// ```
#[macro_export]
macro_rules! pr_emerg (
(once, target: $target:expr, $($arg:tt)+) => (
$crate::printk!(once, target: $target, $crate::print::LogLevel::EMERG, $($arg)+)
);
(once, $($arg:tt)+) => (
$crate::printk!(once, $crate::print::LogLevel::EMERG, $($arg)+)
);

(target: $target:expr, $($arg:tt)+) => (
$crate::printk!(target: $target, $crate::print::LogLevel::EMERG, $($arg)+)
);
Expand Down Expand Up @@ -203,6 +224,13 @@ macro_rules! pr_emerg (
/// ```
#[macro_export]
macro_rules! pr_alert (
(once, target: $target:expr, $($arg:tt)+) => (
$crate::printk!(once, target: $target, $crate::print::LogLevel::ALERT, $($arg)+)
);
(once, $($arg:tt)+) => (
$crate::printk!(once, $crate::print::LogLevel::ALERT, $($arg)+)
);

(target: $target:expr, $($arg:tt)+) => (
$crate::printk!(target: $target, $crate::print::LogLevel::ALERT, $($arg)+)
);
Expand Down Expand Up @@ -230,6 +258,13 @@ macro_rules! pr_alert (
/// ```
#[macro_export]
macro_rules! pr_crit (
(once, target: $target:expr, $($arg:tt)+) => (
$crate::printk!(once, target: $target, $crate::print::LogLevel::CRIT, $($arg)+)
);
(once, $($arg:tt)+) => (
$crate::printk!(once, $crate::print::LogLevel::CRIT, $($arg)+)
);

(target: $target:expr, $($arg:tt)+) => (
$crate::printk!(target: $target, $crate::print::LogLevel::CRIT, $($arg)+)
);
Expand Down Expand Up @@ -257,6 +292,13 @@ macro_rules! pr_crit (
/// ```
#[macro_export]
macro_rules! pr_err (
(once, target: $target:expr, $($arg:tt)+) => (
$crate::printk!(once, target: $target, $crate::print::LogLevel::ERR, $($arg)+)
);
(once, $($arg:tt)+) => (
$crate::printk!(once, $crate::print::LogLevel::ERR, $($arg)+)
);

(target: $target:expr, $($arg:tt)+) => (
$crate::printk!(target: $target, $crate::print::LogLevel::ERR, $($arg)+)
);
Expand Down Expand Up @@ -284,6 +326,13 @@ macro_rules! pr_err (
/// ```
#[macro_export]
macro_rules! pr_warn (
(once, target: $target:expr, $($arg:tt)+) => (
$crate::printk!(once, target: $target, $crate::print::LogLevel::WARNING, $($arg)+)
);
(once, $($arg:tt)+) => (
$crate::printk!(once, $crate::print::LogLevel::WARNING, $($arg)+)
);

(target: $target:expr, $($arg:tt)+) => (
$crate::printk!(target: $target, $crate::print::LogLevel::WARNING, $($arg)+)
);
Expand Down Expand Up @@ -311,6 +360,13 @@ macro_rules! pr_warn (
/// ```
#[macro_export]
macro_rules! pr_notice (
(once, target: $target:expr, $($arg:tt)+) => (
$crate::printk!(once, target: $target, $crate::print::LogLevel::NOTICE, $($arg)+)
);
(once, $($arg:tt)+) => (
$crate::printk!(once, $crate::print::LogLevel::NOTICE, $($arg)+)
);

(target: $target:expr, $($arg:tt)+) => (
$crate::printk!(target: $target, $crate::print::LogLevel::NOTICE, $($arg)+)
);
Expand Down Expand Up @@ -339,6 +395,13 @@ macro_rules! pr_notice (
#[macro_export]
#[doc(alias = "print")]
macro_rules! pr_info (
(once, target: $target:expr, $($arg:tt)+) => (
$crate::printk!(once, target: $target, $crate::print::LogLevel::INFO, $($arg)+)
);
(once, $($arg:tt)+) => (
$crate::printk!(once, $crate::print::LogLevel::INFO, $($arg)+)
);

(target: $target:expr, $($arg:tt)+) => (
$crate::printk!(target: $target, $crate::print::LogLevel::INFO, $($arg)+)
);
Expand Down

0 comments on commit 765221c

Please sign in to comment.