Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / mware / event.c
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 2005 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Events handling implementation
34  *
35  *
36  * \author Giovanni Bajo <rasky@develer.com>
37  */
38
39
40 #include "event.h"
41 #include "cfg/cfg_signal.h"
42 #include "cfg/cfg_timer.h"
43
44 #include <drv/timer.h> /* timer_clock() */
45
46 void event_hook_ignore(UNUSED_ARG(Event *, e))
47 {
48 }
49
50 void event_hook_softint(Event *e)
51 {
52         e->Ev.Int.func(e->Ev.Int.user_data);
53 }
54
55 void event_hook_generic(Event *e)
56 {
57         e->Ev.Gen.completed = true;
58         MEMORY_BARRIER;
59 }
60
61 #if CONFIG_KERN && CONFIG_KERN_SIGNALS
62 void event_hook_signal(Event *e)
63 {
64         sig_post((e)->Ev.Sig.sig_proc, (e)->Ev.Sig.sig_bit);
65 }
66
67 void event_hook_generic_signal(Event *e)
68 {
69         sig_postSignal(&e->Ev.Sig.sig, e->Ev.Sig.sig_proc, e->Ev.Sig.sig_bit);
70 }
71
72 /*
73  * Custom event hook to notify the completion of a event monitored via
74  * event_select().
75  */
76 static void event_hook_generic_multiple_signal(Event *e)
77 {
78         sig_post(e->Ev.Sig.sig_proc, e->Ev.Sig.sig_bit);
79 }
80
81 /*
82  * Custom timer hook to notify the timeout of a event_waitTimeout().
83  */
84 static void event_hook_generic_timeout_signal(void *arg)
85 {
86         Event *e = (Event *)arg;
87
88         sig_postSignal(&e->Ev.Sig.sig, e->Ev.Sig.sig_proc, SIG_TIMEOUT);
89 }
90
91 /*
92  * event_waitTimeout() slow path: this function put the current process to
93  * sleep until the event is notified. The timeout is managed using the custom
94  * timer hook event_hook_generic_timeout_signal(): if the timeout expires the
95  * signal SIG_TIMEOUT is notified via sig_post() to the sigmask embedded in the
96  * event.
97  *
98  * The custom timer hook is required because the default timer's behaviour is
99  * to use the process's sigmask to notify the completion of an event, that is
100  * not suitable for this case, because we're sleeping on the event's sigmask
101  * instead.
102  */
103 static NOINLINE bool event_waitTimeoutSlowPath(Event *e, ticks_t timeout)
104 {
105         bool ret;
106
107         e->Ev.Sig.sig_proc = proc_current();
108         ret = (sig_waitTimeoutSignal(&e->Ev.Sig.sig,
109                         EVENT_GENERIC_SIGNAL, timeout,
110                         event_hook_generic_timeout_signal, e) & SIG_TIMEOUT) ?
111                         false : true;
112         return ret;
113 }
114
115 bool event_waitTimeout(Event *e, ticks_t timeout)
116 {
117         /*
118          * Fast path: check if the event already happened and return
119          * immediately in this case.
120          */
121         if (sig_checkSignal(&e->Ev.Sig.sig,
122                         EVENT_GENERIC_SIGNAL) == EVENT_GENERIC_SIGNAL)
123                 return true;
124         return event_waitTimeoutSlowPath(e, timeout);
125 }
126
127 /*
128  * event_select() slow path: this function handles the case when any event was
129  * not yet notified, so it takes care of making the current process to sleep on
130  * the list of events, mapping them to a different signal bit and issuing a
131  * call to sig_waitTimeout() using the process's sigmask.
132  */
133 static NOINLINE int event_selectSlowPath(Event **evs, int n, ticks_t timeout)
134 {
135         sigmask_t mask = (1 << n) - 1;
136         int i;
137
138         for (i = 0; i < n; i++)
139         {
140                 Event *e = evs[i];
141
142                 /* Map each event to a distinct signal bit */
143                 e->Ev.Sig.sig_proc = proc_current();
144                 e->Ev.Sig.sig_bit = 1 << i;
145                 e->action = event_hook_generic_multiple_signal;
146         }
147         IRQ_ENABLE;
148
149         mask = timeout ? sig_waitTimeout(mask, timeout) : sig_wait(mask);
150         if (mask & SIG_TIMEOUT)
151                 return -1;
152         return UINT8_LOG2(mask);
153 }
154
155 int event_select(Event **evs, int n, ticks_t timeout)
156 {
157         int i;
158
159         ASSERT(n <= SIG_USER_MAX);
160
161         IRQ_DISABLE;
162         /* Fast path: check if one of the event already happened */
163         for (i = 0; i < n; i++)
164         {
165                 Event *e = evs[i];
166
167                 if (__sig_checkSignal(&e->Ev.Sig.sig,
168                                 EVENT_GENERIC_SIGNAL) == EVENT_GENERIC_SIGNAL)
169                 {
170                         IRQ_ENABLE;
171                         return i;
172                 }
173         }
174         /* Otherwise, fallback to the slow path */
175         return event_selectSlowPath(evs, n, timeout);
176 }
177 #else /* !(CONFIG_KERN && CONFIG_KERN_SIGNALS) */
178 bool event_waitTimeout(Event *e, ticks_t timeout)
179 {
180         ticks_t end = timer_clock() + timeout;
181         bool ret;
182
183         while ((ACCESS_SAFE(e->Ev.Gen.completed) == false) ||
184                         TIMER_AFTER(timer_clock(), end))
185                 cpu_relax();
186         ret = e->Ev.Gen.completed;
187         e->Ev.Gen.completed = false;
188         MEMORY_BARRIER;
189
190         return ret;
191 }
192
193 int event_select(Event **evs, int n, ticks_t timeout)
194 {
195         ticks_t end = timer_clock() + timeout;
196         int i;
197
198         while (1)
199         {
200                 for (i = 0; i < n; i++)
201                 {
202                         Event *e = evs[i];
203                         if (ACCESS_SAFE(e->Ev.Gen.completed) == true)
204                         {
205                                 e->Ev.Gen.completed = false;
206                                 MEMORY_BARRIER;
207                                 return i;
208                         }
209                 }
210                 if (timeout && TIMER_AFTER(timer_clock(), end))
211                         break;
212                 cpu_relax();
213         }
214         return -1;
215 }
216 #endif /* CONFIG_KERN && CONFIG_KERN_SIGNALS */