Remove \version svn tag.
[bertos.git] / bertos / kern / sem.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2001, 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2000, 2001 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Semaphore based synchronization services.
34  *
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  */
37
38 #include "sem.h"
39 #include <cfg/debug.h>
40
41 #include <cpu/irq.h> // ASSERT_IRQ_DISABLED()
42
43 #include <kern/proc.h>
44 #include <kern/proc_p.h>
45 #include <kern/signal.h>
46
47 INLINE void sem_verify(struct Semaphore *s)
48 {
49         (void)s;
50         ASSERT(s);
51         LIST_ASSERT_VALID(&s->wait_queue);
52         ASSERT(s->nest_count >= 0);
53         ASSERT(s->nest_count < 128);   // heuristic max
54 }
55
56 /**
57  * \brief Initialize a Semaphore structure.
58  */
59 void sem_init(struct Semaphore *s)
60 {
61         LIST_INIT(&s->wait_queue);
62         s->owner = NULL;
63         s->nest_count = 0;
64 }
65
66
67 /**
68  * \brief Attempt to lock a semaphore without waiting.
69  *
70  * \return true in case of success, false if the semaphore
71  *         was already locked by someone else.
72  *
73  * \note   each call to sem_attempt() must be matched by a
74  *         call to sem_release().
75  *
76  * \see sem_obtain() sem_release()
77  */
78 bool sem_attempt(struct Semaphore *s)
79 {
80         bool result = false;
81
82         proc_forbid();
83         sem_verify(s);
84         if ((!s->owner) || (s->owner == current_process))
85         {
86                 s->owner = current_process;
87                 s->nest_count++;
88                 result = true;
89         }
90         proc_permit();
91
92         return result;
93 }
94
95
96 /**
97  * \brief Lock a semaphore.
98  *
99  * If the semaphore is already owned by another process, the caller
100  * process will be enqueued into the waiting list and sleep until
101  * the semaphore is available.
102  *
103  * \note Each call to sem_obtain() must be matched by a
104  *       call to sem_release().
105  *
106  * \note This routine is optimized for highest speed in
107  *       the most common case: the semaphore is free or locked
108  *       by the calling process itself. Rearranging this code
109  *       is probably a bad idea.
110  *
111  * \sa sem_release() sem_attempt()
112  */
113 void sem_obtain(struct Semaphore *s)
114 {
115         proc_forbid();
116         sem_verify(s);
117
118         /* Is the semaphore already locked by another process? */
119         if (UNLIKELY(s->owner && (s->owner != current_process)))
120         {
121                 /* Append calling process to the wait queue */
122                 ADDTAIL(&s->wait_queue, (Node *)current_process);
123
124                 /*
125                  * We will wake up only when the current owner calls
126                  * sem_release(). Then, the semaphore will already
127                  * be locked for us.
128                  */
129                 proc_permit();
130                 proc_switch();
131         }
132         else
133         {
134                 ASSERT(LIST_EMPTY(&s->wait_queue));
135
136                 /* The semaphore was free: lock it */
137                 s->owner = current_process;
138                 s->nest_count++;
139                 proc_permit();
140         }
141 }
142
143
144 /**
145  * \brief Release a lock on a previously locked semaphore.
146  *
147  * If the nesting count of the semaphore reaches zero,
148  * the next process waiting for it will be awaken.
149  *
150  * \note This routine is optimized for highest speed in
151  *       the most common case: the semaphore has been locked just
152  *       once and nobody else was waiting for it. Rearranging
153  *       this code is probably a bad idea.
154  *
155  * \sa sem_obtain() sem_attempt()
156  */
157 void sem_release(struct Semaphore *s)
158 {
159         Process *proc = NULL;
160
161         proc_forbid();
162         sem_verify(s);
163
164         ASSERT(s->owner == current_process);
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                 /* Disown semaphore */
173                 s->owner = NULL;
174
175                 /* Give semaphore to the first applicant, if any */
176                 if (UNLIKELY((proc = (Process *)list_remHead(&s->wait_queue))))
177                 {
178                         s->nest_count = 1;
179                         s->owner = proc;
180                 }
181         }
182         proc_permit();
183
184         if (proc)
185                 ATOMIC(proc_wakeup(proc));
186 }