4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999, 2001, 2003 Bernie Innocenti <bernie@codewiz.org>
33 * \defgroup event_handling Event handling module
37 * \brief Events handling
39 * This module implements a common system for executing
40 * a user defined action calling a hook function.
43 * Device drivers often need to wait the completion of some event, usually to
44 * allow the hardware to accomplish some asynchronous task.
46 * A common approach is to place a busy wait with a cpu_relax() loop that invokes
47 * the architecture-specific instructions to say that we're not doing much with
50 * Although technically correct, the busy loop degrades the overall system
51 * performance in presence of multiple processes and power consumption.
53 * With the kernel the natural way to implement such wait/complete mechanism is to
54 * use signals via sig_wait() and sig_post()/sig_send().
56 * However, signals in BeRTOS are only available in presence of the kernel (that
57 * is just a compile-time option). This means that each device driver must provide
58 * two different interfaces to implement the wait/complete semantic: one with the
59 * kernel and another without the kernel.
61 * The purpose of the completion events is to provide a generic interface to
62 * implement a synchronization mechanism to block the execution of code until a
63 * specific event happens.
65 * This interface does not depend on the presence of the kernel and it
66 * automatically uses the appropriate event backend to provide the same
67 * behaviour with or without the kernel.
69 * Example usage (wait for a generic device driver initialization):
73 * static void irq_handler(void)
75 * // Completion event has happened, resume the execution of init()
79 * static void init(void)
81 * // Declare the generic completion event
82 * event_initGeneric(&e);
83 * // Submit the hardware initialization request
85 * // Wait for the completion of the event
90 * Example usage: wait multiple generic events via event_select()
95 * void event_notifier(void)
97 * Event *evs[] = { &ev1, &ev2 };
99 * event_initGeneric(&ev1);
100 * event_initGeneric(&ev2);
104 * int id = event_select(evs, countof(evs),
108 * kprintf("no IRQ\n");
111 * kprintf("IRQ %d happened\n", id);
115 * void irq1_handler(void)
120 * // notify the completion of event 1
124 * void irq2_handler(void)
129 * // notify the completion of event 2
134 * \author Bernie Innocenti <bernie@codewiz.org>
136 * $WIZ$ module_name = "event"
142 #include "cfg/cfg_proc.h"
143 #include "cfg/cfg_signal.h"
144 #include "cfg/cfg_timer.h"
145 #include <cfg/compiler.h>
147 #include <cpu/power.h> /* cpu_relax() */
149 #if CONFIG_KERN && CONFIG_KERN_SIGNALS
150 #include <kern/signal.h>
157 void (*action)(struct Event *);
160 #if CONFIG_KERN && CONFIG_KERN_SIGNALS
163 struct Process *sig_proc; /* Process to be signalled */
164 sigbit_t sig_bit; /* Signal to send */
165 Signal sig; /* Local signal structure (used by generic event) */
170 Hook func; /* Pointer to softint hook */
171 void *user_data; /* Data to be passed back to user hook */
176 bool completed; /* Generic event completion */
181 void event_hook_ignore(Event *event);
182 void event_hook_signal(Event *event);
183 void event_hook_softint(Event *event);
184 void event_hook_generic(Event *event);
185 void event_hook_generic_signal(Event *event);
187 /** Initialize the event \a e as a no-op */
188 #define event_initNone(e) \
189 ((e)->action = event_hook_ignore)
191 /** Same as event_initNone(), but returns the initialized event */
192 INLINE Event event_createNone(void)
195 e.action = event_hook_ignore;
199 /** Initialize the event \a e with a software interrupt (call function \a f, with parameter \a u) */
200 #define event_initSoftint(e,f,u) \
201 ((e)->action = event_hook_softint,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
203 /** Same as event_initSoftint(), but returns the initialized event */
204 INLINE Event event_createSoftint(Hook func, void *user_data)
207 e.action = event_hook_softint;
208 e.Ev.Int.func = func;
209 e.Ev.Int.user_data = user_data;
213 #if CONFIG_KERN && CONFIG_KERN_SIGNALS
214 /** Initialize the event \a e with a signal (send signal \a s to process \a p) */
215 #define event_initSignal(e,p,s) \
216 ((e)->action = event_hook_signal,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
218 /** Same as event_initSignal(), but returns the initialized event */
219 INLINE Event event_createSignal(struct Process *proc, sigbit_t bit)
222 e.action = event_hook_signal;
223 e.Ev.Sig.sig_proc = proc;
224 e.Ev.Sig.sig_bit = bit;
229 * Signal used to implement generic events.
231 #define EVENT_GENERIC_SIGNAL SIG_SYSTEM6
233 /** Initialize the generic sleepable event \a e */
234 #define event_initGeneric(e) \
235 ((e)->action = event_hook_generic_signal, \
236 (e)->Ev.Sig.sig_proc = proc_current(), \
237 (e)->Ev.Sig.sig_bit = EVENT_GENERIC_SIGNAL, \
238 (e)->Ev.Sig.sig.wait = 0, (e)->Ev.Sig.sig.recv = 0)
240 #define event_initGeneric(e) \
241 ((e)->action = event_hook_generic, (e)->Ev.Gen.completed = false)
245 * Create a generic sleepable event.
247 * \return the properly initialized generic event structure.
249 INLINE Event event_createGeneric(void)
252 event_initGeneric(&e);
257 * Wait the completion of event \a e.
259 * This function releases the CPU the application is configured to use
260 * the kernel, otherwise it's just a busy wait.
261 * \note It's forbidden to use this function inside irq handling functions.
263 INLINE void event_wait(Event *e)
265 #if CONFIG_KERN_SIGNALS
266 e->Ev.Sig.sig_proc = proc_current();
267 sig_waitSignal(&e->Ev.Sig.sig, EVENT_GENERIC_SIGNAL);
269 while (ACCESS_SAFE(e->Ev.Gen.completed) == false)
271 e->Ev.Gen.completed = false;
277 * Wait for multiple events
279 * On success return the offset in the \a evs vector of the Event that
280 * happened, -1 if the timeout expires.
282 * NOTE: timeout == 0 means no timeout.
284 * \attention The API is work in progress and may change in future versions.
286 int event_select(Event **evs, int n, ticks_t timeout);
289 * Wait the completion of event \a e or \a timeout elapses.
291 * \note It's forbidden to use this function inside irq handling functions.
293 bool event_waitTimeout(Event *e, ticks_t timeout);
298 * Execute the callback function associated with event \a e.
300 * This function can be used also in interrupt routines, but only if the
301 * event was created as a signal or generic event.
303 INLINE void event_do(struct Event *e)
310 #endif /* KERN_EVENT_H */