Re: [PATCH 1/3] kernel.h: Add DO_ONCE statement expression macro

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 2009-05-21 at 16:27 -0700, Randy Dunlap wrote:
> Joe Perches wrote:
> > Add a DO_ONCE statement expression analogous to printk_once
> > that executes any arbitrary statement exactly once.
> Since x is of arbitrary size & time duration, I think that x; should follow
> 		__done = true;
> instead of being before it, as was done in printk_once().  Otherwise there
> could be a sizable window there x could be done more than once.

Makes sense to me.

Add a DO_ONCE statement expression analogous to printk_once
that executes any arbitrary statement exactly once.

This will take the place of printk_once so that
DO_ONCE(pr_<foo>) or any other statement performed
a single time may be easily written.

Signed-off-by: Joe Perches <joe@xxxxxxxxxxx>
---
 include/linux/kernel.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 883cd44..a5520c9 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -637,7 +637,18 @@ static inline void ftrace_dump(void) { }
 #define swap(a, b) \
 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
 
+/*
+ * Do something once (analogous to WARN_ONCE() et al):
+ */
+#define DO_ONCE(x...) ({			\
+	static bool __done = false;		\
+						\
+	if (!__done) {				\
+		__done = true;			\
+		x;				\
+	}					\
+})
+
 /**
  * container_of - cast a member of a structure out to the containing structure
  * @ptr:	the pointer to the member.
-- 
1.6.3.1.10.g659a0.dirty



--
To unsubscribe from this list: send the line "unsubscribe cpufreq" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Devel]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Forum]     [Linux SCSI]

  Powered by Linux