Skip to content

Commit 0f02826

Browse files
committed
rust: support for printk_once
Signed-off-by: Gary Guo <[email protected]>
1 parent 7595f68 commit 0f02826

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

rust/kernel/print.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ pub fn call_printk_cont(lvl: LogLevel, args: fmt::Arguments<'_>) {
145145
/// ```
146146
#[macro_export]
147147
macro_rules! printk (
148+
(once, $($arg:tt)+) => {{
149+
use ::core::sync::atomic::{AtomicBool, Ordering};
150+
151+
#[link_section = ".data.once"]
152+
static ONCE: AtomicBool = AtomicBool::new(false);
153+
154+
if ONCE
155+
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
156+
.is_ok()
157+
{
158+
$crate::printk!($($arg)+);
159+
}
160+
}};
161+
148162
(target: $target:expr, $lvl:expr, $($arg:tt)+) => {{
149163
$crate::print::call_printk(
150164
$lvl,
@@ -176,6 +190,13 @@ macro_rules! printk (
176190
/// ```
177191
#[macro_export]
178192
macro_rules! pr_emerg (
193+
(once, target: $target:expr, $($arg:tt)+) => (
194+
$crate::printk!(once, target: $target, $crate::print::LogLevel::EMERG, $($arg)+)
195+
);
196+
(once, $($arg:tt)+) => (
197+
$crate::printk!(once, $crate::print::LogLevel::EMERG, $($arg)+)
198+
);
199+
179200
(target: $target:expr, $($arg:tt)+) => (
180201
$crate::printk!(target: $target, $crate::print::LogLevel::EMERG, $($arg)+)
181202
);
@@ -203,6 +224,13 @@ macro_rules! pr_emerg (
203224
/// ```
204225
#[macro_export]
205226
macro_rules! pr_alert (
227+
(once, target: $target:expr, $($arg:tt)+) => (
228+
$crate::printk!(once, target: $target, $crate::print::LogLevel::ALERT, $($arg)+)
229+
);
230+
(once, $($arg:tt)+) => (
231+
$crate::printk!(once, $crate::print::LogLevel::ALERT, $($arg)+)
232+
);
233+
206234
(target: $target:expr, $($arg:tt)+) => (
207235
$crate::printk!(target: $target, $crate::print::LogLevel::ALERT, $($arg)+)
208236
);
@@ -230,6 +258,13 @@ macro_rules! pr_alert (
230258
/// ```
231259
#[macro_export]
232260
macro_rules! pr_crit (
261+
(once, target: $target:expr, $($arg:tt)+) => (
262+
$crate::printk!(once, target: $target, $crate::print::LogLevel::CRIT, $($arg)+)
263+
);
264+
(once, $($arg:tt)+) => (
265+
$crate::printk!(once, $crate::print::LogLevel::CRIT, $($arg)+)
266+
);
267+
233268
(target: $target:expr, $($arg:tt)+) => (
234269
$crate::printk!(target: $target, $crate::print::LogLevel::CRIT, $($arg)+)
235270
);
@@ -257,6 +292,13 @@ macro_rules! pr_crit (
257292
/// ```
258293
#[macro_export]
259294
macro_rules! pr_err (
295+
(once, target: $target:expr, $($arg:tt)+) => (
296+
$crate::printk!(once, target: $target, $crate::print::LogLevel::ERR, $($arg)+)
297+
);
298+
(once, $($arg:tt)+) => (
299+
$crate::printk!(once, $crate::print::LogLevel::ERR, $($arg)+)
300+
);
301+
260302
(target: $target:expr, $($arg:tt)+) => (
261303
$crate::printk!(target: $target, $crate::print::LogLevel::ERR, $($arg)+)
262304
);
@@ -284,6 +326,13 @@ macro_rules! pr_err (
284326
/// ```
285327
#[macro_export]
286328
macro_rules! pr_warn (
329+
(once, target: $target:expr, $($arg:tt)+) => (
330+
$crate::printk!(once, target: $target, $crate::print::LogLevel::WARNING, $($arg)+)
331+
);
332+
(once, $($arg:tt)+) => (
333+
$crate::printk!(once, $crate::print::LogLevel::WARNING, $($arg)+)
334+
);
335+
287336
(target: $target:expr, $($arg:tt)+) => (
288337
$crate::printk!(target: $target, $crate::print::LogLevel::WARNING, $($arg)+)
289338
);
@@ -311,6 +360,13 @@ macro_rules! pr_warn (
311360
/// ```
312361
#[macro_export]
313362
macro_rules! pr_notice (
363+
(once, target: $target:expr, $($arg:tt)+) => (
364+
$crate::printk!(once, target: $target, $crate::print::LogLevel::NOTICE, $($arg)+)
365+
);
366+
(once, $($arg:tt)+) => (
367+
$crate::printk!(once, $crate::print::LogLevel::NOTICE, $($arg)+)
368+
);
369+
314370
(target: $target:expr, $($arg:tt)+) => (
315371
$crate::printk!(target: $target, $crate::print::LogLevel::NOTICE, $($arg)+)
316372
);
@@ -339,6 +395,13 @@ macro_rules! pr_notice (
339395
#[macro_export]
340396
#[doc(alias = "print")]
341397
macro_rules! pr_info (
398+
(once, target: $target:expr, $($arg:tt)+) => (
399+
$crate::printk!(once, target: $target, $crate::print::LogLevel::INFO, $($arg)+)
400+
);
401+
(once, $($arg:tt)+) => (
402+
$crate::printk!(once, $crate::print::LogLevel::INFO, $($arg)+)
403+
);
404+
342405
(target: $target:expr, $($arg:tt)+) => (
343406
$crate::printk!(target: $target, $crate::print::LogLevel::INFO, $($arg)+)
344407
);

0 commit comments

Comments
 (0)