2 * ARM semaphore implementation, taken from
4 * i386 semaphore implementation.
6 * (C) Copyright 1999 Linus Torvalds
8 * Modified for ARM by Russell King
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.
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
)
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
);
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
)) {
73 sem
->sleepers
= 1; /* us - see -1 above */
74 spin_unlock_irq(&semaphore_lock
);
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
)
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
);
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
106 if (signal_pending(current
)) {
108 if (atomic_add_negative(sleepers
, &sem
->count
))
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
120 if (!atomic_add_negative(sleepers
- 1, &sem
->count
)) {
126 sem
->sleepers
= 1; /* us - see -1 above */
127 spin_unlock_irq(&semaphore_lock
);
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
);
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
)
151 spin_lock_irq(&semaphore_lock
);
152 sleepers
= sem
->sleepers
+ 1;
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
))
162 spin_unlock_irq(&semaphore_lock
);
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..
179 stmfd sp!, {r0 - r3, ip, lr}
181 ldmfd sp!, {r0 - r3, ip, pc}");
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}");
191 .globl __down_trylock_failed
192 __down_trylock_failed:
193 stmfd sp!, {r1 - r3, ip, lr}
195 ldmfd sp!, {r1 - r3, ip, pc}");
200 stmfd sp!, {r0 - r3, ip, lr}
202 ldmfd sp!, {r0 - r3, ip, pc}");