X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fmware%2Fevent.h;h=0111ef8cbfa37fbf866f11c0976fa7f3ecbc14f6;hb=446aae6172b8e362264b6a3d08f7b61fb23898a9;hp=a357bb18e6ad7e1ae7523e8aad83fe4a697fa2a6;hpb=c22fe24a0da896a52dbc3882390ec18a440ef56a;p=bertos.git diff --git a/bertos/mware/event.h b/bertos/mware/event.h index a357bb18..0111ef8c 100644 --- a/bertos/mware/event.h +++ b/bertos/mware/event.h @@ -27,27 +27,80 @@ * the GNU General Public License. * * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/) - * Copyright 1999, 2001, 2003 Bernardo Innocenti - * + * Copyright 1999, 2001, 2003 Bernie Innocenti * --> * + * \defgroup event_handling Event handling module + * \ingroup core + * \{ + * * \brief Events handling * * This module implements a common system for executing * a user defined action calling a hook function. * - * \version $Id$ * - * \author Bernardo Innocenti + * Device drivers often need to wait the completion of some event, usually to + * allow the hardware to accomplish some asynchronous task. + * + * A common approach is to place a busy wait with a cpu_relax() loop that invokes + * the architecture-specific instructions to say that we're not doing much with + * the processor. + * + * Although technically correct, the busy loop degrades the overall system + * performance in presence of multiple processes and power consumption. + * + * With the kernel the natural way to implement such wait/complete mechanism is to + * use signals via sig_wait() and sig_post()/sig_send(). + * + * However, signals in BeRTOS are only available in presence of the kernel (that + * is just a compile-time option). This means that each device driver must provide + * two different interfaces to implement the wait/complete semantic: one with the + * kernel and another without the kernel. + * + * The purpose of the completion events is to provide a generic interface to + * implement a synchronization mechanism to block the execution of code until a + * specific event happens. + * + * This interface does not depend on the presence of the kernel and it + * automatically uses the appropriate event backend to provide the same + * behaviour with or without the kernel. + * + * Example usage (wait for a generic device driver initialization): + * \code + * static Event e; + * + * static void irq_handler(void) + * { + * // Completion event has happened, resume the execution of init() + * event_do(&e); + * } + * + * static void init(void) + * { + * // Declare the generic completion event + * event_initGeneric(&e); + * // Submit the hardware initialization request + * async_hw_init(); + * // Wait for the completion of the event + * event_wait(&e); + * } + * \endcode + * + * \author Bernie Innocenti */ #ifndef KERN_EVENT_H #define KERN_EVENT_H #include -#include "cfg/cfg_kern.h" +#include "cfg/cfg_proc.h" +#include "cfg/cfg_signal.h" +#include "cfg/cfg_timer.h" -#if CONFIG_KERNEL +#include /* cpu_relax() */ + +#if CONFIG_KERN #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS #include #endif @@ -56,7 +109,6 @@ struct Process; #endif - /// User defined callback type typedef void (*Hook)(void *); @@ -71,18 +123,31 @@ typedef struct Event struct Process *sig_proc; /* Process to be signalled */ sigbit_t sig_bit; /* Signal to send */ } Sig; + + struct + { + struct Process *sig_proc; /* Process to be signalled */ + Signal sig; /* Signal structure */ + } SigGen; #endif struct { 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_signal(Event *event); /** Initialize the event \a e as a no-op */ #define event_initNone(e) \ @@ -98,11 +163,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 +176,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,10 +194,98 @@ INLINE Event event_createSignal(struct Process *proc, sigbit_t bit) #endif -/** Trigger an event */ +#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 +} + +#if CONFIG_TIMER_EVENTS +#include /* 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. + */ INLINE void event_do(struct Event *e) { e->action(e); } +/** \} */ + #endif /* KERN_EVENT_H */