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