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 void __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 if (unlikely(oldcount < 0))
46 rwsem_down_read_failed(sem);
50 * trylock for reading -- returns 1 if successful, 0 if contention
52 static inline int __down_read_trylock(struct rw_semaphore *sem)
56 res = atomic_long_read(&sem->count);
58 new = res + RWSEM_ACTIVE_READ_BIAS;
62 res = atomic_long_cmpxchg(&sem->count, old, new);
64 return res >= 0 ? 1 : 0;
67 static inline long ___down_write(struct rw_semaphore *sem)
71 oldcount = sem->count.counter;
72 sem->count.counter += RWSEM_ACTIVE_WRITE_BIAS;
84 :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
85 :"Ir" (RWSEM_ACTIVE_WRITE_BIAS), "m" (sem->count) : "memory");
90 static inline void __down_write(struct rw_semaphore *sem)
92 if (unlikely(___down_write(sem)))
93 rwsem_down_write_failed(sem);
96 static inline int __down_write_killable(struct rw_semaphore *sem)
98 if (unlikely(___down_write(sem)))
99 if (IS_ERR(rwsem_down_write_failed_killable(sem)))
106 * trylock for writing -- returns 1 if successful, 0 if contention
108 static inline int __down_write_trylock(struct rw_semaphore *sem)
110 long ret = atomic_long_cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
111 RWSEM_ACTIVE_WRITE_BIAS);
112 if (ret == RWSEM_UNLOCKED_VALUE)
117 static inline void __up_read(struct rw_semaphore *sem)
121 oldcount = sem->count.counter;
122 sem->count.counter -= RWSEM_ACTIVE_READ_BIAS;
125 __asm__ __volatile__(
134 :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
135 :"Ir" (RWSEM_ACTIVE_READ_BIAS), "m" (sem->count) : "memory");
137 if (unlikely(oldcount < 0))
138 if ((int)oldcount - RWSEM_ACTIVE_READ_BIAS == 0)
142 static inline void __up_write(struct rw_semaphore *sem)
146 sem->count.counter -= RWSEM_ACTIVE_WRITE_BIAS;
147 count = sem->count.counter;
150 __asm__ __volatile__(
160 :"=&r" (count), "=m" (sem->count), "=&r" (temp)
161 :"Ir" (RWSEM_ACTIVE_WRITE_BIAS), "m" (sem->count) : "memory");
169 * downgrade write lock to read lock
171 static inline void __downgrade_write(struct rw_semaphore *sem)
175 oldcount = sem->count.counter;
176 sem->count.counter -= RWSEM_WAITING_BIAS;
179 __asm__ __volatile__(
188 :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
189 :"Ir" (-RWSEM_WAITING_BIAS), "m" (sem->count) : "memory");
191 if (unlikely(oldcount < 0))
192 rwsem_downgrade_wake(sem);
195 #endif /* __KERNEL__ */
196 #endif /* _ALPHA_RWSEM_H */