]> www.infradead.org Git - users/willy/pagecache.git/commitdiff
locking: Add rwsem_is_write_locked()
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Wed, 6 Sep 2023 18:39:24 +0000 (14:39 -0400)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Wed, 6 Sep 2023 20:57:49 +0000 (16:57 -0400)
Several places want to know whether the lock is held by a writer, instead
of just whether it's held.  We can implement this for both normal and
rt rwsems.  RWSEM_WRITER_LOCKED is declared in rwsem.c and exposing
it outside that file might tempt other people to use it, so just use
a comment to note that's what the 1 means, and help anybody find it if
they're looking to change the implementation.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
include/linux/rwbase_rt.h
include/linux/rwsem.h

index 1d264dd086250e74b43df78a1266d3eb5219eba0..3c25b14edc059ec5267f98db482fb524e17fa371 100644 (file)
@@ -31,6 +31,11 @@ static __always_inline bool rw_base_is_locked(struct rwbase_rt *rwb)
        return atomic_read(&rwb->readers) != READER_BIAS;
 }
 
+static __always_inline bool rw_base_is_write_locked(struct rwbase_rt *rwb)
+{
+       return atomic_read(&rwb->readers) == WRITER_BIAS;
+}
+
 static __always_inline bool rw_base_is_contended(struct rwbase_rt *rwb)
 {
        return atomic_read(&rwb->readers) > 0;
index 1dd530ce8b45b9a7aa3934272b229360d2eee3ef..0f78b8d2e653a16fad800a05f46eabc5db56aa33 100644 (file)
@@ -72,6 +72,11 @@ static inline int rwsem_is_locked(struct rw_semaphore *sem)
        return atomic_long_read(&sem->count) != 0;
 }
 
+static inline int rwsem_is_write_locked(struct rw_semaphore *sem)
+{
+       return atomic_long_read(&sem->count) & 1 /* RWSEM_WRITER_LOCKED */;
+}
+
 #define RWSEM_UNLOCKED_VALUE           0L
 #define __RWSEM_COUNT_INIT(name)       .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
 
@@ -157,6 +162,11 @@ static __always_inline int rwsem_is_locked(struct rw_semaphore *sem)
        return rw_base_is_locked(&sem->rwbase);
 }
 
+static __always_inline int rwsem_is_write_locked(struct rw_semaphore *sem)
+{
+       return rw_base_is_write_locked(&sem->rwbase);
+}
+
 static __always_inline int rwsem_is_contended(struct rw_semaphore *sem)
 {
        return rw_base_is_contended(&sem->rwbase);