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
8 #include <linux/rwsem.h>
9 #include <linux/sched.h>
10 #include <linux/module.h>
13 struct list_head list;
14 struct task_struct *task;
16 #define RWSEM_WAITING_FOR_READ 0x00000001
17 #define RWSEM_WAITING_FOR_WRITE 0x00000002
21 * initialise the semaphore
23 void __init_rwsem(struct 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);
34 spin_lock_init(&sem->wait_lock);
35 INIT_LIST_HEAD(&sem->wait_list);
37 EXPORT_SYMBOL(__init_rwsem);
40 * handle the lock release when processes blocked on it that can now run
41 * - if we come here, then:
42 * - the 'active count' _reached_ zero
43 * - the 'waiting count' is non-zero
44 * - the spinlock must be held by the caller
45 * - woken process blocks are discarded from the list after having task zeroed
46 * - writers are only woken if wakewrite is non-zero
48 static inline struct rw_semaphore *
49 __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
51 struct rwsem_waiter *waiter;
52 struct task_struct *tsk;
55 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
58 if (waiter->flags & RWSEM_WAITING_FOR_WRITE)
60 goto dont_wake_writers;
63 /* if we are allowed to wake writers try to grant a single write lock
64 * if there's a writer at the front of the queue
65 * - we leave the 'waiting count' incremented to signify potential
68 if (waiter->flags & RWSEM_WAITING_FOR_WRITE) {
70 list_del(&waiter->list);
72 /* Don't touch waiter after ->task has been NULLed */
80 /* grant an infinite number of read locks to the front of the queue */
83 while (waiter->flags & RWSEM_WAITING_FOR_READ) {
84 struct list_head *next = waiter->list.next;
86 list_del(&waiter->list);
93 if (list_empty(&sem->wait_list))
95 waiter = list_entry(next, struct rwsem_waiter, list);
98 sem->activity += woken;
105 * wake a single writer
107 static inline struct rw_semaphore *
108 __rwsem_wake_one_writer(struct rw_semaphore *sem)
110 struct rwsem_waiter *waiter;
111 struct task_struct *tsk;
115 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
116 list_del(&waiter->list);
121 wake_up_process(tsk);
122 put_task_struct(tsk);
127 * get a read lock on the semaphore
129 void __sched __down_read(struct rw_semaphore *sem)
131 struct rwsem_waiter waiter;
132 struct task_struct *tsk;
134 spin_lock_irq(&sem->wait_lock);
136 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
139 spin_unlock_irq(&sem->wait_lock);
144 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
146 /* set up my own style of waitqueue */
148 waiter.flags = RWSEM_WAITING_FOR_READ;
149 get_task_struct(tsk);
151 list_add_tail(&waiter.list, &sem->wait_list);
153 /* we don't need to touch the semaphore struct anymore */
154 spin_unlock_irq(&sem->wait_lock);
156 /* wait to be given the lock */
161 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
164 tsk->state = TASK_RUNNING;
170 * trylock for reading -- returns 1 if successful, 0 if contention
172 int __down_read_trylock(struct rw_semaphore *sem)
178 spin_lock_irqsave(&sem->wait_lock, flags);
180 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
186 spin_unlock_irqrestore(&sem->wait_lock, flags);
192 * get a write lock on the semaphore
193 * - we increment the waiting count anyway to indicate an exclusive lock
195 void __sched __down_write_nested(struct 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)) {
205 spin_unlock_irq(&sem->wait_lock);
210 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
212 /* set up my own style of waitqueue */
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 */
227 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
230 tsk->state = TASK_RUNNING;
235 void __sched __down_write(struct 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 rw_semaphore *sem)
248 spin_lock_irqsave(&sem->wait_lock, flags);
250 if (sem->activity == 0 && list_empty(&sem->wait_list)) {
256 spin_unlock_irqrestore(&sem->wait_lock, flags);
262 * release a read lock on the semaphore
264 void __up_read(struct rw_semaphore *sem)
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 rw_semaphore *sem)
283 spin_lock_irqsave(&sem->wait_lock, flags);
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 rw_semaphore *sem)
300 spin_lock_irqsave(&sem->wait_lock, flags);
303 if (!list_empty(&sem->wait_list))
304 sem = __rwsem_do_wake(sem, 0);
306 spin_unlock_irqrestore(&sem->wait_lock, flags);