1 /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for i486+
3 * Written by David Howells (dhowells@redhat.com).
5 * Derived from asm-x86/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.
32 #ifndef _ASM_X86_RWSEM_H
33 #define _ASM_X86_RWSEM_H
35 #ifndef _LINUX_RWSEM_H
36 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
43 * The bias values and the counter type limits the number of
44 * potential readers/writers to 32767 for 32 bits and 2147483647
49 # define RWSEM_ACTIVE_MASK 0xffffffffL
51 # define RWSEM_ACTIVE_MASK 0x0000ffffL
54 #define RWSEM_UNLOCKED_VALUE 0x00000000L
55 #define RWSEM_ACTIVE_BIAS 0x00000001L
56 #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
57 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
58 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
63 static inline void __down_read(struct rw_semaphore
*sem
)
65 asm volatile("# beginning down_read\n\t"
66 LOCK_PREFIX _ASM_INC
"(%1)\n\t"
69 " call call_rwsem_down_read_failed\n"
71 "# ending down_read\n\t"
78 * trylock for reading -- returns 1 if successful, 0 if contention
80 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
83 asm volatile("# beginning __down_read_trylock\n\t"
89 LOCK_PREFIX
" cmpxchg %2,%0\n\t"
92 "# ending __down_read_trylock\n\t"
93 : "+m" (sem
->count
), "=&a" (result
), "=&r" (tmp
)
94 : "i" (RWSEM_ACTIVE_READ_BIAS
)
96 return result
>= 0 ? 1 : 0;
102 static inline void __down_write_nested(struct rw_semaphore
*sem
, int subclass
)
105 asm volatile("# beginning down_write\n\t"
106 LOCK_PREFIX
" xadd %1,(%2)\n\t"
107 /* adds 0xffff0001, returns the old value */
108 " test " __ASM_SEL(%w1
,%k1
) "," __ASM_SEL(%w1
,%k1
) "\n\t"
109 /* was the active mask 0 before? */
111 " call call_rwsem_down_write_failed\n"
113 "# ending down_write"
114 : "+m" (sem
->count
), "=d" (tmp
)
115 : "a" (sem
), "1" (RWSEM_ACTIVE_WRITE_BIAS
)
119 static inline void __down_write(struct rw_semaphore
*sem
)
121 __down_write_nested(sem
, 0);
125 * trylock for writing -- returns 1 if successful, 0 if contention
127 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
130 asm volatile("# beginning __down_write_trylock\n\t"
133 " test " __ASM_SEL(%w1
,%k1
) "," __ASM_SEL(%w1
,%k1
) "\n\t"
134 /* was the active mask 0 before? */
138 LOCK_PREFIX
" cmpxchg %2,%0\n\t"
142 " movzbl %b1, %k1\n\t"
143 "# ending __down_write_trylock\n\t"
144 : "+m" (sem
->count
), "=&a" (result
), "=&r" (tmp
)
145 : "er" (RWSEM_ACTIVE_WRITE_BIAS
)
151 * unlock after reading
153 static inline void __up_read(struct rw_semaphore
*sem
)
156 asm volatile("# beginning __up_read\n\t"
157 LOCK_PREFIX
" xadd %1,(%2)\n\t"
158 /* subtracts 1, returns the old value */
160 " call call_rwsem_wake\n" /* expects old value in %edx */
162 "# ending __up_read\n"
163 : "+m" (sem
->count
), "=d" (tmp
)
164 : "a" (sem
), "1" (-RWSEM_ACTIVE_READ_BIAS
)
169 * unlock after writing
171 static inline void __up_write(struct rw_semaphore
*sem
)
174 asm volatile("# beginning __up_write\n\t"
175 LOCK_PREFIX
" xadd %1,(%2)\n\t"
176 /* subtracts 0xffff0001, returns the old value */
178 " call call_rwsem_wake\n" /* expects old value in %edx */
180 "# ending __up_write\n"
181 : "+m" (sem
->count
), "=d" (tmp
)
182 : "a" (sem
), "1" (-RWSEM_ACTIVE_WRITE_BIAS
)
187 * downgrade write lock to read lock
189 static inline void __downgrade_write(struct rw_semaphore
*sem
)
191 asm volatile("# beginning __downgrade_write\n\t"
192 LOCK_PREFIX _ASM_ADD
"%2,(%1)\n\t"
194 * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
195 * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
198 " call call_rwsem_downgrade_wake\n"
200 "# ending __downgrade_write\n"
202 : "a" (sem
), "er" (-RWSEM_WAITING_BIAS
)
207 * implement atomic add functionality
209 static inline void rwsem_atomic_add(long delta
, struct rw_semaphore
*sem
)
211 asm volatile(LOCK_PREFIX _ASM_ADD
"%1,%0"
217 * implement exchange and add functionality
219 static inline long rwsem_atomic_update(long delta
, struct rw_semaphore
*sem
)
221 return delta
+ xadd(&sem
->count
, delta
);
224 #endif /* __KERNEL__ */
225 #endif /* _ASM_X86_RWSEM_H */