3d00605962e3a4aaca7b1c74d474a66eef2999ce
[bertos.git] / bertos / mware / event.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2001, 2003 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \defgroup event_handling Event handling module
34  * \ingroup core
35  * \{
36  *
37  * \brief Events handling
38  *
39  * This module implements a common system for executing
40  * a user defined action calling a hook function.
41  *
42  *
43  *  Device drivers often need to wait the completion of some event, usually to
44  *  allow the hardware to accomplish some asynchronous task.
45  *
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
48  *  the processor.
49  *
50  *  Although technically correct, the busy loop degrades the overall system
51  *  performance in presence of multiple processes and power consumption.
52  *
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().
55  *
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.
60  *
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.
64  *
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.
68  *
69  *  Example usage (wait for a generic device driver initialization):
70  *  \code
71  *  static Event e;
72  *
73  *  static void irq_handler(void)
74  *  {
75  *      // Completion event has happened, resume the execution of init()
76  *      event_do(&e);
77  *  }
78  *
79  *  static void init(void)
80  *  {
81  *      // Declare the generic completion event
82  *      event_initGeneric(&e);
83  *      // Submit the hardware initialization request
84  *      async_hw_init();
85  *      // Wait for the completion of the event
86  *      event_wait(&e);
87  *  }
88  *  \endcode
89  *
90  * \author Bernie Innocenti <bernie@codewiz.org>
91  */
92
93 #ifndef KERN_EVENT_H
94 #define KERN_EVENT_H
95
96 #include <cfg/compiler.h>
97 #include "cfg/cfg_proc.h"
98 #include "cfg/cfg_signal.h"
99 #include "cfg/cfg_timer.h"
100
101 #include <cpu/power.h> /* cpu_relax() */
102
103 #if CONFIG_KERN
104         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
105                 #include <kern/signal.h>
106         #endif
107
108         /* Forward decl */
109         struct Process;
110 #endif
111
112 /// User defined callback type
113 typedef void (*Hook)(void *);
114
115 typedef struct Event
116 {
117         void (*action)(struct Event *);
118         union
119         {
120 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
121                 struct
122                 {
123                         struct Process *sig_proc;  /* Process to be signalled */
124                         sigbit_t        sig_bit;   /* Signal to send */
125                 } Sig;
126
127                 struct
128                 {
129                         struct Process *sig_proc;  /* Process to be signalled */
130                         Signal          sig;       /* Signal structure */
131                 } SigGen;
132 #endif
133                 struct
134                 {
135                         Hook  func;         /* Pointer to softint hook */
136                         void *user_data;    /* Data to be passed back to user hook */
137                 } Int;
138
139                 struct
140                 {
141                         bool completed;             /* Generic event completion */
142                 } Gen;
143         } Ev;
144 } Event;
145
146 void event_hook_ignore(Event *event);
147 void event_hook_signal(Event *event);
148 void event_hook_softint(Event *event);
149 void event_hook_generic(Event *event);
150 void event_hook_generic_signal(Event *event);
151 void event_hook_generic_timeout(Event *event);
152
153 /** Initialize the event \a e as a no-op */
154 #define event_initNone(e) \
155         ((e)->action = event_hook_ignore)
156
157 /** Same as event_initNone(), but returns the initialized event */
158 INLINE Event event_createNone(void);
159 INLINE Event event_createNone(void)
160 {
161         Event e;
162         e.action = event_hook_ignore;
163         return e;
164 }
165
166 /** Initialize the event \a e with a software interrupt (call function \a f, with parameter \a u) */
167 #define event_initSoftint(e,f,u) \
168         ((e)->action = event_hook_softint,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
169
170 /** Same as event_initSoftint(), but returns the initialized event */
171 INLINE Event event_createSoftint(Hook func, void *user_data)
172 {
173         Event e;
174         e.action = event_hook_softint;
175         e.Ev.Int.func = func;
176         e.Ev.Int.user_data = user_data;
177         return e;
178 }
179
180 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
181
182 /** Initialize the event \a e with a signal (send signal \a s to process \a p) */
183 #define event_initSignal(e,p,s) \
184         ((e)->action = event_hook_signal,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
185
186 /** Same as event_initSignal(), but returns the initialized event */
187 INLINE Event event_createSignal(struct Process *proc, sigbit_t bit)
188 {
189         Event e;
190         e.action = event_hook_signal;
191         e.Ev.Sig.sig_proc = proc;
192         e.Ev.Sig.sig_bit = bit;
193         return e;
194 }
195
196 #endif
197
198 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
199 /** Initialize the generic sleepable event \a e */
200 #define event_initGeneric(e)                                    \
201         ((e)->action = event_hook_generic_signal,               \
202                 (e)->Ev.SigGen.sig_proc = proc_current(),       \
203                 (e)->Ev.SigGen.sig.wait = 0, (e)->Ev.SigGen.sig.recv = 0)
204 #else
205 #define event_initGeneric(e) \
206         ((e)->action = event_hook_generic, (e)->Ev.Gen.completed = false)
207 #endif
208
209 /**
210  * Signal used to implement generic events.
211  */
212 #define EVENT_GENERIC_SIGNAL    SIG_SYSTEM5
213
214 /**
215  * Create a generic sleepable event.
216  *
217  * \return the properly initialized generic event structure.
218  */
219 INLINE Event event_createGeneric(void)
220 {
221         Event e;
222         event_initGeneric(&e);
223         return e;
224 }
225
226 /**
227  * Wait the completion of event \a e.
228  *
229  * This function releases the CPU the application is configured to use
230  * the kernel, otherwise it's just a busy wait.
231  * \note It's forbidden to use this function inside irq handling functions.
232  */
233 INLINE void event_wait(Event *e)
234 {
235 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
236         e->Ev.Sig.sig_proc = proc_current();
237         sig_waitSignal(&e->Ev.SigGen.sig, EVENT_GENERIC_SIGNAL);
238 #else
239         while (ACCESS_SAFE(e->Ev.Gen.completed) == false)
240                 cpu_relax();
241         e->Ev.Gen.completed = false;
242         MEMORY_BARRIER;
243 #endif
244 }
245
246 #if CONFIG_TIMER_EVENTS
247 #include <drv/timer.h> /* timer_clock() */
248
249 /* TODO: move these macros to drv/timer.h */
250 #define TIMER_AFTER(x, y) ((long)(y) - (long)(x) < 0)
251 #define TIMER_BEFORE(x, y) TIMER_AFTER(y, x)
252
253 /**
254  * Wait the completion of event \a e or \a timeout elapses.
255  *
256  * \note It's forbidden to use this function inside irq handling functions.
257  */
258 INLINE bool event_waitTimeout(Event *e, ticks_t timeout)
259 {
260         bool ret;
261
262 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
263         e->Ev.Sig.sig_proc = proc_current();
264         ret = (sig_waitTimeoutSignal(&e->Ev.SigGen.sig,
265                                 EVENT_GENERIC_SIGNAL, timeout) & SIG_TIMEOUT) ?
266                                 false : true;
267 #else
268         ticks_t end = timer_clock() + timeout;
269
270         while ((ACCESS_SAFE(e->Ev.Gen.completed) == false) ||
271                         TIMER_AFTER(timer_clock(), end))
272                 cpu_relax();
273         ret = e->Ev.Gen.completed;
274         e->Ev.Gen.completed = false;
275 #endif
276         MEMORY_BARRIER;
277         return ret;
278 }
279 #endif /* CONFIG_TIMER_EVENTS */
280
281 /**
282  * Trigger an event.
283  *
284  * Execute the callback function associated with event \a e.
285  *
286  * This function can be used also in interrupt routines, but only if the
287  * event was created as a signal or generic event.
288  */
289 INLINE void event_do(struct Event *e)
290 {
291         e->action(e);
292 }
293
294 /** \} */
295
296 #endif /* KERN_EVENT_H */