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/export.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
20 int rwsem_is_locked(struct rw_semaphore *sem)
25 if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) {
26 ret = (sem->activity != 0);
27 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
31 EXPORT_SYMBOL(rwsem_is_locked);
34 * initialise the semaphore
36 void __init_rwsem(struct rw_semaphore *sem, const char *name,
37 struct lock_class_key *key)
39 #ifdef CONFIG_DEBUG_LOCK_ALLOC
41 * Make sure we are not reinitializing a held semaphore:
43 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
44 lockdep_init_map(&sem->dep_map, name, key, 0);
47 raw_spin_lock_init(&sem->wait_lock);
48 INIT_LIST_HEAD(&sem->wait_list);
50 EXPORT_SYMBOL(__init_rwsem);
53 * handle the lock release when processes blocked on it that can now run
54 * - if we come here, then:
55 * - the 'active count' _reached_ zero
56 * - the 'waiting count' is non-zero
57 * - the spinlock must be held by the caller
58 * - woken process blocks are discarded from the list after having task zeroed
59 * - writers are only woken if wakewrite is non-zero
61 static inline struct rw_semaphore *
62 __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
64 struct rwsem_waiter *waiter;
65 struct task_struct *tsk;
68 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
71 if (waiter->flags & RWSEM_WAITING_FOR_WRITE)
73 goto dont_wake_writers;
77 * as we support write lock stealing, we can't set sem->activity
78 * to -1 here to indicate we get the lock. Instead, we wake it up
79 * to let it go get it again.
81 if (waiter->flags & RWSEM_WAITING_FOR_WRITE) {
82 wake_up_process(waiter->task);
86 /* grant an infinite number of read locks to the front of the queue */
89 while (waiter->flags & RWSEM_WAITING_FOR_READ) {
90 struct list_head *next = waiter->list.next;
92 list_del(&waiter->list);
99 if (list_empty(&sem->wait_list))
101 waiter = list_entry(next, struct rwsem_waiter, list);
104 sem->activity += woken;
111 * wake a single writer
113 static inline struct rw_semaphore *
114 __rwsem_wake_one_writer(struct rw_semaphore *sem)
116 struct rwsem_waiter *waiter;
118 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
119 wake_up_process(waiter->task);
125 * get a read lock on the semaphore
127 void __sched __down_read(struct rw_semaphore *sem)
129 struct rwsem_waiter waiter;
130 struct task_struct *tsk;
133 raw_spin_lock_irqsave(&sem->wait_lock, flags);
135 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
138 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
143 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
145 /* set up my own style of waitqueue */
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 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
155 /* wait to be given the lock */
160 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
163 tsk->state = TASK_RUNNING;
169 * trylock for reading -- returns 1 if successful, 0 if contention
171 int __down_read_trylock(struct rw_semaphore *sem)
177 raw_spin_lock_irqsave(&sem->wait_lock, flags);
179 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
185 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
191 * get a write lock on the semaphore
193 void __sched __down_write_nested(struct rw_semaphore *sem, int subclass)
195 struct rwsem_waiter waiter;
196 struct task_struct *tsk;
199 raw_spin_lock_irqsave(&sem->wait_lock, flags);
201 /* set up my own style of waitqueue */
204 waiter.flags = RWSEM_WAITING_FOR_WRITE;
205 list_add_tail(&waiter.list, &sem->wait_list);
207 /* wait for someone to release the lock */
210 * That is the key to support write lock stealing: allows the
211 * task already on CPU to get the lock soon rather than put
212 * itself into sleep and waiting for system woke it or someone
213 * else in the head of the wait list up.
215 if (sem->activity == 0)
217 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
218 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
220 raw_spin_lock_irqsave(&sem->wait_lock, flags);
224 list_del(&waiter.list);
226 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
229 void __sched __down_write(struct rw_semaphore *sem)
231 __down_write_nested(sem, 0);
235 * trylock for writing -- returns 1 if successful, 0 if contention
237 int __down_write_trylock(struct rw_semaphore *sem)
242 raw_spin_lock_irqsave(&sem->wait_lock, flags);
244 if (sem->activity == 0) {
250 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
256 * release a read lock on the semaphore
258 void __up_read(struct rw_semaphore *sem)
262 raw_spin_lock_irqsave(&sem->wait_lock, flags);
264 if (--sem->activity == 0 && !list_empty(&sem->wait_list))
265 sem = __rwsem_wake_one_writer(sem);
267 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
271 * release a write lock on the semaphore
273 void __up_write(struct rw_semaphore *sem)
277 raw_spin_lock_irqsave(&sem->wait_lock, flags);
280 if (!list_empty(&sem->wait_list))
281 sem = __rwsem_do_wake(sem, 1);
283 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
287 * downgrade a write lock into a read lock
288 * - just wake up any readers at the front of the queue
290 void __downgrade_write(struct rw_semaphore *sem)
294 raw_spin_lock_irqsave(&sem->wait_lock, flags);
297 if (!list_empty(&sem->wait_list))
298 sem = __rwsem_do_wake(sem, 0);
300 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);