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