Doc fixes.
[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 #ifndef KERN_EVENT_H
45 #define KERN_EVENT_H
46
47 #include <cfg/compiler.h>
48 #include <appconfig.h>
49
50 #if CONFIG_KERNEL
51         #include <config_kern.h>
52         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
53                 #include <kern/signal.h>
54         #endif
55
56         /* Forward decl */
57         struct Process;
58 #endif
59
60
61 /// User defined callback type
62 typedef void (*Hook)(void *);
63
64 typedef struct Event
65 {
66         void (*action)(struct Event *);
67         union
68         {
69 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
70                 struct
71                 {
72                         struct Process *sig_proc;  /* Process to be signalled */
73                         sigbit_t        sig_bit;   /* Signal to send */
74                 } Sig;
75 #endif
76                 struct
77                 {
78                         Hook  func;         /* Pointer to softint hook */
79                         void *user_data;    /* Data to be passed back to user hook */
80                 } Int;
81         } Ev;
82 } Event;
83
84 void event_hook_ignore(Event *event);
85 void event_hook_signal(Event *event);
86 void event_hook_softint(Event *event);
87
88 /** Initialize the event \a e as a no-op */
89 #define event_initNone(e) \
90         ((e)->action = event_hook_ignore)
91
92 /** Same as event_initNone(), but returns the initialized event */
93 INLINE Event event_createNone(void);
94 INLINE Event event_createNone(void)
95 {
96         Event e;
97         e.action = event_hook_ignore;
98         return e;
99 }
100
101 /** Initialize the event \a e with a software interrupt (call function \a f, with parameter \a u) */
102 #define event_initSoftInt(e,f,u) \
103         ((e)->action = event_hook_softint,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
104
105 /** Same as event_initSoftInt(), but returns the initialized event */
106 INLINE Event event_createSoftInt(Hook func, void *user_data)
107 {
108         Event e;
109         e.action = event_hook_softint;
110         e.Ev.Int.func = func;
111         e.Ev.Int.user_data = user_data;
112         return e;
113 }
114
115
116 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
117
118 /** Initialize the event \a e with a signal (send signal \a s to process \a p) */
119 #define event_initSignal(e,p,s) \
120         ((e)->action = event_hook_signal,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
121
122 /** Same as event_initSignal(), but returns the initialized event */
123 INLINE Event event_createSignal(struct Process *proc, sigbit_t bit)
124 {
125         Event e;
126         e.action = event_hook_signal;
127         e.Ev.Sig.sig_proc = proc;
128         e.Ev.Sig.sig_bit = bit;
129         return e;
130 }
131
132 #endif
133
134 /** Trigger an event */
135 INLINE void event_do(struct Event *e)
136 {
137         e->action(e);
138 }
139
140 #endif /* KERN_EVENT_H */