blob: fb14b9f2e05b7591ff41b89a1fd41d618eaba852 [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 Hughesf8349362012-06-18 15:00:06 -070048 ASSERT_TRUE(mu.TryLock());
49 MutexTester::AssertDepth(mu, 1U);
50 mu.Unlock();
51 MutexTester::AssertDepth(mu, 0U);
52}
53
54// GCC doesn't get recursive mutexes, so we have to turn off thread safety analysis.
55static void RecursiveLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS {
56 Mutex mu("test mutex");
57 MutexTester::AssertDepth(mu, 0U);
58 mu.Lock();
59 MutexTester::AssertDepth(mu, 1U);
60 mu.Lock();
61 MutexTester::AssertDepth(mu, 2U);
62 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070063 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080064 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070065 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080066}
67
68TEST(Mutex, RecursiveLockUnlock) {
Elliott Hughesf8349362012-06-18 15:00:06 -070069 RecursiveLockUnlockTest();
70}
71
72// GCC doesn't get recursive mutexes, so we have to turn off thread safety analysis.
73static void RecursiveTryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080074 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070075 MutexTester::AssertDepth(mu, 0U);
Elliott Hughesf8349362012-06-18 15:00:06 -070076 ASSERT_TRUE(mu.TryLock());
Elliott Hughes3efb8412012-03-16 16:09:38 -070077 MutexTester::AssertDepth(mu, 1U);
Elliott Hughesf8349362012-06-18 15:00:06 -070078 ASSERT_TRUE(mu.TryLock());
Elliott Hughes3efb8412012-03-16 16:09:38 -070079 MutexTester::AssertDepth(mu, 2U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080080 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070081 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080082 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070083 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080084}
85
86TEST(Mutex, RecursiveTryLockUnlock) {
Elliott Hughesf8349362012-06-18 15:00:06 -070087 RecursiveTryLockUnlockTest();
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080088}
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080089
90} // namespace art