sem_release(): Simplify (made by rasky on scfirm).
[bertos.git] / kern / sem.c
1 /*!
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \brief Semaphore based synchronization services.
10  *
11  * \version $Id$
12  *
13  * \author Bernardo Innocenti <bernie@develer.com>
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.5  2004/10/21 10:48:57  bernie
19  *#* sem_release(): Simplify (made by rasky on scfirm).
20  *#*
21  *#* Revision 1.4  2004/08/25 14:12:09  rasky
22  *#* Aggiornato il comment block dei log RCS
23  *#*
24  *#* Revision 1.3  2004/08/08 05:53:23  bernie
25  *#* Use DISABLE_IRQSAVE/ENABLE_IRQRESTORE; Cleanup documentation.
26  *#*
27  *#* Revision 1.2  2004/06/03 11:27:09  bernie
28  *#* Add dual-license information.
29  *#*
30  *#* Revision 1.1  2004/05/23 17:27:00  bernie
31  *#* Import kern/ subdirectory.
32  *#*
33  *#*/
34
35 #include "sem.h"
36 #include "proc.h"
37 #include "proc_p.h"
38 #include "signal.h"
39 #include "hw.h"
40
41
42 /*!
43  * \brief Initialize a Semaphore structure
44  */
45 void sem_init(struct Semaphore *s)
46 {
47         INITLIST(&s->wait_queue);
48         s->owner = NULL;
49         s->nest_count = 0;
50 }
51
52
53 /*!
54  * \brief Attempt to lock a semaphore without waiting.
55  *
56  * \return true in case of success, false if the semaphore
57  *         was already locked by someone else.
58  *
59  * \note   each call to sem_attempt() must be matched by a
60  *         call to sem_release().
61  *
62  * \see sem_obtain() sem_release()
63  */
64 bool sem_attempt(struct Semaphore *s)
65 {
66         cpuflags_t flags;
67         DISABLE_IRQSAVE(flags);
68
69         if ((!s->owner) || (s->owner == CurrentProcess))
70         {
71                 s->owner = CurrentProcess;
72                 s->nest_count++;
73                 ENABLE_INTS;
74                 return true;
75         }
76
77         ENABLE_IRQRESTORE(flags);
78         return false;
79 }
80
81
82 /*!
83  * \brief Lock a semaphore.
84  *
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.
88  *
89  * \note Each call to sem_obtain() must be matched by a
90  *       call to sem_release().
91  *
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.
96  *
97  * \sa sem_release() sem_attempt()
98  */
99 void sem_obtain(struct Semaphore *s)
100 {
101         cpuflags_t flags;
102         DISABLE_IRQSAVE(flags);
103
104         /* Is the semaphore already locked by another process? */
105         if (UNLIKELY(s->owner && (s->owner != CurrentProcess)))
106         {
107                 /* Append calling process to the wait queue */
108                 ADDTAIL(&s->wait_queue, (Node *)CurrentProcess);
109                 ENABLE_IRQRESTORE(flags);
110
111                 /*
112                  * We will wake up only when the current owner calls
113                  * sem_release(). Then, the semaphore will already
114                  * be locked for us.
115                  */
116                 proc_schedule();
117         }
118         else
119         {
120                 /* The semaphore was free: lock it */
121                 s->owner = CurrentProcess;
122                 s->nest_count++;
123                 ENABLE_IRQRESTORE(flags);
124         }
125 }
126
127
128 /*!
129  * \brief Releases a lock on a previously locked semaphore.
130  *
131  * If the nesting count of the semaphore reaches zero,
132  * the next process waiting for it will be awaken.
133  *
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.
138  *
139  * \sa sem_obtain() sem_attempt()
140  */
141 void sem_release(struct Semaphore *s)
142 {
143         cpuflags_t flags;
144         DISABLE_IRQSAVE(flags);
145
146         /*
147          * Decrement nesting count and check if the semaphore
148          * has been fully unlocked
149          */
150         if (--s->nest_count == 0)
151         {
152                 Process *proc;
153
154                 /* Disown semaphore */
155                 s->owner = NULL;
156
157                 /* Give semaphore to the first applicant, if any */
158                 if ((proc = (Process*)REMHEAD(&s->wait_queue)))
159                 {
160                         s->nest_count = 1;
161                         s->owner = proc;
162                         SCHED_ENQUEUE(proc);
163                 }
164         }
165
166         ENABLE_IRQRESTORE(flags);
167 }
168