mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Frederic Weisbecker <fweisbec@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Liu Chuansheng <chuansheng.liu@intel.com>,
	Ingo Molnar <mingo@kernel.org>
Subject: [PATCH 1/3] core: New macro to execute code only once
Date: Tue, 15 Oct 2013 22:50:50 +0200	[thread overview]
Message-ID: <1381870252-5430-2-git-send-email-fweisbec@gmail.com> (raw)
In-Reply-To: <1381870252-5430-1-git-send-email-fweisbec@gmail.com>

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 <fweisbec@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Liu Chuansheng <chuansheng.liu@intel.com>
Cc: Ingo Molnar <mingo@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


  reply	other threads:[~2013-10-15 20:51 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-15 20:50 [RFC PATCH 0/3] headers: Consolidate once/cond style macros Frederic Weisbecker
2013-10-15 20:50 ` Frederic Weisbecker [this message]
2013-10-15 20:50 ` [PATCH 2/3] core: Convert printk_once to use DO_ONCE Frederic Weisbecker
2013-10-15 21:00   ` Joe Perches
2013-10-15 21:12     ` Frederic Weisbecker
2013-10-15 21:24       ` Joe Perches
2013-10-16 11:53         ` Frederic Weisbecker
2013-10-16 12:59           ` Steven Rostedt
2013-10-16 13:21             ` Frederic Weisbecker
2013-10-15 20:50 ` [PATCH 3/3] bug: Convert warn macros to use once and cond helpers Frederic Weisbecker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1381870252-5430-2-git-send-email-fweisbec@gmail.com \
    --to=fweisbec@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=chuansheng.liu@intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome