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.
36 * \author Bernie Innocenti <bernie@codewiz.org>
40 #include <cfg/debug.h>
42 #include <cpu/irq.h> // ASSERT_IRQ_DISABLED()
44 #include <kern/proc.h>
45 #include <kern/proc_p.h>
46 #include <kern/signal.h>
48 INLINE void sem_verify(struct Semaphore *s)
52 LIST_ASSERT_VALID(&s->wait_queue);
53 ASSERT(s->nest_count >= 0);
54 ASSERT(s->nest_count < 128); // heuristic max
58 * \brief Initialize a Semaphore structure.
60 void sem_init(struct Semaphore *s)
62 LIST_INIT(&s->wait_queue);
69 * \brief Attempt to lock a semaphore without waiting.
71 * \return true in case of success, false if the semaphore
72 * was already locked by someone else.
74 * \note each call to sem_attempt() must be matched by a
75 * call to sem_release().
77 * \see sem_obtain() sem_release()
79 bool sem_attempt(struct Semaphore *s)
85 if ((!s->owner) || (s->owner == current_process))
87 s->owner = current_process;
98 * \brief Lock a semaphore.
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.
104 * \note Each call to sem_obtain() must be matched by a
105 * call to sem_release().
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.
112 * \sa sem_release() sem_attempt()
114 void sem_obtain(struct Semaphore *s)
119 /* Is the semaphore already locked by another process? */
120 if (UNLIKELY(s->owner && (s->owner != current_process)))
122 /* Append calling process to the wait queue */
123 ADDTAIL(&s->wait_queue, (Node *)current_process);
126 * We will wake up only when the current owner calls
127 * sem_release(). Then, the semaphore will already
135 ASSERT(LIST_EMPTY(&s->wait_queue));
137 /* The semaphore was free: lock it */
138 s->owner = current_process;
146 * \brief Release a lock on a previously locked semaphore.
148 * If the nesting count of the semaphore reaches zero,
149 * the next process waiting for it will be awaken.
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.
156 * \sa sem_obtain() sem_attempt()
158 void sem_release(struct Semaphore *s)
160 Process *proc = NULL;
165 ASSERT(s->owner == current_process);
168 * Decrement nesting count and check if the semaphore
169 * has been fully unlocked.
171 if (--s->nest_count == 0)
173 /* Disown semaphore */
176 /* Give semaphore to the first applicant, if any */
177 if (UNLIKELY((proc = (Process *)list_remHead(&s->wait_queue))))
186 ATOMIC(proc_wakeup(proc));