Subject: Add rwsem_is_contended() Add a function to rw_semaphores to check if there are any processes waiting for the lock. Signed-off-by: Christoph Lameter --- include/linux/rwsem.h | 2 ++ include/linux/sched.h | 9 +++++++++ lib/rwsem-spinlock.c | 12 ++++++++++++ lib/rwsem.c | 6 ++++++ 4 files changed, 29 insertions(+) Index: linux-2.6/include/linux/rwsem.h =================================================================== --- linux-2.6.orig/include/linux/rwsem.h 2008-04-18 14:04:12.000000000 -0700 +++ linux-2.6/include/linux/rwsem.h 2008-04-18 14:05:12.000000000 -0700 @@ -59,6 +59,8 @@ extern void up_write(struct rw_semaphore */ extern void downgrade_write(struct rw_semaphore *sem); +extern int rwsem_is_contended(struct rw_semaphore *sem); + #ifdef CONFIG_DEBUG_LOCK_ALLOC /* * nested locking. NOTE: rwsems are not allowed to recurse Index: linux-2.6/include/linux/sched.h =================================================================== --- linux-2.6.orig/include/linux/sched.h 2008-04-18 14:04:16.000000000 -0700 +++ linux-2.6/include/linux/sched.h 2008-04-18 14:05:12.000000000 -0700 @@ -1984,6 +1984,15 @@ static inline int spin_needbreak(spinloc #endif } +static inline int rwsem_needbreak(struct rw_semaphore *sem) +{ +#ifdef CONFIG_PREEMPT + return rwsem_is_contended(sem); +#else + return 0; +#endif +} + /* * Reevaluate whether the task has signals pending delivery. * Wake the task if so. Index: linux-2.6/lib/rwsem-spinlock.c =================================================================== --- linux-2.6.orig/lib/rwsem-spinlock.c 2008-04-18 14:26:49.000000000 -0700 +++ linux-2.6/lib/rwsem-spinlock.c 2008-04-18 14:30:49.000000000 -0700 @@ -305,6 +305,18 @@ void __downgrade_write(struct rw_semapho spin_unlock_irqrestore(&sem->wait_lock, flags); } +int rwsem_is_contended(struct rw_semaphore *sem) +{ + /* + * Racy check for an empty list. False positives or negatives + * would be okay. False positive may cause a useless dropping of + * locks. False negatives may cause locks to be held a bit + * longer until the next check. + */ + return !list_empty(&sem->wait_list); +} + +EXPORT_SYMBOL(rwsem_is_contended); EXPORT_SYMBOL(__init_rwsem); EXPORT_SYMBOL(__down_read); EXPORT_SYMBOL(__down_read_trylock); Index: linux-2.6/lib/rwsem.c =================================================================== --- linux-2.6.orig/lib/rwsem.c 2008-04-18 15:15:13.000000000 -0700 +++ linux-2.6/lib/rwsem.c 2008-04-18 15:15:23.000000000 -0700 @@ -251,6 +251,12 @@ asmregparm struct rw_semaphore *rwsem_do return sem; } +int rwsem_is_contended(struct rw_semaphore *sem) +{ + return !list_empty(&sem->wait_list); +} + +EXPORT_SYMBOL(rwsem_is_contended); EXPORT_SYMBOL(rwsem_down_read_failed); EXPORT_SYMBOL(rwsem_down_write_failed); EXPORT_SYMBOL(rwsem_wake);