Aggiornato il comment block dei log RCS
[bertos.git] / kern / signal.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999, 2000, 2001 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief IPC signals implementation.
10  *
11  * Signals are a low-level IPC primitive.  A process receives a signal
12  * when some external event has happened.  Like interrupt requests,
13  * signals do not carry any additional information.  If processing a
14  * specific event requires additional data, the process must obtain it
15  * through some other mechanism.
16  *
17  * Despite the name, one shouldn't confuse these signals with POSIX
18  * signals.  POSIX signals are usually executed synchronously, like
19  * software interrupts.
20  *
21  * In this implementation, each process has a limited set of signal
22  * bits (usually 32) and can wait for multiple signals at the same
23  * time using sig_wait().  Signals can also be polled using sig_check(),
24  * but a process spinning on its signals usually defeats their purpose
25  * of providing a multitasking-friendly infrastructure for event-driven
26  * applications.
27  *
28  * Signals are like flags: they are either active or inactive.  After an
29  * external event has delivered a particular signal, it remains raised until
30  * the process acknowledges it using either sig_wait() or sig_check().
31  * Counting signals is not a reliable way to count how many times a
32  * particular event has occurred, because the same signal may be
33  * delivered twice before the process can notice.
34  *
35  * Any execution context, including an interrupt handler, can deliver
36  * a signal to a process using sig_signal().  Multiple distinct signals
37  * may be delivered at once with a single invocation of sig_signal(),
38  * although this is rarely useful.
39  *
40  * There's no hardcoded mapping of specific events to signal bits.
41  * The meaning of a particular signal bit is defined by an agreement
42  * between the delivering entity and the receiving process.
43  * For instance, a terminal driver may be written to deliver
44  * a signal bit called SIG_INT when it reads the CTRL-C sequence
45  * from the keyboard, and a process may react to it by quitting.
46  *
47  * The SIG_SINGLE bit is reserved for a special purpose (this is
48  * more a suggestion than a constraint).  When a process wants
49  * wait for a single event on the fly, it needs not allocate a
50  * free signal from its pool.  Instead, SIG_SINGLE can be used
51  *
52  * The "event" module is a higher-level interface that can optionally
53  * deliver signals to processes.  Messages provide even higher-level
54  * IPC services built on signals.  Semaphore arbitration is also
55  * implemented using signals.
56  *
57  * Signals are very low overhead.  Using them exclusively to wait
58  * for multiple asynchronous events results in very simple dispatch
59  * logic with low processor and resource usage.
60  *
61  *
62  * \version $Id$
63  *
64  * \author Bernardo Innocenti <bernie@develer.com>
65  */
66
67 /*#*
68  *#* $Log$
69  *#* Revision 1.7  2004/08/25 14:12:09  rasky
70  *#* Aggiornato il comment block dei log RCS
71  *#*
72  *#* Revision 1.6  2004/08/14 19:37:57  rasky
73  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
74  *#*
75  *#* Revision 1.5  2004/08/04 21:50:33  bernie
76  *#* Add extensive documentation.
77  *#*
78  *#* Revision 1.4  2004/07/30 14:30:27  rasky
79  *#* Resa la sig_signal interrupt safe (con il nuovo scheduler IRQ-safe)
80  *#* Rimossa event_doIntr (ora inutile) e semplificata la logica delle macro con funzioni inline
81  *#*
82  *#* Revision 1.3  2004/07/30 14:24:16  rasky
83  *#* Task switching con salvataggio perfetto stato di interrupt (SR)
84  *#* Kernel monitor per dump informazioni su stack dei processi
85  *#*
86  *#* Revision 1.2  2004/06/03 11:27:09  bernie
87  *#* Add dual-license information.
88  *#*
89  *#* Revision 1.1  2004/05/23 17:27:00  bernie
90  *#* Import kern/ subdirectory.
91  *#*
92  *#*/
93
94 #include "signal.h"
95 #include "proc.h"
96 #include "proc_p.h"
97 #include "hw.h"
98 #include <drv/kdebug.h>
99
100 // FIXME
101 #if CONFIG_KERN_SIGNALS
102
103 /*!
104  * Check if any of the signals in \a sigs has occurred and clear them.
105  * Return the signals that have occurred.
106  */
107 sigset_t sig_check(sigset_t sigs)
108 {
109         sigset_t result;
110         cpuflags_t flags;
111
112         DISABLE_IRQSAVE(flags);
113         result = CurrentProcess->sig_recv & sigs;
114         CurrentProcess->sig_recv &= ~sigs;
115         ENABLE_IRQRESTORE(flags);
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 sigset_t sig_wait(sigset_t sigs)
125 {
126         sigset_t result;
127         cpuflags_t flags;
128
129         DISABLE_IRQSAVE(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         ENABLE_IRQRESTORE(flags);
146         return result;
147 }
148
149
150 /*!
151  * Send the signals \a sigs to the process \a proc.
152  * The process will be awaken if it was waiting for any of them.
153  *
154  * \note This call is interrupt safe.
155  */
156 void sig_signal(Process *proc, sigset_t sigs)
157 {
158         cpuflags_t flags;
159         DISABLE_IRQSAVE(flags);
160
161         /* Set the signals */
162         proc->sig_recv |= sigs;
163
164         /* Check if process needs to be awaken */
165         if (proc->sig_recv & proc->sig_wait)
166         {
167                 /* Wake up process and enqueue in ready list */
168                 proc->sig_wait = 0;
169                 SCHED_ENQUEUE(proc);
170         }
171
172         ENABLE_IRQRESTORE(flags);
173 }
174
175 #endif /* CONFIG_KERN_SIGNALS */
176