5 * include/asm-s390/rwsem.h
8 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
9 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
11 * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
16 * The MSW of the count is the negated number of active writers and waiting
17 * lockers, and the LSW is the total number of active locks
19 * The lock count is initialized to 0 (no active and no waiting lockers).
21 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
22 * uncontended lock. This can be determined because XADD returns the old value.
23 * Readers increment by 1 and see a positive value when uncontended, negative
24 * if there are writers (and maybe) readers waiting (in which case it goes to
27 * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
28 * be extended to 65534 by manually checking the whole MSW rather than relying
31 * The value of ACTIVE_BIAS supports up to 65535 active processes.
33 * This should be totally fair - if anything is waiting, a process that wants a
34 * lock will go to the back of the queue. When the currently active lock is
35 * released, if there's a writer at the front of the queue, then that and only
36 * that will be woken up; if there's a bunch of consequtive readers at the
37 * front, then they'll all be woken up, but no other readers will be.
40 #ifndef _LINUX_RWSEM_H
41 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
45 #define RWSEM_UNLOCKED_VALUE 0x00000000
46 #define RWSEM_ACTIVE_BIAS 0x00000001
47 #define RWSEM_ACTIVE_MASK 0x0000ffff
48 #define RWSEM_WAITING_BIAS (-0x00010000)
49 #else /* CONFIG_64BIT */
50 #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
51 #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
52 #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
53 #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
54 #endif /* CONFIG_64BIT */
55 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
56 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
61 static inline void __down_read(struct rw_semaphore *sem)
72 #else /* CONFIG_64BIT */
78 #endif /* CONFIG_64BIT */
79 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
80 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
83 rwsem_down_read_failed(sem);
87 * trylock for reading -- returns 1 if successful, 0 if contention
89 static inline int __down_read_trylock(struct rw_semaphore *sem)
102 #else /* CONFIG_64BIT */
110 #endif /* CONFIG_64BIT */
111 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
112 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
114 return old >= 0 ? 1 : 0;
120 static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
122 signed long old, new, tmp;
124 tmp = RWSEM_ACTIVE_WRITE_BIAS;
132 #else /* CONFIG_64BIT */
138 #endif /* CONFIG_64BIT */
139 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
140 : "Q" (sem->count), "m" (tmp)
143 rwsem_down_write_failed(sem);
146 static inline void __down_write(struct rw_semaphore *sem)
148 __down_write_nested(sem, 0);
152 * trylock for writing -- returns 1 if successful, 0 if contention
154 static inline int __down_write_trylock(struct rw_semaphore *sem)
165 #else /* CONFIG_64BIT */
171 #endif /* CONFIG_64BIT */
173 : "=&d" (old), "=Q" (sem->count)
174 : "Q" (sem->count), "d" (RWSEM_ACTIVE_WRITE_BIAS)
176 return (old == RWSEM_UNLOCKED_VALUE) ? 1 : 0;
180 * unlock after reading
182 static inline void __up_read(struct rw_semaphore *sem)
184 signed long old, new;
193 #else /* CONFIG_64BIT */
199 #endif /* CONFIG_64BIT */
200 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
201 : "Q" (sem->count), "i" (-RWSEM_ACTIVE_READ_BIAS)
204 if ((new & RWSEM_ACTIVE_MASK) == 0)
209 * unlock after writing
211 static inline void __up_write(struct rw_semaphore *sem)
213 signed long old, new, tmp;
215 tmp = -RWSEM_ACTIVE_WRITE_BIAS;
223 #else /* CONFIG_64BIT */
229 #endif /* CONFIG_64BIT */
230 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
231 : "Q" (sem->count), "m" (tmp)
234 if ((new & RWSEM_ACTIVE_MASK) == 0)
239 * downgrade write lock to read lock
241 static inline void __downgrade_write(struct rw_semaphore *sem)
243 signed long old, new, tmp;
245 tmp = -RWSEM_WAITING_BIAS;
253 #else /* CONFIG_64BIT */
259 #endif /* CONFIG_64BIT */
260 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
261 : "Q" (sem->count), "m" (tmp)
264 rwsem_downgrade_wake(sem);
268 * implement atomic add functionality
270 static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
272 signed long old, new;
281 #else /* CONFIG_64BIT */
287 #endif /* CONFIG_64BIT */
288 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
289 : "Q" (sem->count), "d" (delta)
294 * implement exchange and add functionality
296 static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
298 signed long old, new;
307 #else /* CONFIG_64BIT */
313 #endif /* CONFIG_64BIT */
314 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
315 : "Q" (sem->count), "d" (delta)
320 #endif /* _S390_RWSEM_H */