blob: 2f07601562e752a3a2646ffc67b8fba06e7c9c91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Ingo Molnar4ea21762006-07-03 00:24:53 -070043#include <linux/lockdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45struct rwsem_waiter;
46
47extern struct rw_semaphore *FASTCALL(rwsem_down_read_failed(struct rw_semaphore *sem));
48extern struct rw_semaphore *FASTCALL(rwsem_down_write_failed(struct rw_semaphore *sem));
49extern struct rw_semaphore *FASTCALL(rwsem_wake(struct rw_semaphore *));
50extern struct rw_semaphore *FASTCALL(rwsem_downgrade_wake(struct rw_semaphore *sem));
51
52/*
53 * the semaphore definition
54 */
55struct rw_semaphore {
56 signed long count;
57#define RWSEM_UNLOCKED_VALUE 0x00000000
58#define RWSEM_ACTIVE_BIAS 0x00000001
59#define RWSEM_ACTIVE_MASK 0x0000ffff
60#define RWSEM_WAITING_BIAS (-0x00010000)
61#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
62#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
63 spinlock_t wait_lock;
64 struct list_head wait_list;
Ingo Molnar4ea21762006-07-03 00:24:53 -070065#ifdef CONFIG_DEBUG_LOCK_ALLOC
66 struct lockdep_map dep_map;
67#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Ingo Molnar4ea21762006-07-03 00:24:53 -070070#ifdef CONFIG_DEBUG_LOCK_ALLOC
71# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
72#else
73# define __RWSEM_DEP_MAP_INIT(lockname)
74#endif
75
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define __RWSEM_INITIALIZER(name) \
78{ RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
Ingo Molnar4ea21762006-07-03 00:24:53 -070079 __RWSEM_DEP_MAP_INIT(name) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#define DECLARE_RWSEM(name) \
82 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
83
Ingo Molnar4ea21762006-07-03 00:24:53 -070084extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
85 struct lock_class_key *key);
86
87#define init_rwsem(sem) \
88do { \
89 static struct lock_class_key __key; \
90 \
91 __init_rwsem((sem), #sem, &__key); \
92} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * lock for reading
96 */
97static inline void __down_read(struct rw_semaphore *sem)
98{
99 __asm__ __volatile__(
100 "# beginning down_read\n\t"
101LOCK_PREFIX " incl (%%eax)\n\t" /* adds 0x00000001, returns the old value */
102 " js 2f\n\t" /* jump if we weren't granted the lock */
103 "1:\n\t"
104 LOCK_SECTION_START("")
105 "2:\n\t"
106 " pushl %%ecx\n\t"
107 " pushl %%edx\n\t"
108 " call rwsem_down_read_failed\n\t"
109 " popl %%edx\n\t"
110 " popl %%ecx\n\t"
111 " jmp 1b\n"
112 LOCK_SECTION_END
113 "# ending down_read\n\t"
114 : "=m"(sem->count)
115 : "a"(sem), "m"(sem->count)
116 : "memory", "cc");
117}
118
119/*
120 * trylock for reading -- returns 1 if successful, 0 if contention
121 */
122static inline int __down_read_trylock(struct rw_semaphore *sem)
123{
124 __s32 result, tmp;
125 __asm__ __volatile__(
126 "# beginning __down_read_trylock\n\t"
127 " movl %0,%1\n\t"
128 "1:\n\t"
129 " movl %1,%2\n\t"
130 " addl %3,%2\n\t"
131 " jle 2f\n\t"
132LOCK_PREFIX " cmpxchgl %2,%0\n\t"
133 " jnz 1b\n\t"
134 "2:\n\t"
135 "# ending __down_read_trylock\n\t"
136 : "+m"(sem->count), "=&a"(result), "=&r"(tmp)
137 : "i"(RWSEM_ACTIVE_READ_BIAS)
138 : "memory", "cc");
139 return result>=0 ? 1 : 0;
140}
141
142/*
143 * lock for writing
144 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700145static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 int tmp;
148
149 tmp = RWSEM_ACTIVE_WRITE_BIAS;
150 __asm__ __volatile__(
151 "# beginning down_write\n\t"
152LOCK_PREFIX " xadd %%edx,(%%eax)\n\t" /* subtract 0x0000ffff, returns the old value */
153 " testl %%edx,%%edx\n\t" /* was the count 0 before? */
154 " jnz 2f\n\t" /* jump if we weren't granted the lock */
155 "1:\n\t"
156 LOCK_SECTION_START("")
157 "2:\n\t"
158 " pushl %%ecx\n\t"
159 " call rwsem_down_write_failed\n\t"
160 " popl %%ecx\n\t"
161 " jmp 1b\n"
162 LOCK_SECTION_END
163 "# ending down_write"
164 : "=m"(sem->count), "=d"(tmp)
165 : "a"(sem), "1"(tmp), "m"(sem->count)
166 : "memory", "cc");
167}
168
Ingo Molnar4ea21762006-07-03 00:24:53 -0700169static inline void __down_write(struct rw_semaphore *sem)
170{
171 __down_write_nested(sem, 0);
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
175 * trylock for writing -- returns 1 if successful, 0 if contention
176 */
177static inline int __down_write_trylock(struct rw_semaphore *sem)
178{
179 signed long ret = cmpxchg(&sem->count,
180 RWSEM_UNLOCKED_VALUE,
181 RWSEM_ACTIVE_WRITE_BIAS);
182 if (ret == RWSEM_UNLOCKED_VALUE)
183 return 1;
184 return 0;
185}
186
187/*
188 * unlock after reading
189 */
190static inline void __up_read(struct rw_semaphore *sem)
191{
192 __s32 tmp = -RWSEM_ACTIVE_READ_BIAS;
193 __asm__ __volatile__(
194 "# beginning __up_read\n\t"
195LOCK_PREFIX " xadd %%edx,(%%eax)\n\t" /* subtracts 1, returns the old value */
196 " js 2f\n\t" /* jump if the lock is being waited upon */
197 "1:\n\t"
198 LOCK_SECTION_START("")
199 "2:\n\t"
200 " decw %%dx\n\t" /* do nothing if still outstanding active readers */
201 " jnz 1b\n\t"
202 " pushl %%ecx\n\t"
203 " call rwsem_wake\n\t"
204 " popl %%ecx\n\t"
205 " jmp 1b\n"
206 LOCK_SECTION_END
207 "# ending __up_read\n"
208 : "=m"(sem->count), "=d"(tmp)
209 : "a"(sem), "1"(tmp), "m"(sem->count)
210 : "memory", "cc");
211}
212
213/*
214 * unlock after writing
215 */
216static inline void __up_write(struct rw_semaphore *sem)
217{
218 __asm__ __volatile__(
219 "# beginning __up_write\n\t"
220 " movl %2,%%edx\n\t"
221LOCK_PREFIX " xaddl %%edx,(%%eax)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
222 " jnz 2f\n\t" /* jump if the lock is being waited upon */
223 "1:\n\t"
224 LOCK_SECTION_START("")
225 "2:\n\t"
226 " decw %%dx\n\t" /* did the active count reduce to 0? */
227 " jnz 1b\n\t" /* jump back if not */
228 " pushl %%ecx\n\t"
229 " call rwsem_wake\n\t"
230 " popl %%ecx\n\t"
231 " jmp 1b\n"
232 LOCK_SECTION_END
233 "# ending __up_write\n"
234 : "=m"(sem->count)
235 : "a"(sem), "i"(-RWSEM_ACTIVE_WRITE_BIAS), "m"(sem->count)
236 : "memory", "cc", "edx");
237}
238
239/*
240 * downgrade write lock to read lock
241 */
242static inline void __downgrade_write(struct rw_semaphore *sem)
243{
244 __asm__ __volatile__(
245 "# beginning __downgrade_write\n\t"
246LOCK_PREFIX " addl %2,(%%eax)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */
247 " js 2f\n\t" /* jump if the lock is being waited upon */
248 "1:\n\t"
249 LOCK_SECTION_START("")
250 "2:\n\t"
251 " pushl %%ecx\n\t"
252 " pushl %%edx\n\t"
253 " call rwsem_downgrade_wake\n\t"
254 " popl %%edx\n\t"
255 " popl %%ecx\n\t"
256 " jmp 1b\n"
257 LOCK_SECTION_END
258 "# ending __downgrade_write\n"
259 : "=m"(sem->count)
260 : "a"(sem), "i"(-RWSEM_WAITING_BIAS), "m"(sem->count)
261 : "memory", "cc");
262}
263
264/*
265 * implement atomic add functionality
266 */
267static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
268{
269 __asm__ __volatile__(
270LOCK_PREFIX "addl %1,%0"
271 : "=m"(sem->count)
272 : "ir"(delta), "m"(sem->count));
273}
274
275/*
276 * implement exchange and add functionality
277 */
278static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
279{
280 int tmp = delta;
281
282 __asm__ __volatile__(
283LOCK_PREFIX "xadd %0,(%2)"
284 : "+r"(tmp), "=m"(sem->count)
285 : "r"(sem), "m"(sem->count)
286 : "memory");
287
288 return tmp+delta;
289}
290
Rik Van Rieleb92f4e2005-10-29 18:15:44 -0700291static inline int rwsem_is_locked(struct rw_semaphore *sem)
292{
293 return (sem->count != 0);
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296#endif /* __KERNEL__ */
297#endif /* _I386_RWSEM_H */