Removed 'This file is part of DevLib ...'
[bertos.git] / 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 Bernardo Innocenti <bernie@develer.com>
31  *
32  * -->
33  *
34  * \brief Events handling
35  *
36  * This module implements a common system for executing
37  * a user defined action calling a hook function.
38  *
39  * \version $Id$
40  *
41  * \author Bernardo Innocenti <bernie@develer.com>
42  */
43
44 /*#*
45  *#* $Log$
46  *#* Revision 1.8  2006/07/19 12:56:27  bernie
47  *#* Convert to new Doxygen style.
48  *#*
49  *#* Revision 1.7  2006/03/20 17:52:22  bernie
50  *#* Add missing forward declaration.
51  *#*
52  *#* Revision 1.6  2006/02/24 01:17:44  bernie
53  *#* Update for new emulator.
54  *#*
55  *#* Revision 1.5  2006/02/24 00:26:21  bernie
56  *#* Fix header name.
57  *#*
58  *#* Revision 1.4  2006/02/10 12:24:42  bernie
59  *#* Fix standalone build.
60  *#*
61  *#* Revision 1.3  2006/01/16 03:27:49  bernie
62  *#* Rename sig_t to sigbit_t to avoid clash with POSIX.
63  *#*
64  *#* Revision 1.2  2005/11/27 03:02:55  bernie
65  *#* Convert to appconfig.h.
66  *#*
67  *#* Revision 1.1  2005/11/27 01:39:48  bernie
68  *#* Move event.[ch] from kern/ to mware/.
69  *#*
70  *#* Revision 1.11  2005/04/11 19:10:28  bernie
71  *#* Include top-level headers from cfg/ subdir.
72  *#*
73  *#* Revision 1.10  2005/01/24 04:22:02  bernie
74  *#* Update copyright information.
75  *#*
76  *#* Revision 1.9  2005/01/24 04:19:55  bernie
77  *#* Remove obsolete names.
78  *#*
79  *#* Revision 1.8  2005/01/24 04:19:05  bernie
80  *#* Function pointer based event dispatching.
81  *#*
82  *#* Revision 1.7  2004/08/25 14:12:09  rasky
83  *#* Aggiornato il comment block dei log RCS
84  *#*
85  *#* Revision 1.6  2004/08/14 19:37:57  rasky
86  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
87  *#*
88  *#* Revision 1.5  2004/07/30 14:30:27  rasky
89  *#* Resa la sig_signal interrupt safe (con il nuovo scheduler IRQ-safe)
90  *#* Rimossa event_doIntr (ora inutile) e semplificata la logica delle macro con funzioni inline
91  *#*
92  *#* Revision 1.4  2004/06/07 15:58:00  aleph
93  *#* Add function prototypes
94  *#*
95  *#* Revision 1.3  2004/06/06 18:25:44  bernie
96  *#* Rename event macros to look like regular functions.
97  *#*
98  *#* Revision 1.2  2004/06/03 11:27:09  bernie
99  *#* Add dual-license information.
100  *#*
101  *#* Revision 1.1  2004/05/23 17:27:00  bernie
102  *#* Import kern/ subdirectory.
103  *#*
104  *#*/
105 #ifndef KERN_EVENT_H
106 #define KERN_EVENT_H
107
108 #include <cfg/compiler.h>
109 #include <appconfig.h>
110
111 #if CONFIG_KERNEL
112         #include <config_kern.h>
113         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
114                 #include <kern/signal.h>
115         #endif
116
117         /* Forward decl */
118         struct Process;
119 #endif
120
121
122 /// User defined callback type
123 typedef void (*Hook)(void *);
124
125 typedef struct Event
126 {
127         void (*action)(struct Event *);
128         union
129         {
130 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
131                 struct
132                 {
133                         struct Process *sig_proc;  /* Process to be signalled */
134                         sigbit_t        sig_bit;   /* Signal to send */
135                 } Sig;
136 #endif
137                 struct
138                 {
139                         Hook  func;         /* Pointer to softint hook */
140                         void *user_data;    /* Data to be passed back to user hook */
141                 } Int;
142         } Ev;
143 } Event;
144
145 void event_hook_ignore(Event *event);
146 void event_hook_signal(Event *event);
147 void event_hook_softint(Event *event);
148
149 /** Initialize the event \a e as a no-op */
150 #define event_initNone(e) \
151         ((e)->action = event_hook_ignore)
152
153 /** Same as event_initNone(), but returns the initialized event */
154 INLINE Event event_createNone(void);
155 INLINE Event event_createNone(void)
156 {
157         Event e;
158         e.action = event_hook_ignore;
159         return e;
160 }
161
162 /** Initialize the event \a e with a software interrupt (call function \a f, with parameter \a u) */
163 #define event_initSoftInt(e,f,u) \
164         ((e)->action = event_hook_softint,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
165
166 /** Same as event_initSoftInt(), but returns the initialized event */
167 INLINE Event event_createSoftInt(Hook func, void *user_data)
168 {
169         Event e;
170         e.action = event_hook_softint;
171         e.Ev.Int.func = func;
172         e.Ev.Int.user_data = user_data;
173         return e;
174 }
175
176
177 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
178
179 /** Initialize the event \a e with a signal (send signal \a s to process \a p) */
180 #define event_initSignal(e,p,s) \
181         ((e)->action = event_hook_signal,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
182
183 /** Same as event_initSignal(), but returns the initialized event */
184 INLINE Event event_createSignal(struct Process *proc, sigbit_t bit)
185 {
186         Event e;
187         e.action = event_hook_signal;
188         e.Ev.Sig.sig_proc = proc;
189         e.Ev.Sig.sig_bit = bit;
190         return e;
191 }
192
193 #endif
194
195 /** Trigger an event */
196 INLINE void event_do(struct Event *e)
197 {
198         e->action(e);
199 }
200
201 #endif /* KERN_EVENT_H */