mware: fix generic completion events behaviour without the kernel
[bertos.git] / bertos / mware / event.h
index a357bb18e6ad7e1ae7523e8aad83fe4a697fa2a6..83f0e13eff995182c2a18c1faa1a293f203963f7 100644 (file)
@@ -27,8 +27,7 @@
  * the GNU General Public License.
  *
  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
- * Copyright 1999, 2001, 2003 Bernardo Innocenti <bernie@develer.com>
- *
+ * Copyright 1999, 2001, 2003 Bernie Innocenti <bernie@codewiz.org>
  * -->
  *
  * \brief Events handling
  * This module implements a common system for executing
  * a user defined action calling a hook function.
  *
- * \version $Id$
- *
- * \author Bernardo Innocenti <bernie@develer.com>
+ * \author Bernie Innocenti <bernie@codewiz.org>
  */
 
 #ifndef KERN_EVENT_H
 #define KERN_EVENT_H
 
 #include <cfg/compiler.h>
-#include "cfg/cfg_kern.h"
+#include "cfg/cfg_proc.h"
+#include "cfg/cfg_signal.h"
+#include "cfg/cfg_timer.h"
+
+#include <cpu/power.h> /* cpu_relax() */
 
-#if CONFIG_KERNEL
+#if CONFIG_KERN
        #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
                #include <kern/signal.h>
        #endif
@@ -77,12 +78,19 @@ typedef struct Event
                        Hook  func;         /* Pointer to softint hook */
                        void *user_data;    /* Data to be passed back to user hook */
                } Int;
+
+               struct
+               {
+                       bool completed;             /* Generic event completion */
+               } Gen;
        } Ev;
 } Event;
 
 void event_hook_ignore(Event *event);
 void event_hook_signal(Event *event);
 void event_hook_softint(Event *event);
+void event_hook_generic(Event *event);
+void event_hook_generic_timeout(Event *event);
 
 /** Initialize the event \a e as a no-op */
 #define event_initNone(e) \
@@ -98,11 +106,11 @@ INLINE Event event_createNone(void)
 }
 
 /** Initialize the event \a e with a software interrupt (call function \a f, with parameter \a u) */
-#define event_initSoftInt(e,f,u) \
+#define event_initSoftint(e,f,u) \
        ((e)->action = event_hook_softint,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
 
-/** Same as event_initSoftInt(), but returns the initialized event */
-INLINE Event event_createSoftInt(Hook func, void *user_data)
+/** Same as event_initSoftint(), but returns the initialized event */
+INLINE Event event_createSoftint(Hook func, void *user_data)
 {
        Event e;
        e.action = event_hook_softint;
@@ -111,7 +119,6 @@ INLINE Event event_createSoftInt(Hook func, void *user_data)
        return e;
 }
 
-
 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
 
 /** Initialize the event \a e with a signal (send signal \a s to process \a p) */
@@ -130,6 +137,84 @@ INLINE Event event_createSignal(struct Process *proc, sigbit_t bit)
 
 #endif
 
+/**
+ * Prevent the compiler from optimizing access to the variable \a x, enforcing
+ * a refetch from memory. This also forbid from reordering successing instances
+ * of ACCESS_SAFE().
+ *
+ * TODO: move this to cfg/compiler.h
+ */
+#define ACCESS_SAFE(x) (*(volatile typeof(x) *)&(x))
+
+#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
+/** Initialize the generic sleepable event \a e */
+#define event_initGeneric(e) \
+       event_initSignal(e, proc_current(), SIG_SYSTEM5)
+#else
+#define event_initGeneric(e) \
+       ((e)->action = event_hook_generic, (e)->Ev.Gen.completed = false)
+#endif
+
+/**
+ * Create a generic sleepable event.
+ *
+ * \return the properly initialized generic event structure.
+ */
+INLINE Event event_createGeneric(void)
+{
+       Event e;
+       event_initGeneric(&e);
+       return e;
+}
+
+/**
+ * Wait the completion of event \a e.
+ */
+INLINE void event_wait(Event *e)
+{
+#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
+       e->Ev.Sig.sig_proc = proc_current();
+       sig_wait(e->Ev.Sig.sig_bit);
+#else
+       while (ACCESS_SAFE(e->Ev.Gen.completed) == false)
+               cpu_relax();
+       e->Ev.Gen.completed = false;
+       MEMORY_BARRIER;
+#endif
+}
+
+#if CONFIG_TIMER_EVENTS
+#include <drv/timer.h> /* timer_clock() */
+
+/* TODO: move these macros to drv/timer.h */
+#define TIMER_AFTER(x, y) ((long)(y) - (long)(x) < 0)
+#define TIMER_BEFORE(x, y) TIMER_AFTER(y, x)
+
+/**
+ * Wait the completion of event \a e or \a timeout elapses.
+ */
+INLINE bool event_waitTimeout(Event *e, ticks_t timeout)
+{
+#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
+       e->Ev.Sig.sig_proc = proc_current();
+       return (sig_waitTimeout(e->Ev.Sig.sig_bit, timeout) & SIG_TIMEOUT) ?
+                               false : true;
+#else
+       ticks_t end = timer_clock() + timeout;
+       bool ret;
+
+       while ((ACCESS_SAFE(e->Ev.Gen.completed) == false) ||
+                       TIMER_AFTER(timer_clock(), end))
+               cpu_relax();
+       ret = e->Ev.Gen.completed;
+       e->Ev.Gen.completed = false;
+       MEMORY_BARRIER;
+
+       return ret;
+#endif
+}
+#endif /* CONFIG_TIMER_EVENTS */
+
 /** Trigger an event */
 INLINE void event_do(struct Event *e)
 {