blob: 4220f2b2ec44e80290ba5549076383981e8d2975 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080016
17#include "mutex.h"
18
19#include "gtest/gtest.h"
20
21namespace art {
22
Elliott Hughes3efb8412012-03-16 16:09:38 -070023struct MutexTester {
24 static void AssertDepth(Mutex& mu, uint32_t expected_depth) {
25 ASSERT_EQ(expected_depth, mu.GetDepth());
Elliott Hughesf1498432012-03-28 19:34:27 -070026
27 // This test is single-threaded, so we also know _who_ should hold the lock.
28 if (expected_depth == 0) {
29 mu.AssertNotHeld();
30 } else {
31 mu.AssertHeld();
32 }
Elliott Hughes3efb8412012-03-16 16:09:38 -070033 }
34};
35
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080036TEST(Mutex, LockUnlock) {
37 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070038 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080039 mu.Lock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070040 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080041 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070042 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080043}
44
Elliott Hughes72d63d42012-06-18 16:51:20 -070045// GCC has trouble with our mutex tests, so we have to turn off thread safety analysis.
Elliott Hughes81414052012-06-18 16:43:50 -070046static void TryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080047 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070048 MutexTester::AssertDepth(mu, 0U);
Elliott Hughes81414052012-06-18 16:43:50 -070049 ASSERT_TRUE(mu.TryLock());
50 MutexTester::AssertDepth(mu, 1U);
51 mu.Unlock();
52 MutexTester::AssertDepth(mu, 0U);
53}
54
55TEST(Mutex, TryLockUnlock) {
56 TryLockUnlockTest();
Elliott Hughesf8349362012-06-18 15:00:06 -070057}
58
Elliott Hughes72d63d42012-06-18 16:51:20 -070059// GCC has trouble with our mutex tests, so we have to turn off thread safety analysis.
Elliott Hughesf8349362012-06-18 15:00:06 -070060static void RecursiveLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS {
61 Mutex mu("test mutex");
62 MutexTester::AssertDepth(mu, 0U);
63 mu.Lock();
64 MutexTester::AssertDepth(mu, 1U);
65 mu.Lock();
66 MutexTester::AssertDepth(mu, 2U);
67 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070068 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080069 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070070 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080071}
72
73TEST(Mutex, RecursiveLockUnlock) {
Elliott Hughesf8349362012-06-18 15:00:06 -070074 RecursiveLockUnlockTest();
75}
76
Elliott Hughes72d63d42012-06-18 16:51:20 -070077// GCC has trouble with our mutex tests, so we have to turn off thread safety analysis.
Elliott Hughesf8349362012-06-18 15:00:06 -070078static void RecursiveTryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080079 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070080 MutexTester::AssertDepth(mu, 0U);
Elliott Hughesf8349362012-06-18 15:00:06 -070081 ASSERT_TRUE(mu.TryLock());
Elliott Hughes3efb8412012-03-16 16:09:38 -070082 MutexTester::AssertDepth(mu, 1U);
Elliott Hughesf8349362012-06-18 15:00:06 -070083 ASSERT_TRUE(mu.TryLock());
Elliott Hughes3efb8412012-03-16 16:09:38 -070084 MutexTester::AssertDepth(mu, 2U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080085 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070086 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080087 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070088 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080089}
90
91TEST(Mutex, RecursiveTryLockUnlock) {
Elliott Hughesf8349362012-06-18 15:00:06 -070092 RecursiveTryLockUnlockTest();
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080093}
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080094
95} // namespace art