1 /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for x86_64+
3 * Written by David Howells (dhowells@redhat.com).
4 * Ported by Andi Kleen <ak@suse.de> to x86-64.
6 * Derived from asm-i386/semaphore.h and asm-i386/rwsem.h
9 * The MSW of the count is the negated number of active writers and waiting
10 * lockers, and the LSW is the total number of active locks
12 * The lock count is initialized to 0 (no active and no waiting lockers).
14 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
15 * uncontended lock. This can be determined because XADD returns the old value.
16 * Readers increment by 1 and see a positive value when uncontended, negative
17 * if there are writers (and maybe) readers waiting (in which case it goes to
20 * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
21 * be extended to 65534 by manually checking the whole MSW rather than relying
24 * The value of ACTIVE_BIAS supports up to 65535 active processes.
26 * This should be totally fair - if anything is waiting, a process that wants a
27 * lock will go to the back of the queue. When the currently active lock is
28 * released, if there's a writer at the front of the queue, then that and only
29 * that will be woken up; if there's a bunch of consecutive readers at the
30 * front, then they'll all be woken up, but no other readers will be.
33 #ifndef _X8664_RWSEM_H
34 #define _X8664_RWSEM_H
36 #ifndef _LINUX_RWSEM_H
37 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
42 #include <linux/list.h>
43 #include <linux/spinlock.h>
47 extern struct rw_semaphore
*rwsem_down_read_failed(struct rw_semaphore
*sem
);
48 extern struct rw_semaphore
*rwsem_down_write_failed(struct rw_semaphore
*sem
);
49 extern struct rw_semaphore
*rwsem_wake(struct rw_semaphore
*);
50 extern struct rw_semaphore
*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
;
74 #define __RWSEM_DEBUG_INIT , 0
76 #define __RWSEM_DEBUG_INIT /* */
79 #define __RWSEM_INITIALIZER(name) \
80 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
83 #define DECLARE_RWSEM(name) \
84 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
86 static inline void init_rwsem(struct rw_semaphore
*sem
)
88 sem
->count
= RWSEM_UNLOCKED_VALUE
;
89 spin_lock_init(&sem
->wait_lock
);
90 INIT_LIST_HEAD(&sem
->wait_list
);
99 static inline void __down_read(struct rw_semaphore
*sem
)
101 __asm__
__volatile__(
102 "# beginning down_read\n\t"
103 LOCK_PREFIX
" incl (%%rdi)\n\t" /* adds 0x00000001, returns the old value */
104 " js 2f\n\t" /* jump if we weren't granted the lock */
106 LOCK_SECTION_START("") \
108 " call rwsem_down_read_failed_thunk\n\t"
111 "# ending down_read\n\t"
119 * trylock for reading -- returns 1 if successful, 0 if contention
121 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
124 __asm__
__volatile__(
125 "# beginning __down_read_trylock\n\t"
131 LOCK_PREFIX
" cmpxchgl %2,%0\n\t"
134 "# ending __down_read_trylock\n\t"
135 : "+m"(sem
->count
), "=&a"(result
), "=&r"(tmp
)
136 : "i"(RWSEM_ACTIVE_READ_BIAS
)
138 return result
>=0 ? 1 : 0;
145 static inline void __down_write(struct rw_semaphore
*sem
)
149 tmp
= RWSEM_ACTIVE_WRITE_BIAS
;
150 __asm__
__volatile__(
151 "# beginning down_write\n\t"
152 LOCK_PREFIX
" xaddl %0,(%%rdi)\n\t" /* subtract 0x0000ffff, returns the old value */
153 " testl %0,%0\n\t" /* was the count 0 before? */
154 " jnz 2f\n\t" /* jump if we weren't granted the lock */
156 LOCK_SECTION_START("")
158 " call rwsem_down_write_failed_thunk\n\t"
161 "# ending down_write"
168 * trylock for writing -- returns 1 if successful, 0 if contention
170 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
172 signed long ret
= cmpxchg(&sem
->count
,
173 RWSEM_UNLOCKED_VALUE
,
174 RWSEM_ACTIVE_WRITE_BIAS
);
175 if (ret
== RWSEM_UNLOCKED_VALUE
)
181 * unlock after reading
183 static inline void __up_read(struct rw_semaphore
*sem
)
185 __s32 tmp
= -RWSEM_ACTIVE_READ_BIAS
;
186 __asm__
__volatile__(
187 "# beginning __up_read\n\t"
188 LOCK_PREFIX
" xaddl %[tmp],(%%rdi)\n\t" /* subtracts 1, returns the old value */
189 " js 2f\n\t" /* jump if the lock is being waited upon */
191 LOCK_SECTION_START("")
193 " decw %w[tmp]\n\t" /* do nothing if still outstanding active readers */
195 " call rwsem_wake_thunk\n\t"
198 "# ending __up_read\n"
199 : "+m"(sem
->count
), [tmp
] "+r" (tmp
)
205 * unlock after writing
207 static inline void __up_write(struct rw_semaphore
*sem
)
210 __asm__
__volatile__(
211 "# beginning __up_write\n\t"
212 " movl %[bias],%[tmp]\n\t"
213 LOCK_PREFIX
" xaddl %[tmp],(%%rdi)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
214 " jnz 2f\n\t" /* jump if the lock is being waited upon */
216 LOCK_SECTION_START("")
218 " decw %w[tmp]\n\t" /* did the active count reduce to 0? */
219 " jnz 1b\n\t" /* jump back if not */
220 " call rwsem_wake_thunk\n\t"
223 "# ending __up_write\n"
224 : "+m"(sem
->count
), [tmp
] "=r" (tmp
)
225 : "D"(sem
), [bias
] "i"(-RWSEM_ACTIVE_WRITE_BIAS
)
230 * downgrade write lock to read lock
232 static inline void __downgrade_write(struct rw_semaphore
*sem
)
234 __asm__
__volatile__(
235 "# beginning __downgrade_write\n\t"
236 LOCK_PREFIX
" addl %[bias],(%%rdi)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */
237 " js 2f\n\t" /* jump if the lock is being waited upon */
239 LOCK_SECTION_START("")
241 " call rwsem_downgrade_thunk\n"
244 "# ending __downgrade_write\n"
246 : "D"(sem
), [bias
] "i"(-RWSEM_WAITING_BIAS
), "m"(sem
->count
)
251 * implement atomic add functionality
253 static inline void rwsem_atomic_add(int delta
, struct rw_semaphore
*sem
)
255 __asm__
__volatile__(
256 LOCK_PREFIX
"addl %1,%0"
258 :"ir"(delta
), "m"(sem
->count
));
262 * implement exchange and add functionality
264 static inline int rwsem_atomic_update(int delta
, struct rw_semaphore
*sem
)
268 __asm__
__volatile__(
269 LOCK_PREFIX
"xaddl %0,(%2)"
270 : "=r"(tmp
), "=m"(sem
->count
)
271 : "r"(sem
), "m"(sem
->count
), "0" (tmp
)
277 #endif /* __KERNEL__ */
278 #endif /* _X8664_RWSEM_H */