ac4e7d969bb4afa5bd505a182a16a59b9a31372f
[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  * \version $Id$
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38
39 #include "sem.h"
40 #include <cfg/debug.h>
41
42 #include <cpu/irq.h> // ASSERT_IRQ_DISABLED()
43
44 #include <kern/proc.h>
45 #include <kern/proc_p.h>
46 #include <kern/signal.h>
47
48 INLINE void sem_verify(struct Semaphore *s)
49 {
50         (void)s;
51         ASSERT(s);
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  * \brief Initialize a Semaphore structure.
59  */
60 void sem_init(struct Semaphore *s)
61 {
62         LIST_INIT(&s->wait_queue);
63         s->owner = NULL;
64         s->nest_count = 0;
65 }
66
67
68 /**
69  * \brief Attempt to lock a semaphore without waiting.
70  *
71  * \return true in case of success, false if the semaphore
72  *         was already locked by someone else.
73  *
74  * \note   each call to sem_attempt() must be matched by a
75  *         call to sem_release().
76  *
77  * \see sem_obtain() sem_release()
78  */
79 bool sem_attempt(struct Semaphore *s)
80 {
81         bool result = false;
82
83         proc_forbid();
84         sem_verify(s);
85         if ((!s->owner) || (s->owner == CurrentProcess))
86         {
87                 s->owner = CurrentProcess;
88                 s->nest_count++;
89                 result = true;
90         }
91         proc_permit();
92
93         return result;
94 }
95
96
97 /**
98  * \brief Lock a semaphore.
99  *
100  * If the semaphore is already owned by another process, the caller
101  * process will be enqueued into the waiting list and sleep until
102  * the semaphore is available.
103  *
104  * \note Each call to sem_obtain() must be matched by a
105  *       call to sem_release().
106  *
107  * \note This routine is optimized for highest speed in
108  *       the most common case: the semaphore is free or locked
109  *       by the calling process itself. Rearranging this code
110  *       is probably a bad idea.
111  *
112  * \sa sem_release() sem_attempt()
113  */
114 void sem_obtain(struct Semaphore *s)
115 {
116         proc_forbid();
117         sem_verify(s);
118
119         /* Is the semaphore already locked by another process? */
120         if (UNLIKELY(s->owner && (s->owner != CurrentProcess)))
121         {
122                 /* Append calling process to the wait queue */
123                 ADDTAIL(&s->wait_queue, (Node *)CurrentProcess);
124
125                 /*
126                  * We will wake up only when the current owner calls
127                  * sem_release(). Then, the semaphore will already
128                  * be locked for us.
129                  */
130                 proc_permit();
131                 proc_switch();
132         }
133         else
134         {
135                 ASSERT(LIST_EMPTY(&s->wait_queue));
136
137                 /* The semaphore was free: lock it */
138                 s->owner = CurrentProcess;
139                 s->nest_count++;
140                 proc_permit();
141         }
142 }
143
144
145 /**
146  * \brief Release a lock on a previously locked semaphore.
147  *
148  * If the nesting count of the semaphore reaches zero,
149  * the next process waiting for it will be awaken.
150  *
151  * \note This routine is optimized for highest speed in
152  *       the most common case: the semaphore has been locked just
153  *       once and nobody else was waiting for it. Rearranging
154  *       this code is probably a bad idea.
155  *
156  * \sa sem_obtain() sem_attempt()
157  */
158 void sem_release(struct Semaphore *s)
159 {
160         proc_forbid();
161         sem_verify(s);
162
163         ASSERT(s->owner == CurrentProcess);
164
165         /*
166          * Decrement nesting count and check if the semaphore
167          * has been fully unlocked.
168          */
169         if (--s->nest_count == 0)
170         {
171                 Process *proc;
172
173                 /* Disown semaphore */
174                 s->owner = NULL;
175
176                 /* Give semaphore to the first applicant, if any */
177                 if (UNLIKELY((proc = (Process *)list_remHead(&s->wait_queue))))
178                 {
179                         s->nest_count = 1;
180                         s->owner = proc;
181                         ATOMIC(SCHED_ENQUEUE(proc));
182                 }
183         }
184
185         proc_permit();
186 }