Simplify semaphore implementation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / semaphore.c
blobbef977b169665cf8ad318354b31872dd348b5390
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
6 */
8 #include <linux/compiler.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/semaphore.h>
13 #include <linux/spinlock.h>
16 * Some notes on the implementation:
18 * down_trylock() and up() can be called from interrupt context.
19 * So we have to disable interrupts when taking the lock.
21 * The ->count variable defines how many more tasks can acquire the
22 * semaphore. If it's zero, there may be tasks waiting on the list.
25 static noinline void __down(struct semaphore *sem);
26 static noinline int __down_interruptible(struct semaphore *sem);
27 static noinline int __down_killable(struct semaphore *sem);
28 static noinline int __down_timeout(struct semaphore *sem, long jiffies);
29 static noinline void __up(struct semaphore *sem);
31 void down(struct semaphore *sem)
33 unsigned long flags;
35 spin_lock_irqsave(&sem->lock, flags);
36 if (likely(sem->count > 0))
37 sem->count--;
38 else
39 __down(sem);
40 spin_unlock_irqrestore(&sem->lock, flags);
42 EXPORT_SYMBOL(down);
44 int down_interruptible(struct semaphore *sem)
46 unsigned long flags;
47 int result = 0;
49 spin_lock_irqsave(&sem->lock, flags);
50 if (likely(sem->count > 0))
51 sem->count--;
52 else
53 result = __down_interruptible(sem);
54 spin_unlock_irqrestore(&sem->lock, flags);
56 return result;
58 EXPORT_SYMBOL(down_interruptible);
60 int down_killable(struct semaphore *sem)
62 unsigned long flags;
63 int result = 0;
65 spin_lock_irqsave(&sem->lock, flags);
66 if (likely(sem->count > 0))
67 sem->count--;
68 else
69 result = __down_killable(sem);
70 spin_unlock_irqrestore(&sem->lock, flags);
72 return result;
74 EXPORT_SYMBOL(down_killable);
76 /**
77 * down_trylock - try to acquire the semaphore, without waiting
78 * @sem: the semaphore to be acquired
80 * Try to acquire the semaphore atomically. Returns 0 if the mutex has
81 * been acquired successfully and 1 if it is contended.
83 * NOTE: This return value is inverted from both spin_trylock and
84 * mutex_trylock! Be careful about this when converting code.
86 * Unlike mutex_trylock, this function can be used from interrupt context,
87 * and the semaphore can be released by any task or interrupt.
89 int down_trylock(struct semaphore *sem)
91 unsigned long flags;
92 int count;
94 spin_lock_irqsave(&sem->lock, flags);
95 count = sem->count - 1;
96 if (likely(count >= 0))
97 sem->count = count;
98 spin_unlock_irqrestore(&sem->lock, flags);
100 return (count < 0);
102 EXPORT_SYMBOL(down_trylock);
104 int down_timeout(struct semaphore *sem, long jiffies)
106 unsigned long flags;
107 int result = 0;
109 spin_lock_irqsave(&sem->lock, flags);
110 if (likely(sem->count > 0))
111 sem->count--;
112 else
113 result = __down_timeout(sem, jiffies);
114 spin_unlock_irqrestore(&sem->lock, flags);
116 return result;
118 EXPORT_SYMBOL(down_timeout);
120 void up(struct semaphore *sem)
122 unsigned long flags;
124 spin_lock_irqsave(&sem->lock, flags);
125 if (likely(list_empty(&sem->wait_list)))
126 sem->count++;
127 else
128 __up(sem);
129 spin_unlock_irqrestore(&sem->lock, flags);
131 EXPORT_SYMBOL(up);
133 /* Functions for the contended case */
135 struct semaphore_waiter {
136 struct list_head list;
137 struct task_struct *task;
138 int up;
142 * Because this function is inlined, the 'state' parameter will be
143 * constant, and thus optimised away by the compiler. Likewise the
144 * 'timeout' parameter for the cases without timeouts.
146 static inline int __sched __down_common(struct semaphore *sem, long state,
147 long timeout)
149 struct task_struct *task = current;
150 struct semaphore_waiter waiter;
152 list_add_tail(&waiter.list, &sem->wait_list);
153 waiter.task = task;
154 waiter.up = 0;
156 for (;;) {
157 if (state == TASK_INTERRUPTIBLE && signal_pending(task))
158 goto interrupted;
159 if (state == TASK_KILLABLE && fatal_signal_pending(task))
160 goto interrupted;
161 if (timeout <= 0)
162 goto timed_out;
163 __set_task_state(task, state);
164 spin_unlock_irq(&sem->lock);
165 timeout = schedule_timeout(timeout);
166 spin_lock_irq(&sem->lock);
167 if (waiter.up)
168 return 0;
171 timed_out:
172 list_del(&waiter.list);
173 return -ETIME;
175 interrupted:
176 list_del(&waiter.list);
177 return -EINTR;
180 static noinline void __sched __down(struct semaphore *sem)
182 __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
185 static noinline int __sched __down_interruptible(struct semaphore *sem)
187 return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
190 static noinline int __sched __down_killable(struct semaphore *sem)
192 return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
195 static noinline int __sched __down_timeout(struct semaphore *sem, long jiffies)
197 return __down_common(sem, TASK_UNINTERRUPTIBLE, jiffies);
200 static noinline void __sched __up(struct semaphore *sem)
202 struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
203 struct semaphore_waiter, list);
204 list_del(&waiter->list);
205 waiter->up = 1;
206 wake_up_process(waiter->task);