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
56 #if CONFIG_KERN_PRI && CONFIG_KERN_PRI_INHERIT
58 #define proc_updatePri(proc) (proc_setPri(proc, (proc)->orig_pri))
61 INLINE void pri_inheritBlock(Semaphore *s)
63 Process *owner = s->owner;
66 * Enqueue the blocking process in the owner's inheritance
67 * list. Notice that such process might have inherited its
68 * current priority from someone else.
70 current_process->inh_link.pri = __prio_proc(current_process);
71 LIST_ENQUEUE(&owner->inh_list, ¤t_process->inh_link);
72 current_process->inh_blocked_by = s;
75 * As long as a process has the power of boosting the priority
76 * of its lock owner...
78 while (current_process->inh_link.pri > prio_proc(owner)) {
81 /* Boost the priority of the owner */
84 /* If the owner is not blocked, we're done */
85 if (!p->inh_blocked_by)
89 * Otherwise update the position of the owner
90 * (which is `p' at each round!) in the inheritance
91 * list it lies in and set up `owner' for the
94 REMOVE(&p->inh_link.link);
95 p->inh_link.pri = prio_proc(p);
96 owner = p->inh_blocked_by->owner;
97 LIST_ENQUEUE(&owner->inh_list, &p->inh_link);
102 INLINE void pri_inheritUnblock(Semaphore *s, Process *proc)
104 Process *owner = s->owner;
109 * This process has nothing more to do on a priority
112 REMOVE(&proc->inh_link.link);
113 proc->inh_blocked_by = NULL;
116 * Each process in the former owner's priority inheritance
117 * list that is blocked on 's' needs to be removed from
118 * there and added to the priority inheritance list of
119 * this process, since it's going to be the new owner for
122 FOREACH_NODE_SAFE(n, temp, &owner->inh_list) {
123 p = containerof(n, Process, inh_link.link);
124 /* Ensures only the processes blocked on 's' are affected! */
125 if (p->inh_blocked_by == s) {
126 REMOVE(&p->inh_link.link);
127 LIST_ENQUEUE(&proc->inh_list, &p->inh_link);
129 /* And again, update the priority of the new owner */
130 if (p->inh_link.pri > prio_proc(proc))
131 proc_updatePri(proc);
135 proc_updatePri(owner);
138 INLINE void pri_inheritBlock(UNUSED_ARG(Semaphore *, s))
142 INLINE void pri_inheritUnblock(UNUSED_ARG(Semaphore *, s), UNUSED_ARG(Process *, proc))
145 #endif /* CONFIG_KERN_PRI_INHERIT */
149 * \brief Initialize a Semaphore structure.
151 void sem_init(struct Semaphore *s)
153 LIST_INIT(&s->wait_queue);
160 * \brief Attempt to lock a semaphore without waiting.
162 * \return true in case of success, false if the semaphore
163 * was already locked by someone else.
165 * \note each call to sem_attempt() must be matched by a
166 * call to sem_release().
168 * \see sem_obtain() sem_release()
170 bool sem_attempt(struct Semaphore *s)
176 if ((!s->owner) || (s->owner == current_process))
178 s->owner = current_process;
189 * \brief Lock a semaphore.
191 * If the semaphore is already owned by another process, the caller
192 * process will be enqueued into the waiting list and sleep until
193 * the semaphore is available.
195 * \note Each call to sem_obtain() must be matched by a
196 * call to sem_release().
198 * \note This routine is optimized for highest speed in
199 * the most common case: the semaphore is free or locked
200 * by the calling process itself. Rearranging this code
201 * is probably a bad idea.
203 * \sa sem_release() sem_attempt()
205 void sem_obtain(struct Semaphore *s)
210 /* Is the semaphore already locked by another process? */
211 if (UNLIKELY(s->owner && (s->owner != current_process)))
213 /* Append calling process to the wait queue */
214 ADDTAIL(&s->wait_queue, (Node *)current_process);
216 /* Trigger priority inheritance logic, if enabled */
220 * We will wake up only when the current owner calls
221 * sem_release(). Then, the semaphore will already
229 ASSERT(LIST_EMPTY(&s->wait_queue));
231 /* The semaphore was free: lock it */
232 s->owner = current_process;
240 * \brief Release a lock on a previously locked semaphore.
242 * If the nesting count of the semaphore reaches zero,
243 * the next process waiting for it will be awaken.
245 * \note This routine is optimized for highest speed in
246 * the most common case: the semaphore has been locked just
247 * once and nobody else was waiting for it. Rearranging
248 * this code is probably a bad idea.
250 * \sa sem_obtain() sem_attempt()
252 void sem_release(struct Semaphore *s)
254 Process *proc = NULL;
259 ASSERT(s->owner == current_process);
262 * Decrement nesting count and check if the semaphore
263 * has been fully unlocked.
265 if (--s->nest_count == 0)
267 /* Give semaphore to the first applicant, if any */
268 if (UNLIKELY((proc = (Process *)list_remHead(&s->wait_queue))))
270 /* Undo the effects of priority inheritance, if enabled */
271 pri_inheritUnblock(s, proc);
276 /* Disown semaphore */
283 ATOMIC(proc_wakeup(proc));