move Hook type definition in cfg/compiler.h for a more generic usage
[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 #if CONFIG_KERN_SIGNALS
51 void event_hook_signal(Event *e)
52 {
53         sig_post((e)->Ev.Sig.sig_proc, (e)->Ev.Sig.sig_bit);
54 }
55
56 void event_hook_generic_signal(Event *e)
57 {
58         sig_postSignal(&e->Ev.SigGen.sig,
59                                 e->Ev.SigGen.sig_proc,
60                                 EVENT_GENERIC_SIGNAL);
61 }
62 #endif
63
64 void event_hook_softint(Event *e)
65 {
66         e->Ev.Int.func(e->Ev.Int.user_data);
67 }
68
69 void event_hook_generic(Event *e)
70 {
71         e->Ev.Gen.completed = true;
72         MEMORY_BARRIER;
73 }
74
75 /**
76  * Wait for multiple events
77  *
78  * On success return the offset in the \a evs vector of the Event that
79  * happened, -1 if the timeout expires.
80  *
81  * NOTE: timeout == 0 means no timeout.
82  */
83 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
84 int event_select(Event **evs, int n, ticks_t timeout)
85 {
86         sigmask_t mask = (1 << n) - 1;
87         int i;
88
89         ASSERT(n <= SIG_USER_MAX);
90         for (i = 0; i < n; i++)
91         {
92                 Event *e = evs[i];
93                 /* Map each event to a distinct signal bit */
94                 event_initSignal(e, proc_current(), 1 << i);
95         }
96         mask = timeout ? sig_waitTimeout(mask, timeout) : sig_wait(mask);
97         i = UINT8_LOG2(mask);
98
99         return i < n ? i : -1;
100 }
101 #else
102 int event_select(Event **evs, int n, ticks_t timeout)
103 {
104         ticks_t end = timer_clock() + timeout;
105         int i;
106
107         while (1)
108         {
109                 for (i = 0; i < n; i++)
110                 {
111                         Event *e = evs[i];
112                         if (ACCESS_SAFE(e->Ev.Gen.completed) == true)
113                         {
114                                 e->Ev.Gen.completed = false;
115                                 MEMORY_BARRIER;
116                                 return i;
117                         }
118                 }
119                 if (timeout && TIMER_AFTER(timer_clock(), end))
120                         break;
121                 cpu_relax();
122         }
123         return -1;
124 }
125 #endif