mware: fix generic completion events behaviour without the kernel
[bertos.git] / bertos / mware / event.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2001, 2003 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Events handling
34  *
35  * This module implements a common system for executing
36  * a user defined action calling a hook function.
37  *
38  * \author Bernie Innocenti <bernie@codewiz.org>
39  */
40
41 #ifndef KERN_EVENT_H
42 #define KERN_EVENT_H
43
44 #include <cfg/compiler.h>
45 #include "cfg/cfg_proc.h"
46 #include "cfg/cfg_signal.h"
47 #include "cfg/cfg_timer.h"
48
49 #include <cpu/power.h> /* cpu_relax() */
50
51 #if CONFIG_KERN
52         #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
53                 #include <kern/signal.h>
54         #endif
55
56         /* Forward decl */
57         struct Process;
58 #endif
59
60
61 /// User defined callback type
62 typedef void (*Hook)(void *);
63
64 typedef struct Event
65 {
66         void (*action)(struct Event *);
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                         sigbit_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
82                 struct
83                 {
84                         bool completed;             /* Generic event completion */
85                 } Gen;
86         } Ev;
87 } Event;
88
89 void event_hook_ignore(Event *event);
90 void event_hook_signal(Event *event);
91 void event_hook_softint(Event *event);
92 void event_hook_generic(Event *event);
93 void event_hook_generic_timeout(Event *event);
94
95 /** Initialize the event \a e as a no-op */
96 #define event_initNone(e) \
97         ((e)->action = event_hook_ignore)
98
99 /** Same as event_initNone(), but returns the initialized event */
100 INLINE Event event_createNone(void);
101 INLINE Event event_createNone(void)
102 {
103         Event e;
104         e.action = event_hook_ignore;
105         return e;
106 }
107
108 /** Initialize the event \a e with a software interrupt (call function \a f, with parameter \a u) */
109 #define event_initSoftint(e,f,u) \
110         ((e)->action = event_hook_softint,(e)->Ev.Int.func = (f), (e)->Ev.Int.user_data = (u))
111
112 /** Same as event_initSoftint(), but returns the initialized event */
113 INLINE Event event_createSoftint(Hook func, void *user_data)
114 {
115         Event e;
116         e.action = event_hook_softint;
117         e.Ev.Int.func = func;
118         e.Ev.Int.user_data = user_data;
119         return e;
120 }
121
122 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
123
124 /** Initialize the event \a e with a signal (send signal \a s to process \a p) */
125 #define event_initSignal(e,p,s) \
126         ((e)->action = event_hook_signal,(e)->Ev.Sig.sig_proc = (p), (e)->Ev.Sig.sig_bit = (s))
127
128 /** Same as event_initSignal(), but returns the initialized event */
129 INLINE Event event_createSignal(struct Process *proc, sigbit_t bit)
130 {
131         Event e;
132         e.action = event_hook_signal;
133         e.Ev.Sig.sig_proc = proc;
134         e.Ev.Sig.sig_bit = bit;
135         return e;
136 }
137
138 #endif
139
140 /**
141  * Prevent the compiler from optimizing access to the variable \a x, enforcing
142  * a refetch from memory. This also forbid from reordering successing instances
143  * of ACCESS_SAFE().
144  *
145  * TODO: move this to cfg/compiler.h
146  */
147 #define ACCESS_SAFE(x) (*(volatile typeof(x) *)&(x))
148
149 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
150 /** Initialize the generic sleepable event \a e */
151 #define event_initGeneric(e) \
152         event_initSignal(e, proc_current(), SIG_SYSTEM5)
153 #else
154 #define event_initGeneric(e) \
155         ((e)->action = event_hook_generic, (e)->Ev.Gen.completed = false)
156 #endif
157
158 /**
159  * Create a generic sleepable event.
160  *
161  * \return the properly initialized generic event structure.
162  */
163 INLINE Event event_createGeneric(void)
164 {
165         Event e;
166         event_initGeneric(&e);
167         return e;
168 }
169
170 /**
171  * Wait the completion of event \a e.
172  */
173 INLINE void event_wait(Event *e)
174 {
175 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
176         e->Ev.Sig.sig_proc = proc_current();
177         sig_wait(e->Ev.Sig.sig_bit);
178 #else
179         while (ACCESS_SAFE(e->Ev.Gen.completed) == false)
180                 cpu_relax();
181         e->Ev.Gen.completed = false;
182         MEMORY_BARRIER;
183 #endif
184 }
185
186 #if CONFIG_TIMER_EVENTS
187 #include <drv/timer.h> /* timer_clock() */
188
189 /* TODO: move these macros to drv/timer.h */
190 #define TIMER_AFTER(x, y) ((long)(y) - (long)(x) < 0)
191 #define TIMER_BEFORE(x, y) TIMER_AFTER(y, x)
192
193 /**
194  * Wait the completion of event \a e or \a timeout elapses.
195  */
196 INLINE bool event_waitTimeout(Event *e, ticks_t timeout)
197 {
198 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
199         e->Ev.Sig.sig_proc = proc_current();
200         return (sig_waitTimeout(e->Ev.Sig.sig_bit, timeout) & SIG_TIMEOUT) ?
201                                 false : true;
202 #else
203         ticks_t end = timer_clock() + timeout;
204         bool ret;
205
206         while ((ACCESS_SAFE(e->Ev.Gen.completed) == false) ||
207                         TIMER_AFTER(timer_clock(), end))
208                 cpu_relax();
209         ret = e->Ev.Gen.completed;
210         e->Ev.Gen.completed = false;
211         MEMORY_BARRIER;
212
213         return ret;
214 #endif
215 }
216 #endif /* CONFIG_TIMER_EVENTS */
217
218 /** Trigger an event */
219 INLINE void event_do(struct Event *e)
220 {
221         e->action(e);
222 }
223
224 #endif /* KERN_EVENT_H */