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.8 2005/01/22 04:20:42 bernie
19 *#* Add integrity checks.
21 *#* Revision 1.7 2004/11/28 23:20:25 bernie
22 *#* Remove obsolete INITLIST macro.
24 *#* Revision 1.6 2004/10/21 10:57:21 bernie
25 *#* Use proc_forbid()/proc_permit().
27 *#* Revision 1.5 2004/10/21 10:48:57 bernie
28 *#* sem_release(): Simplify (made by rasky on scfirm).
30 *#* Revision 1.4 2004/08/25 14:12:09 rasky
31 *#* Aggiornato il comment block dei log RCS
33 *#* Revision 1.3 2004/08/08 05:53:23 bernie
34 *#* Use DISABLE_IRQSAVE/ENABLE_IRQRESTORE; Cleanup documentation.
36 *#* Revision 1.2 2004/06/03 11:27:09 bernie
37 *#* Add dual-license information.
39 *#* Revision 1.1 2004/05/23 17:27:00 bernie
40 *#* Import kern/ subdirectory.
50 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
59 * \brief Initialize a Semaphore structure.
61 void sem_init(struct Semaphore *s)
63 LIST_INIT(&s->wait_queue);
70 * \brief Attempt to lock a semaphore without waiting.
72 * \return true in case of success, false if the semaphore
73 * was already locked by someone else.
75 * \note each call to sem_attempt() must be matched by a
76 * call to sem_release().
78 * \see sem_obtain() sem_release()
80 bool sem_attempt(struct Semaphore *s)
86 if ((!s->owner) || (s->owner == CurrentProcess))
88 s->owner = CurrentProcess;
99 * \brief Lock a semaphore.
101 * If the semaphore is already owned by another process, the caller
102 * process will be enqueued into the waiting list and sleep until
103 * the semaphore is available.
105 * \note Each call to sem_obtain() must be matched by a
106 * call to sem_release().
108 * \note This routine is optimized for highest speed in
109 * the most common case: the semaphore is free or locked
110 * by the calling process itself. Rearranging this code
111 * is probably a bad idea.
113 * \sa sem_release() sem_attempt()
115 void sem_obtain(struct Semaphore *s)
120 /* Is the semaphore already locked by another process? */
121 if (UNLIKELY(s->owner && (s->owner != CurrentProcess)))
123 /* Append calling process to the wait queue */
124 ADDTAIL(&s->wait_queue, (Node *)CurrentProcess);
127 * We will wake up only when the current owner calls
128 * sem_release(). Then, the semaphore will already
136 ASSERT(ISLISTEMPTY(&s->wait_queue));
138 /* The semaphore was free: lock it */
139 s->owner = CurrentProcess;
147 * \brief Release a lock on a previously locked semaphore.
149 * If the nesting count of the semaphore reaches zero,
150 * the next process waiting for it will be awaken.
152 * \note This routine is optimized for highest speed in
153 * the most common case: the semaphore has been locked just
154 * once and nobody else was waiting for it. Rearranging
155 * this code is probably a bad idea.
157 * \sa sem_obtain() sem_attempt()
159 void sem_release(struct Semaphore *s)
164 ASSERT(s->owner == CurrentProcess);
167 * Decrement nesting count and check if the semaphore
168 * has been fully unlocked.
170 if (--s->nest_count == 0)
174 /* Disown semaphore */
177 /* Give semaphore to the first applicant, if any */
178 if (UNLIKELY((proc = (Process *)REMHEAD(&s->wait_queue))))