4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2001, 2004 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999, 2000, 2001 Bernie Innocenti <bernie@codewiz.org>
33 * \brief Semaphore based synchronization services.
35 * \author Bernie Innocenti <bernie@codewiz.org>
39 #include <cfg/debug.h>
41 #include <cpu/irq.h> // ASSERT_IRQ_DISABLED()
43 #include <kern/proc.h>
44 #include <kern/proc_p.h>
45 #include <kern/signal.h>
47 INLINE void sem_verify(struct Semaphore *s)
51 LIST_ASSERT_VALID(&s->wait_queue);
52 ASSERT(s->nest_count >= 0);
53 ASSERT(s->nest_count < 128); // heuristic max
57 * \brief Initialize a Semaphore structure.
59 void sem_init(struct Semaphore *s)
61 LIST_INIT(&s->wait_queue);
68 * \brief Attempt to lock a semaphore without waiting.
70 * \return true in case of success, false if the semaphore
71 * was already locked by someone else.
73 * \note each call to sem_attempt() must be matched by a
74 * call to sem_release().
76 * \see sem_obtain() sem_release()
78 bool sem_attempt(struct Semaphore *s)
84 if ((!s->owner) || (s->owner == current_process))
86 s->owner = current_process;
97 * \brief Lock a semaphore.
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.
103 * \note Each call to sem_obtain() must be matched by a
104 * call to sem_release().
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.
111 * \sa sem_release() sem_attempt()
113 void sem_obtain(struct Semaphore *s)
118 /* Is the semaphore already locked by another process? */
119 if (UNLIKELY(s->owner && (s->owner != current_process)))
121 /* Append calling process to the wait queue */
122 ADDTAIL(&s->wait_queue, (Node *)current_process);
125 * We will wake up only when the current owner calls
126 * sem_release(). Then, the semaphore will already
134 ASSERT(LIST_EMPTY(&s->wait_queue));
136 /* The semaphore was free: lock it */
137 s->owner = current_process;
145 * \brief Release a lock on a previously locked semaphore.
147 * If the nesting count of the semaphore reaches zero,
148 * the next process waiting for it will be awaken.
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.
155 * \sa sem_obtain() sem_attempt()
157 void sem_release(struct Semaphore *s)
159 Process *proc = NULL;
164 ASSERT(s->owner == current_process);
167 * Decrement nesting count and check if the semaphore
168 * has been fully unlocked.
170 if (--s->nest_count == 0)
172 /* Disown semaphore */
175 /* Give semaphore to the first applicant, if any */
176 if (UNLIKELY((proc = (Process *)list_remHead(&s->wait_queue))))
185 ATOMIC(proc_wakeup(proc));