1 #ifndef _ASM_GENERIC_RWSEM_H
2 #define _ASM_GENERIC_RWSEM_H
5 #error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead."
11 * R/W semaphores originally for PPC using the stuff in lib/rwsem.c.
12 * Adapted largely from include/asm-i386/rwsem.h
13 * by Paul Mackerras <paulus@samba.org>.
17 * the semaphore definition
20 # define RWSEM_ACTIVE_MASK 0xffffffffL
22 # define RWSEM_ACTIVE_MASK 0x0000ffffL
25 #define RWSEM_UNLOCKED_VALUE 0x00000000L
26 #define RWSEM_ACTIVE_BIAS 0x00000001L
27 #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
28 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
29 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
34 static inline void __down_read(struct rw_semaphore
*sem
)
36 if (unlikely(atomic_long_inc_return_acquire(&sem
->count
) <= 0))
37 rwsem_down_read_failed(sem
);
40 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
44 while ((tmp
= atomic_long_read(&sem
->count
)) >= 0) {
45 if (tmp
== atomic_long_cmpxchg_acquire(&sem
->count
, tmp
,
46 tmp
+ RWSEM_ACTIVE_READ_BIAS
)) {
56 static inline void __down_write(struct rw_semaphore
*sem
)
60 tmp
= atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS
,
62 if (unlikely(tmp
!= RWSEM_ACTIVE_WRITE_BIAS
))
63 rwsem_down_write_failed(sem
);
66 static inline int __down_write_killable(struct rw_semaphore
*sem
)
70 tmp
= atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS
,
72 if (unlikely(tmp
!= RWSEM_ACTIVE_WRITE_BIAS
))
73 if (IS_ERR(rwsem_down_write_failed_killable(sem
)))
78 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
82 tmp
= atomic_long_cmpxchg_acquire(&sem
->count
, RWSEM_UNLOCKED_VALUE
,
83 RWSEM_ACTIVE_WRITE_BIAS
);
84 return tmp
== RWSEM_UNLOCKED_VALUE
;
88 * unlock after reading
90 static inline void __up_read(struct rw_semaphore
*sem
)
94 tmp
= atomic_long_dec_return_release(&sem
->count
);
95 if (unlikely(tmp
< -1 && (tmp
& RWSEM_ACTIVE_MASK
) == 0))
100 * unlock after writing
102 static inline void __up_write(struct rw_semaphore
*sem
)
104 if (unlikely(atomic_long_sub_return_release(RWSEM_ACTIVE_WRITE_BIAS
,
110 * downgrade write lock to read lock
112 static inline void __downgrade_write(struct rw_semaphore
*sem
)
117 * When downgrading from exclusive to shared ownership,
118 * anything inside the write-locked region cannot leak
119 * into the read side. In contrast, anything in the
120 * read-locked region is ok to be re-ordered into the
121 * write side. As such, rely on RELEASE semantics.
123 tmp
= atomic_long_add_return_release(-RWSEM_WAITING_BIAS
, &sem
->count
);
125 rwsem_downgrade_wake(sem
);
128 #endif /* __KERNEL__ */
129 #endif /* _ASM_GENERIC_RWSEM_H */