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