+#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
+/** Initialize the generic sleepable event \a e */
+#define event_initGeneric(e) \
+ ((e)->action = event_hook_generic_signal, \
+ (e)->Ev.SigGen.sig_proc = proc_current(), \
+ (e)->Ev.SigGen.sig.wait = 0, (e)->Ev.SigGen.sig.recv = 0)
+#else
+#define event_initGeneric(e) \
+ ((e)->action = event_hook_generic, (e)->Ev.Gen.completed = false)
+#endif
+
+/**
+ * Signal used to implement generic events.
+ */
+#define EVENT_GENERIC_SIGNAL SIG_SYSTEM5
+
+/**
+ * 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.
+ *
+ * This function releases the CPU the application is configured to use
+ * the kernel, otherwise it's just a busy wait.
+ * \note It's forbidden to use this function inside irq handling functions.
+ */
+INLINE void event_wait(Event *e)
+{
+#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
+ e->Ev.Sig.sig_proc = proc_current();
+ sig_waitSignal(&e->Ev.SigGen.sig, EVENT_GENERIC_SIGNAL);
+#else
+ while (ACCESS_SAFE(e->Ev.Gen.completed) == false)
+ cpu_relax();
+ e->Ev.Gen.completed = false;
+ MEMORY_BARRIER;
+#endif
+}
+
+int event_select(Event **evs, int n, ticks_t timeout);
+
+#if CONFIG_TIMER_EVENTS
+#include <drv/timer.h> /* timer_clock() */
+
+/**
+ * Wait the completion of event \a e or \a timeout elapses.
+ *
+ * \note It's forbidden to use this function inside irq handling functions.
+ */
+INLINE bool event_waitTimeout(Event *e, ticks_t timeout)
+{
+ bool ret;
+
+#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
+ e->Ev.Sig.sig_proc = proc_current();
+ ret = (sig_waitTimeoutSignal(&e->Ev.SigGen.sig,
+ EVENT_GENERIC_SIGNAL, timeout) & SIG_TIMEOUT) ?
+ false : true;
+#else
+ ticks_t end = timer_clock() + timeout;
+
+ 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;
+#endif
+ MEMORY_BARRIER;
+ return ret;
+}
+#endif /* CONFIG_TIMER_EVENTS */
+
+/**
+ * Trigger an event.
+ *
+ * Execute the callback function associated with event \a e.
+ *
+ * This function can be used also in interrupt routines, but only if the
+ * event was created as a signal or generic event.
+ */