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 2004 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999, 2000, 2001 Bernardo Innocenti <bernie@develer.com>
34 * \brief IPC signals implementation.
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.
42 * Despite the name, one shouldn't confuse these signals with POSIX
43 * signals. POSIX signals are usually executed synchronously, like
44 * software interrupts.
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
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.
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.
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.
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
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.
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.
89 * \author Bernardo Innocenti <bernie@develer.com>
94 *#* Revision 1.14 2006/07/19 12:56:27 bernie
95 *#* Convert to new Doxygen style.
97 *#* Revision 1.13 2006/02/24 01:17:05 bernie
98 *#* Update for new emulator.
100 *#* Revision 1.12 2005/11/04 16:20:02 bernie
101 *#* Fix reference to README.devlib in header.
103 *#* Revision 1.11 2005/04/11 19:10:28 bernie
104 *#* Include top-level headers from cfg/ subdir.
106 *#* Revision 1.10 2004/12/13 12:07:06 bernie
107 *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
109 *#* Revision 1.9 2004/12/08 08:57:35 bernie
110 *#* Rename sigset_t to sigmask_t.
112 *#* Revision 1.8 2004/09/14 21:06:44 bernie
113 *#* Use debug.h instead of kdebug.h.
115 *#* Revision 1.7 2004/08/25 14:12:09 rasky
116 *#* Aggiornato il comment block dei log RCS
118 *#* Revision 1.6 2004/08/14 19:37:57 rasky
119 *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
121 *#* Revision 1.5 2004/08/04 21:50:33 bernie
122 *#* Add extensive documentation.
124 *#* Revision 1.4 2004/07/30 14:30:27 rasky
125 *#* Resa la sig_signal interrupt safe (con il nuovo scheduler IRQ-safe)
126 *#* Rimossa event_doIntr (ora inutile) e semplificata la logica delle macro con funzioni inline
128 *#* Revision 1.3 2004/07/30 14:24:16 rasky
129 *#* Task switching con salvataggio perfetto stato di interrupt (SR)
130 *#* Kernel monitor per dump informazioni su stack dei processi
132 *#* Revision 1.2 2004/06/03 11:27:09 bernie
133 *#* Add dual-license information.
135 *#* Revision 1.1 2004/05/23 17:27:00 bernie
136 *#* Import kern/ subdirectory.
142 #include <kern/proc.h>
143 #include <kern/proc_p.h>
144 #include <cfg/debug.h>
146 #if CONFIG_KERN_SIGNALS
149 * Check if any of the signals in \a sigs has occurred and clear them.
150 * Return the signals that have occurred.
152 sigmask_t sig_check(sigmask_t sigs)
157 IRQ_SAVE_DISABLE(flags);
158 result = CurrentProcess->sig_recv & sigs;
159 CurrentProcess->sig_recv &= ~sigs;
167 * Sleep until any of the signals in \a sigs occurs.
168 * Return the signal(s) that have awaked the process.
170 sigmask_t sig_wait(sigmask_t sigs)
175 IRQ_SAVE_DISABLE(flags);
177 /* Loop until we get at least one of the signals */
178 while (!(result = CurrentProcess->sig_recv & sigs))
180 /* go to sleep and proc_schedule() another process */
181 CurrentProcess->sig_wait = sigs;
184 /* When we come back here, a signal must be arrived */
185 ASSERT(!CurrentProcess->sig_wait);
186 ASSERT(CurrentProcess->sig_recv);
189 /* Signals found: clear them and return */
190 CurrentProcess->sig_recv &= ~sigs;
198 * Send the signals \a sigs to the process \a proc.
199 * The process will be awaken if it was waiting for any of them.
201 * \note This call is interrupt safe.
203 void sig_signal(Process *proc, sigmask_t sigs)
206 IRQ_SAVE_DISABLE(flags);
208 /* Set the signals */
209 proc->sig_recv |= sigs;
211 /* Check if process needs to be awaken */
212 if (proc->sig_recv & proc->sig_wait)
214 /* Wake up process and enqueue in ready list */
222 #endif /* CONFIG_KERN_SIGNALS */