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
;
73 #define __RWSEM_DEBUG_INIT , 0
75 #define __RWSEM_DEBUG_INIT /* */
78 #define __RWSEM_INITIALIZER(name) \
79 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
82 #define DECLARE_RWSEM(name) \
83 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
85 static inline void init_rwsem(struct rw_semaphore
*sem
)
87 sem
->count
= RWSEM_UNLOCKED_VALUE
;
88 spin_lock_init(&sem
->wait_lock
);
89 INIT_LIST_HEAD(&sem
->wait_list
);
98 static inline void __down_read(struct rw_semaphore
*sem
)
100 __asm__
__volatile__(
101 "# beginning down_read\n\t"
102 LOCK_PREFIX
" incl (%%eax)\n\t" /* adds 0x00000001, returns the old value */
103 " js 2f\n\t" /* jump if we weren't granted the lock */
105 LOCK_SECTION_START("")
109 " call rwsem_down_read_failed\n\t"
114 "# ending down_read\n\t"
116 : "a"(sem
), "m"(sem
->count
)
121 * trylock for reading -- returns 1 if successful, 0 if contention
123 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
126 __asm__
__volatile__(
127 "# beginning __down_read_trylock\n\t"
133 LOCK_PREFIX
" cmpxchgl %2,%0\n\t"
136 "# ending __down_read_trylock\n\t"
137 : "+m"(sem
->count
), "=&a"(result
), "=&r"(tmp
)
138 : "i"(RWSEM_ACTIVE_READ_BIAS
)
140 return result
>=0 ? 1 : 0;
146 static inline void __down_write(struct rw_semaphore
*sem
)
150 tmp
= RWSEM_ACTIVE_WRITE_BIAS
;
151 __asm__
__volatile__(
152 "# beginning down_write\n\t"
153 LOCK_PREFIX
" xadd %%edx,(%%eax)\n\t" /* subtract 0x0000ffff, returns the old value */
154 " testl %%edx,%%edx\n\t" /* was the count 0 before? */
155 " jnz 2f\n\t" /* jump if we weren't granted the lock */
157 LOCK_SECTION_START("")
160 " call rwsem_down_write_failed\n\t"
164 "# ending down_write"
165 : "=m"(sem
->count
), "=d"(tmp
)
166 : "a"(sem
), "1"(tmp
), "m"(sem
->count
)
171 * trylock for writing -- returns 1 if successful, 0 if contention
173 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
175 signed long ret
= cmpxchg(&sem
->count
,
176 RWSEM_UNLOCKED_VALUE
,
177 RWSEM_ACTIVE_WRITE_BIAS
);
178 if (ret
== RWSEM_UNLOCKED_VALUE
)
184 * unlock after reading
186 static inline void __up_read(struct rw_semaphore
*sem
)
188 __s32 tmp
= -RWSEM_ACTIVE_READ_BIAS
;
189 __asm__
__volatile__(
190 "# beginning __up_read\n\t"
191 LOCK_PREFIX
" xadd %%edx,(%%eax)\n\t" /* subtracts 1, returns the old value */
192 " js 2f\n\t" /* jump if the lock is being waited upon */
194 LOCK_SECTION_START("")
196 " decw %%dx\n\t" /* do nothing if still outstanding active readers */
199 " call rwsem_wake\n\t"
203 "# ending __up_read\n"
204 : "=m"(sem
->count
), "=d"(tmp
)
205 : "a"(sem
), "1"(tmp
), "m"(sem
->count
)
210 * unlock after writing
212 static inline void __up_write(struct rw_semaphore
*sem
)
214 __asm__
__volatile__(
215 "# beginning __up_write\n\t"
217 LOCK_PREFIX
" xaddl %%edx,(%%eax)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
218 " jnz 2f\n\t" /* jump if the lock is being waited upon */
220 LOCK_SECTION_START("")
222 " decw %%dx\n\t" /* did the active count reduce to 0? */
223 " jnz 1b\n\t" /* jump back if not */
225 " call rwsem_wake\n\t"
229 "# ending __up_write\n"
231 : "a"(sem
), "i"(-RWSEM_ACTIVE_WRITE_BIAS
), "m"(sem
->count
)
232 : "memory", "cc", "edx");
236 * downgrade write lock to read lock
238 static inline void __downgrade_write(struct rw_semaphore
*sem
)
240 __asm__
__volatile__(
241 "# beginning __downgrade_write\n\t"
242 LOCK_PREFIX
" addl %2,(%%eax)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */
243 " js 2f\n\t" /* jump if the lock is being waited upon */
245 LOCK_SECTION_START("")
249 " call rwsem_downgrade_wake\n\t"
254 "# ending __downgrade_write\n"
256 : "a"(sem
), "i"(-RWSEM_WAITING_BIAS
), "m"(sem
->count
)
261 * implement atomic add functionality
263 static inline void rwsem_atomic_add(int delta
, struct rw_semaphore
*sem
)
265 __asm__
__volatile__(
266 LOCK_PREFIX
"addl %1,%0"
268 : "ir"(delta
), "m"(sem
->count
));
272 * implement exchange and add functionality
274 static inline int rwsem_atomic_update(int delta
, struct rw_semaphore
*sem
)
278 __asm__
__volatile__(
279 LOCK_PREFIX
"xadd %0,(%2)"
280 : "+r"(tmp
), "=m"(sem
->count
)
281 : "r"(sem
), "m"(sem
->count
)
287 static inline int rwsem_is_locked(struct rw_semaphore
*sem
)
289 return (sem
->count
!= 0);
292 #endif /* __KERNEL__ */
293 #endif /* _I386_RWSEM_H */