Use proc_forbid()/proc_permit().
[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.6  2004/10/21 10:57:21  bernie
19  *#* Use proc_forbid()/proc_permit().
20  *#*
21  *#* Revision 1.5  2004/10/21 10:48:57  bernie
22  *#* sem_release(): Simplify (made by rasky on scfirm).
23  *#*
24  *#* Revision 1.4  2004/08/25 14:12:09  rasky
25  *#* Aggiornato il comment block dei log RCS
26  *#*
27  *#* Revision 1.3  2004/08/08 05:53:23  bernie
28  *#* Use DISABLE_IRQSAVE/ENABLE_IRQRESTORE; Cleanup documentation.
29  *#*
30  *#* Revision 1.2  2004/06/03 11:27:09  bernie
31  *#* Add dual-license information.
32  *#*
33  *#* Revision 1.1  2004/05/23 17:27:00  bernie
34  *#* Import kern/ subdirectory.
35  *#*/
36
37 #include "sem.h"
38 #include "proc.h"
39 #include "proc_p.h"
40 #include "signal.h"
41 #include "hw.h"
42
43
44 /*!
45  * \brief Initialize a Semaphore structure.
46  */
47 void sem_init(struct Semaphore *s)
48 {
49         INITLIST(&s->wait_queue);
50         s->owner = NULL;
51         s->nest_count = 0;
52 }
53
54
55 /*!
56  * \brief Attempt to lock a semaphore without waiting.
57  *
58  * \return true in case of success, false if the semaphore
59  *         was already locked by someone else.
60  *
61  * \note   each call to sem_attempt() must be matched by a
62  *         call to sem_release().
63  *
64  * \see sem_obtain() sem_release()
65  */
66 bool sem_attempt(struct Semaphore *s)
67 {
68         bool result = false;
69
70         proc_forbid();
71         if ((!s->owner) || (s->owner == CurrentProcess))
72         {
73                 s->owner = CurrentProcess;
74                 s->nest_count++;
75                 result = true;
76         }
77         proc_permit();
78
79         return result;
80 }
81
82
83 /*!
84  * \brief Lock a semaphore.
85  *
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.
89  *
90  * \note Each call to sem_obtain() must be matched by a
91  *       call to sem_release().
92  *
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.
97  *
98  * \sa sem_release() sem_attempt()
99  */
100 void sem_obtain(struct Semaphore *s)
101 {
102         proc_forbid();
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
110                 /*
111                  * We will wake up only when the current owner calls
112                  * sem_release(). Then, the semaphore will already
113                  * be locked for us.
114                  */
115                 proc_permit();
116                 proc_schedule();
117         }
118         else
119         {
120                 /* The semaphore was free: lock it */
121                 s->owner = CurrentProcess;
122                 s->nest_count++;
123                 proc_permit();
124         }
125 }
126
127
128 /*!
129  * \brief Release 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         proc_forbid();
144
145         /*
146          * Decrement nesting count and check if the semaphore
147          * has been fully unlocked.
148          */
149         if (--s->nest_count == 0)
150         {
151                 Process *proc;
152
153                 /* Disown semaphore */
154                 s->owner = NULL;
155
156                 /* Give semaphore to the first applicant, if any */
157                 if (UNLIKELY((proc = (Process *)REMHEAD(&s->wait_queue))))
158                 {
159                         s->nest_count = 1;
160                         s->owner = proc;
161                         SCHED_ENQUEUE(proc);
162                 }
163         }
164
165         proc_permit();
166 }