[PATCH 1/5] Add a timeout polling loop convenience wrapper

Sascha Hauer s.hauer at pengutronix.de
Thu Feb 9 08:39:54 EST 2012


Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 include/clock.h |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/include/clock.h b/include/clock.h
index af5b939..123f874 100644
--- a/include/clock.h
+++ b/include/clock.h
@@ -40,4 +40,23 @@ void mdelay(unsigned long msecs);
 #define MSECOND ((uint64_t)(1000 * 1000))
 #define USECOND ((uint64_t)(1000))
 
+/*
+ * Convenience wrapper to implement a typical polling loop with
+ * timeout. returns 0 if the condition became true within the
+ * timeout or -ETIMEDOUT otherwise
+ */
+#define wait_on_timeout(timeout, condition) \
+({								\
+	int __ret = 0;						\
+	uint64_t __to_start = get_time_ns();			\
+								\
+	while (!(condition)) {					\
+		if (is_timeout(__to_start, (timeout))) {	\
+			__ret = -ETIMEDOUT;			\
+			break;					\
+		}						\
+	}							\
+	__ret;							\
+})
+
 #endif /* CLOCK_H */
-- 
1.7.9




More information about the barebox mailing list