From e3f6933418e2b939f0e9e6e7a65abb4a4669a685 Mon Sep 17 00:00:00 2001 From: Waiman Long Date: Wed, 17 Apr 2013 15:23:12 -0400 Subject: [PATCH] --- yaml --- r: 362827 b: refs/heads/master c: 0dc8c730c98a06a4d927f8d08bd0dd6de973b8dd h: refs/heads/master i: 362825: 3388db77127d8a665869b9d3da491df121935210 362823: 0ea70bd09a6b77259a23f694f7a82ceaf9db148b v: v3 --- [refs] | 2 +- trunk/arch/x86/include/asm/mutex.h | 10 ++++++++++ trunk/kernel/mutex.c | 19 ++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/[refs] b/[refs] index c5ab2f8a4d05..9f2aa618f2b2 100644 --- a/[refs] +++ b/[refs] @@ -1,2 +1,2 @@ --- -refs/heads/master: 41fcb9f230bf773656d1768b73000ef720bf00c3 +refs/heads/master: 0dc8c730c98a06a4d927f8d08bd0dd6de973b8dd diff --git a/trunk/arch/x86/include/asm/mutex.h b/trunk/arch/x86/include/asm/mutex.h index 7d3a48275394..bc2a0b0dcea6 100644 --- a/trunk/arch/x86/include/asm/mutex.h +++ b/trunk/arch/x86/include/asm/mutex.h @@ -3,3 +3,13 @@ #else # include #endif + +#ifndef __ASM_MUTEX_H +#define __ASM_MUTEX_H +/* + * For the x86 architecture, it allows any negative number (besides -1) in + * the mutex count to indicate that some other threads are waiting on the + * mutex. + */ +#define __ARCH_ALLOW_ANY_NEGATIVE_MUTEX_COUNT 1 +#endif diff --git a/trunk/kernel/mutex.c b/trunk/kernel/mutex.c index 262d7177adad..70ebd855d9e8 100644 --- a/trunk/kernel/mutex.c +++ b/trunk/kernel/mutex.c @@ -37,6 +37,17 @@ # include #endif +/* + * A mutex count of -1 indicates that waiters are sleeping waiting for the + * mutex. Some architectures can allow any negative number, not just -1, for + * this purpose. + */ +#ifdef __ARCH_ALLOW_ANY_NEGATIVE_MUTEX_COUNT +#define MUTEX_SHOW_NO_WAITER(mutex) (atomic_read(&(mutex)->count) >= 0) +#else +#define MUTEX_SHOW_NO_WAITER(mutex) (atomic_read(&(mutex)->count) != -1) +#endif + void __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) { @@ -217,7 +228,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, if (owner && !mutex_spin_on_owner(lock, owner)) break; - if (atomic_cmpxchg(&lock->count, 1, 0) == 1) { + if ((atomic_read(&lock->count) == 1) && + (atomic_cmpxchg(&lock->count, 1, 0) == 1)) { lock_acquired(&lock->dep_map, ip); mutex_set_owner(lock); preempt_enable(); @@ -251,7 +263,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, list_add_tail(&waiter.list, &lock->wait_list); waiter.task = task; - if (atomic_xchg(&lock->count, -1) == 1) + if (MUTEX_SHOW_NO_WAITER(lock) && (atomic_xchg(&lock->count, -1) == 1)) goto done; lock_contended(&lock->dep_map, ip); @@ -266,7 +278,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, * that when we release the lock, we properly wake up the * other waiters: */ - if (atomic_xchg(&lock->count, -1) == 1) + if (MUTEX_SHOW_NO_WAITER(lock) && + (atomic_xchg(&lock->count, -1) == 1)) break; /*