Skip to contentSkip to conversation

rwsem: rwsem_is_{read,write}_locked

RFC PATCH In review Peter Zijlstra 1 participant 2 messages Feb 24, 2009

Subsystems (8)
x86 PowerPC s390 SuperH +4 more subsystems
lore
Share & tools
lore

Peter Zijlstra

author maintainer
#0 lore raw

RFC 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.

---
 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
index 1570c0b..fdc8dc3 100644
@@ -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
index fbee74b..4b4bfa5 100644
@@ -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
index 24cd928..27d1acf 100644
@@ -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
index 9d2a179..d9ab6c6 100644
@@ -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
index 1987f3e..5aded81 100644
@@ -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
index 1dc129a..a3f43c9 100644
@@ -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
index ca7517d..a22dc3b 100644
@@ -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
index e39edf5..e960d99 100644
@@ -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
index 6c3c0f6..00c76f7 100644
@@ -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
index efd348f..9d7531b 100644
@@ -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
  */
author maintainer
#1 lore raw
Quoted 11 lines · Peter Zijlstra

Christoph requested an extended is_locked interface for rwsems. I think the below ought to do, then again, I might have missed something.

---

+++ b/include/linux/rwsem.h

include/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.