]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - lib/rwsem-spinlock.c
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
20 int rwsem_is_locked(struct rw_semaphore
*sem
)
25 if (spin_trylock_irqsave(&sem
->wait_lock
, flags
)) {
26 ret
= (sem
->activity
!= 0);
27 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 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
;
76 /* if we are allowed to wake writers try to grant a single write lock
77 * if there's a writer at the front of the queue
78 * - we leave the 'waiting count' incremented to signify potential
81 if (waiter
->flags
& RWSEM_WAITING_FOR_WRITE
) {
83 list_del(&waiter
->list
);
85 /* Don't touch waiter after ->task has been NULLed */
93 /* grant an infinite number of read locks to the front of the queue */
96 while (waiter
->flags
& RWSEM_WAITING_FOR_READ
) {
97 struct list_head
*next
= waiter
->list
.next
;
99 list_del(&waiter
->list
);
103 wake_up_process(tsk
);
104 put_task_struct(tsk
);
106 if (list_empty(&sem
->wait_list
))
108 waiter
= list_entry(next
, struct rwsem_waiter
, list
);
111 sem
->activity
+= woken
;
118 * wake a single writer
120 static inline struct rw_semaphore
*
121 __rwsem_wake_one_writer(struct rw_semaphore
*sem
)
123 struct rwsem_waiter
*waiter
;
124 struct task_struct
*tsk
;
128 waiter
= list_entry(sem
->wait_list
.next
, struct rwsem_waiter
, list
);
129 list_del(&waiter
->list
);
134 wake_up_process(tsk
);
135 put_task_struct(tsk
);
140 * get a read lock on the semaphore
142 void __sched
__down_read(struct rw_semaphore
*sem
)
144 struct rwsem_waiter waiter
;
145 struct task_struct
*tsk
;
148 spin_lock_irqsave(&sem
->wait_lock
, flags
);
150 if (sem
->activity
>= 0 && list_empty(&sem
->wait_list
)) {
153 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
158 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
160 /* set up my own style of waitqueue */
162 waiter
.flags
= RWSEM_WAITING_FOR_READ
;
163 get_task_struct(tsk
);
165 list_add_tail(&waiter
.list
, &sem
->wait_list
);
167 /* we don't need to touch the semaphore struct anymore */
168 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
170 /* wait to be given the lock */
175 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
178 tsk
->state
= TASK_RUNNING
;
184 * trylock for reading -- returns 1 if successful, 0 if contention
186 int __down_read_trylock(struct rw_semaphore
*sem
)
192 spin_lock_irqsave(&sem
->wait_lock
, flags
);
194 if (sem
->activity
>= 0 && list_empty(&sem
->wait_list
)) {
200 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
206 * get a write lock on the semaphore
207 * - we increment the waiting count anyway to indicate an exclusive lock
209 void __sched
__down_write_nested(struct rw_semaphore
*sem
, int subclass
)
211 struct rwsem_waiter waiter
;
212 struct task_struct
*tsk
;
215 spin_lock_irqsave(&sem
->wait_lock
, flags
);
217 if (sem
->activity
== 0 && list_empty(&sem
->wait_list
)) {
220 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
225 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
227 /* set up my own style of waitqueue */
229 waiter
.flags
= RWSEM_WAITING_FOR_WRITE
;
230 get_task_struct(tsk
);
232 list_add_tail(&waiter
.list
, &sem
->wait_list
);
234 /* we don't need to touch the semaphore struct anymore */
235 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
237 /* wait to be given the lock */
242 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
245 tsk
->state
= TASK_RUNNING
;
250 void __sched
__down_write(struct rw_semaphore
*sem
)
252 __down_write_nested(sem
, 0);
256 * trylock for writing -- returns 1 if successful, 0 if contention
258 int __down_write_trylock(struct rw_semaphore
*sem
)
263 spin_lock_irqsave(&sem
->wait_lock
, flags
);
265 if (sem
->activity
== 0 && list_empty(&sem
->wait_list
)) {
271 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
277 * release a read lock on the semaphore
279 void __up_read(struct rw_semaphore
*sem
)
283 spin_lock_irqsave(&sem
->wait_lock
, flags
);
285 if (--sem
->activity
== 0 && !list_empty(&sem
->wait_list
))
286 sem
= __rwsem_wake_one_writer(sem
);
288 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
292 * release a write lock on the semaphore
294 void __up_write(struct rw_semaphore
*sem
)
298 spin_lock_irqsave(&sem
->wait_lock
, flags
);
301 if (!list_empty(&sem
->wait_list
))
302 sem
= __rwsem_do_wake(sem
, 1);
304 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
308 * downgrade a write lock into a read lock
309 * - just wake up any readers at the front of the queue
311 void __downgrade_write(struct rw_semaphore
*sem
)
315 spin_lock_irqsave(&sem
->wait_lock
, flags
);
318 if (!list_empty(&sem
->wait_list
))
319 sem
= __rwsem_do_wake(sem
, 0);
321 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);