summaryrefslogtreecommitdiffstats
path: root/rt-locks-provide-atomic_dec_and_mutex_lock-for-rt.patch
blob: d87b14c8f9b7fc070acb2884432dcbea12b1f7e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
From 6c917f22b7b5b9c0be5065d83e3b2b2d76d2da0a 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>
Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com>
---
 kernel/rt.c |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)

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.0.4