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