public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] add function spin_event_timeout()
@ 2009-03-09 15:18 Timur Tabi
  2009-03-09 15:31 ` Will Newton
  2009-03-09 16:06 ` Peter Zijlstra
  0 siblings, 2 replies; 5+ messages in thread
From: Timur Tabi @ 2009-03-09 15:18 UTC (permalink / raw)
  To: linux-kernel, rdreier, jirislaby

The function spin_event_timeout() takes a condition and timeout value
(in jiffies) as parameters.  It spins until either the condition is true
or the timeout expires.  It returns non-zero if the condition is true,
zero otherwise.

Signed-off-by: Timur Tabi <timur@freescale.com>
---

v2: changes based on feedback

 include/linux/delay.h |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/include/linux/delay.h b/include/linux/delay.h
index fd832c6..235ca25 100644
--- a/include/linux/delay.h
+++ b/include/linux/delay.h
@@ -51,4 +51,26 @@ static inline void ssleep(unsigned int seconds)
 	msleep(seconds * 1000);
 }
 
+/**
+ * spin_event_timeout - spin until a condition gets true or a timeout elapses
+ * @condition: a C expression for the event to wait for
+ * @timeout: timeout, in jiffies
+ *
+ * The process spins until the @condition evaluates to true or the @timeout
+ * elapses.
+ *
+ * The function returns non-zero if the @condition evaluated to true, or
+ * zero if the @timeout elapsed.  If both occurs (e.g. the loop was
+ * pre-empted and the @condition became true in the meantime, but when the
+ * loop resumed the @timeout had already elapsed), then non-zero will be
+ * returned.
+ */
+#define spin_event_timeout(condition, timeout)			\
+({								\
+	unsigned long __timeout = jiffies + (timeout);		\
+	while (!(condition) && time_before(jiffies, __timeout))	\
+		cpu_relax();					\
+	(condition);						\
+})
+
 #endif /* defined(_LINUX_DELAY_H) */
-- 
1.6.1.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-03-09 18:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-09 15:18 [PATCH v2] add function spin_event_timeout() Timur Tabi
2009-03-09 15:31 ` Will Newton
2009-03-09 15:34   ` Timur Tabi
2009-03-09 16:06 ` Peter Zijlstra
2009-03-09 18:16   ` Timur Tabi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox