4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
33 * \brief Events handling implementation
36 * \author Giovanni Bajo <rasky@develer.com>
41 #include "cfg/cfg_signal.h"
42 #include "cfg/cfg_timer.h"
44 #include <drv/timer.h> /* timer_clock() */
46 void event_hook_ignore(UNUSED_ARG(Event *, e))
50 void event_hook_softint(Event *e)
52 e->Ev.Int.func(e->Ev.Int.user_data);
55 void event_hook_generic(Event *e)
57 e->Ev.Gen.completed = true;
61 #if CONFIG_KERN && CONFIG_KERN_SIGNALS
62 void event_hook_signal(Event *e)
64 sig_post((e)->Ev.Sig.sig_proc, (e)->Ev.Sig.sig_bit);
67 void event_hook_generic_signal(Event *e)
69 sig_postSignal(&e->Ev.Sig.sig, e->Ev.Sig.sig_proc, e->Ev.Sig.sig_bit);
73 * Custom event hook to notify the completion of a event monitored via
76 static void event_hook_generic_multiple_signal(Event *e)
78 sig_post(e->Ev.Sig.sig_proc, e->Ev.Sig.sig_bit);
82 * Custom timer hook to notify the timeout of a event_waitTimeout().
84 static void event_hook_generic_timeout_signal(void *arg)
86 Event *e = (Event *)arg;
88 sig_postSignal(&e->Ev.Sig.sig, e->Ev.Sig.sig_proc, SIG_TIMEOUT);
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
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
103 static NOINLINE bool event_waitTimeoutSlowPath(Event *e, ticks_t timeout)
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) ?
115 bool event_waitTimeout(Event *e, ticks_t timeout)
118 * Fast path: check if the event already happened and return
119 * immediately in this case.
121 if (sig_checkSignal(&e->Ev.Sig.sig,
122 EVENT_GENERIC_SIGNAL) == EVENT_GENERIC_SIGNAL)
124 return event_waitTimeoutSlowPath(e, timeout);
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.
133 static NOINLINE int event_selectSlowPath(Event **evs, int n, ticks_t timeout)
135 sigmask_t mask = (1 << n) - 1;
138 for (i = 0; i < n; i++)
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;
149 mask = timeout ? sig_waitTimeout(mask, timeout) : sig_wait(mask);
150 if (mask & SIG_TIMEOUT)
152 return UINT8_LOG2(mask);
155 int event_select(Event **evs, int n, ticks_t timeout)
159 ASSERT(n <= SIG_USER_MAX);
162 /* Fast path: check if one of the event already happened */
163 for (i = 0; i < n; i++)
167 if (__sig_checkSignal(&e->Ev.Sig.sig,
168 EVENT_GENERIC_SIGNAL) == EVENT_GENERIC_SIGNAL)
174 /* Otherwise, fallback to the slow path */
175 return event_selectSlowPath(evs, n, timeout);
177 #else /* !(CONFIG_KERN && CONFIG_KERN_SIGNALS) */
178 bool event_waitTimeout(Event *e, ticks_t timeout)
180 ticks_t end = timer_clock() + timeout;
183 while ((ACCESS_SAFE(e->Ev.Gen.completed) == false) ||
184 TIMER_AFTER(timer_clock(), end))
186 ret = e->Ev.Gen.completed;
187 e->Ev.Gen.completed = false;
193 int event_select(Event **evs, int n, ticks_t timeout)
195 ticks_t end = timer_clock() + timeout;
200 for (i = 0; i < n; i++)
203 if (ACCESS_SAFE(e->Ev.Gen.completed) == true)
205 e->Ev.Gen.completed = false;
210 if (timeout && TIMER_AFTER(timer_clock(), end))
216 #endif /* CONFIG_KERN && CONFIG_KERN_SIGNALS */