3bc9aab7f8cb1b5bdd27e48366c5c8e4ab3de178
[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  * All Rights Reserved.
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.1  2004/05/23 17:27:00  bernie
23  * Import kern/ subdirectory.
24  *
25  */
26
27 #include "signal.h"
28 #include "proc.h"
29 #include "proc_p.h"
30 #include "hw.h"
31
32 // FIXME
33 #if CONFIG_KERN_SIGNALS
34
35 /*!
36  * Check if any of the signals in \a sigs has occurred and clear them.
37  * Return the signals that have occurred.
38  */
39 sigset_t sig_check(sigset_t sigs)
40 {
41         sigset_t result;
42
43         DISABLE_INTS;
44         result = CurrentProcess->sig_recv & sigs;
45         CurrentProcess->sig_recv &= ~sigs;
46         ENABLE_INTS;
47         return result;
48 }
49
50
51 /*!
52  * Sleep until any of the signals in \a sigs occurs.
53  * Return the signal(s) that have awaked the process.
54  */
55 sigset_t sig_wait(sigset_t sigs)
56 {
57         sigset_t result;
58
59         DISABLE_INTS;
60
61         for(;;)
62         {
63                 /* Check if we got at least one of the signals */
64                 if ((result = CurrentProcess->sig_recv & sigs))
65                 {
66                         /* Yes, clear signals and return */
67                         CurrentProcess->sig_recv &= ~sigs;
68                         ENABLE_INTS;
69                         return result;
70                 }
71
72                 /* No, go to sleep and proc_schedule() another process */
73                 CurrentProcess->sig_wait = sigs;
74                 proc_schedule();
75         }
76 }
77
78
79 /*!
80  * Send the signals \a sigs to the process \a proc.
81  * The process will be awaken if it was waiting for any of them.
82  *
83  * This call is interrupt safe (no \c DISABLE_INTS/ENABLE_INTS protection)
84  */
85 void _sig_signal(Process *proc, sigset_t sigs)
86 {
87         /* Set the signals */
88         proc->sig_recv |= sigs;
89
90         /* Check if process needs to be awaken */
91         if (proc->sig_recv & proc->sig_wait)
92         {
93                 /* Wake up process and enqueue in ready list */
94                 proc->sig_wait = 0;
95                 SCHED_ENQUEUE(proc);
96         }
97 }
98
99
100 /*!
101  * Same as _sig_signal() with interrupt protection.
102  *
103  * \note Inlined manually because some compilers are too
104  *       stupid to it automatically.
105  */
106 void sig_signal(Process *proc, sigset_t sigs)
107 {
108         DISABLE_INTS;
109
110         /* Set the signals */
111         proc->sig_recv |= sigs;
112
113         /* Check if process needs to be awaken */
114         if (proc->sig_recv & proc->sig_wait)
115         {
116                 /* Wake up process and enqueue in ready list */
117                 proc->sig_wait = 0;
118                 SCHED_ENQUEUE(proc);
119         }
120
121         ENABLE_INTS;
122 }
123
124 #endif /* CONFIG_KERN_SIGNALS */
125