diff mbox series

[08/17] linux/compat.h: Add wait_event_timeout macro

Message ID 20200701162959.9814-9-vicooodin@gmail.com
State New
Headers show
Series Add new board: Xen guest for ARM64 | expand

Commit Message

Nastya Vicodin July 1, 2020, 4:29 p.m. UTC
From: Oleksandr Andrushchenko <oleksandr_andrushchenko at epam.com>

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 <oleksandr_andrushchenko at epam.com>
Signed-off-by: Anastasiia Lukianenko <anastasiia_lukianenko at epam.com>
---
 include/linux/compat.h | 45 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Comments

Anastasiia Lukianenko July 3, 2020, 1:02 p.m. UTC | #1
Hi Heinrich,

On Thu, 2020-07-02 at 06:08 +0200, Heinrich Schuchardt wrote:
> On 7/1/20 6:29 PM, Anastasiia Lukianenko wrote:
> > From: Oleksandr Andrushchenko <oleksandr_andrushchenko at epam.com>
> > 
> > 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 <
> > oleksandr_andrushchenko at epam.com>
> > Signed-off-by: Anastasiia Lukianenko <
> > anastasiia_lukianenko at epam.com>
> > ---
> >  include/linux/compat.h | 45
> > ++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 45 insertions(+)
> > 
> > diff --git a/include/linux/compat.h b/include/linux/compat.h
> > index 712eeaef4e..5375b7d3b8 100644
> > --- a/include/linux/compat.h
> > +++ b/include/linux/compat.h
> > @@ -1,12 +1,20 @@
> >  #ifndef _LINUX_COMPAT_H_
> >  #define _LINUX_COMPAT_H_
> > 
> > +#include <console.h>
> >  #include <log.h>
> >  #include <malloc.h>
> > +
> > +#include <asm/processor.h>
> > +
> >  #include <linux/types.h>
> >  #include <linux/err.h>
> >  #include <linux/kernel.h>
> > 
> > +#ifdef CONFIG_XEN
> > +#include <xen/events.h>
> > +#endif
> > +
> >  struct unused {};
> >  typedef struct unused unused_t;
> > 
> > @@ -122,6 +130,43 @@ 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;							\
> > +})
> > +
> > +/*
> > + * 0 if the @condition evaluated to %false after the @timeout
> > elapsed,
> > + * 1 if the @condition evaluated to %true
> > + */
> 
> Please, document all arguments. Use Sphinx style as in
> 
> 
https://urldefense.com/v3/__https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html*function-documentation__;Iw!!GF_29dbcQIUBPA!jxP9Fy4gB94pb9T2mEndgiT2VqEyLEBMrYaWyDyW68eZlNMGungcRuQt_ImPQPyw3zQYqiU$
>  .
> 

Ok, I will fix it in the next version.

> Best regards
> 
> Heinrich.

Best regards,
Anastasiia

> 
> > +#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 */
> > 
> 
>
diff mbox series

Patch

diff --git a/include/linux/compat.h b/include/linux/compat.h
index 712eeaef4e..5375b7d3b8 100644
--- a/include/linux/compat.h
+++ b/include/linux/compat.h
@@ -1,12 +1,20 @@ 
 #ifndef _LINUX_COMPAT_H_
 #define _LINUX_COMPAT_H_
 
+#include <console.h>
 #include <log.h>
 #include <malloc.h>
+
+#include <asm/processor.h>
+
 #include <linux/types.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
 
+#ifdef CONFIG_XEN
+#include <xen/events.h>
+#endif
+
 struct unused {};
 typedef struct unused unused_t;
 
@@ -122,6 +130,43 @@  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;							\
+})
+
+/*
+ * 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 */