Convert to appconfig.h.
[bertos.git] / mware / event.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999, 2001, 2003 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \brief Events handling
10  *
11  * This module implements a common system for executing
12  * a user defined action calling a hook function.
13  *
14  * \version $Id$
15  *
16  * \author Bernardo Innocenti <bernie@develer.com>
17  */
18
19 /*#*
20  *#* $Log$
21  *#* Revision 1.2  2005/11/27 03:02:55  bernie
22  *#* Convert to appconfig.h.
23  *#*
24  *#* Revision 1.1  2005/11/27 01:39:48  bernie
25  *#* Move event.[ch] from kern/ to mware/.
26  *#*
27  *#* Revision 1.11  2005/04/11 19:10:28  bernie
28  *#* Include top-level headers from cfg/ subdir.
29  *#*
30  *#* Revision 1.10  2005/01/24 04:22:02  bernie
31  *#* Update copyright information.
32  *#*
33  *#* Revision 1.9  2005/01/24 04:19:55  bernie
34  *#* Remove obsolete names.
35  *#*
36  *#* Revision 1.8  2005/01/24 04:19:05  bernie
37  *#* Function pointer based event dispatching.
38  *#*
39  *#* Revision 1.7  2004/08/25 14:12:09  rasky
40  *#* Aggiornato il comment block dei log RCS
41  *#*
42  *#* Revision 1.6  2004/08/14 19:37:57  rasky
43  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
44  *#*
45  *#* Revision 1.5  2004/07/30 14:30:27  rasky
46  *#* Resa la sig_signal interrupt safe (con il nuovo scheduler IRQ-safe)
47  *#* Rimossa event_doIntr (ora inutile) e semplificata la logica delle macro con funzioni inline
48  *#*
49  *#* Revision 1.4  2004/06/07 15:58:00  aleph
50  *#* Add function prototypes
51  *#*
52  *#* Revision 1.3  2004/06/06 18:25:44  bernie
53  *#* Rename event macros to look like regular functions.
54  *#*
55  *#* Revision 1.2  2004/06/03 11:27:09  bernie
56  *#* Add dual-license information.
57  *#*
58  *#* Revision 1.1  2004/05/23 17:27:00  bernie
59  *#* Import kern/ subdirectory.
60  *#*
61  *#*/
62 #ifndef KERN_EVENT_H
63 #define KERN_EVENT_H
64
65 #include <appconfig.h>
66
67 #if CONFIG_KERNEL
68         #include <cfg/config_kern.h>
69         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
70                 #include "signal.h"
71         #endif
72 #endif
73
74
75 //! User defined callback type
76 typedef void (*Hook)(void *);
77
78 typedef struct Event
79 {
80         void (*action)(struct Event *);
81         union
82         {
83 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
84                 struct
85                 {
86                         struct Process *sig_proc;  /* Process to be signalled */
87                         sig_t           sig_bit;   /* Signal to send */
88                 } Sig;
89 #endif
90                 struct
91                 {
92                         Hook  func;         /* Pointer to softint hook */
93                         void *user_data;    /* Data to be passed back to user hook */
94                 } Int;
95         } Ev;
96 } Event;
97
98 void event_hook_ignore(Event *event);
99 void event_hook_signal(Event *event);
100 void event_hook_softint(Event *event);
101
102 /*! Initialize the event \a e as a no-op */
103 #define event_initNone(e) \
104         ((e)->action = event_hook_ignore)
105
106 /*! Same as event_initNone(), but returns the initialized event */
107 INLINE Event event_createNone(void);
108 INLINE Event event_createNone(void)
109 {
110         Event e;
111         e.action = event_hook_ignore;
112         return e;
113 }
114
115 /*! Initialize the event \a e with a software interrupt (call function \a f, with parameter \a u) */
116 #define event_initSoftInt(e,f,u) \
117         ((e)->action = event_hook_softint,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
118
119 /*! Same as event_initSoftInt(), but returns the initialized event */
120 INLINE Event event_createSoftInt(Hook func, void* user_data)
121 {
122         Event e;
123         e.action = event_hook_softint;
124         e.Ev.Int.func = func;
125         e.Ev.Int.user_data = user_data;
126         return e;
127 }
128
129
130 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
131
132 /*! Initialize the event \a e with a signal (send signal \a s to process \a p) */
133 #define event_initSignal(e,p,s) \
134         ((e)->action = event_hook_signal,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
135
136 /*! Same as event_initSignal(), but returns the initialized event */
137 INLINE Event event_createSignal(struct Process* proc, sig_t bit)
138 {
139         Event e;
140         e.action = event_hook_signal;
141         e.Ev.Sig.sig_proc = proc;
142         e.Ev.Sig.sig_bit = bit;
143         return e;
144 }
145
146 #endif
147
148 /*! Trigger an event */
149 INLINE void event_do(struct Event* e)
150 {
151         e->action(e);
152 }
153
154 #endif /* KERN_EVENT_H */