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 void rwsemtrace(struct rw_semaphore
*sem
, const char *str
)
24 printk("[%d] %s({%d,%d})\n",
25 current
->pid
, str
, sem
->activity
,
26 list_empty(&sem
->wait_list
) ? 0 : 1);
31 * initialise the semaphore
33 void fastcall
init_rwsem(struct rw_semaphore
*sem
)
36 spin_lock_init(&sem
->wait_lock
);
37 INIT_LIST_HEAD(&sem
->wait_list
);
44 * handle the lock release when processes blocked on it that can now run
45 * - if we come here, then:
46 * - the 'active count' _reached_ zero
47 * - the 'waiting count' is non-zero
48 * - the spinlock must be held by the caller
49 * - woken process blocks are discarded from the list after having task zeroed
50 * - writers are only woken if wakewrite is non-zero
52 static inline struct rw_semaphore
*
53 __rwsem_do_wake(struct rw_semaphore
*sem
, int wakewrite
)
55 struct rwsem_waiter
*waiter
;
56 struct task_struct
*tsk
;
59 rwsemtrace(sem
, "Entering __rwsem_do_wake");
61 waiter
= list_entry(sem
->wait_list
.next
, struct rwsem_waiter
, list
);
64 if (waiter
->flags
& RWSEM_WAITING_FOR_WRITE
)
66 goto dont_wake_writers
;
69 /* if we are allowed to wake writers try to grant a single write lock
70 * if there's a writer at the front of the queue
71 * - we leave the 'waiting count' incremented to signify potential
74 if (waiter
->flags
& RWSEM_WAITING_FOR_WRITE
) {
76 list_del(&waiter
->list
);
78 /* Don't touch waiter after ->task has been NULLed */
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
;
107 rwsemtrace(sem
, "Leaving __rwsem_do_wake");
112 * wake a single writer
114 static inline struct rw_semaphore
*
115 __rwsem_wake_one_writer(struct rw_semaphore
*sem
)
117 struct rwsem_waiter
*waiter
;
118 struct task_struct
*tsk
;
122 waiter
= list_entry(sem
->wait_list
.next
, struct rwsem_waiter
, list
);
123 list_del(&waiter
->list
);
128 wake_up_process(tsk
);
129 put_task_struct(tsk
);
134 * get a read lock on the semaphore
136 void fastcall __sched
__down_read(struct rw_semaphore
*sem
)
138 struct rwsem_waiter waiter
;
139 struct task_struct
*tsk
;
141 rwsemtrace(sem
, "Entering __down_read");
143 spin_lock_irq(&sem
->wait_lock
);
145 if (sem
->activity
>= 0 && list_empty(&sem
->wait_list
)) {
148 spin_unlock_irq(&sem
->wait_lock
);
153 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
155 /* set up my own style of waitqueue */
157 waiter
.flags
= RWSEM_WAITING_FOR_READ
;
158 get_task_struct(tsk
);
160 list_add_tail(&waiter
.list
, &sem
->wait_list
);
162 /* we don't need to touch the semaphore struct anymore */
163 spin_unlock_irq(&sem
->wait_lock
);
165 /* wait to be given the lock */
170 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
173 tsk
->state
= TASK_RUNNING
;
176 rwsemtrace(sem
, "Leaving __down_read");
180 * trylock for reading -- returns 1 if successful, 0 if contention
182 int fastcall
__down_read_trylock(struct rw_semaphore
*sem
)
187 rwsemtrace(sem
, "Entering __down_read_trylock");
189 spin_lock_irqsave(&sem
->wait_lock
, flags
);
191 if (sem
->activity
>= 0 && list_empty(&sem
->wait_list
)) {
197 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
199 rwsemtrace(sem
, "Leaving __down_read_trylock");
204 * get a write lock on the semaphore
205 * - we increment the waiting count anyway to indicate an exclusive lock
207 void fastcall __sched
__down_write(struct rw_semaphore
*sem
)
209 struct rwsem_waiter waiter
;
210 struct task_struct
*tsk
;
212 rwsemtrace(sem
, "Entering __down_write");
214 spin_lock_irq(&sem
->wait_lock
);
216 if (sem
->activity
== 0 && list_empty(&sem
->wait_list
)) {
219 spin_unlock_irq(&sem
->wait_lock
);
224 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
226 /* set up my own style of waitqueue */
228 waiter
.flags
= RWSEM_WAITING_FOR_WRITE
;
229 get_task_struct(tsk
);
231 list_add_tail(&waiter
.list
, &sem
->wait_list
);
233 /* we don't need to touch the semaphore struct anymore */
234 spin_unlock_irq(&sem
->wait_lock
);
236 /* wait to be given the lock */
241 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
244 tsk
->state
= TASK_RUNNING
;
247 rwsemtrace(sem
, "Leaving __down_write");
251 * trylock for writing -- returns 1 if successful, 0 if contention
253 int fastcall
__down_write_trylock(struct rw_semaphore
*sem
)
258 rwsemtrace(sem
, "Entering __down_write_trylock");
260 spin_lock_irqsave(&sem
->wait_lock
, flags
);
262 if (sem
->activity
== 0 && list_empty(&sem
->wait_list
)) {
268 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
270 rwsemtrace(sem
, "Leaving __down_write_trylock");
275 * release a read lock on the semaphore
277 void fastcall
__up_read(struct rw_semaphore
*sem
)
281 rwsemtrace(sem
, "Entering __up_read");
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
);
290 rwsemtrace(sem
, "Leaving __up_read");
294 * release a write lock on the semaphore
296 void fastcall
__up_write(struct rw_semaphore
*sem
)
300 rwsemtrace(sem
, "Entering __up_write");
302 spin_lock_irqsave(&sem
->wait_lock
, flags
);
305 if (!list_empty(&sem
->wait_list
))
306 sem
= __rwsem_do_wake(sem
, 1);
308 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
310 rwsemtrace(sem
, "Leaving __up_write");
314 * downgrade a write lock into a read lock
315 * - just wake up any readers at the front of the queue
317 void fastcall
__downgrade_write(struct rw_semaphore
*sem
)
321 rwsemtrace(sem
, "Entering __downgrade_write");
323 spin_lock_irqsave(&sem
->wait_lock
, flags
);
326 if (!list_empty(&sem
->wait_list
))
327 sem
= __rwsem_do_wake(sem
, 0);
329 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
331 rwsemtrace(sem
, "Leaving __downgrade_write");
334 EXPORT_SYMBOL(init_rwsem
);
335 EXPORT_SYMBOL(__down_read
);
336 EXPORT_SYMBOL(__down_read_trylock
);
337 EXPORT_SYMBOL(__down_write
);
338 EXPORT_SYMBOL(__down_write_trylock
);
339 EXPORT_SYMBOL(__up_read
);
340 EXPORT_SYMBOL(__up_write
);
341 EXPORT_SYMBOL(__downgrade_write
);
343 EXPORT_SYMBOL(rwsemtrace
);