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.4 2004/08/25 14:12:09 rasky
19 *#* Aggiornato il comment block dei log RCS
21 *#* Revision 1.3 2004/08/08 05:53:23 bernie
22 *#* Use DISABLE_IRQSAVE/ENABLE_IRQRESTORE; Cleanup documentation.
24 *#* Revision 1.2 2004/06/03 11:27:09 bernie
25 *#* Add dual-license information.
27 *#* Revision 1.1 2004/05/23 17:27:00 bernie
28 *#* Import kern/ subdirectory.
40 * \brief Initialize a Semaphore structure
42 void sem_init(struct Semaphore *s)
44 INITLIST(&s->wait_queue);
51 * \brief Attempt to lock a semaphore without waiting.
53 * \return true in case of success, false if the semaphore
54 * was already locked by someone else.
56 * \note each call to sem_attempt() must be matched by a
57 * call to sem_release().
59 * \see sem_obtain() sem_release()
61 bool sem_attempt(struct Semaphore *s)
64 DISABLE_IRQSAVE(flags);
66 if ((!s->owner) || (s->owner == CurrentProcess))
68 s->owner = CurrentProcess;
74 ENABLE_IRQRESTORE(flags);
80 * \brief Lock a semaphore.
82 * If the semaphore is already owned by another process, the caller
83 * process will be enqueued into the waiting list and sleep until
84 * the semaphore is available.
86 * \note Each call to sem_obtain() must be matched by a
87 * call to sem_release().
89 * \note This routine is optimized for highest speed in
90 * the most common case: the semaphore is free or locked
91 * by the calling process itself. Rearranging this code
92 * is probably a bad idea.
94 * \sa sem_release() sem_attempt()
96 void sem_obtain(struct Semaphore *s)
99 DISABLE_IRQSAVE(flags);
101 /* Is the semaphore already locked by another process? */
102 if (UNLIKELY(s->owner && (s->owner != CurrentProcess)))
104 /* Append calling process to the wait queue */
105 ADDTAIL(&s->wait_queue, (Node *)CurrentProcess);
106 ENABLE_IRQRESTORE(flags);
109 * We will wake up only when the current owner calls
110 * sem_release(). Then, the semaphore will already
117 /* The semaphore was free: lock it */
118 s->owner = CurrentProcess;
120 ENABLE_IRQRESTORE(flags);
126 * \brief Releases a lock on a previously locked semaphore.
128 * If the nesting count of the semaphore reaches zero,
129 * the next process waiting for it will be awaken.
131 * \note This routine is optimized for highest speed in
132 * the most common case: the semaphore has been locked just
133 * once and nobody else was waiting for it. Rearranging
134 * this code is probably a bad idea.
136 * \sa sem_obtain() sem_attempt()
138 void sem_release(struct Semaphore *s)
141 DISABLE_IRQSAVE(flags);
144 * Decremement nesting count and check if the semaphore
145 * has been fully unlocked
147 if (--s->nest_count == 0)
149 /* Disown semaphore */
152 /* Anybody still waiting for this semaphore? */
153 if (!ISLISTEMPTY(&s->wait_queue))
155 /* Give semaphore to the first applicant */
156 Process *proc = (Process *)s->wait_queue.head;
157 REMOVE((Node *)proc);
164 ENABLE_IRQRESTORE(flags);