Linux 5.1.15
[linux/fpc-iii.git] / include / asm-generic / rwsem.h
blob93e67a055a4d85ca82436f71ed160d971e33e46e
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_RWSEM_H
3 #define _ASM_GENERIC_RWSEM_H
5 #ifndef _LINUX_RWSEM_H
6 #error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead."
7 #endif
9 #ifdef __KERNEL__
12 * R/W semaphores originally for PPC using the stuff in lib/rwsem.c.
13 * Adapted largely from include/asm-i386/rwsem.h
14 * by Paul Mackerras <paulus@samba.org>.
18 * the semaphore definition
20 #ifdef CONFIG_64BIT
21 # define RWSEM_ACTIVE_MASK 0xffffffffL
22 #else
23 # define RWSEM_ACTIVE_MASK 0x0000ffffL
24 #endif
26 #define RWSEM_UNLOCKED_VALUE 0x00000000L
27 #define RWSEM_ACTIVE_BIAS 0x00000001L
28 #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
29 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
30 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
33 * lock for reading
35 static inline void __down_read(struct rw_semaphore *sem)
37 if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0))
38 rwsem_down_read_failed(sem);
41 static inline int __down_read_killable(struct rw_semaphore *sem)
43 if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) {
44 if (IS_ERR(rwsem_down_read_failed_killable(sem)))
45 return -EINTR;
48 return 0;
51 static inline int __down_read_trylock(struct rw_semaphore *sem)
53 long tmp;
55 while ((tmp = atomic_long_read(&sem->count)) >= 0) {
56 if (tmp == atomic_long_cmpxchg_acquire(&sem->count, tmp,
57 tmp + RWSEM_ACTIVE_READ_BIAS)) {
58 return 1;
61 return 0;
65 * lock for writing
67 static inline void __down_write(struct rw_semaphore *sem)
69 long tmp;
71 tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
72 &sem->count);
73 if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
74 rwsem_down_write_failed(sem);
77 static inline int __down_write_killable(struct rw_semaphore *sem)
79 long tmp;
81 tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
82 &sem->count);
83 if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
84 if (IS_ERR(rwsem_down_write_failed_killable(sem)))
85 return -EINTR;
86 return 0;
89 static inline int __down_write_trylock(struct rw_semaphore *sem)
91 long tmp;
93 tmp = atomic_long_cmpxchg_acquire(&sem->count, RWSEM_UNLOCKED_VALUE,
94 RWSEM_ACTIVE_WRITE_BIAS);
95 return tmp == RWSEM_UNLOCKED_VALUE;
99 * unlock after reading
101 static inline void __up_read(struct rw_semaphore *sem)
103 long tmp;
105 tmp = atomic_long_dec_return_release(&sem->count);
106 if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0))
107 rwsem_wake(sem);
111 * unlock after writing
113 static inline void __up_write(struct rw_semaphore *sem)
115 if (unlikely(atomic_long_sub_return_release(RWSEM_ACTIVE_WRITE_BIAS,
116 &sem->count) < 0))
117 rwsem_wake(sem);
121 * downgrade write lock to read lock
123 static inline void __downgrade_write(struct rw_semaphore *sem)
125 long tmp;
128 * When downgrading from exclusive to shared ownership,
129 * anything inside the write-locked region cannot leak
130 * into the read side. In contrast, anything in the
131 * read-locked region is ok to be re-ordered into the
132 * write side. As such, rely on RELEASE semantics.
134 tmp = atomic_long_add_return_release(-RWSEM_WAITING_BIAS, &sem->count);
135 if (tmp < 0)
136 rwsem_downgrade_wake(sem);
139 #endif /* __KERNEL__ */
140 #endif /* _ASM_GENERIC_RWSEM_H */