blob: 7c7189b50268a33b97ec69fcaf8d7892e369bf48 [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);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080048 mu.TryLock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070049 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080050 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070051 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080052}
53
54TEST(Mutex, RecursiveLockUnlock) {
55 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070056 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080057 mu.Lock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070058 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080059 mu.Lock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070060 MutexTester::AssertDepth(mu, 2U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080061 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070062 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080063 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070064 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080065}
66
67TEST(Mutex, RecursiveTryLockUnlock) {
68 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070069 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080070 mu.TryLock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070071 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080072 mu.TryLock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070073 MutexTester::AssertDepth(mu, 2U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080074 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070075 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080076 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070077 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080078}
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080079
80} // namespace art