Fix a bug when a signal arrive before timeout, please review.
[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  * In this implementation, each process has a limited set of signal
47  * bits (usually 32) and can wait for multiple signals at the same
48  * time using sig_wait().  Signals can also be polled using sig_check(),
49  * but a process spinning on its signals usually defeats their purpose
50  * of providing a multitasking-friendly infrastructure for event-driven
51  * applications.
52  *
53  * Signals are like flags: they are either active or inactive.  After an
54  * external event has delivered a particular signal, it remains raised until
55  * the process acknowledges it using either sig_wait() or sig_check().
56  * Counting signals is not a reliable way to count how many times a
57  * particular event has occurred, because the same signal may be
58  * delivered twice before the process can notice.
59  *
60  * Any execution context, including an interrupt handler, can deliver
61  * a signal to a process using sig_signal().  Multiple distinct signals
62  * may be delivered at once with a single invocation of sig_signal(),
63  * although this is rarely useful.
64  *
65  * There's no hardcoded mapping of specific events to signal bits.
66  * The meaning of a particular signal bit is defined by an agreement
67  * between the delivering entity and the receiving process.
68  * For instance, a terminal driver may be written to deliver
69  * a signal bit called SIG_INT when it reads the CTRL-C sequence
70  * from the keyboard, and a process may react to it by quitting.
71  *
72  * The SIG_SINGLE bit is reserved for a special purpose (this is
73  * more a suggestion than a constraint).  When a process wants
74  * wait for a single event on the fly, it needs not allocate a
75  * free signal from its pool.  Instead, SIG_SINGLE can be used
76  *
77  * The "event" module is a higher-level interface that can optionally
78  * deliver signals to processes.  Messages provide even higher-level
79  * IPC services built on signals.  Semaphore arbitration is also
80  * implemented using signals.
81  *
82  * Signals are very low overhead.  Using them exclusively to wait
83  * for multiple asynchronous events results in very simple dispatch
84  * logic with low processor and resource usage.
85  *
86  *
87  * \version $Id$
88  *
89  * \author Bernardo Innocenti <bernie@develer.com>
90  */
91
92 #include "signal.h"
93
94 #include <cfg/debug.h>
95 #include <drv/timer.h>
96 #include <kern/proc.h>
97 #include <kern/proc_p.h>
98  
99
100 #if CONFIG_KERN_SIGNALS
101
102 /**
103  * Check if any of the signals in \a sigs has occurred and clear them.
104  * \return the signals that have occurred.
105  */
106 sigmask_t sig_check(sigmask_t sigs)
107 {
108         sigmask_t result;
109         cpuflags_t flags;
110
111         IRQ_SAVE_DISABLE(flags);
112         result = CurrentProcess->sig_recv & sigs;
113         CurrentProcess->sig_recv &= ~sigs;
114         IRQ_RESTORE(flags);
115
116         return result;
117 }
118
119
120 /**
121  * Sleep until any of the signals in \a sigs occurs.
122  * \return the signal(s) that have awaked the process.
123  */
124 sigmask_t sig_wait(sigmask_t sigs)
125 {
126         sigmask_t result;
127         cpuflags_t flags;
128
129         IRQ_SAVE_DISABLE(flags);
130
131         /* Loop until we get at least one of the signals */
132         while (!(result = CurrentProcess->sig_recv & sigs))
133         {
134                 /* go to sleep and proc_schedule() another process */
135                 CurrentProcess->sig_wait = sigs;
136                 proc_schedule();
137
138                 /* When we come back here, a signal must be arrived */
139                 ASSERT(!CurrentProcess->sig_wait);
140                 ASSERT(CurrentProcess->sig_recv);
141         }
142
143         /* Signals found: clear them and return */
144         CurrentProcess->sig_recv &= ~sigs;
145
146         IRQ_RESTORE(flags);
147         return result;
148 }
149
150 /**
151  * Sleep until any of the signals in \a sigs or \a timeout ticks elapse.
152  * If the timeout elapse a SIG_TIMEOUT is added to the received signal(s).
153  * \return the signal(s) that have awaked the process.
154  * \note Caller must check return value to check which signal has awaked the process.
155  */
156 sigmask_t sig_waitTimeout(sigmask_t sigs, ticks_t timeout)
157 {
158         Timer t;
159         sigmask_t res;
160         cpuflags_t flags;
161
162         ASSERT(!sig_check(SIG_TIMEOUT));
163         ASSERT(!(sigs & SIG_TIMEOUT));
164         /* IRQ are needed to run timer */
165         ASSERT(IRQ_ENABLED);
166
167         timer_set_event_signal(&t, proc_current(), SIG_TIMEOUT);
168         timer_setDelay(&t, timeout);
169         timer_add(&t);
170         res = sig_wait(SIG_TIMEOUT | sigs);
171
172         IRQ_SAVE_DISABLE(flags);
173         /* Remove timer if sigs occur before timer signal */
174         if (!(res & SIG_TIMEOUT) && !sig_check(SIG_TIMEOUT))
175                 timer_abort(&t);
176         IRQ_RESTORE(flags);
177 }
178
179
180 /**
181  * Send the signals \a sigs to the process \a proc.
182  * The process will be awaken if it was waiting for any of them.
183  *
184  * \note This call is interrupt safe.
185  */
186 void sig_signal(Process *proc, sigmask_t sigs)
187 {
188         cpuflags_t flags;
189         IRQ_SAVE_DISABLE(flags);
190
191         /* Set the signals */
192         proc->sig_recv |= sigs;
193
194         /* Check if process needs to be awaken */
195         if (proc->sig_recv & proc->sig_wait)
196         {
197                 /* Wake up process and enqueue in ready list */
198                 proc->sig_wait = 0;
199                 SCHED_ENQUEUE(proc);
200         }
201
202         IRQ_RESTORE(flags);
203 }
204
205 #endif /* CONFIG_KERN_SIGNALS */
206