rwsem: rwsem_is_{read,write}_locked
Peter Zijlstra
maintainerRFC PATCH 1/1 rwsem: rwsem_is_{read,write}_locked
Christoph requested an extended is_locked interface for rwsems. I think the below ought to do, then again, I might have missed something.
Signed-off-by Peter Zijlstra <[email protected]>
--- arch/alpha/include/asm/rwsem.h | 5 ----- arch/ia64/include/asm/rwsem.h | 5 ----- arch/powerpc/include/asm/rwsem.h | 5 ----- arch/s390/include/asm/rwsem.h | 5 ----- arch/sh/include/asm/rwsem.h | 5 ----- arch/sparc/include/asm/rwsem.h | 5 ----- arch/x86/include/asm/rwsem.h | 5 ----- arch/xtensa/include/asm/rwsem.h | 5 ----- include/linux/rwsem-spinlock.h | 5 +++++ include/linux/rwsem.h | 16 ++++++++++++++++ 10 files changed, 21 insertions(+), 40 deletions(-)
arch/alpha/include/asm/rwsem.h +0 -5
@@ -250,10 +250,5 @@ static inline long rwsem_atomic_update(long val, struct rw_semaphore *sem)#endif } -static inline int rwsem_is_locked (struct rw_semaphore *sem) -{ -return (sem->count !=0 ); -} -#endif /* __KERNEL__ */ #endif /* _ALPHA_RWSEM_H */
arch/ia64/include/asm/rwsem.h +0 -5
@@ -174,9 +174,4 @@ __downgrade_write (struct rw_semaphore *sem)#define rwsem_atomic_add (delta, sem) atomic64_add(delta, (atomic64_t *)(&(sem)->count))#define rwsem_atomic_update (delta, sem) atomic64_add_return(delta, (atomic64_t *)(&(sem)->count)) -static inline int rwsem_is_locked (struct rw_semaphore *sem) -{ -return (sem->count !=0 ); -} -#endif /* _ASM_IA64_RWSEM_H */
arch/powerpc/include/asm/rwsem.h +0 -5
@@ -164,10 +164,5 @@ static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)return atomic_add_return (delta, (atomic_t *)(&sem->count)); } -static inline int rwsem_is_locked (struct rw_semaphore *sem) -{ -return (sem->count !=0 ); -} -#endif /* __KERNEL__ */ #endif /* _ASM_POWERPC_RWSEM_H */
arch/s390/include/asm/rwsem.h +0 -5
@@ -378,10 +378,5 @@ static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)return new; } -static inline int rwsem_is_locked (struct rw_semaphore *sem) -{ -return (sem->count !=0 ); -} -#endif /* __KERNEL__ */ #endif /* _S390_RWSEM_H */
arch/sh/include/asm/rwsem.h +0 -5
@@ -179,10 +179,5 @@ static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)return atomic_add_return (delta, (atomic_t *)(&sem->count)); } -static inline int rwsem_is_locked (struct rw_semaphore *sem) -{ -return (sem->count !=0 ); -} -#endif /* __KERNEL__ */ #endif /* _ASM_SH_RWSEM_H */
arch/sparc/include/asm/rwsem.h +0 -5
@@ -74,11 +74,6 @@ static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)atomic_add (delta, (atomic_t *)(&sem->count)); } -static inline int rwsem_is_locked (struct rw_semaphore *sem) -{ -return (sem->count !=0 ); -} -#endif /* __KERNEL__ */ #endif /* _SPARC64_RWSEM_H */
arch/x86/include/asm/rwsem.h +0 -5
@@ -256,10 +256,5 @@ static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)return tmp + delta; } -static inline int rwsem_is_locked (struct rw_semaphore *sem) -{ -return (sem->count !=0 ); -} -#endif /* __KERNEL__ */ #endif /* _ASM_X86_RWSEM_H */
arch/xtensa/include/asm/rwsem.h +0 -5
@@ -160,9 +160,4 @@ static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem) return atomic_add_return(delta, (atomic_t *)(&sem->count)); } -static inline int rwsem_is_locked (struct rw_semaphore *sem) -{ -return (sem->count !=0 ); -} -#endif /* _XTENSA_RWSEM_H */
include/linux/rwsem-spinlock.h +5 -0
@@ -74,5 +74,10 @@ static inline int rwsem_is_locked(struct rw_semaphore *sem)return (sem->activity !=0 ); } +static inline int rwsem_is_write_locked (struct rw_semaphore *sem) +{ +return (sem->activity <0 ); +} +#endif /* __KERNEL__ */ #endif /* _LINUX_RWSEM_SPINLOCK_H */
include/linux/rwsem.h +16 -0
@@ -20,8 +20,24 @@ struct rw_semaphore;#include <linux/rwsem-spinlock.h> /* use a generic implementation */ #else #include <asm/rwsem.h> /* use an arch-specific implementation */ + +static inline int rwsem_is_locked (struct rw_semaphore *sem) +{ +return (sem->count !=0 ); +} + +static inline int rwsem_is_write_locked (struct rw_semaphore *sem) +{ +return (sem->count &RWSEM_ACTIVE_MASK ) ==RWSEM_ACTIVE_MASK ; +} +#endif +static inline int rwsem_is_read_locked (struct rw_semaphore *sem) +{ +return rwsem_is_locked (sem) && !rwsem_is_write_locked (sem); +} +/* * lock for reading */
Peter Zijlstra
maintainerQuoted 11 lines · Peter Zijlstra
Christoph requested an extended
is_lockedinterface for rwsems. I think the below ought to do, then again, I might have missed something.Signed-off-by Peter Zijlstra <[email protected]>---
+++
b/include/linux/rwsem.hinclude/linux/rwsem.h +0
-0@@ -20,8 +20,24 @@ struct rw_semaphore;#include <linux/rwsem-spinlock.h> /* use a generic implementation */ #else #include <asm/rwsem.h> /* use an arch-specific implementation */
include/linux/rwsem.h struct rw_semaphore; · line 32
Quoted 10 lines
include/linux/rwsem.h +10
-0+ +static inline int rwsem_is_locked (struct rw_semaphore *sem) +{ +return (sem->count !=0 ); +} + +static inline int rwsem_is_write_locked (struct rw_semaphore *sem) +{ +return (sem->count &RWSEM_ACTIVE_MASK ) ==RWSEM_ACTIVE_MASK ; +}
OK, that's not right. Concurrent down_write() calls xadd RWSEM_ACTIVE_WRITE_BIAS, which has a 1 in that mask, so we're going to deviate an unspecified number.
That one will make it roll over into (count & MASK) >= 0 which is the exact range readers use too, this might be a tad harder than hoped.
Will ponder in the morning.