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.6 2004/10/21 10:57:21 bernie
19 *#* Use proc_forbid()/proc_permit().
21 *#* Revision 1.5 2004/10/21 10:48:57 bernie
22 *#* sem_release(): Simplify (made by rasky on scfirm).
24 *#* Revision 1.4 2004/08/25 14:12:09 rasky
25 *#* Aggiornato il comment block dei log RCS
27 *#* Revision 1.3 2004/08/08 05:53:23 bernie
28 *#* Use DISABLE_IRQSAVE/ENABLE_IRQRESTORE; Cleanup documentation.
30 *#* Revision 1.2 2004/06/03 11:27:09 bernie
31 *#* Add dual-license information.
33 *#* Revision 1.1 2004/05/23 17:27:00 bernie
34 *#* Import kern/ subdirectory.
45 * \brief Initialize a Semaphore structure.
47 void sem_init(struct Semaphore *s)
49 INITLIST(&s->wait_queue);
56 * \brief Attempt to lock a semaphore without waiting.
58 * \return true in case of success, false if the semaphore
59 * was already locked by someone else.
61 * \note each call to sem_attempt() must be matched by a
62 * call to sem_release().
64 * \see sem_obtain() sem_release()
66 bool sem_attempt(struct Semaphore *s)
71 if ((!s->owner) || (s->owner == CurrentProcess))
73 s->owner = CurrentProcess;
84 * \brief Lock a semaphore.
86 * If the semaphore is already owned by another process, the caller
87 * process will be enqueued into the waiting list and sleep until
88 * the semaphore is available.
90 * \note Each call to sem_obtain() must be matched by a
91 * call to sem_release().
93 * \note This routine is optimized for highest speed in
94 * the most common case: the semaphore is free or locked
95 * by the calling process itself. Rearranging this code
96 * is probably a bad idea.
98 * \sa sem_release() sem_attempt()
100 void sem_obtain(struct Semaphore *s)
104 /* Is the semaphore already locked by another process? */
105 if (UNLIKELY(s->owner && (s->owner != CurrentProcess)))
107 /* Append calling process to the wait queue */
108 ADDTAIL(&s->wait_queue, (Node *)CurrentProcess);
111 * We will wake up only when the current owner calls
112 * sem_release(). Then, the semaphore will already
120 /* The semaphore was free: lock it */
121 s->owner = CurrentProcess;
129 * \brief Release a lock on a previously locked semaphore.
131 * If the nesting count of the semaphore reaches zero,
132 * the next process waiting for it will be awaken.
134 * \note This routine is optimized for highest speed in
135 * the most common case: the semaphore has been locked just
136 * once and nobody else was waiting for it. Rearranging
137 * this code is probably a bad idea.
139 * \sa sem_obtain() sem_attempt()
141 void sem_release(struct Semaphore *s)
146 * Decrement nesting count and check if the semaphore
147 * has been fully unlocked.
149 if (--s->nest_count == 0)
153 /* Disown semaphore */
156 /* Give semaphore to the first applicant, if any */
157 if (UNLIKELY((proc = (Process *)REMHEAD(&s->wait_queue))))