2 * i386 semaphore implementation.
4 * (C) Copyright 1999 Linus Torvalds
6 * Portions Copyright 1999 Red Hat, Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * rw semaphores implemented November 1999 by Benjamin LaHaise <bcrl@kvack.org>
15 #include <linux/config.h>
16 #include <linux/sched.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <asm/semaphore.h>
22 * Semaphores are implemented using a two-way counter:
23 * The "count" variable is decremented for each process
24 * that tries to acquire the semaphore, while the "sleeping"
25 * variable is a count of such acquires.
27 * Notably, the inline "up()" and "down()" functions can
28 * efficiently test if they need to do any extra work (up
29 * needs to do something only if count was negative before
30 * the increment operation.
32 * "sleeping" and the contention routine ordering is protected
33 * by the spinlock in the semaphore's waitqueue head.
35 * Note that these functions are only called when there is
36 * contention on the lock, and as such all this is the
37 * "non-critical" part of the whole semaphore business. The
38 * critical part is the inline stuff in <asm/semaphore.h>
39 * where we want to avoid any extra jumps and calls.
44 * - only on a boundary condition do we need to care. When we go
45 * from a negative count to a non-negative, we wake people up.
46 * - when we go from a non-negative count to a negative do we
47 * (a) synchronize with the "sleeper" count and (b) make sure
48 * that we're on the wakeup list before we synchronize so that
49 * we cannot lose wakeup events.
52 static fastcall void __attribute_used__ __up(struct semaphore *sem)
57 static fastcall void __attribute_used__ __sched __down(struct semaphore * sem)
59 struct task_struct *tsk = current;
60 DECLARE_WAITQUEUE(wait, tsk);
63 tsk->state = TASK_UNINTERRUPTIBLE;
64 spin_lock_irqsave(&sem->wait.lock, flags);
65 add_wait_queue_exclusive_locked(&sem->wait, &wait);
69 int sleepers = sem->sleepers;
72 * Add "everybody else" into it. They aren't
73 * playing, because we own the spinlock in
74 * the wait_queue_head.
76 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
80 sem->sleepers = 1; /* us - see -1 above */
81 spin_unlock_irqrestore(&sem->wait.lock, flags);
85 spin_lock_irqsave(&sem->wait.lock, flags);
86 tsk->state = TASK_UNINTERRUPTIBLE;
88 remove_wait_queue_locked(&sem->wait, &wait);
89 wake_up_locked(&sem->wait);
90 spin_unlock_irqrestore(&sem->wait.lock, flags);
91 tsk->state = TASK_RUNNING;
94 static fastcall int __attribute_used__ __sched __down_interruptible(struct semaphore * sem)
97 struct task_struct *tsk = current;
98 DECLARE_WAITQUEUE(wait, tsk);
101 tsk->state = TASK_INTERRUPTIBLE;
102 spin_lock_irqsave(&sem->wait.lock, flags);
103 add_wait_queue_exclusive_locked(&sem->wait, &wait);
107 int sleepers = sem->sleepers;
110 * With signals pending, this turns into
111 * the trylock failure case - we won't be
112 * sleeping, and we* can't get the lock as
113 * it has contention. Just correct the count
116 if (signal_pending(current)) {
119 atomic_add(sleepers, &sem->count);
124 * Add "everybody else" into it. They aren't
125 * playing, because we own the spinlock in
126 * wait_queue_head. The "-1" is because we're
127 * still hoping to get the semaphore.
129 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
133 sem->sleepers = 1; /* us - see -1 above */
134 spin_unlock_irqrestore(&sem->wait.lock, flags);
138 spin_lock_irqsave(&sem->wait.lock, flags);
139 tsk->state = TASK_INTERRUPTIBLE;
141 remove_wait_queue_locked(&sem->wait, &wait);
142 wake_up_locked(&sem->wait);
143 spin_unlock_irqrestore(&sem->wait.lock, flags);
145 tsk->state = TASK_RUNNING;
150 * Trylock failed - make sure we correct for
151 * having decremented the count.
153 * We could have done the trylock with a
154 * single "cmpxchg" without failure cases,
155 * but then it wouldn't work on a 386.
157 static fastcall int __attribute_used__ __down_trylock(struct semaphore * sem)
162 spin_lock_irqsave(&sem->wait.lock, flags);
163 sleepers = sem->sleepers + 1;
167 * Add "everybody else" and us into it. They aren't
168 * playing, because we own the spinlock in the
171 if (!atomic_add_negative(sleepers, &sem->count)) {
172 wake_up_locked(&sem->wait);
175 spin_unlock_irqrestore(&sem->wait.lock, flags);
181 * The semaphore operations have a special calling sequence that
182 * allow us to do a simpler in-line version of them. These routines
183 * need to convert that sequence back into the C sequence when
184 * there is contention on the semaphore.
186 * %eax contains the semaphore pointer on entry. Save the C-clobbered
187 * registers (%eax, %edx and %ecx) except %eax whish is either a return
188 * value or just clobbered..
191 ".section .sched.text\n"
193 ".globl __down_failed\n"
195 #if defined(CONFIG_FRAME_POINTER)
204 #if defined(CONFIG_FRAME_POINTER)
212 ".section .sched.text\n"
214 ".globl __down_failed_interruptible\n"
215 "__down_failed_interruptible:\n\t"
216 #if defined(CONFIG_FRAME_POINTER)
222 "call __down_interruptible\n\t"
225 #if defined(CONFIG_FRAME_POINTER)
233 ".section .sched.text\n"
235 ".globl __down_failed_trylock\n"
236 "__down_failed_trylock:\n\t"
237 #if defined(CONFIG_FRAME_POINTER)
243 "call __down_trylock\n\t"
246 #if defined(CONFIG_FRAME_POINTER)
254 ".section .sched.text\n"
256 ".globl __up_wakeup\n"
267 * rw spinlock fallbacks
269 #if defined(CONFIG_SMP)
271 ".section .sched.text\n"
273 ".globl __write_lock_failed\n"
274 "__write_lock_failed:\n\t"
275 LOCK "addl $" RW_LOCK_BIAS_STR ",(%eax)\n"
277 "cmpl $" RW_LOCK_BIAS_STR ",(%eax)\n\t"
279 LOCK "subl $" RW_LOCK_BIAS_STR ",(%eax)\n\t"
280 "jnz __write_lock_failed\n\t"
285 ".section .sched.text\n"
287 ".globl __read_lock_failed\n"
288 "__read_lock_failed:\n\t"
293 LOCK "decl (%eax)\n\t"
294 "js __read_lock_failed\n\t"