Skip to content

Commit f036be9

Browse files
author
Ingo Molnar
committed
printk: introduce printk_once()
This pattern shows up frequently in the kernel: static int once = 1; ... if (once) { once = 0; printk(KERN_ERR "message\n"); } ... So add a printk_once() helper macro that reduces this to a single line of: printk_once(KERN_ERR "message\n"); It works analogously to WARN_ONCE() & friends. (We use a macro not an inline because vararg expansion in inlines looks awkward and the macro is simple enough.) Signed-off-by: Ingo Molnar <[email protected]>
1 parent eda58a8 commit f036be9

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

include/linux/kernel.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,19 @@ extern struct ratelimit_state printk_ratelimit_state;
242242
extern int printk_ratelimit(void);
243243
extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
244244
unsigned int interval_msec);
245+
246+
/*
247+
* Print a one-time message (analogous to WARN_ONCE() et al):
248+
*/
249+
#define printk_once(x...) ({ \
250+
static int __print_once = 1; \
251+
\
252+
if (__print_once) { \
253+
__print_once = 0; \
254+
printk(x); \
255+
} \
256+
})
257+
245258
#else
246259
static inline int vprintk(const char *s, va_list args)
247260
__attribute__ ((format (printf, 1, 0)));
@@ -253,6 +266,10 @@ static inline int printk_ratelimit(void) { return 0; }
253266
static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
254267
unsigned int interval_msec) \
255268
{ return false; }
269+
270+
/* No effect, but we still get type checking even in the !PRINTK case: */
271+
#define printk_once(x...) printk(x)
272+
256273
#endif
257274

258275
extern int printk_needs_cpu(int cpu);

0 commit comments

Comments
 (0)