summaryrefslogtreecommitdiffstats
path: root/kernel/mutex.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2009-06-10 16:19:40 -0700
committerLinus Torvalds <torvalds@linux-foundation.org>2009-06-10 16:19:40 -0700
commite7241d771419b8a8671ebc46a043c324ccb0dcf7 (patch)
tree77f1bc9c635e2ac29396816ce9ef9cbdb6853884 /kernel/mutex.c
parent3f6280ddf25fa656d0e17960588e52bee48a7547 (diff)
parent04dce7d9d429ea5ea04e9432d1726c930f4d67da (diff)
downloadop-kernel-dev-e7241d771419b8a8671ebc46a043c324ccb0dcf7.zip
op-kernel-dev-e7241d771419b8a8671ebc46a043c324ccb0dcf7.tar.gz
Merge branch 'locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip
* 'locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip: spinlock: Add missing __raw_spin_lock_flags() stub for UP mutex: add atomic_dec_and_mutex_lock(), fix locking, rtmutex.c: Documentation cleanup mutex: add atomic_dec_and_mutex_lock()
Diffstat (limited to 'kernel/mutex.c')
-rw-r--r--kernel/mutex.c25
1 files changed, 24 insertions, 1 deletions
diff --git a/kernel/mutex.c b/kernel/mutex.c
index 6ca5fe9..e5cc0cd 100644
--- a/kernel/mutex.c
+++ b/kernel/mutex.c
@@ -473,5 +473,28 @@ int __sched mutex_trylock(struct mutex *lock)
return ret;
}
-
EXPORT_SYMBOL(mutex_trylock);
+
+/**
+ * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
+ * @cnt: the atomic which we are to dec
+ * @lock: the mutex to return holding if we dec to 0
+ *
+ * return true and hold lock if we dec to 0, return false otherwise
+ */
+int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
+{
+ /* dec if we can't possibly hit 0 */
+ if (atomic_add_unless(cnt, -1, 1))
+ return 0;
+ /* we might hit 0, so take the lock */
+ mutex_lock(lock);
+ if (!atomic_dec_and_test(cnt)) {
+ /* when we actually did the dec, we didn't hit 0 */
+ mutex_unlock(lock);
+ return 0;
+ }
+ /* we hit 0, and we hold the lock */
+ return 1;
+}
+EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
OpenPOWER on IntegriCloud