Re: [RESEND PATCH v2 08/18] linux/compat.h: Add wait_event_timeout macro

2020-08-14 Thread Tom Rini
On Thu, Aug 06, 2020 at 12:42:51PM +0300, Anastasiia Lukianenko wrote:

> From: Oleksandr Andrushchenko 
> 
> Add  wait_event_timeout - sleep until a condition gets true or a
> timeout elapses.
> 
> This is a stripped version of the same from Linux kernel with the
> following u-boot specific modifications:
> - no wait queues supported
> - use u-boot timer to detect timeouts
> - check for Ctrl-C pressed during wait
> 
> Signed-off-by: Oleksandr Andrushchenko 
> Signed-off-by: Anastasiia Lukianenko 

Applied to u-boot/master, thanks!

-- 
Tom


signature.asc
Description: PGP signature


[RESEND PATCH v2 08/18] linux/compat.h: Add wait_event_timeout macro

2020-08-06 Thread Anastasiia Lukianenko
From: Oleksandr Andrushchenko 

Add  wait_event_timeout - sleep until a condition gets true or a
timeout elapses.

This is a stripped version of the same from Linux kernel with the
following u-boot specific modifications:
- no wait queues supported
- use u-boot timer to detect timeouts
- check for Ctrl-C pressed during wait

Signed-off-by: Oleksandr Andrushchenko 
Signed-off-by: Anastasiia Lukianenko 
---

Changes since v1:
 - add comment
 - correct code style

 include/linux/compat.h | 54 ++
 1 file changed, 54 insertions(+)

diff --git a/include/linux/compat.h b/include/linux/compat.h
index 712eeaef4e..363b2b9425 100644
--- a/include/linux/compat.h
+++ b/include/linux/compat.h
@@ -1,12 +1,20 @@
 #ifndef _LINUX_COMPAT_H_
 #define _LINUX_COMPAT_H_
 
+#include 
 #include 
 #include 
+
+#include 
+
 #include 
 #include 
 #include 
 
+#ifdef CONFIG_XEN
+#include 
+#endif
+
 struct unused {};
 typedef struct unused unused_t;
 
@@ -122,6 +130,52 @@ static inline void kmem_cache_destroy(struct kmem_cache 
*cachep)
 #define add_wait_queue(...)do { } while (0)
 #define remove_wait_queue(...) do { } while (0)
 
+#ifndef CONFIG_XEN
+#define eventchn_poll()
+#endif
+
+#define __wait_event_timeout(condition, timeout, ret)  \
+({ \
+   ulong __ret = ret; /* explicit shadow */\
+   ulong start = get_timer(0); \
+   for (;;) {  \
+   eventchn_poll();\
+   if (condition) {\
+   __ret = 1;  \
+   break;  \
+   }   \
+   if ((get_timer(start) > timeout) || ctrlc()) {  \
+   __ret = 0;  \
+   break;  \
+   }   \
+   cpu_relax();\
+   }   \
+   __ret;  \
+})
+
+/**
+ * wait_event_timeout() - Wait until the event occurs before the timeout.
+ * @wr_head: The wait queue to wait on.
+ * @condition: Expression for the event to wait for.
+ * @timeout: Maximum waiting time.
+ *
+ * We wait until the @condition evaluates to %true (succeed) or
+ * %false (@timeout elapsed).
+ *
+ * Return:
+ * 0 - if the @condition evaluated to %false after the @timeout elapsed
+ * 1 - if the @condition evaluated to %true
+ */
+#define wait_event_timeout(wq_head, condition, timeout)
\
+({ \
+   ulong __ret;\
+   if (condition)  \
+   __ret = 1;  \
+   else\
+   __ret = __wait_event_timeout(condition, timeout, __ret);\
+   __ret;  \
+})
+
 #define KERNEL_VERSION(a,b,c)  (((a) << 16) + ((b) << 8) + (c))
 
 /* This is also defined in ARMv8's mmu.h */
-- 
2.17.1



[PATCH v2 08/18] linux/compat.h: Add wait_event_timeout macro

2020-07-20 Thread Anastasiia Lukianenko
From: Oleksandr Andrushchenko 

Add  wait_event_timeout - sleep until a condition gets true or a
timeout elapses.

This is a stripped version of the same from Linux kernel with the
following u-boot specific modifications:
- no wait queues supported
- use u-boot timer to detect timeouts
- check for Ctrl-C pressed during wait

Signed-off-by: Oleksandr Andrushchenko 
Signed-off-by: Anastasiia Lukianenko 
---
 include/linux/compat.h | 54 ++
 1 file changed, 54 insertions(+)

diff --git a/include/linux/compat.h b/include/linux/compat.h
index 712eeaef4e..363b2b9425 100644
--- a/include/linux/compat.h
+++ b/include/linux/compat.h
@@ -1,12 +1,20 @@
 #ifndef _LINUX_COMPAT_H_
 #define _LINUX_COMPAT_H_
 
+#include 
 #include 
 #include 
+
+#include 
+
 #include 
 #include 
 #include 
 
+#ifdef CONFIG_XEN
+#include 
+#endif
+
 struct unused {};
 typedef struct unused unused_t;
 
@@ -122,6 +130,52 @@ static inline void kmem_cache_destroy(struct kmem_cache 
*cachep)
 #define add_wait_queue(...)do { } while (0)
 #define remove_wait_queue(...) do { } while (0)
 
+#ifndef CONFIG_XEN
+#define eventchn_poll()
+#endif
+
+#define __wait_event_timeout(condition, timeout, ret)  \
+({ \
+   ulong __ret = ret; /* explicit shadow */\
+   ulong start = get_timer(0); \
+   for (;;) {  \
+   eventchn_poll();\
+   if (condition) {\
+   __ret = 1;  \
+   break;  \
+   }   \
+   if ((get_timer(start) > timeout) || ctrlc()) {  \
+   __ret = 0;  \
+   break;  \
+   }   \
+   cpu_relax();\
+   }   \
+   __ret;  \
+})
+
+/**
+ * wait_event_timeout() - Wait until the event occurs before the timeout.
+ * @wr_head: The wait queue to wait on.
+ * @condition: Expression for the event to wait for.
+ * @timeout: Maximum waiting time.
+ *
+ * We wait until the @condition evaluates to %true (succeed) or
+ * %false (@timeout elapsed).
+ *
+ * Return:
+ * 0 - if the @condition evaluated to %false after the @timeout elapsed
+ * 1 - if the @condition evaluated to %true
+ */
+#define wait_event_timeout(wq_head, condition, timeout)
\
+({ \
+   ulong __ret;\
+   if (condition)  \
+   __ret = 1;  \
+   else\
+   __ret = __wait_event_timeout(condition, timeout, __ret);\
+   __ret;  \
+})
+
 #define KERNEL_VERSION(a,b,c)  (((a) << 16) + ((b) << 8) + (c))
 
 /* This is also defined in ARMv8's mmu.h */
-- 
2.17.1