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