Add function prototypes
[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.4  2004/06/07 15:58:00  aleph
22  * Add function prototypes
23  *
24  * Revision 1.3  2004/06/06 18:25:44  bernie
25  * Rename event macros to look like regular functions.
26  *
27  * Revision 1.2  2004/06/03 11:27:09  bernie
28  * Add dual-license information.
29  *
30  * Revision 1.1  2004/05/23 17:27:00  bernie
31  * Import kern/ subdirectory.
32  *
33  */
34 #ifndef KERN_EVENT_H
35 #define KERN_EVENT_H
36
37 #include <config.h>
38
39 #ifdef CONFIG_KERNEL
40         #include "config_kern.h"
41         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
42                 #include "signal.h"
43         #endif
44 #endif
45
46
47 /* Forward decl */
48 struct Process;
49
50
51 /*! Event types */
52 enum EventAction
53 {
54         EVENT_IGNORE,   /*!< No-op event */
55 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
56         EVENT_SIGNAL,   /*!< Send a signal to a process */
57 #endif
58         EVENT_SOFTINT   /*!< Trigger a software interrupt (i.e.: call user hook) */
59 };
60
61 //! User defined callback type
62 typedef void (*Hook)(void *);
63
64 typedef struct Event
65 {
66         enum EventAction action;
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                         sig_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
85 /*! Initialize the event \a e as a no-op */
86 #define event_initNone(e) \
87         ((e)->action = EVENT_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_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_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 INLINE Event event_createSoftInt(Hook func, void* user_data)
105 {
106         Event e;
107         e.action = EVENT_SOFTINT;
108         e.Ev.Int.func = func;
109         e.Ev.Int.user_data = user_data;
110         return e;
111 }
112
113
114 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
115
116 /*! Initialize the event \a e with a signal (send signal \a s to process \a p) */
117 #define event_initSignal(e,p,s) \
118         ((e)->action = EVENT_SIGNAL,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
119
120 /*! Same as event_initSignal(), but returns the initialized event */
121 INLINE Event event_createSignal(struct Process* proc, sig_t bit);
122 INLINE Event event_createSignal(struct Process* proc, sig_t bit)
123 {
124         Event e;
125         e.action = EVENT_SOFTINT;
126         e.Ev.Sig.sig_proc = proc;
127         e.Ev.Sig.sig_bit = bit;
128         return e;
129 }
130
131 /*! Trigger an event */
132 #define event_do(e) \
133 do { \
134         if ((e)->action == EVENT_SIGNAL) \
135                 sig_signal((e)->Ev.Sig.sig_proc, (e)->Ev.Sig.sig_bit); \
136         else if ((e)->action == EVENT_SOFTINT) \
137                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
138 } while (0)
139
140 /*! Trigger an event (to be used inside interrupts) */
141 #define event_doIntr(e) \
142 do { \
143         if ((e)->action == EVENT_SIGNAL) \
144                 _sig_signal((e)->Ev.Sig.sig_proc, (e)->Ev.Sig.sig_bit); \
145         else if ((e)->action == EVENT_SOFTINT) \
146                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
147 } while (0)
148
149 #else /* !CONFIG_KERN_SIGNALS */
150
151 /*! Trigger an event */
152 #define event_do(e) \
153 do { \
154         if ((e)->action == EVENT_SOFTINT) \
155                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
156 } while (0)
157
158 /*! Trigger an event (to be used inside interrupts) */
159 #define event_doIntr(e) \
160 do { \
161         if ((e)->action == EVENT_SOFTINT) \
162                 (e)->Ev.Int.func((e)->Ev.Int.user_data); \
163 } while (0)
164
165 #endif
166
167 #ifdef CONFIG_KERN_OLDNAMES
168         #define INITEVENT_SIG  event_initSignal
169         #define INITEVENT_INT  event_initSoftInt
170         #define DOEVENT        event_do
171         #define DOEVENT_INTR   event_doIntr
172 #endif
173
174 #endif /* KERN_EVENT_H */