Introduce DO_COND(), DO_ONCE() and DO_ONCE_COND(). These
macros should ease the consolidation of CPP code when
it is deemed to be executed only once and/or after a
condition is verified. This incluse printk_once, WARN_ON,
WARN_ON_ONCE, etc...

Signed-off-by: Frederic Weisbecker <fweis...@gmail.com>
Cc: Andrew Morton <a...@linux-foundation.org>
Cc: Steven Rostedt <rost...@goodmis.org>
Cc: Linus Torvalds <torva...@linux-foundation.org>
Cc: H. Peter Anvin <h...@zytor.com>
Cc: Peter Zijlstra <pet...@infradead.org>
Cc: Thomas Gleixner <t...@linutronix.de>
Cc: Liu Chuansheng <chuansheng....@intel.com>
Cc: Ingo Molnar <mi...@kernel.org>
---
 include/linux/once.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 include/linux/once.h

diff --git a/include/linux/once.h b/include/linux/once.h
new file mode 100644
index 0000000..9399f37
--- /dev/null
+++ b/include/linux/once.h
@@ -0,0 +1,26 @@
+#ifndef _LINUX_ONCE_H
+#define _LINUX_ONCE_H
+
+#include <linux/compiler.h>
+
+#define DO_COND(condition, to_do) ({           \
+       int __ret = !!(condition);              \
+       if (unlikely(__ret)) {                  \
+               to_do;                          \
+       }                                       \
+       unlikely(__ret);                        \
+})
+
+#define DO_ONCE(to_do) ({                      \
+       static bool __done;                     \
+                                               \
+       if (!__done) {                          \
+               __done = true;                  \
+               to_do;                          \
+       }                                       \
+})
+
+#define DO_ONCE_COND(condition, to_do)         \
+       DO_COND(condition, DO_ONCE(to_do))
+
+#endif /* _LINUX_ONCE_H */
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to