Task switching con salvataggio perfetto stato di interrupt (SR)
[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  * Each process can wait for just one signal.
12  * Multiple processes can wait for the same signal. In this
13  * case, each signal will wake-up one of them.
14  *
15  * \version $Id$
16  *
17  * \author Bernardo Innocenti <bernie@develer.com>
18  */
19
20 /*
21  * $Log$
22  * Revision 1.3  2004/07/30 14:24:16  rasky
23  * Task switching con salvataggio perfetto stato di interrupt (SR)
24  * Kernel monitor per dump informazioni su stack dei processi
25  *
26  * Revision 1.2  2004/06/03 11:27:09  bernie
27  * Add dual-license information.
28  *
29  * Revision 1.1  2004/05/23 17:27:00  bernie
30  * Import kern/ subdirectory.
31  *
32  */
33
34 #include "signal.h"
35 #include "proc.h"
36 #include "proc_p.h"
37 #include "hw.h"
38
39 // FIXME
40 #if CONFIG_KERN_SIGNALS
41
42 /*!
43  * Check if any of the signals in \a sigs has occurred and clear them.
44  * Return the signals that have occurred.
45  */
46 sigset_t sig_check(sigset_t sigs)
47 {
48         sigset_t result;
49         cpuflags_t flags;
50
51         DISABLE_IRQSAVE(flags);
52         result = CurrentProcess->sig_recv & sigs;
53         CurrentProcess->sig_recv &= ~sigs;
54         ENABLE_IRQRESTORE(flags);
55         return result;
56 }
57
58
59 /*!
60  * Sleep until any of the signals in \a sigs occurs.
61  * Return the signal(s) that have awaked the process.
62  */
63 sigset_t sig_wait(sigset_t sigs)
64 {
65         sigset_t result;
66         cpuflags_t flags;
67
68         DISABLE_IRQSAVE(flags);
69
70         /* Loop until we get at least one of the signals */
71         while (!(result = CurrentProcess->sig_recv & sigs))
72         {
73                 /* go to sleep and proc_schedule() another process */
74                 CurrentProcess->sig_wait = sigs;
75                 proc_schedule();
76         }
77
78         /* Signals found: clear them and return */
79         CurrentProcess->sig_recv &= ~sigs;
80         ENABLE_IRQRESTORE(flags);
81         return result;
82 }
83
84
85 /*!
86  * Send the signals \a sigs to the process \a proc.
87  * The process will be awaken if it was waiting for any of them.
88  *
89  * This call is interrupt safe (no \c DISABLE_INTS/ENABLE_INTS protection)
90  */
91 void _sig_signal(Process *proc, sigset_t sigs)
92 {
93         /* Set the signals */
94         proc->sig_recv |= sigs;
95
96         /* Check if process needs to be awaken */
97         if (proc->sig_recv & proc->sig_wait)
98         {
99                 /* Wake up process and enqueue in ready list */
100                 proc->sig_wait = 0;
101                 SCHED_ENQUEUE(proc);
102         }
103 }
104
105
106 /*!
107  * Same as _sig_signal() with interrupt protection.
108  *
109  * \note Inlined manually because some compilers are too
110  *       stupid to it automatically.
111  */
112 void sig_signal(Process *proc, sigset_t sigs)
113 {
114         DISABLE_INTS;
115
116         /* Set the signals */
117         proc->sig_recv |= sigs;
118
119         /* Check if process needs to be awaken */
120         if (proc->sig_recv & proc->sig_wait)
121         {
122                 /* Wake up process and enqueue in ready list */
123                 proc->sig_wait = 0;
124                 SCHED_ENQUEUE(proc);
125         }
126
127         ENABLE_INTS;
128 }
129
130 #endif /* CONFIG_KERN_SIGNALS */
131