]> bbs.cooldavid.org Git - net-next-2.6.git/blob - include/asm-i386/rwsem.h
[PATCH] lockdep: clean up rwsems
[net-next-2.6.git] / include / asm-i386 / rwsem.h
1 /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for i486+
2  *
3  * Written by David Howells (dhowells@redhat.com).
4  *
5  * Derived from asm-i386/semaphore.h
6  *
7  *
8  * The MSW of the count is the negated number of active writers and waiting
9  * lockers, and the LSW is the total number of active locks
10  *
11  * The lock count is initialized to 0 (no active and no waiting lockers).
12  *
13  * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
14  * uncontended lock. This can be determined because XADD returns the old value.
15  * Readers increment by 1 and see a positive value when uncontended, negative
16  * if there are writers (and maybe) readers waiting (in which case it goes to
17  * sleep).
18  *
19  * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
20  * be extended to 65534 by manually checking the whole MSW rather than relying
21  * on the S flag.
22  *
23  * The value of ACTIVE_BIAS supports up to 65535 active processes.
24  *
25  * This should be totally fair - if anything is waiting, a process that wants a
26  * lock will go to the back of the queue. When the currently active lock is
27  * released, if there's a writer at the front of the queue, then that and only
28  * that will be woken up; if there's a bunch of consequtive readers at the
29  * front, then they'll all be woken up, but no other readers will be.
30  */
31
32 #ifndef _I386_RWSEM_H
33 #define _I386_RWSEM_H
34
35 #ifndef _LINUX_RWSEM_H
36 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
37 #endif
38
39 #ifdef __KERNEL__
40
41 #include <linux/list.h>
42 #include <linux/spinlock.h>
43
44 struct rwsem_waiter;
45
46 extern struct rw_semaphore *FASTCALL(rwsem_down_read_failed(struct rw_semaphore *sem));
47 extern struct rw_semaphore *FASTCALL(rwsem_down_write_failed(struct rw_semaphore *sem));
48 extern struct rw_semaphore *FASTCALL(rwsem_wake(struct rw_semaphore *));
49 extern struct rw_semaphore *FASTCALL(rwsem_downgrade_wake(struct rw_semaphore *sem));
50
51 /*
52  * the semaphore definition
53  */
54 struct rw_semaphore {
55         signed long             count;
56 #define RWSEM_UNLOCKED_VALUE            0x00000000
57 #define RWSEM_ACTIVE_BIAS               0x00000001
58 #define RWSEM_ACTIVE_MASK               0x0000ffff
59 #define RWSEM_WAITING_BIAS              (-0x00010000)
60 #define RWSEM_ACTIVE_READ_BIAS          RWSEM_ACTIVE_BIAS
61 #define RWSEM_ACTIVE_WRITE_BIAS         (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
62         spinlock_t              wait_lock;
63         struct list_head        wait_list;
64 };
65
66 #define __RWSEM_INITIALIZER(name) \
67 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
68         }
69
70 #define DECLARE_RWSEM(name) \
71         struct rw_semaphore name = __RWSEM_INITIALIZER(name)
72
73 static inline void init_rwsem(struct rw_semaphore *sem)
74 {
75         sem->count = RWSEM_UNLOCKED_VALUE;
76         spin_lock_init(&sem->wait_lock);
77         INIT_LIST_HEAD(&sem->wait_list);
78 }
79
80 /*
81  * lock for reading
82  */
83 static inline void __down_read(struct rw_semaphore *sem)
84 {
85         __asm__ __volatile__(
86                 "# beginning down_read\n\t"
87 LOCK_PREFIX     "  incl      (%%eax)\n\t" /* adds 0x00000001, returns the old value */
88                 "  js        2f\n\t" /* jump if we weren't granted the lock */
89                 "1:\n\t"
90                 LOCK_SECTION_START("")
91                 "2:\n\t"
92                 "  pushl     %%ecx\n\t"
93                 "  pushl     %%edx\n\t"
94                 "  call      rwsem_down_read_failed\n\t"
95                 "  popl      %%edx\n\t"
96                 "  popl      %%ecx\n\t"
97                 "  jmp       1b\n"
98                 LOCK_SECTION_END
99                 "# ending down_read\n\t"
100                 : "=m"(sem->count)
101                 : "a"(sem), "m"(sem->count)
102                 : "memory", "cc");
103 }
104
105 /*
106  * trylock for reading -- returns 1 if successful, 0 if contention
107  */
108 static inline int __down_read_trylock(struct rw_semaphore *sem)
109 {
110         __s32 result, tmp;
111         __asm__ __volatile__(
112                 "# beginning __down_read_trylock\n\t"
113                 "  movl      %0,%1\n\t"
114                 "1:\n\t"
115                 "  movl      %1,%2\n\t"
116                 "  addl      %3,%2\n\t"
117                 "  jle       2f\n\t"
118 LOCK_PREFIX     "  cmpxchgl  %2,%0\n\t"
119                 "  jnz       1b\n\t"
120                 "2:\n\t"
121                 "# ending __down_read_trylock\n\t"
122                 : "+m"(sem->count), "=&a"(result), "=&r"(tmp)
123                 : "i"(RWSEM_ACTIVE_READ_BIAS)
124                 : "memory", "cc");
125         return result>=0 ? 1 : 0;
126 }
127
128 /*
129  * lock for writing
130  */
131 static inline void __down_write(struct rw_semaphore *sem)
132 {
133         int tmp;
134
135         tmp = RWSEM_ACTIVE_WRITE_BIAS;
136         __asm__ __volatile__(
137                 "# beginning down_write\n\t"
138 LOCK_PREFIX     "  xadd      %%edx,(%%eax)\n\t" /* subtract 0x0000ffff, returns the old value */
139                 "  testl     %%edx,%%edx\n\t" /* was the count 0 before? */
140                 "  jnz       2f\n\t" /* jump if we weren't granted the lock */
141                 "1:\n\t"
142                 LOCK_SECTION_START("")
143                 "2:\n\t"
144                 "  pushl     %%ecx\n\t"
145                 "  call      rwsem_down_write_failed\n\t"
146                 "  popl      %%ecx\n\t"
147                 "  jmp       1b\n"
148                 LOCK_SECTION_END
149                 "# ending down_write"
150                 : "=m"(sem->count), "=d"(tmp)
151                 : "a"(sem), "1"(tmp), "m"(sem->count)
152                 : "memory", "cc");
153 }
154
155 /*
156  * trylock for writing -- returns 1 if successful, 0 if contention
157  */
158 static inline int __down_write_trylock(struct rw_semaphore *sem)
159 {
160         signed long ret = cmpxchg(&sem->count,
161                                   RWSEM_UNLOCKED_VALUE, 
162                                   RWSEM_ACTIVE_WRITE_BIAS);
163         if (ret == RWSEM_UNLOCKED_VALUE)
164                 return 1;
165         return 0;
166 }
167
168 /*
169  * unlock after reading
170  */
171 static inline void __up_read(struct rw_semaphore *sem)
172 {
173         __s32 tmp = -RWSEM_ACTIVE_READ_BIAS;
174         __asm__ __volatile__(
175                 "# beginning __up_read\n\t"
176 LOCK_PREFIX     "  xadd      %%edx,(%%eax)\n\t" /* subtracts 1, returns the old value */
177                 "  js        2f\n\t" /* jump if the lock is being waited upon */
178                 "1:\n\t"
179                 LOCK_SECTION_START("")
180                 "2:\n\t"
181                 "  decw      %%dx\n\t" /* do nothing if still outstanding active readers */
182                 "  jnz       1b\n\t"
183                 "  pushl     %%ecx\n\t"
184                 "  call      rwsem_wake\n\t"
185                 "  popl      %%ecx\n\t"
186                 "  jmp       1b\n"
187                 LOCK_SECTION_END
188                 "# ending __up_read\n"
189                 : "=m"(sem->count), "=d"(tmp)
190                 : "a"(sem), "1"(tmp), "m"(sem->count)
191                 : "memory", "cc");
192 }
193
194 /*
195  * unlock after writing
196  */
197 static inline void __up_write(struct rw_semaphore *sem)
198 {
199         __asm__ __volatile__(
200                 "# beginning __up_write\n\t"
201                 "  movl      %2,%%edx\n\t"
202 LOCK_PREFIX     "  xaddl     %%edx,(%%eax)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
203                 "  jnz       2f\n\t" /* jump if the lock is being waited upon */
204                 "1:\n\t"
205                 LOCK_SECTION_START("")
206                 "2:\n\t"
207                 "  decw      %%dx\n\t" /* did the active count reduce to 0? */
208                 "  jnz       1b\n\t" /* jump back if not */
209                 "  pushl     %%ecx\n\t"
210                 "  call      rwsem_wake\n\t"
211                 "  popl      %%ecx\n\t"
212                 "  jmp       1b\n"
213                 LOCK_SECTION_END
214                 "# ending __up_write\n"
215                 : "=m"(sem->count)
216                 : "a"(sem), "i"(-RWSEM_ACTIVE_WRITE_BIAS), "m"(sem->count)
217                 : "memory", "cc", "edx");
218 }
219
220 /*
221  * downgrade write lock to read lock
222  */
223 static inline void __downgrade_write(struct rw_semaphore *sem)
224 {
225         __asm__ __volatile__(
226                 "# beginning __downgrade_write\n\t"
227 LOCK_PREFIX     "  addl      %2,(%%eax)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */
228                 "  js        2f\n\t" /* jump if the lock is being waited upon */
229                 "1:\n\t"
230                 LOCK_SECTION_START("")
231                 "2:\n\t"
232                 "  pushl     %%ecx\n\t"
233                 "  pushl     %%edx\n\t"
234                 "  call      rwsem_downgrade_wake\n\t"
235                 "  popl      %%edx\n\t"
236                 "  popl      %%ecx\n\t"
237                 "  jmp       1b\n"
238                 LOCK_SECTION_END
239                 "# ending __downgrade_write\n"
240                 : "=m"(sem->count)
241                 : "a"(sem), "i"(-RWSEM_WAITING_BIAS), "m"(sem->count)
242                 : "memory", "cc");
243 }
244
245 /*
246  * implement atomic add functionality
247  */
248 static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
249 {
250         __asm__ __volatile__(
251 LOCK_PREFIX     "addl %1,%0"
252                 : "=m"(sem->count)
253                 : "ir"(delta), "m"(sem->count));
254 }
255
256 /*
257  * implement exchange and add functionality
258  */
259 static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
260 {
261         int tmp = delta;
262
263         __asm__ __volatile__(
264 LOCK_PREFIX     "xadd %0,(%2)"
265                 : "+r"(tmp), "=m"(sem->count)
266                 : "r"(sem), "m"(sem->count)
267                 : "memory");
268
269         return tmp+delta;
270 }
271
272 static inline int rwsem_is_locked(struct rw_semaphore *sem)
273 {
274         return (sem->count != 0);
275 }
276
277 #endif /* __KERNEL__ */
278 #endif /* _I386_RWSEM_H */