Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / arch / sparc / kernel / semaphore.c
blobd6638be10b35ea570c52c1d045d7c70f9b957119
1 /* $Id: semaphore.c,v 1.4 2000/11/10 04:02:03 davem Exp $
2 * Generic semaphore code. Buyer beware. Do your own
3 * specific changes in <asm/semaphore-helper.h>
4 */
6 #include <linux/sched.h>
7 #include <asm/semaphore-helper.h>
9 /*
10 * Semaphores are implemented using a two-way counter:
11 * The "count" variable is decremented for each process
12 * that tries to sleep, while the "waking" variable is
13 * incremented when the "up()" code goes to wake up waiting
14 * processes.
16 * Notably, the inline "up()" and "down()" functions can
17 * efficiently test if they need to do any extra work (up
18 * needs to do something only if count was negative before
19 * the increment operation.
21 * waking_non_zero() (from asm/semaphore.h) must execute
22 * atomically.
24 * When __up() is called, the count was negative before
25 * incrementing it, and we need to wake up somebody.
27 * This routine adds one to the count of processes that need to
28 * wake up and exit. ALL waiting processes actually wake up but
29 * only the one that gets to the "waking" field first will gate
30 * through and acquire the semaphore. The others will go back
31 * to sleep.
33 * Note that these functions are only called when there is
34 * contention on the lock, and as such all this is the
35 * "non-critical" part of the whole semaphore business. The
36 * critical part is the inline stuff in <asm/semaphore.h>
37 * where we want to avoid any extra jumps and calls.
39 void __up(struct semaphore *sem)
41 wake_one_more(sem);
42 wake_up(&sem->wait);
46 * Perform the "down" function. Return zero for semaphore acquired,
47 * return negative for signalled out of the function.
49 * If called from __down, the return is ignored and the wait loop is
50 * not interruptible. This means that a task waiting on a semaphore
51 * using "down()" cannot be killed until someone does an "up()" on
52 * the semaphore.
54 * If called from __down_interruptible, the return value gets checked
55 * upon return. If the return value is negative then the task continues
56 * with the negative value in the return register (it can be tested by
57 * the caller).
59 * Either form may be used in conjunction with "up()".
63 #define DOWN_VAR \
64 struct task_struct *tsk = current; \
65 DECLARE_WAITQUEUE(wait, tsk);
67 #define DOWN_HEAD(task_state) \
70 tsk->state = (task_state); \
71 add_wait_queue(&sem->wait, &wait); \
73 /* \
74 * Ok, we're set up. sem->count is known to be less than zero \
75 * so we must wait. \
76 * \
77 * We can let go the lock for purposes of waiting. \
78 * We re-acquire it after awaking so as to protect \
79 * all semaphore operations. \
80 * \
81 * If "up()" is called before we call waking_non_zero() then \
82 * we will catch it right away. If it is called later then \
83 * we will have to go through a wakeup cycle to catch it. \
84 * \
85 * Multiple waiters contend for the semaphore lock to see \
86 * who gets to gate through and who has to wait some more. \
87 */ \
88 for (;;) {
90 #define DOWN_TAIL(task_state) \
91 tsk->state = (task_state); \
92 } \
93 tsk->state = TASK_RUNNING; \
94 remove_wait_queue(&sem->wait, &wait);
96 void __down(struct semaphore * sem)
98 DOWN_VAR
99 DOWN_HEAD(TASK_UNINTERRUPTIBLE)
100 if (waking_non_zero(sem))
101 break;
102 schedule();
103 DOWN_TAIL(TASK_UNINTERRUPTIBLE)
106 int __down_interruptible(struct semaphore * sem)
108 int ret = 0;
109 DOWN_VAR
110 DOWN_HEAD(TASK_INTERRUPTIBLE)
112 ret = waking_non_zero_interruptible(sem, tsk);
113 if (ret)
115 if (ret == 1)
116 /* ret != 0 only if we get interrupted -arca */
117 ret = 0;
118 break;
120 schedule();
121 DOWN_TAIL(TASK_INTERRUPTIBLE)
122 return ret;
125 int __down_trylock(struct semaphore * sem)
127 return waking_non_zero_trylock(sem);
130 /* rw mutexes
131 * Implemented by Jakub Jelinek (jakub@redhat.com) based on
132 * i386 implementation by Ben LaHaise (bcrl@redhat.com).
135 extern inline int ldstub(unsigned char *p)
137 int ret;
138 asm volatile("ldstub %1, %0" : "=r" (ret) : "m" (*p) : "memory");
139 return ret;
142 void down_read_failed_biased(struct rw_semaphore *sem)
144 DOWN_VAR
146 add_wait_queue(&sem->wait, &wait); /* put ourselves at the head of the list */
148 for (;;) {
149 if (!ldstub(&sem->read_not_granted))
150 break;
151 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
152 if (sem->read_not_granted)
153 schedule();
156 remove_wait_queue(&sem->wait, &wait);
157 tsk->state = TASK_RUNNING;
160 void down_write_failed_biased(struct rw_semaphore *sem)
162 DOWN_VAR
164 add_wait_queue_exclusive(&sem->write_bias_wait, &wait); /* put ourselves at the end of the list */
166 for (;;) {
167 if (!ldstub(&sem->write_not_granted))
168 break;
169 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
170 if (sem->write_not_granted)
171 schedule();
174 remove_wait_queue(&sem->write_bias_wait, &wait);
175 tsk->state = TASK_RUNNING;
177 /* if the lock is currently unbiased, awaken the sleepers
178 * FIXME: this wakes up the readers early in a bit of a
179 * stampede -> bad!
181 if (sem->count >= 0)
182 wake_up(&sem->wait);
185 /* Wait for the lock to become unbiased. Readers
186 * are non-exclusive. =)
188 void down_read_failed(struct rw_semaphore *sem)
190 DOWN_VAR
192 __up_read(sem); /* this takes care of granting the lock */
194 add_wait_queue(&sem->wait, &wait);
196 while (sem->count < 0) {
197 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
198 if (sem->count >= 0)
199 break;
200 schedule();
203 remove_wait_queue(&sem->wait, &wait);
204 tsk->state = TASK_RUNNING;
207 /* Wait for the lock to become unbiased. Since we're
208 * a writer, we'll make ourselves exclusive.
210 void down_write_failed(struct rw_semaphore *sem)
212 DOWN_VAR
214 __up_write(sem); /* this takes care of granting the lock */
216 add_wait_queue_exclusive(&sem->wait, &wait);
218 while (sem->count < 0) {
219 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
220 if (sem->count >= 0)
221 break; /* we must attempt to acquire or bias the lock */
222 schedule();
225 remove_wait_queue(&sem->wait, &wait);
226 tsk->state = TASK_RUNNING;
229 void __rwsem_wake(struct rw_semaphore *sem, unsigned long readers)
231 if (readers) {
232 /* Due to lame ldstub we don't do here
233 a BUG() consistency check */
234 sem->read_not_granted = 0;
235 wake_up(&sem->wait);
236 } else {
237 sem->write_not_granted = 0;
238 wake_up(&sem->write_bias_wait);