x86 mmiotrace: use resource_size_t for phys addresses
[linux-2.6/verdex.git] / kernel / semaphore.c
blob1a064adab658cbacc72c2fe8a88ab7e6f3a2efeb
1 /*
2 * Copyright (c) 2008 Intel Corporation
3 * Author: Matthew Wilcox <willy@linux.intel.com>
5 * Distributed under the terms of the GNU GPL, version 2
7 * This file implements counting semaphores.
8 * A counting semaphore may be acquired 'n' times before sleeping.
9 * See mutex.c for single-acquisition sleeping locks which enforce
10 * rules which allow code to be debugged more easily.
14 * Some notes on the implementation:
16 * The spinlock controls access to the other members of the semaphore.
17 * down_trylock() and up() can be called from interrupt context, so we
18 * have to disable interrupts when taking the lock. It turns out various
19 * parts of the kernel expect to be able to use down() on a semaphore in
20 * interrupt context when they know it will succeed, so we have to use
21 * irqsave variants for down(), down_interruptible() and down_killable()
22 * too.
24 * The ->count variable represents how many more tasks can acquire this
25 * semaphore. If it's zero, there may be tasks waiting on the wait_list.
28 #include <linux/compiler.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/sched.h>
32 #include <linux/semaphore.h>
33 #include <linux/spinlock.h>
34 #include <linux/ftrace.h>
36 static noinline void __down(struct semaphore *sem);
37 static noinline int __down_interruptible(struct semaphore *sem);
38 static noinline int __down_killable(struct semaphore *sem);
39 static noinline int __down_timeout(struct semaphore *sem, long jiffies);
40 static noinline void __up(struct semaphore *sem);
42 /**
43 * down - acquire the semaphore
44 * @sem: the semaphore to be acquired
46 * Acquires the semaphore. If no more tasks are allowed to acquire the
47 * semaphore, calling this function will put the task to sleep until the
48 * semaphore is released.
50 * Use of this function is deprecated, please use down_interruptible() or
51 * down_killable() instead.
53 void down(struct semaphore *sem)
55 unsigned long flags;
57 ftrace_special(sem->count, 0, __LINE__);
58 spin_lock_irqsave(&sem->lock, flags);
59 if (likely(sem->count > 0))
60 sem->count--;
61 else
62 __down(sem);
63 spin_unlock_irqrestore(&sem->lock, flags);
65 EXPORT_SYMBOL(down);
67 /**
68 * down_interruptible - acquire the semaphore unless interrupted
69 * @sem: the semaphore to be acquired
71 * Attempts to acquire the semaphore. If no more tasks are allowed to
72 * acquire the semaphore, calling this function will put the task to sleep.
73 * If the sleep is interrupted by a signal, this function will return -EINTR.
74 * If the semaphore is successfully acquired, this function returns 0.
76 int down_interruptible(struct semaphore *sem)
78 unsigned long flags;
79 int result = 0;
81 spin_lock_irqsave(&sem->lock, flags);
82 if (likely(sem->count > 0))
83 sem->count--;
84 else
85 result = __down_interruptible(sem);
86 spin_unlock_irqrestore(&sem->lock, flags);
88 return result;
90 EXPORT_SYMBOL(down_interruptible);
92 /**
93 * down_killable - acquire the semaphore unless killed
94 * @sem: the semaphore to be acquired
96 * Attempts to acquire the semaphore. If no more tasks are allowed to
97 * acquire the semaphore, calling this function will put the task to sleep.
98 * If the sleep is interrupted by a fatal signal, this function will return
99 * -EINTR. If the semaphore is successfully acquired, this function returns
100 * 0.
102 int down_killable(struct semaphore *sem)
104 unsigned long flags;
105 int result = 0;
107 spin_lock_irqsave(&sem->lock, flags);
108 if (likely(sem->count > 0))
109 sem->count--;
110 else
111 result = __down_killable(sem);
112 spin_unlock_irqrestore(&sem->lock, flags);
114 return result;
116 EXPORT_SYMBOL(down_killable);
119 * down_trylock - try to acquire the semaphore, without waiting
120 * @sem: the semaphore to be acquired
122 * Try to acquire the semaphore atomically. Returns 0 if the mutex has
123 * been acquired successfully or 1 if it it cannot be acquired.
125 * NOTE: This return value is inverted from both spin_trylock and
126 * mutex_trylock! Be careful about this when converting code.
128 * Unlike mutex_trylock, this function can be used from interrupt context,
129 * and the semaphore can be released by any task or interrupt.
131 int down_trylock(struct semaphore *sem)
133 unsigned long flags;
134 int count;
136 spin_lock_irqsave(&sem->lock, flags);
137 count = sem->count - 1;
138 if (likely(count >= 0))
139 sem->count = count;
140 spin_unlock_irqrestore(&sem->lock, flags);
142 return (count < 0);
144 EXPORT_SYMBOL(down_trylock);
147 * down_timeout - acquire the semaphore within a specified time
148 * @sem: the semaphore to be acquired
149 * @jiffies: how long to wait before failing
151 * Attempts to acquire the semaphore. If no more tasks are allowed to
152 * acquire the semaphore, calling this function will put the task to sleep.
153 * If the semaphore is not released within the specified number of jiffies,
154 * this function returns -ETIME. It returns 0 if the semaphore was acquired.
156 int down_timeout(struct semaphore *sem, long jiffies)
158 unsigned long flags;
159 int result = 0;
161 spin_lock_irqsave(&sem->lock, flags);
162 if (likely(sem->count > 0))
163 sem->count--;
164 else
165 result = __down_timeout(sem, jiffies);
166 spin_unlock_irqrestore(&sem->lock, flags);
168 return result;
170 EXPORT_SYMBOL(down_timeout);
173 * up - release the semaphore
174 * @sem: the semaphore to release
176 * Release the semaphore. Unlike mutexes, up() may be called from any
177 * context and even by tasks which have never called down().
179 void up(struct semaphore *sem)
181 unsigned long flags;
183 spin_lock_irqsave(&sem->lock, flags);
184 if (likely(list_empty(&sem->wait_list)))
185 sem->count++;
186 else
187 __up(sem);
188 spin_unlock_irqrestore(&sem->lock, flags);
190 EXPORT_SYMBOL(up);
192 /* Functions for the contended case */
194 struct semaphore_waiter {
195 struct list_head list;
196 struct task_struct *task;
197 int up;
201 * Because this function is inlined, the 'state' parameter will be
202 * constant, and thus optimised away by the compiler. Likewise the
203 * 'timeout' parameter for the cases without timeouts.
205 static inline int __sched __down_common(struct semaphore *sem, long state,
206 long timeout)
208 struct task_struct *task = current;
209 struct semaphore_waiter waiter;
211 list_add_tail(&waiter.list, &sem->wait_list);
212 waiter.task = task;
213 waiter.up = 0;
215 for (;;) {
216 if (state == TASK_INTERRUPTIBLE && signal_pending(task))
217 goto interrupted;
218 if (state == TASK_KILLABLE && fatal_signal_pending(task))
219 goto interrupted;
220 if (timeout <= 0)
221 goto timed_out;
222 __set_task_state(task, state);
223 spin_unlock_irq(&sem->lock);
224 timeout = schedule_timeout(timeout);
225 spin_lock_irq(&sem->lock);
226 if (waiter.up)
227 return 0;
230 timed_out:
231 list_del(&waiter.list);
232 return -ETIME;
234 interrupted:
235 list_del(&waiter.list);
236 return -EINTR;
239 static noinline void __sched __down(struct semaphore *sem)
241 __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
244 static noinline int __sched __down_interruptible(struct semaphore *sem)
246 return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
249 static noinline int __sched __down_killable(struct semaphore *sem)
251 return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
254 static noinline int __sched __down_timeout(struct semaphore *sem, long jiffies)
256 return __down_common(sem, TASK_UNINTERRUPTIBLE, jiffies);
259 static noinline void __sched __up(struct semaphore *sem)
261 struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
262 struct semaphore_waiter, list);
263 list_del(&waiter->list);
264 waiter->up = 1;
265 wake_up_process(waiter->task);