Luca's patch ported
[cbs-scheduler.git] / lib / rwsem-spinlock.c
blobd07a74485c4054f0f9dbefbb696a3c510af2dc2d
1 /* rwsem-spinlock.c: R/W semaphores: contention handling functions for
2 * generic spinlock implementation
4 * Copyright (c) 2001 David Howells (dhowells@redhat.com).
5 * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
6 * - Derived also from comments by Linus
7 */
8 #include <linux/rwsem.h>
9 #include <linux/sched.h>
10 #include <linux/module.h>
12 struct rwsem_waiter {
13 struct list_head list;
14 struct task_struct *task;
15 unsigned int flags;
16 #define RWSEM_WAITING_FOR_READ 0x00000001
17 #define RWSEM_WAITING_FOR_WRITE 0x00000002
21 * initialise the semaphore
23 void __compat_init_rwsem(struct compat_rw_semaphore *sem, const char *name,
24 struct lock_class_key *key)
26 #ifdef CONFIG_DEBUG_LOCK_ALLOC
28 * Make sure we are not reinitializing a held semaphore:
30 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
31 lockdep_init_map(&sem->dep_map, name, key, 0);
32 #endif
33 sem->activity = 0;
34 spin_lock_init(&sem->wait_lock);
35 INIT_LIST_HEAD(&sem->wait_list);
39 * handle the lock release when processes blocked on it that can now run
40 * - if we come here, then:
41 * - the 'active count' _reached_ zero
42 * - the 'waiting count' is non-zero
43 * - the spinlock must be held by the caller
44 * - woken process blocks are discarded from the list after having task zeroed
45 * - writers are only woken if wakewrite is non-zero
47 static inline struct compat_rw_semaphore *
48 __rwsem_do_wake(struct compat_rw_semaphore *sem, int wakewrite)
50 struct rwsem_waiter *waiter;
51 struct task_struct *tsk;
52 int woken;
54 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
56 if (!wakewrite) {
57 if (waiter->flags & RWSEM_WAITING_FOR_WRITE)
58 goto out;
59 goto dont_wake_writers;
62 /* if we are allowed to wake writers try to grant a single write lock
63 * if there's a writer at the front of the queue
64 * - we leave the 'waiting count' incremented to signify potential
65 * contention
67 if (waiter->flags & RWSEM_WAITING_FOR_WRITE) {
68 sem->activity = -1;
69 list_del(&waiter->list);
70 tsk = waiter->task;
71 /* Don't touch waiter after ->task has been NULLed */
72 smp_mb();
73 waiter->task = NULL;
74 wake_up_process(tsk);
75 put_task_struct(tsk);
76 goto out;
79 /* grant an infinite number of read locks to the front of the queue */
80 dont_wake_writers:
81 woken = 0;
82 while (waiter->flags & RWSEM_WAITING_FOR_READ) {
83 struct list_head *next = waiter->list.next;
85 list_del(&waiter->list);
86 tsk = waiter->task;
87 smp_mb();
88 waiter->task = NULL;
89 wake_up_process(tsk);
90 put_task_struct(tsk);
91 woken++;
92 if (list_empty(&sem->wait_list))
93 break;
94 waiter = list_entry(next, struct rwsem_waiter, list);
97 sem->activity += woken;
99 out:
100 return sem;
104 * wake a single writer
106 static inline struct compat_rw_semaphore *
107 __rwsem_wake_one_writer(struct compat_rw_semaphore *sem)
109 struct rwsem_waiter *waiter;
110 struct task_struct *tsk;
112 sem->activity = -1;
114 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
115 list_del(&waiter->list);
117 tsk = waiter->task;
118 smp_mb();
119 waiter->task = NULL;
120 wake_up_process(tsk);
121 put_task_struct(tsk);
122 return sem;
126 * get a read lock on the semaphore
128 void __sched __down_read(struct compat_rw_semaphore *sem)
130 struct rwsem_waiter waiter;
131 struct task_struct *tsk;
133 spin_lock_irq(&sem->wait_lock);
135 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
136 /* granted */
137 sem->activity++;
138 spin_unlock_irq(&sem->wait_lock);
139 goto out;
142 tsk = current;
143 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
145 /* set up my own style of waitqueue */
146 waiter.task = tsk;
147 waiter.flags = RWSEM_WAITING_FOR_READ;
148 get_task_struct(tsk);
150 list_add_tail(&waiter.list, &sem->wait_list);
152 /* we don't need to touch the semaphore struct anymore */
153 spin_unlock_irq(&sem->wait_lock);
155 /* wait to be given the lock */
156 for (;;) {
157 if (!waiter.task)
158 break;
159 schedule();
160 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
163 tsk->state = TASK_RUNNING;
164 out:
169 * trylock for reading -- returns 1 if successful, 0 if contention
171 int __down_read_trylock(struct compat_rw_semaphore *sem)
173 unsigned long flags;
174 int ret = 0;
177 spin_lock_irqsave(&sem->wait_lock, flags);
179 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
180 /* granted */
181 sem->activity++;
182 ret = 1;
185 spin_unlock_irqrestore(&sem->wait_lock, flags);
187 return ret;
191 * get a write lock on the semaphore
192 * - we increment the waiting count anyway to indicate an exclusive lock
194 void __sched
195 __down_write_nested(struct compat_rw_semaphore *sem, int subclass)
197 struct rwsem_waiter waiter;
198 struct task_struct *tsk;
200 spin_lock_irq(&sem->wait_lock);
202 if (sem->activity == 0 && list_empty(&sem->wait_list)) {
203 /* granted */
204 sem->activity = -1;
205 spin_unlock_irq(&sem->wait_lock);
206 goto out;
209 tsk = current;
210 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
212 /* set up my own style of waitqueue */
213 waiter.task = tsk;
214 waiter.flags = RWSEM_WAITING_FOR_WRITE;
215 get_task_struct(tsk);
217 list_add_tail(&waiter.list, &sem->wait_list);
219 /* we don't need to touch the semaphore struct anymore */
220 spin_unlock_irq(&sem->wait_lock);
222 /* wait to be given the lock */
223 for (;;) {
224 if (!waiter.task)
225 break;
226 schedule();
227 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
230 tsk->state = TASK_RUNNING;
231 out:
235 void __sched __down_write(struct compat_rw_semaphore *sem)
237 __down_write_nested(sem, 0);
241 * trylock for writing -- returns 1 if successful, 0 if contention
243 int __down_write_trylock(struct compat_rw_semaphore *sem)
245 unsigned long flags;
246 int ret = 0;
248 spin_lock_irqsave(&sem->wait_lock, flags);
250 if (sem->activity == 0 && list_empty(&sem->wait_list)) {
251 /* granted */
252 sem->activity = -1;
253 ret = 1;
256 spin_unlock_irqrestore(&sem->wait_lock, flags);
258 return ret;
262 * release a read lock on the semaphore
264 void __up_read(struct compat_rw_semaphore *sem)
266 unsigned long flags;
268 spin_lock_irqsave(&sem->wait_lock, flags);
270 if (--sem->activity == 0 && !list_empty(&sem->wait_list))
271 sem = __rwsem_wake_one_writer(sem);
273 spin_unlock_irqrestore(&sem->wait_lock, flags);
277 * release a write lock on the semaphore
279 void __up_write(struct compat_rw_semaphore *sem)
281 unsigned long flags;
283 spin_lock_irqsave(&sem->wait_lock, flags);
285 sem->activity = 0;
286 if (!list_empty(&sem->wait_list))
287 sem = __rwsem_do_wake(sem, 1);
289 spin_unlock_irqrestore(&sem->wait_lock, flags);
293 * downgrade a write lock into a read lock
294 * - just wake up any readers at the front of the queue
296 void __downgrade_write(struct compat_rw_semaphore *sem)
298 unsigned long flags;
300 spin_lock_irqsave(&sem->wait_lock, flags);
302 sem->activity = 1;
303 if (!list_empty(&sem->wait_list))
304 sem = __rwsem_do_wake(sem, 0);
306 spin_unlock_irqrestore(&sem->wait_lock, flags);
309 EXPORT_SYMBOL(__compat_init_rwsem);
310 EXPORT_SYMBOL(__down_read);
311 EXPORT_SYMBOL(__down_read_trylock);
312 EXPORT_SYMBOL(__down_write_nested);
313 EXPORT_SYMBOL(__down_write);
314 EXPORT_SYMBOL(__down_write_trylock);
315 EXPORT_SYMBOL(__up_read);
316 EXPORT_SYMBOL(__up_write);
317 EXPORT_SYMBOL(__downgrade_write);