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