Sistema l'errore da me commesso in fase di conversione...
[bertos.git] / kern / sem.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2001, 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 README.devlib for information.
7  * -->
8  *
9  * \brief Semaphore based synchronization services.
10  *
11  * \version $Id$
12  *
13  * \author Bernardo Innocenti <bernie@develer.com>
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.12  2006/07/19 12:56:27  bernie
19  *#* Convert to new Doxygen style.
20  *#*
21  *#* Revision 1.11  2006/02/24 01:17:05  bernie
22  *#* Update for new emulator.
23  *#*
24  *#* Revision 1.10  2005/11/04 16:20:02  bernie
25  *#* Fix reference to README.devlib in header.
26  *#*
27  *#* Revision 1.9  2005/04/11 19:10:28  bernie
28  *#* Include top-level headers from cfg/ subdir.
29  *#*
30  *#* Revision 1.8  2005/01/22 04:20:42  bernie
31  *#* Add integrity checks.
32  *#*
33  *#* Revision 1.7  2004/11/28 23:20:25  bernie
34  *#* Remove obsolete INITLIST macro.
35  *#*
36  *#* Revision 1.6  2004/10/21 10:57:21  bernie
37  *#* Use proc_forbid()/proc_permit().
38  *#*
39  *#* Revision 1.5  2004/10/21 10:48:57  bernie
40  *#* sem_release(): Simplify (made by rasky on scfirm).
41  *#*
42  *#* Revision 1.4  2004/08/25 14:12:09  rasky
43  *#* Aggiornato il comment block dei log RCS
44  *#*
45  *#* Revision 1.3  2004/08/08 05:53:23  bernie
46  *#* Use DISABLE_IRQSAVE/ENABLE_IRQRESTORE; Cleanup documentation.
47  *#*
48  *#* Revision 1.2  2004/06/03 11:27:09  bernie
49  *#* Add dual-license information.
50  *#*
51  *#* Revision 1.1  2004/05/23 17:27:00  bernie
52  *#* Import kern/ subdirectory.
53  *#*/
54
55 #include "sem.h"
56 #include <kern/proc.h>
57 #include <kern/proc_p.h>
58 #include <kern/signal.h>
59 #include <cfg/debug.h>
60
61 INLINE void sem_verify(struct Semaphore *s)
62 {
63         LIST_ASSERT_VALID(&s->wait_queue);
64         ASSERT(s->nest_count >= 0);
65         ASSERT(s->nest_count < 128);   // heuristic max
66 }
67
68
69 /**
70  * \brief Initialize a Semaphore structure.
71  */
72 void sem_init(struct Semaphore *s)
73 {
74         LIST_INIT(&s->wait_queue);
75         s->owner = NULL;
76         s->nest_count = 0;
77 }
78
79
80 /**
81  * \brief Attempt to lock a semaphore without waiting.
82  *
83  * \return true in case of success, false if the semaphore
84  *         was already locked by someone else.
85  *
86  * \note   each call to sem_attempt() must be matched by a
87  *         call to sem_release().
88  *
89  * \see sem_obtain() sem_release()
90  */
91 bool sem_attempt(struct Semaphore *s)
92 {
93         bool result = false;
94
95         proc_forbid();
96         sem_verify(s);
97         if ((!s->owner) || (s->owner == CurrentProcess))
98         {
99                 s->owner = CurrentProcess;
100                 s->nest_count++;
101                 result = true;
102         }
103         proc_permit();
104
105         return result;
106 }
107
108
109 /**
110  * \brief Lock a semaphore.
111  *
112  * If the semaphore is already owned by another process, the caller
113  * process will be enqueued into the waiting list and sleep until
114  * the semaphore is available.
115  *
116  * \note Each call to sem_obtain() must be matched by a
117  *       call to sem_release().
118  *
119  * \note This routine is optimized for highest speed in
120  *       the most common case: the semaphore is free or locked
121  *       by the calling process itself. Rearranging this code
122  *       is probably a bad idea.
123  *
124  * \sa sem_release() sem_attempt()
125  */
126 void sem_obtain(struct Semaphore *s)
127 {
128         proc_forbid();
129         sem_verify(s);
130
131         /* Is the semaphore already locked by another process? */
132         if (UNLIKELY(s->owner && (s->owner != CurrentProcess)))
133         {
134                 /* Append calling process to the wait queue */
135                 ADDTAIL(&s->wait_queue, (Node *)CurrentProcess);
136
137                 /*
138                  * We will wake up only when the current owner calls
139                  * sem_release(). Then, the semaphore will already
140                  * be locked for us.
141                  */
142                 proc_permit();
143                 proc_schedule();
144         }
145         else
146         {
147                 ASSERT(LIST_EMPTY(&s->wait_queue));
148
149                 /* The semaphore was free: lock it */
150                 s->owner = CurrentProcess;
151                 s->nest_count++;
152                 proc_permit();
153         }
154 }
155
156
157 /**
158  * \brief Release a lock on a previously locked semaphore.
159  *
160  * If the nesting count of the semaphore reaches zero,
161  * the next process waiting for it will be awaken.
162  *
163  * \note This routine is optimized for highest speed in
164  *       the most common case: the semaphore has been locked just
165  *       once and nobody else was waiting for it. Rearranging
166  *       this code is probably a bad idea.
167  *
168  * \sa sem_obtain() sem_attempt()
169  */
170 void sem_release(struct Semaphore *s)
171 {
172         proc_forbid();
173         sem_verify(s);
174
175         ASSERT(s->owner == CurrentProcess);
176
177         /*
178          * Decrement nesting count and check if the semaphore
179          * has been fully unlocked.
180          */
181         if (--s->nest_count == 0)
182         {
183                 Process *proc;
184
185                 /* Disown semaphore */
186                 s->owner = NULL;
187
188                 /* Give semaphore to the first applicant, if any */
189                 if (UNLIKELY((proc = (Process *)list_remHead(&s->wait_queue))))
190                 {
191                         s->nest_count = 1;
192                         s->owner = proc;
193                         SCHED_ENQUEUE(proc);
194                 }
195         }
196
197         proc_permit();
198 }