5abd7062679aeacd91f085741c18db31e71d29f8
[bertos.git] / kern / signal.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 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2000, 2001 Bernardo Innocenti <bernie@develer.com>
31  *
32  * -->
33  *
34  * \brief IPC signals implementation.
35  *
36  * Signals are a low-level IPC primitive.  A process receives a signal
37  * when some external event has happened.  Like interrupt requests,
38  * signals do not carry any additional information.  If processing a
39  * specific event requires additional data, the process must obtain it
40  * through some other mechanism.
41  *
42  * Despite the name, one shouldn't confuse these signals with POSIX
43  * signals.  POSIX signals are usually executed synchronously, like
44  * software interrupts.
45  *
46  * Signals are very low overhead.  Using them exclusively to wait
47  * for multiple asynchronous events results in very simple dispatch
48  * logic with low processor and resource usage.
49  *
50  * The "event" module is a higher-level interface that can optionally
51  * deliver signals to processes.  Messages provide even higher-level
52  * IPC services built on signals.  Semaphore arbitration is also
53  * implemented using signals.
54  *
55  * In this implementation, each process has a limited set of signal
56  * bits (usually 32) and can wait for multiple signals at the same
57  * time using sig_wait().  Signals can also be polled using sig_check(),
58  * but a process spinning on its signals usually defeats their purpose
59  * of providing a multitasking-friendly infrastructure for event-driven
60  * applications.
61  *
62  * Signals are like flags: they are either active or inactive.  After an
63  * external event has delivered a particular signal, it remains raised until
64  * the process acknowledges it using either sig_wait() or sig_check().
65  * Counting signals is not a reliable way to count how many times a
66  * particular event has occurred, because the same signal may be
67  * delivered twice before the process can notice.
68  *
69  * Any execution context, including an interrupt handler, can deliver
70  * a signal to a process using sig_signal().  Multiple independent signals
71  * may be delivered at once with a single invocation of sig_signal(),
72  * although this is rarely useful.
73  *
74  * \section signal_allocation Signal Allocation
75  *
76  * There's no hardcoded mapping of specific events to signal bits.
77  * The meaning of a particular signal bit is defined by an agreement
78  * between the delivering entity and the receiving process.
79  * For instance, a terminal driver may be designed to deliver
80  * a signal bit called SIG_INT when it reads the CTRL-C sequence
81  * from the keyboard, and a process may react to it by quitting.
82  *
83  * \section sig_single SIG_SINGLE
84  *
85  * The SIG_SINGLE bit is reserved as a convenient shortcut in those
86  * simple scenarios where a process needs to wait on just one event
87  * synchronously.  By using SIG_SINGLE, there's no need to allocate
88  * a specific signal from the free pool.  The constraints for safely
89  * accessing SIG_SINGLE are:
90  *  - The process MUST sig_wait() exclusively on SIG_SINGLE
91  *  - SIG_SIGNAL MUST NOT be left pending after use (sig_wait() will reset
92  *        it automatically)
93  *  - Do not sleep between starting the asynchronous task that will fire
94  *    SIG_SINGLE, and the call to  sig_wait().
95  *  - Do not call system functions that may implicitly sleep, such as
96  *    timer_delayTickes().
97  *
98  * \version $Id$
99  *
100  * \author Bernardo Innocenti <bernie@develer.com>
101  */
102
103 #include "signal.h"
104
105 #include <cfg/debug.h>
106 #include <drv/timer.h>
107 #include <kern/proc.h>
108 #include <kern/proc_p.h>
109  
110
111 #if CONFIG_KERN_SIGNALS
112
113 /**
114  * Check if any of the signals in \a sigs has occurred and clear them.
115  * \return the signals that have occurred.
116  */
117 sigmask_t sig_check(sigmask_t sigs)
118 {
119         sigmask_t result;
120         cpuflags_t flags;
121
122         IRQ_SAVE_DISABLE(flags);
123         result = CurrentProcess->sig_recv & sigs;
124         CurrentProcess->sig_recv &= ~sigs;
125         IRQ_RESTORE(flags);
126
127         return result;
128 }
129
130
131 /**
132  * Sleep until any of the signals in \a sigs occurs.
133  * \return the signal(s) that have awoken the process.
134  */
135 sigmask_t sig_wait(sigmask_t sigs)
136 {
137         sigmask_t result;
138         cpuflags_t flags;
139
140         IRQ_SAVE_DISABLE(flags);
141
142         /* Loop until we get at least one of the signals */
143         while (!(result = CurrentProcess->sig_recv & sigs))
144         {
145                 /* go to sleep and proc_schedule() another process */
146                 CurrentProcess->sig_wait = sigs;
147                 proc_schedule();
148
149                 /* When we come back here, a signal must be arrived */
150                 ASSERT(!CurrentProcess->sig_wait);
151                 ASSERT(CurrentProcess->sig_recv);
152         }
153
154         /* Signals found: clear them and return */
155         CurrentProcess->sig_recv &= ~sigs;
156
157         IRQ_RESTORE(flags);
158         return result;
159 }
160
161 /**
162  * Sleep until any of the signals in \a sigs or \a timeout ticks elapse.
163  * If the timeout elapse a SIG_TIMEOUT is added to the received signal(s).
164  * \return the signal(s) that have awoken the process.
165  * \note Caller must check return value to check which signal awoke the process.
166  */
167 sigmask_t sig_waitTimeout(sigmask_t sigs, ticks_t timeout)
168 {
169         Timer t;
170         sigmask_t res;
171         cpuflags_t flags;
172
173         ASSERT(!sig_check(SIG_TIMEOUT));
174         ASSERT(!(sigs & SIG_TIMEOUT));
175         /* IRQ are needed to run timer */
176         ASSERT(IRQ_ENABLED());
177
178         timer_set_event_signal(&t, proc_current(), SIG_TIMEOUT);
179         timer_setDelay(&t, timeout);
180         timer_add(&t);
181         res = sig_wait(SIG_TIMEOUT | sigs);
182
183         IRQ_SAVE_DISABLE(flags);
184         /* Remove timer if sigs occur before timer signal */
185         if (!(res & SIG_TIMEOUT) && !sig_check(SIG_TIMEOUT))
186                 timer_abort(&t);
187         IRQ_RESTORE(flags);
188         return res;
189 }
190
191
192 /**
193  * Send the signals \a sigs to the process \a proc.
194  * The process will be awoken if it was waiting for any of them.
195  *
196  * \note This call is interrupt safe.
197  */
198 void sig_signal(Process *proc, sigmask_t sigs)
199 {
200         cpuflags_t flags;
201         IRQ_SAVE_DISABLE(flags);
202
203         /* Set the signals */
204         proc->sig_recv |= sigs;
205
206         /* Check if process needs to be awoken */
207         if (proc->sig_recv & proc->sig_wait)
208         {
209                 /* Wake up process and enqueue in ready list */
210                 proc->sig_wait = 0;
211                 SCHED_ENQUEUE(proc);
212         }
213
214         IRQ_RESTORE(flags);
215 }
216
217 #endif /* CONFIG_KERN_SIGNALS */
218