[PATCH] Fix get_wchan() FIXME wrt. order of functions
[linux-flexiantxendom0-3.2.10.git] / lib / rwsem.c
1 /* rwsem.c: R/W semaphores: contention handling functions
2  *
3  * Written by David Howells (dhowells@redhat.com).
4  * Derived from arch/i386/kernel/semaphore.c
5  */
6 #include <linux/rwsem.h>
7 #include <linux/sched.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10
11 struct rwsem_waiter {
12         struct list_head        list;
13         struct task_struct      *task;
14         unsigned int            flags;
15 #define RWSEM_WAITING_FOR_READ  0x00000001
16 #define RWSEM_WAITING_FOR_WRITE 0x00000002
17 };
18
19 #if RWSEM_DEBUG
20 #undef rwsemtrace
21 void rwsemtrace(struct rw_semaphore *sem, const char *str)
22 {
23         printk("sem=%p\n",sem);
24         printk("(sem)=%08lx\n",sem->count);
25         if (sem->debug)
26                 printk("[%d] %s({%08lx})\n",current->pid,str,sem->count);
27 }
28 #endif
29
30 /*
31  * handle the lock being released whilst there are processes blocked on it that can now run
32  * - if we come here, then:
33  *   - the 'active part' of the count (&0x0000ffff) reached zero but has been re-incremented
34  *   - the 'waiting part' of the count (&0xffff0000) is negative (and will still be so)
35  *   - there must be someone on the queue
36  * - the spinlock must be held by the caller
37  * - woken process blocks are discarded from the list after having flags zeroised
38  * - writers are only woken if wakewrite is non-zero
39  */
40 static inline struct rw_semaphore *__rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
41 {
42         struct rwsem_waiter *waiter;
43         struct list_head *next;
44         signed long oldcount;
45         int woken, loop;
46
47         rwsemtrace(sem,"Entering __rwsem_do_wake");
48
49         if (!wakewrite)
50                 goto dont_wake_writers;
51
52         /* only wake someone up if we can transition the active part of the count from 0 -> 1 */
53  try_again:
54         oldcount = rwsem_atomic_update(RWSEM_ACTIVE_BIAS,sem) - RWSEM_ACTIVE_BIAS;
55         if (oldcount & RWSEM_ACTIVE_MASK)
56                 goto undo;
57
58         waiter = list_entry(sem->wait_list.next,struct rwsem_waiter,list);
59
60         /* try to grant a single write lock if there's a writer at the front of the queue
61          * - note we leave the 'active part' of the count incremented by 1 and the waiting part
62          *   incremented by 0x00010000
63          */
64         if (!(waiter->flags & RWSEM_WAITING_FOR_WRITE))
65                 goto readers_only;
66
67         list_del(&waiter->list);
68         waiter->flags = 0;
69         wake_up_process(waiter->task);
70         goto out;
71
72         /* don't want to wake any writers */
73  dont_wake_writers:
74         waiter = list_entry(sem->wait_list.next,struct rwsem_waiter,list);
75         if (waiter->flags & RWSEM_WAITING_FOR_WRITE)
76                 goto out;
77
78         /* grant an infinite number of read locks to the readers at the front of the queue
79          * - note we increment the 'active part' of the count by the number of readers (less one
80          *   for the activity decrement we've already done) before waking any processes up
81          */
82  readers_only:
83         woken = 0;
84         do {
85                 woken++;
86
87                 if (waiter->list.next==&sem->wait_list)
88                         break;
89
90                 waiter = list_entry(waiter->list.next,struct rwsem_waiter,list);
91
92         } while (waiter->flags & RWSEM_WAITING_FOR_READ);
93
94         loop = woken;
95         woken *= RWSEM_ACTIVE_BIAS-RWSEM_WAITING_BIAS;
96         woken -= RWSEM_ACTIVE_BIAS;
97         rwsem_atomic_add(woken,sem);
98
99         next = sem->wait_list.next;
100         for (; loop>0; loop--) {
101                 waiter = list_entry(next,struct rwsem_waiter,list);
102                 next = waiter->list.next;
103                 waiter->flags = 0;
104                 wake_up_process(waiter->task);
105         }
106
107         sem->wait_list.next = next;
108         next->prev = &sem->wait_list;
109
110  out:
111         rwsemtrace(sem,"Leaving __rwsem_do_wake");
112         return sem;
113
114         /* undo the change to count, but check for a transition 1->0 */
115  undo:
116         if (rwsem_atomic_update(-RWSEM_ACTIVE_BIAS,sem)!=0)
117                 goto out;
118         goto try_again;
119 }
120
121 /*
122  * wait for a lock to be granted
123  */
124 static inline struct rw_semaphore *rwsem_down_failed_common(struct rw_semaphore *sem,
125                                                                  struct rwsem_waiter *waiter,
126                                                                  signed long adjustment)
127 {
128         struct task_struct *tsk = current;
129         signed long count;
130
131         set_task_state(tsk,TASK_UNINTERRUPTIBLE);
132
133         /* set up my own style of waitqueue */
134         spin_lock(&sem->wait_lock);
135         waiter->task = tsk;
136
137         list_add_tail(&waiter->list,&sem->wait_list);
138
139         /* note that we're now waiting on the lock, but no longer actively read-locking */
140         count = rwsem_atomic_update(adjustment,sem);
141
142         /* if there are no longer active locks, wake the front queued process(es) up
143          * - it might even be this process, since the waker takes a more active part
144          */
145         if (!(count & RWSEM_ACTIVE_MASK))
146                 sem = __rwsem_do_wake(sem,1);
147
148         spin_unlock(&sem->wait_lock);
149
150         /* wait to be given the lock */
151         for (;;) {
152                 if (!waiter->flags)
153                         break;
154                 schedule();
155                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
156         }
157
158         tsk->state = TASK_RUNNING;
159
160         return sem;
161 }
162
163 /*
164  * wait for the read lock to be granted
165  */
166 struct rw_semaphore fastcall __sched *rwsem_down_read_failed(struct rw_semaphore *sem)
167 {
168         struct rwsem_waiter waiter;
169
170         rwsemtrace(sem,"Entering rwsem_down_read_failed");
171
172         waiter.flags = RWSEM_WAITING_FOR_READ;
173         rwsem_down_failed_common(sem,&waiter,RWSEM_WAITING_BIAS-RWSEM_ACTIVE_BIAS);
174
175         rwsemtrace(sem,"Leaving rwsem_down_read_failed");
176         return sem;
177 }
178
179 /*
180  * wait for the write lock to be granted
181  */
182 struct rw_semaphore fastcall __sched *rwsem_down_write_failed(struct rw_semaphore *sem)
183 {
184         struct rwsem_waiter waiter;
185
186         rwsemtrace(sem,"Entering rwsem_down_write_failed");
187
188         waiter.flags = RWSEM_WAITING_FOR_WRITE;
189         rwsem_down_failed_common(sem,&waiter,-RWSEM_ACTIVE_BIAS);
190
191         rwsemtrace(sem,"Leaving rwsem_down_write_failed");
192         return sem;
193 }
194
195 /*
196  * handle waking up a waiter on the semaphore
197  * - up_read has decremented the active part of the count if we come here
198  */
199 struct rw_semaphore fastcall *rwsem_wake(struct rw_semaphore *sem)
200 {
201         rwsemtrace(sem,"Entering rwsem_wake");
202
203         spin_lock(&sem->wait_lock);
204
205         /* do nothing if list empty */
206         if (!list_empty(&sem->wait_list))
207                 sem = __rwsem_do_wake(sem,1);
208
209         spin_unlock(&sem->wait_lock);
210
211         rwsemtrace(sem,"Leaving rwsem_wake");
212
213         return sem;
214 }
215
216 /*
217  * downgrade a write lock into a read lock
218  * - caller incremented waiting part of count, and discovered it to be still negative
219  * - just wake up any readers at the front of the queue
220  */
221 struct rw_semaphore fastcall *rwsem_downgrade_wake(struct rw_semaphore *sem)
222 {
223         rwsemtrace(sem,"Entering rwsem_downgrade_wake");
224
225         spin_lock(&sem->wait_lock);
226
227         /* do nothing if list empty */
228         if (!list_empty(&sem->wait_list))
229                 sem = __rwsem_do_wake(sem,0);
230
231         spin_unlock(&sem->wait_lock);
232
233         rwsemtrace(sem,"Leaving rwsem_downgrade_wake");
234         return sem;
235 }
236
237 EXPORT_SYMBOL_NOVERS(rwsem_down_read_failed);
238 EXPORT_SYMBOL_NOVERS(rwsem_down_write_failed);
239 EXPORT_SYMBOL_NOVERS(rwsem_wake);
240 EXPORT_SYMBOL_NOVERS(rwsem_downgrade_wake);
241 #if RWSEM_DEBUG
242 EXPORT_SYMBOL(rwsemtrace);
243 #endif