1 /* SPDX-License-Identifier: GPL-2.0 */
7 * Copyright IBM Corp. 2002
8 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
10 * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
15 * The MSW of the count is the negated number of active writers and waiting
16 * lockers, and the LSW is the total number of active locks
18 * The lock count is initialized to 0 (no active and no waiting lockers).
20 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
21 * uncontended lock. This can be determined because XADD returns the old value.
22 * Readers increment by 1 and see a positive value when uncontended, negative
23 * if there are writers (and maybe) readers waiting (in which case it goes to
26 * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
27 * be extended to 65534 by manually checking the whole MSW rather than relying
30 * The value of ACTIVE_BIAS supports up to 65535 active processes.
32 * This should be totally fair - if anything is waiting, a process that wants a
33 * lock will go to the back of the queue. When the currently active lock is
34 * released, if there's a writer at the front of the queue, then that and only
35 * that will be woken up; if there's a bunch of consecutive readers at the
36 * front, then they'll all be woken up, but no other readers will be.
39 #ifndef _LINUX_RWSEM_H
40 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
43 #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
44 #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
45 #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
46 #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
47 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
48 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
53 static inline void __down_read(struct rw_semaphore *sem)
63 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
64 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
67 rwsem_down_read_failed(sem);
71 * trylock for reading -- returns 1 if successful, 0 if contention
73 static inline int __down_read_trylock(struct rw_semaphore *sem)
85 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
86 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
88 return old >= 0 ? 1 : 0;
94 static inline long ___down_write(struct rw_semaphore *sem)
96 signed long old, new, tmp;
98 tmp = RWSEM_ACTIVE_WRITE_BIAS;
105 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
106 : "Q" (sem->count), "m" (tmp)
112 static inline void __down_write(struct rw_semaphore *sem)
114 if (___down_write(sem))
115 rwsem_down_write_failed(sem);
118 static inline int __down_write_killable(struct rw_semaphore *sem)
120 if (___down_write(sem))
121 if (IS_ERR(rwsem_down_write_failed_killable(sem)))
128 * trylock for writing -- returns 1 if successful, 0 if contention
130 static inline int __down_write_trylock(struct rw_semaphore *sem)
141 : "=&d" (old), "=Q" (sem->count)
142 : "Q" (sem->count), "d" (RWSEM_ACTIVE_WRITE_BIAS)
144 return (old == RWSEM_UNLOCKED_VALUE) ? 1 : 0;
148 * unlock after reading
150 static inline void __up_read(struct rw_semaphore *sem)
152 signed long old, new;
160 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
161 : "Q" (sem->count), "i" (-RWSEM_ACTIVE_READ_BIAS)
164 if ((new & RWSEM_ACTIVE_MASK) == 0)
169 * unlock after writing
171 static inline void __up_write(struct rw_semaphore *sem)
173 signed long old, new, tmp;
175 tmp = -RWSEM_ACTIVE_WRITE_BIAS;
182 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
183 : "Q" (sem->count), "m" (tmp)
186 if ((new & RWSEM_ACTIVE_MASK) == 0)
191 * downgrade write lock to read lock
193 static inline void __downgrade_write(struct rw_semaphore *sem)
195 signed long old, new, tmp;
197 tmp = -RWSEM_WAITING_BIAS;
204 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
205 : "Q" (sem->count), "m" (tmp)
208 rwsem_downgrade_wake(sem);
211 #endif /* _S390_RWSEM_H */