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