* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / arch / arm / kernel / semaphore.c
blob1bb21be3fe22babc6f5baa88e71a49e207698a25
1 /*
2 * ARM semaphore implementation, taken from
4 * i386 semaphore implementation.
6 * (C) Copyright 1999 Linus Torvalds
8 * Modified for ARM by Russell King
9 */
10 #include <linux/sched.h>
12 #include <asm/semaphore.h>
15 * Semaphores are implemented using a two-way counter:
16 * The "count" variable is decremented for each process
17 * that tries to aquire the semaphore, while the "sleeping"
18 * variable is a count of such aquires.
20 * Notably, the inline "up()" and "down()" functions can
21 * efficiently test if they need to do any extra work (up
22 * needs to do something only if count was negative before
23 * the increment operation.
25 * "sleeping" and the contention routine ordering is
26 * protected by the semaphore spinlock.
28 * Note that these functions are only called when there is
29 * contention on the lock, and as such all this is the
30 * "non-critical" part of the whole semaphore business. The
31 * critical part is the inline stuff in <asm/semaphore.h>
32 * where we want to avoid any extra jumps and calls.
36 * Logic:
37 * - only on a boundary condition do we need to care. When we go
38 * from a negative count to a non-negative, we wake people up.
39 * - when we go from a non-negative count to a negative do we
40 * (a) synchronize with the "sleeper" count and (b) make sure
41 * that we're on the wakeup list before we synchronize so that
42 * we cannot lose wakeup events.
45 void __up(struct semaphore *sem)
47 wake_up(&sem->wait);
50 static spinlock_t semaphore_lock = SPIN_LOCK_UNLOCKED;
52 void __down(struct semaphore * sem)
54 struct task_struct *tsk = current;
55 DECLARE_WAITQUEUE(wait, tsk);
56 tsk->state = TASK_UNINTERRUPTIBLE;
57 add_wait_queue(&sem->wait, &wait);
59 spin_lock_irq(&semaphore_lock);
60 sem->sleepers++;
61 for (;;) {
62 int sleepers = sem->sleepers;
65 * Add "everybody else" into it. They aren't
66 * playing, because we own the spinlock.
68 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
69 sem->sleepers = 0;
70 wake_up(&sem->wait);
71 break;
73 sem->sleepers = 1; /* us - see -1 above */
74 spin_unlock_irq(&semaphore_lock);
76 schedule();
77 tsk->state = TASK_UNINTERRUPTIBLE;
78 spin_lock_irq(&semaphore_lock);
80 spin_unlock_irq(&semaphore_lock);
81 remove_wait_queue(&sem->wait, &wait);
82 tsk->state = TASK_RUNNING;
85 int __down_interruptible(struct semaphore * sem)
87 int retval;
88 struct task_struct *tsk = current;
89 DECLARE_WAITQUEUE(wait, tsk);
90 tsk->state = TASK_INTERRUPTIBLE;
91 add_wait_queue(&sem->wait, &wait);
93 spin_lock_irq(&semaphore_lock);
94 sem->sleepers ++;
95 for (;;) {
96 int sleepers = sem->sleepers;
99 * With signals pending, this turns into
100 * the trylock failure case - we won't be
101 * sleeping, and we* can't get the lock as
102 * it has contention. Just correct the count
103 * and exit.
105 retval = -EINTR;
106 if (signal_pending(current)) {
107 sem->sleepers = 0;
108 if (atomic_add_negative(sleepers, &sem->count))
109 break;
110 wake_up(&sem->wait);
111 break;
115 * Add "everybody else" into it. They aren't
116 * playing, because we own the spinlock. The
117 * "-1" is because we're still hoping to get
118 * the lock.
120 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
121 wake_up(&sem->wait);
122 retval = 0;
123 sem->sleepers = 0;
124 break;
126 sem->sleepers = 1; /* us - see -1 above */
127 spin_unlock_irq(&semaphore_lock);
129 schedule();
130 tsk->state = TASK_INTERRUPTIBLE;
131 spin_lock_irq(&semaphore_lock);
133 spin_unlock_irq(&semaphore_lock);
134 tsk->state = TASK_RUNNING;
135 remove_wait_queue(&sem->wait, &wait);
136 return retval;
140 * Trylock failed - make sure we correct for
141 * having decremented the count.
143 * We could have done the trylock with a
144 * single "cmpxchg" without failure cases,
145 * but then it wouldn't work on a 386.
147 int __down_trylock(struct semaphore * sem)
149 int sleepers;
151 spin_lock_irq(&semaphore_lock);
152 sleepers = sem->sleepers + 1;
153 sem->sleepers = 0;
156 * Add "everybody else" and us into it. They aren't
157 * playing, because we own the spinlock.
159 if (!atomic_add_negative(sleepers, &sem->count))
160 wake_up(&sem->wait);
162 spin_unlock_irq(&semaphore_lock);
163 return 1;
167 * The semaphore operations have a special calling sequence that
168 * allow us to do a simpler in-line version of them. These routines
169 * need to convert that sequence back into the C sequence when
170 * there is contention on the semaphore.
172 * r0 contains the semaphore pointer on entry. Save the C-clobbered
173 * registers (r0 to r3, ip and lr) except r0 in the cases where it
174 * is used as a return value..
176 asm(".align 5
177 .globl __down_failed
178 __down_failed:
179 stmfd sp!, {r0 - r3, ip, lr}
180 bl __down
181 ldmfd sp!, {r0 - r3, ip, pc}");
183 asm(".align 5
184 .globl __down_interruptible_failed
185 __down_interruptible_failed:
186 stmfd sp!, {r1 - r3, ip, lr}
187 bl __down_interruptible
188 ldmfd sp!, {r1 - r3, ip, pc}");
190 asm(".align 5
191 .globl __down_trylock_failed
192 __down_trylock_failed:
193 stmfd sp!, {r1 - r3, ip, lr}
194 bl __down_trylock
195 ldmfd sp!, {r1 - r3, ip, pc}");
197 asm(".align 5
198 .globl __up_wakeup
199 __up_wakeup:
200 stmfd sp!, {r0 - r3, ip, lr}
201 bl __up
202 ldmfd sp!, {r0 - r3, ip, pc}");