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