2 * x86_64 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/init.h>
18 #include <asm/errno.h>
20 #include <asm/semaphore.h>
23 * Semaphores are implemented using a two-way counter:
24 * The "count" variable is decremented for each process
25 * that tries to acquire the semaphore, while the "sleeping"
26 * variable is a count of such acquires.
28 * Notably, the inline "up()" and "down()" functions can
29 * efficiently test if they need to do any extra work (up
30 * needs to do something only if count was negative before
31 * the increment operation.
33 * "sleeping" and the contention routine ordering is protected
34 * by the spinlock in the semaphore's waitqueue head.
36 * Note that these functions are only called when there is
37 * contention on the lock, and as such all this is the
38 * "non-critical" part of the whole semaphore business. The
39 * critical part is the inline stuff in <asm/semaphore.h>
40 * where we want to avoid any extra jumps and calls.
45 * - only on a boundary condition do we need to care. When we go
46 * from a negative count to a non-negative, we wake people up.
47 * - when we go from a non-negative count to a negative do we
48 * (a) synchronize with the "sleeper" count and (b) make sure
49 * that we're on the wakeup list before we synchronize so that
50 * we cannot lose wakeup events.
53 void __up(struct semaphore *sem)
58 void __sched __down(struct semaphore * sem)
60 struct task_struct *tsk = current;
61 DECLARE_WAITQUEUE(wait, tsk);
64 tsk->state = TASK_UNINTERRUPTIBLE;
65 spin_lock_irqsave(&sem->wait.lock, flags);
66 add_wait_queue_exclusive_locked(&sem->wait, &wait);
70 int sleepers = sem->sleepers;
73 * Add "everybody else" into it. They aren't
74 * playing, because we own the spinlock in
75 * the wait_queue_head.
77 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
81 sem->sleepers = 1; /* us - see -1 above */
82 spin_unlock_irqrestore(&sem->wait.lock, flags);
86 spin_lock_irqsave(&sem->wait.lock, flags);
87 tsk->state = TASK_UNINTERRUPTIBLE;
89 remove_wait_queue_locked(&sem->wait, &wait);
90 wake_up_locked(&sem->wait);
91 spin_unlock_irqrestore(&sem->wait.lock, flags);
92 tsk->state = TASK_RUNNING;
95 int __sched __down_interruptible(struct semaphore * sem)
98 struct task_struct *tsk = current;
99 DECLARE_WAITQUEUE(wait, tsk);
102 tsk->state = TASK_INTERRUPTIBLE;
103 spin_lock_irqsave(&sem->wait.lock, flags);
104 add_wait_queue_exclusive_locked(&sem->wait, &wait);
108 int sleepers = sem->sleepers;
111 * With signals pending, this turns into
112 * the trylock failure case - we won't be
113 * sleeping, and we* can't get the lock as
114 * it has contention. Just correct the count
117 if (signal_pending(current)) {
120 atomic_add(sleepers, &sem->count);
125 * Add "everybody else" into it. They aren't
126 * playing, because we own the spinlock in
127 * wait_queue_head. The "-1" is because we're
128 * still hoping to get the semaphore.
130 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
134 sem->sleepers = 1; /* us - see -1 above */
135 spin_unlock_irqrestore(&sem->wait.lock, flags);
139 spin_lock_irqsave(&sem->wait.lock, flags);
140 tsk->state = TASK_INTERRUPTIBLE;
142 remove_wait_queue_locked(&sem->wait, &wait);
143 wake_up_locked(&sem->wait);
144 spin_unlock_irqrestore(&sem->wait.lock, flags);
146 tsk->state = TASK_RUNNING;
151 * Trylock failed - make sure we correct for
152 * having decremented the count.
154 * We could have done the trylock with a
155 * single "cmpxchg" without failure cases,
156 * but then it wouldn't work on a 386.
158 int __down_trylock(struct semaphore * sem)
163 spin_lock_irqsave(&sem->wait.lock, flags);
164 sleepers = sem->sleepers + 1;
168 * Add "everybody else" and us into it. They aren't
169 * playing, because we own the spinlock in the
172 if (!atomic_add_negative(sleepers, &sem->count)) {
173 wake_up_locked(&sem->wait);
176 spin_unlock_irqrestore(&sem->wait.lock, flags);