blob: 63aa14bd7ce02a2b08bdb82dc39dfbc634d18a09 [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
45TEST(Mutex, TryLockUnlock) {
46 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070047 MutexTester::AssertDepth(mu, 0U);
Elliott Hughes91117af2012-06-18 15:46:39 -070048 bool locked = mu.TryLock();
49 ASSERT_TRUE(locked);
50 if (locked) { // Keep GCC happy.
51 MutexTester::AssertDepth(mu, 1U);
52 mu.Unlock();
53 MutexTester::AssertDepth(mu, 0U);
54 }
Elliott Hughesf8349362012-06-18 15:00:06 -070055}
56
57// GCC doesn't get recursive mutexes, so we have to turn off thread safety analysis.
58static void RecursiveLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS {
59 Mutex mu("test mutex");
60 MutexTester::AssertDepth(mu, 0U);
61 mu.Lock();
62 MutexTester::AssertDepth(mu, 1U);
63 mu.Lock();
64 MutexTester::AssertDepth(mu, 2U);
65 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070066 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080067 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070068 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080069}
70
71TEST(Mutex, RecursiveLockUnlock) {
Elliott Hughesf8349362012-06-18 15:00:06 -070072 RecursiveLockUnlockTest();
73}
74
75// GCC doesn't get recursive mutexes, so we have to turn off thread safety analysis.
76static void RecursiveTryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080077 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070078 MutexTester::AssertDepth(mu, 0U);
Elliott Hughesf8349362012-06-18 15:00:06 -070079 ASSERT_TRUE(mu.TryLock());
Elliott Hughes3efb8412012-03-16 16:09:38 -070080 MutexTester::AssertDepth(mu, 1U);
Elliott Hughesf8349362012-06-18 15:00:06 -070081 ASSERT_TRUE(mu.TryLock());
Elliott Hughes3efb8412012-03-16 16:09:38 -070082 MutexTester::AssertDepth(mu, 2U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080083 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070084 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080085 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070086 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080087}
88
89TEST(Mutex, RecursiveTryLockUnlock) {
Elliott Hughesf8349362012-06-18 15:00:06 -070090 RecursiveTryLockUnlockTest();
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080091}
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080092
93} // namespace art