1 /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for i486+
3 * Written by David Howells (dhowells@redhat.com).
5 * Derived from asm-i386/semaphore.h
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
11 * The lock count is initialized to 0 (no active and no waiting lockers).
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
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
23 * The value of ACTIVE_BIAS supports up to 65535 active processes.
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.
35 #ifndef _LINUX_RWSEM_H
36 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
41 #include <linux/list.h>
42 #include <linux/spinlock.h>
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));
52 * the semaphore definition
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)
63 struct list_head wait_list;
66 #define __RWSEM_INITIALIZER(name) \
67 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
70 #define DECLARE_RWSEM(name) \
71 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
73 static inline void init_rwsem(struct rw_semaphore *sem)
75 sem->count = RWSEM_UNLOCKED_VALUE;
76 spin_lock_init(&sem->wait_lock);
77 INIT_LIST_HEAD(&sem->wait_list);
83 static inline void __down_read(struct rw_semaphore *sem)
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 */
90 LOCK_SECTION_START("")
94 " call rwsem_down_read_failed\n\t"
99 "# ending down_read\n\t"
101 : "a"(sem), "m"(sem->count)
106 * trylock for reading -- returns 1 if successful, 0 if contention
108 static inline int __down_read_trylock(struct rw_semaphore *sem)
111 __asm__ __volatile__(
112 "# beginning __down_read_trylock\n\t"
118 LOCK_PREFIX " cmpxchgl %2,%0\n\t"
121 "# ending __down_read_trylock\n\t"
122 : "+m"(sem->count), "=&a"(result), "=&r"(tmp)
123 : "i"(RWSEM_ACTIVE_READ_BIAS)
125 return result>=0 ? 1 : 0;
131 static inline void __down_write(struct rw_semaphore *sem)
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 */
142 LOCK_SECTION_START("")
145 " call rwsem_down_write_failed\n\t"
149 "# ending down_write"
150 : "=m"(sem->count), "=d"(tmp)
151 : "a"(sem), "1"(tmp), "m"(sem->count)
156 * trylock for writing -- returns 1 if successful, 0 if contention
158 static inline int __down_write_trylock(struct rw_semaphore *sem)
160 signed long ret = cmpxchg(&sem->count,
161 RWSEM_UNLOCKED_VALUE,
162 RWSEM_ACTIVE_WRITE_BIAS);
163 if (ret == RWSEM_UNLOCKED_VALUE)
169 * unlock after reading
171 static inline void __up_read(struct rw_semaphore *sem)
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 */
179 LOCK_SECTION_START("")
181 " decw %%dx\n\t" /* do nothing if still outstanding active readers */
184 " call rwsem_wake\n\t"
188 "# ending __up_read\n"
189 : "=m"(sem->count), "=d"(tmp)
190 : "a"(sem), "1"(tmp), "m"(sem->count)
195 * unlock after writing
197 static inline void __up_write(struct rw_semaphore *sem)
199 __asm__ __volatile__(
200 "# beginning __up_write\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 */
205 LOCK_SECTION_START("")
207 " decw %%dx\n\t" /* did the active count reduce to 0? */
208 " jnz 1b\n\t" /* jump back if not */
210 " call rwsem_wake\n\t"
214 "# ending __up_write\n"
216 : "a"(sem), "i"(-RWSEM_ACTIVE_WRITE_BIAS), "m"(sem->count)
217 : "memory", "cc", "edx");
221 * downgrade write lock to read lock
223 static inline void __downgrade_write(struct rw_semaphore *sem)
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 */
230 LOCK_SECTION_START("")
234 " call rwsem_downgrade_wake\n\t"
239 "# ending __downgrade_write\n"
241 : "a"(sem), "i"(-RWSEM_WAITING_BIAS), "m"(sem->count)
246 * implement atomic add functionality
248 static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
250 __asm__ __volatile__(
251 LOCK_PREFIX "addl %1,%0"
253 : "ir"(delta), "m"(sem->count));
257 * implement exchange and add functionality
259 static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
263 __asm__ __volatile__(
264 LOCK_PREFIX "xadd %0,(%2)"
265 : "+r"(tmp), "=m"(sem->count)
266 : "r"(sem), "m"(sem->count)
272 static inline int rwsem_is_locked(struct rw_semaphore *sem)
274 return (sem->count != 0);
277 #endif /* __KERNEL__ */
278 #endif /* _I386_RWSEM_H */