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