Use static thread safety analysis when available, and fix the bugs GCC finds.
It's impossible to express the Heap locking and the ThreadList locking with
GCC, but Clang is supposed to be able to do it. This patch does what's possible
for now.
Change-Id: Ib64a890c9d27c6ce255d5003cb755c2ef1beba95
diff --git a/src/mutex_test.cc b/src/mutex_test.cc
index 7c7189b..fb14b9f 100644
--- a/src/mutex_test.cc
+++ b/src/mutex_test.cc
@@ -45,18 +45,37 @@
TEST(Mutex, TryLockUnlock) {
Mutex mu("test mutex");
MutexTester::AssertDepth(mu, 0U);
- mu.TryLock();
+ ASSERT_TRUE(mu.TryLock());
+ MutexTester::AssertDepth(mu, 1U);
+ mu.Unlock();
+ MutexTester::AssertDepth(mu, 0U);
+}
+
+// GCC doesn't get recursive mutexes, so we have to turn off thread safety analysis.
+static void RecursiveLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS {
+ Mutex mu("test mutex");
+ MutexTester::AssertDepth(mu, 0U);
+ mu.Lock();
+ MutexTester::AssertDepth(mu, 1U);
+ mu.Lock();
+ MutexTester::AssertDepth(mu, 2U);
+ mu.Unlock();
MutexTester::AssertDepth(mu, 1U);
mu.Unlock();
MutexTester::AssertDepth(mu, 0U);
}
TEST(Mutex, RecursiveLockUnlock) {
+ RecursiveLockUnlockTest();
+}
+
+// GCC doesn't get recursive mutexes, so we have to turn off thread safety analysis.
+static void RecursiveTryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS {
Mutex mu("test mutex");
MutexTester::AssertDepth(mu, 0U);
- mu.Lock();
+ ASSERT_TRUE(mu.TryLock());
MutexTester::AssertDepth(mu, 1U);
- mu.Lock();
+ ASSERT_TRUE(mu.TryLock());
MutexTester::AssertDepth(mu, 2U);
mu.Unlock();
MutexTester::AssertDepth(mu, 1U);
@@ -65,16 +84,7 @@
}
TEST(Mutex, RecursiveTryLockUnlock) {
- Mutex mu("test mutex");
- MutexTester::AssertDepth(mu, 0U);
- mu.TryLock();
- MutexTester::AssertDepth(mu, 1U);
- mu.TryLock();
- MutexTester::AssertDepth(mu, 2U);
- mu.Unlock();
- MutexTester::AssertDepth(mu, 1U);
- mu.Unlock();
- MutexTester::AssertDepth(mu, 0U);
+ RecursiveTryLockUnlockTest();
}
} // namespace art