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.5 2004/10/21 10:48:57 bernie
19 *#* sem_release(): Simplify (made by rasky on scfirm).
21 *#* Revision 1.4 2004/08/25 14:12:09 rasky
22 *#* Aggiornato il comment block dei log RCS
24 *#* Revision 1.3 2004/08/08 05:53:23 bernie
25 *#* Use DISABLE_IRQSAVE/ENABLE_IRQRESTORE; Cleanup documentation.
27 *#* Revision 1.2 2004/06/03 11:27:09 bernie
28 *#* Add dual-license information.
30 *#* Revision 1.1 2004/05/23 17:27:00 bernie
31 *#* Import kern/ subdirectory.
43 * \brief Initialize a Semaphore structure
45 void sem_init(struct Semaphore *s)
47 INITLIST(&s->wait_queue);
54 * \brief Attempt to lock a semaphore without waiting.
56 * \return true in case of success, false if the semaphore
57 * was already locked by someone else.
59 * \note each call to sem_attempt() must be matched by a
60 * call to sem_release().
62 * \see sem_obtain() sem_release()
64 bool sem_attempt(struct Semaphore *s)
67 DISABLE_IRQSAVE(flags);
69 if ((!s->owner) || (s->owner == CurrentProcess))
71 s->owner = CurrentProcess;
77 ENABLE_IRQRESTORE(flags);
83 * \brief Lock a semaphore.
85 * If the semaphore is already owned by another process, the caller
86 * process will be enqueued into the waiting list and sleep until
87 * the semaphore is available.
89 * \note Each call to sem_obtain() must be matched by a
90 * call to sem_release().
92 * \note This routine is optimized for highest speed in
93 * the most common case: the semaphore is free or locked
94 * by the calling process itself. Rearranging this code
95 * is probably a bad idea.
97 * \sa sem_release() sem_attempt()
99 void sem_obtain(struct Semaphore *s)
102 DISABLE_IRQSAVE(flags);
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);
109 ENABLE_IRQRESTORE(flags);
112 * We will wake up only when the current owner calls
113 * sem_release(). Then, the semaphore will already
120 /* The semaphore was free: lock it */
121 s->owner = CurrentProcess;
123 ENABLE_IRQRESTORE(flags);
129 * \brief Releases 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)
144 DISABLE_IRQSAVE(flags);
147 * Decrement nesting count and check if the semaphore
148 * has been fully unlocked
150 if (--s->nest_count == 0)
154 /* Disown semaphore */
157 /* Give semaphore to the first applicant, if any */
158 if ((proc = (Process*)REMHEAD(&s->wait_queue)))
166 ENABLE_IRQRESTORE(flags);