4 * Copyright 2001, 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999, 2000, 2001 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README for information.
9 * \brief Semaphore based synchronization services.
13 * \author Bernardo Innocenti <bernie@develer.com>
18 *#* Revision 1.9 2005/04/11 19:10:28 bernie
19 *#* Include top-level headers from cfg/ subdir.
21 *#* Revision 1.8 2005/01/22 04:20:42 bernie
22 *#* Add integrity checks.
24 *#* Revision 1.7 2004/11/28 23:20:25 bernie
25 *#* Remove obsolete INITLIST macro.
27 *#* Revision 1.6 2004/10/21 10:57:21 bernie
28 *#* Use proc_forbid()/proc_permit().
30 *#* Revision 1.5 2004/10/21 10:48:57 bernie
31 *#* sem_release(): Simplify (made by rasky on scfirm).
33 *#* Revision 1.4 2004/08/25 14:12:09 rasky
34 *#* Aggiornato il comment block dei log RCS
36 *#* Revision 1.3 2004/08/08 05:53:23 bernie
37 *#* Use DISABLE_IRQSAVE/ENABLE_IRQRESTORE; Cleanup documentation.
39 *#* Revision 1.2 2004/06/03 11:27:09 bernie
40 *#* Add dual-license information.
42 *#* Revision 1.1 2004/05/23 17:27:00 bernie
43 *#* Import kern/ subdirectory.
51 #include <cfg/debug.h>
53 INLINE void sem_verify(struct Semaphore *s)
55 LIST_ASSERT_VALID(&s->wait_queue);
56 ASSERT(s->nest_count >= 0);
57 ASSERT(s->nest_count < 128); // heuristic max
62 * \brief Initialize a Semaphore structure.
64 void sem_init(struct Semaphore *s)
66 LIST_INIT(&s->wait_queue);
73 * \brief Attempt to lock a semaphore without waiting.
75 * \return true in case of success, false if the semaphore
76 * was already locked by someone else.
78 * \note each call to sem_attempt() must be matched by a
79 * call to sem_release().
81 * \see sem_obtain() sem_release()
83 bool sem_attempt(struct Semaphore *s)
89 if ((!s->owner) || (s->owner == CurrentProcess))
91 s->owner = CurrentProcess;
102 * \brief Lock a semaphore.
104 * If the semaphore is already owned by another process, the caller
105 * process will be enqueued into the waiting list and sleep until
106 * the semaphore is available.
108 * \note Each call to sem_obtain() must be matched by a
109 * call to sem_release().
111 * \note This routine is optimized for highest speed in
112 * the most common case: the semaphore is free or locked
113 * by the calling process itself. Rearranging this code
114 * is probably a bad idea.
116 * \sa sem_release() sem_attempt()
118 void sem_obtain(struct Semaphore *s)
123 /* Is the semaphore already locked by another process? */
124 if (UNLIKELY(s->owner && (s->owner != CurrentProcess)))
126 /* Append calling process to the wait queue */
127 ADDTAIL(&s->wait_queue, (Node *)CurrentProcess);
130 * We will wake up only when the current owner calls
131 * sem_release(). Then, the semaphore will already
139 ASSERT(ISLISTEMPTY(&s->wait_queue));
141 /* The semaphore was free: lock it */
142 s->owner = CurrentProcess;
150 * \brief Release a lock on a previously locked semaphore.
152 * If the nesting count of the semaphore reaches zero,
153 * the next process waiting for it will be awaken.
155 * \note This routine is optimized for highest speed in
156 * the most common case: the semaphore has been locked just
157 * once and nobody else was waiting for it. Rearranging
158 * this code is probably a bad idea.
160 * \sa sem_obtain() sem_attempt()
162 void sem_release(struct Semaphore *s)
167 ASSERT(s->owner == CurrentProcess);
170 * Decrement nesting count and check if the semaphore
171 * has been fully unlocked.
173 if (--s->nest_count == 0)
177 /* Disown semaphore */
180 /* Give semaphore to the first applicant, if any */
181 if (UNLIKELY((proc = (Process *)REMHEAD(&s->wait_queue))))