Resa la sig_signal interrupt safe (con il nuovo scheduler IRQ-safe)
[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.4  2004/07/30 14:30:27  rasky
23  * Resa la sig_signal interrupt safe (con il nuovo scheduler IRQ-safe)
24  * Rimossa event_doIntr (ora inutile) e semplificata la logica delle macro con funzioni inline
25  *
26  * Revision 1.3  2004/07/30 14:24:16  rasky
27  * Task switching con salvataggio perfetto stato di interrupt (SR)
28  * Kernel monitor per dump informazioni su stack dei processi
29  *
30  * Revision 1.2  2004/06/03 11:27:09  bernie
31  * Add dual-license information.
32  *
33  * Revision 1.1  2004/05/23 17:27:00  bernie
34  * Import kern/ subdirectory.
35  *
36  */
37
38 #include "signal.h"
39 #include "proc.h"
40 #include "proc_p.h"
41 #include "hw.h"
42
43 // FIXME
44 #if CONFIG_KERN_SIGNALS
45
46 /*!
47  * Check if any of the signals in \a sigs has occurred and clear them.
48  * Return the signals that have occurred.
49  */
50 sigset_t sig_check(sigset_t sigs)
51 {
52         sigset_t result;
53         cpuflags_t flags;
54
55         DISABLE_IRQSAVE(flags);
56         result = CurrentProcess->sig_recv & sigs;
57         CurrentProcess->sig_recv &= ~sigs;
58         ENABLE_IRQRESTORE(flags);
59         return result;
60 }
61
62
63 /*!
64  * Sleep until any of the signals in \a sigs occurs.
65  * Return the signal(s) that have awaked the process.
66  */
67 sigset_t sig_wait(sigset_t sigs)
68 {
69         sigset_t result;
70         cpuflags_t flags;
71
72         DISABLE_IRQSAVE(flags);
73
74         /* Loop until we get at least one of the signals */
75         while (!(result = CurrentProcess->sig_recv & sigs))
76         {
77                 /* go to sleep and proc_schedule() another process */
78                 CurrentProcess->sig_wait = sigs;
79                 proc_schedule();
80         }
81
82         /* Signals found: clear them and return */
83         CurrentProcess->sig_recv &= ~sigs;
84         ENABLE_IRQRESTORE(flags);
85         return result;
86 }
87
88
89 /*!
90  * Send the signals \a sigs to the process \a proc.
91  * The process will be awaken if it was waiting for any of them.
92  *
93  * \note This call is interrupt safe.
94  */
95 void sig_signal(Process *proc, sigset_t sigs)
96 {
97         cpuflags_t flags;
98         DISABLE_IRQSAVE(flags);
99
100         /* Set the signals */
101         proc->sig_recv |= sigs;
102
103         /* Check if process needs to be awaken */
104         if (proc->sig_recv & proc->sig_wait)
105         {
106                 /* Wake up process and enqueue in ready list */
107                 proc->sig_wait = 0;
108                 SCHED_ENQUEUE(proc);
109         }
110
111         ENABLE_IRQRESTORE(flags);
112 }
113
114 #endif /* CONFIG_KERN_SIGNALS */
115