1 /* rwsem-spinlock.c: R/W semaphores: contention handling functions for generic spinlock
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
,list_empty(&sem
->wait_list
)?0:1);
30 * initialise the semaphore
32 void init_rwsem(struct rw_semaphore
*sem
)
35 spin_lock_init(&sem
->wait_lock
);
36 INIT_LIST_HEAD(&sem
->wait_list
);
43 * handle the lock being released whilst there are processes blocked on it that can now run
44 * - if we come here, then:
45 * - the 'active count' _reached_ zero
46 * - the 'waiting count' is non-zero
47 * - the spinlock must be held by the caller
48 * - woken process blocks are discarded from the list after having flags zeroised
49 * - writers are only woken if wakewrite is non-zero
51 static inline struct rw_semaphore
*__rwsem_do_wake(struct rw_semaphore
*sem
, int wakewrite
)
53 struct rwsem_waiter
*waiter
;
56 rwsemtrace(sem
,"Entering __rwsem_do_wake");
58 waiter
= list_entry(sem
->wait_list
.next
,struct rwsem_waiter
,list
);
61 if (waiter
->flags
& RWSEM_WAITING_FOR_WRITE
)
63 goto dont_wake_writers
;
66 /* if we are allowed to wake writers try to grant a single write lock if there's a
67 * writer at the front of the queue
68 * - we leave the 'waiting count' incremented to signify potential contention
70 if (waiter
->flags
& RWSEM_WAITING_FOR_WRITE
) {
72 list_del(&waiter
->list
);
74 wake_up_process(waiter
->task
);
78 /* grant an infinite number of read locks to the readers at the front of the queue */
81 while (waiter
->flags
&RWSEM_WAITING_FOR_READ
) {
82 struct list_head
*next
= waiter
->list
.next
;
84 list_del(&waiter
->list
);
86 wake_up_process(waiter
->task
);
88 if (list_empty(&sem
->wait_list
))
90 waiter
= list_entry(next
,struct rwsem_waiter
,list
);
93 sem
->activity
+= woken
;
96 rwsemtrace(sem
,"Leaving __rwsem_do_wake");
101 * wake a single writer
103 static inline struct rw_semaphore
*__rwsem_wake_one_writer(struct rw_semaphore
*sem
)
105 struct rwsem_waiter
*waiter
;
109 waiter
= list_entry(sem
->wait_list
.next
,struct rwsem_waiter
,list
);
110 list_del(&waiter
->list
);
113 wake_up_process(waiter
->task
);
118 * get a read lock on the semaphore
120 void __down_read(struct rw_semaphore
*sem
)
122 struct rwsem_waiter waiter
;
123 struct task_struct
*tsk
;
125 rwsemtrace(sem
,"Entering __down_read");
127 spin_lock(&sem
->wait_lock
);
129 if (sem
->activity
>=0 && list_empty(&sem
->wait_list
)) {
132 spin_unlock(&sem
->wait_lock
);
137 set_task_state(tsk
,TASK_UNINTERRUPTIBLE
);
139 /* set up my own style of waitqueue */
141 waiter
.flags
= RWSEM_WAITING_FOR_READ
;
143 list_add_tail(&waiter
.list
,&sem
->wait_list
);
145 /* we don't need to touch the semaphore struct anymore */
146 spin_unlock(&sem
->wait_lock
);
148 /* wait to be given the lock */
153 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
156 tsk
->state
= TASK_RUNNING
;
159 rwsemtrace(sem
,"Leaving __down_read");
163 * trylock for reading -- returns 1 if successful, 0 if contention
165 int __down_read_trylock(struct rw_semaphore
*sem
)
168 rwsemtrace(sem
,"Entering __down_read_trylock");
170 spin_lock(&sem
->wait_lock
);
172 if (sem
->activity
>=0 && list_empty(&sem
->wait_list
)) {
178 spin_unlock(&sem
->wait_lock
);
180 rwsemtrace(sem
,"Leaving __down_read_trylock");
185 * get a write lock on the semaphore
186 * - note that we increment the waiting count anyway to indicate an exclusive lock
188 void __down_write(struct rw_semaphore
*sem
)
190 struct rwsem_waiter waiter
;
191 struct task_struct
*tsk
;
193 rwsemtrace(sem
,"Entering __down_write");
195 spin_lock(&sem
->wait_lock
);
197 if (sem
->activity
==0 && list_empty(&sem
->wait_list
)) {
200 spin_unlock(&sem
->wait_lock
);
205 set_task_state(tsk
,TASK_UNINTERRUPTIBLE
);
207 /* set up my own style of waitqueue */
209 waiter
.flags
= RWSEM_WAITING_FOR_WRITE
;
211 list_add_tail(&waiter
.list
,&sem
->wait_list
);
213 /* we don't need to touch the semaphore struct anymore */
214 spin_unlock(&sem
->wait_lock
);
216 /* wait to be given the lock */
221 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
224 tsk
->state
= TASK_RUNNING
;
227 rwsemtrace(sem
,"Leaving __down_write");
231 * trylock for writing -- returns 1 if successful, 0 if contention
233 int __down_write_trylock(struct rw_semaphore
*sem
)
236 rwsemtrace(sem
,"Entering __down_write_trylock");
238 spin_lock(&sem
->wait_lock
);
240 if (sem
->activity
==0 && list_empty(&sem
->wait_list
)) {
246 spin_unlock(&sem
->wait_lock
);
248 rwsemtrace(sem
,"Leaving __down_write_trylock");
253 * release a read lock on the semaphore
255 void __up_read(struct rw_semaphore
*sem
)
257 rwsemtrace(sem
,"Entering __up_read");
259 spin_lock(&sem
->wait_lock
);
261 if (--sem
->activity
==0 && !list_empty(&sem
->wait_list
))
262 sem
= __rwsem_wake_one_writer(sem
);
264 spin_unlock(&sem
->wait_lock
);
266 rwsemtrace(sem
,"Leaving __up_read");
270 * release a write lock on the semaphore
272 void __up_write(struct rw_semaphore
*sem
)
274 rwsemtrace(sem
,"Entering __up_write");
276 spin_lock(&sem
->wait_lock
);
279 if (!list_empty(&sem
->wait_list
))
280 sem
= __rwsem_do_wake(sem
, 1);
282 spin_unlock(&sem
->wait_lock
);
284 rwsemtrace(sem
,"Leaving __up_write");
288 * downgrade a write lock into a read lock
289 * - just wake up any readers at the front of the queue
291 void __downgrade_write(struct rw_semaphore
*sem
)
293 rwsemtrace(sem
,"Entering __downgrade_write");
295 spin_lock(&sem
->wait_lock
);
298 if (!list_empty(&sem
->wait_list
))
299 sem
= __rwsem_do_wake(sem
,0);
301 spin_unlock(&sem
->wait_lock
);
303 rwsemtrace(sem
,"Leaving __downgrade_write");
306 EXPORT_SYMBOL(init_rwsem
);
307 EXPORT_SYMBOL(__down_read
);
308 EXPORT_SYMBOL(__down_read_trylock
);
309 EXPORT_SYMBOL(__down_write
);
310 EXPORT_SYMBOL(__down_write_trylock
);
311 EXPORT_SYMBOL(__up_read
);
312 EXPORT_SYMBOL(__up_write
);
313 EXPORT_SYMBOL(__downgrade_write
);
315 EXPORT_SYMBOL(rwsemtrace
);