1 /* SPDX-License-Identifier: GPL-2.0 */
6 * Written by Ivan Kokshaysky <ink@jurassic.park.msu.ru>, 2001.
7 * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
10 #ifndef _LINUX_RWSEM_H
11 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
16 #include <linux/compiler.h>
18 #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
19 #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
20 #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
21 #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
22 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
23 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
25 static inline int ___down_read(struct rw_semaphore *sem)
29 oldcount = sem->count.counter;
30 sem->count.counter += RWSEM_ACTIVE_READ_BIAS;
42 :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
43 :"Ir" (RWSEM_ACTIVE_READ_BIAS), "m" (sem->count) : "memory");
45 return (oldcount < 0);
48 static inline void __down_read(struct rw_semaphore *sem)
50 if (unlikely(___down_read(sem)))
51 rwsem_down_read_failed(sem);
54 static inline int __down_read_killable(struct rw_semaphore *sem)
56 if (unlikely(___down_read(sem)))
57 if (IS_ERR(rwsem_down_read_failed_killable(sem)))
64 * trylock for reading -- returns 1 if successful, 0 if contention
66 static inline int __down_read_trylock(struct rw_semaphore *sem)
70 res = atomic_long_read(&sem->count);
72 new = res + RWSEM_ACTIVE_READ_BIAS;
76 res = atomic_long_cmpxchg(&sem->count, old, new);
78 return res >= 0 ? 1 : 0;
81 static inline long ___down_write(struct rw_semaphore *sem)
85 oldcount = sem->count.counter;
86 sem->count.counter += RWSEM_ACTIVE_WRITE_BIAS;
98 :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
99 :"Ir" (RWSEM_ACTIVE_WRITE_BIAS), "m" (sem->count) : "memory");
104 static inline void __down_write(struct rw_semaphore *sem)
106 if (unlikely(___down_write(sem)))
107 rwsem_down_write_failed(sem);
110 static inline int __down_write_killable(struct rw_semaphore *sem)
112 if (unlikely(___down_write(sem))) {
113 if (IS_ERR(rwsem_down_write_failed_killable(sem)))
121 * trylock for writing -- returns 1 if successful, 0 if contention
123 static inline int __down_write_trylock(struct rw_semaphore *sem)
125 long ret = atomic_long_cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
126 RWSEM_ACTIVE_WRITE_BIAS);
127 if (ret == RWSEM_UNLOCKED_VALUE)
132 static inline void __up_read(struct rw_semaphore *sem)
136 oldcount = sem->count.counter;
137 sem->count.counter -= RWSEM_ACTIVE_READ_BIAS;
140 __asm__ __volatile__(
149 :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
150 :"Ir" (RWSEM_ACTIVE_READ_BIAS), "m" (sem->count) : "memory");
152 if (unlikely(oldcount < 0))
153 if ((int)oldcount - RWSEM_ACTIVE_READ_BIAS == 0)
157 static inline void __up_write(struct rw_semaphore *sem)
161 sem->count.counter -= RWSEM_ACTIVE_WRITE_BIAS;
162 count = sem->count.counter;
165 __asm__ __volatile__(
175 :"=&r" (count), "=m" (sem->count), "=&r" (temp)
176 :"Ir" (RWSEM_ACTIVE_WRITE_BIAS), "m" (sem->count) : "memory");
184 * downgrade write lock to read lock
186 static inline void __downgrade_write(struct rw_semaphore *sem)
190 oldcount = sem->count.counter;
191 sem->count.counter -= RWSEM_WAITING_BIAS;
194 __asm__ __volatile__(
203 :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
204 :"Ir" (-RWSEM_WAITING_BIAS), "m" (sem->count) : "memory");
206 if (unlikely(oldcount < 0))
207 rwsem_downgrade_wake(sem);
210 #endif /* __KERNEL__ */
211 #endif /* _ALPHA_RWSEM_H */