blob: e1d2c14eb2d90b859bb5dd78dfdb7badbd292cab [file] [log] [blame]
From 25b665a79185fd81c0e912cb3849fa3baf10140a Mon Sep 17 00:00:00 2001
From: Thomas Gleixner <tglx@linutronix.de>
Date: Fri, 3 Jul 2009 13:16:24 -0500
Subject: [PATCH] rt-locks: provide atomic_dec_and_mutex_lock for -rt
commit 25b665a79185fd81c0e912cb3849fa3baf10140a in tip.
Add the missing function
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
diff --git a/kernel/rt.c b/kernel/rt.c
index 6dd475a..59ca169 100644
--- a/kernel/rt.c
+++ b/kernel/rt.c
@@ -517,3 +517,27 @@ void __sema_init(struct semaphore *sem, int val,
}
}
EXPORT_SYMBOL(__sema_init);
+
+/**
+ * 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);
--
1.7.1.1