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>
43 #include <linux/lockdep.h>
47 extern struct rw_semaphore
*FASTCALL(rwsem_down_read_failed(struct rw_semaphore
*sem
));
48 extern struct rw_semaphore
*FASTCALL(rwsem_down_write_failed(struct rw_semaphore
*sem
));
49 extern struct rw_semaphore
*FASTCALL(rwsem_wake(struct rw_semaphore
*));
50 extern struct rw_semaphore
*FASTCALL(rwsem_downgrade_wake(struct rw_semaphore
*sem
));
53 * the semaphore definition
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)
64 struct list_head wait_list
;
65 #ifdef CONFIG_DEBUG_LOCK_ALLOC
66 struct lockdep_map dep_map
;
70 #ifdef CONFIG_DEBUG_LOCK_ALLOC
71 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
73 # define __RWSEM_DEP_MAP_INIT(lockname)
77 #define __RWSEM_INITIALIZER(name) \
78 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
79 __RWSEM_DEP_MAP_INIT(name) }
81 #define DECLARE_RWSEM(name) \
82 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
84 extern void __init_rwsem(struct rw_semaphore
*sem
, const char *name
,
85 struct lock_class_key
*key
);
87 #define init_rwsem(sem) \
89 static struct lock_class_key __key; \
91 __init_rwsem((sem), #sem, &__key); \
97 static inline void __down_read(struct rw_semaphore
*sem
)
100 "# beginning down_read\n\t"
101 LOCK_PREFIX
" incl (%%eax)\n\t" /* adds 0x00000001, returns the old value */
102 " js 2f\n\t" /* jump if we weren't granted the lock */
104 LOCK_SECTION_START("")
108 " call rwsem_down_read_failed\n\t"
113 "# ending down_read\n\t"
120 * trylock for reading -- returns 1 if successful, 0 if contention
122 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
125 __asm__
__volatile__(
126 "# beginning __down_read_trylock\n\t"
132 LOCK_PREFIX
" cmpxchgl %2,%0\n\t"
135 "# ending __down_read_trylock\n\t"
136 : "+m" (sem
->count
), "=&a" (result
), "=&r" (tmp
)
137 : "i" (RWSEM_ACTIVE_READ_BIAS
)
139 return result
>=0 ? 1 : 0;
145 static inline void __down_write_nested(struct rw_semaphore
*sem
, int subclass
)
149 tmp
= RWSEM_ACTIVE_WRITE_BIAS
;
150 __asm__
__volatile__(
151 "# beginning down_write\n\t"
152 LOCK_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 */
156 LOCK_SECTION_START("")
159 " call rwsem_down_write_failed\n\t"
163 "# ending down_write"
164 : "+m" (sem
->count
), "=d" (tmp
)
165 : "a" (sem
), "1" (tmp
)
169 static inline void __down_write(struct rw_semaphore
*sem
)
171 __down_write_nested(sem
, 0);
175 * trylock for writing -- returns 1 if successful, 0 if contention
177 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
179 signed long ret
= cmpxchg(&sem
->count
,
180 RWSEM_UNLOCKED_VALUE
,
181 RWSEM_ACTIVE_WRITE_BIAS
);
182 if (ret
== RWSEM_UNLOCKED_VALUE
)
188 * unlock after reading
190 static inline void __up_read(struct rw_semaphore
*sem
)
192 __s32 tmp
= -RWSEM_ACTIVE_READ_BIAS
;
193 __asm__
__volatile__(
194 "# beginning __up_read\n\t"
195 LOCK_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 */
198 LOCK_SECTION_START("")
200 " decw %%dx\n\t" /* do nothing if still outstanding active readers */
203 " call rwsem_wake\n\t"
207 "# ending __up_read\n"
208 : "+m" (sem
->count
), "=d" (tmp
)
209 : "a" (sem
), "1" (tmp
)
214 * unlock after writing
216 static inline void __up_write(struct rw_semaphore
*sem
)
218 __asm__
__volatile__(
219 "# beginning __up_write\n\t"
221 LOCK_PREFIX
" xaddl %%edx,(%%eax)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
222 " jnz 2f\n\t" /* jump if the lock is being waited upon */
224 LOCK_SECTION_START("")
226 " decw %%dx\n\t" /* did the active count reduce to 0? */
227 " jnz 1b\n\t" /* jump back if not */
229 " call rwsem_wake\n\t"
233 "# ending __up_write\n"
235 : "a" (sem
), "i" (-RWSEM_ACTIVE_WRITE_BIAS
)
236 : "memory", "cc", "edx");
240 * downgrade write lock to read lock
242 static inline void __downgrade_write(struct rw_semaphore
*sem
)
244 __asm__
__volatile__(
245 "# beginning __downgrade_write\n\t"
246 LOCK_PREFIX
" addl %2,(%%eax)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */
247 " js 2f\n\t" /* jump if the lock is being waited upon */
249 LOCK_SECTION_START("")
253 " call rwsem_downgrade_wake\n\t"
258 "# ending __downgrade_write\n"
260 : "a" (sem
), "i" (-RWSEM_WAITING_BIAS
)
265 * implement atomic add functionality
267 static inline void rwsem_atomic_add(int delta
, struct rw_semaphore
*sem
)
269 __asm__
__volatile__(
270 LOCK_PREFIX
"addl %1,%0"
276 * implement exchange and add functionality
278 static inline int rwsem_atomic_update(int delta
, struct rw_semaphore
*sem
)
282 __asm__
__volatile__(
283 LOCK_PREFIX
"xadd %0,%1"
284 : "+r" (tmp
), "+m" (sem
->count
)
290 static inline int rwsem_is_locked(struct rw_semaphore
*sem
)
292 return (sem
->count
!= 0);
295 #endif /* __KERNEL__ */
296 #endif /* _I386_RWSEM_H */