blob: b59b305224420bbfa93678a271fa93d494ad9cd1 [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 -070023#if !defined(__APPLE__)
24struct MutexTester {
25 static void AssertDepth(Mutex& mu, uint32_t expected_depth) {
26 ASSERT_EQ(expected_depth, mu.GetDepth());
27 }
28};
29
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080030TEST(Mutex, LockUnlock) {
31 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070032 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080033 mu.Lock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070034 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080035 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070036 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080037}
38
39TEST(Mutex, TryLockUnlock) {
40 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070041 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080042 mu.TryLock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070043 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080044 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070045 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080046}
47
48TEST(Mutex, RecursiveLockUnlock) {
49 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070050 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080051 mu.Lock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070052 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080053 mu.Lock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070054 MutexTester::AssertDepth(mu, 2U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080055 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070056 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080057 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070058 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080059}
60
61TEST(Mutex, RecursiveTryLockUnlock) {
62 Mutex mu("test mutex");
Elliott Hughes3efb8412012-03-16 16:09:38 -070063 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080064 mu.TryLock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070065 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080066 mu.TryLock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070067 MutexTester::AssertDepth(mu, 2U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080068 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070069 MutexTester::AssertDepth(mu, 1U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080070 mu.Unlock();
Elliott Hughes3efb8412012-03-16 16:09:38 -070071 MutexTester::AssertDepth(mu, 0U);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080072}
Elliott Hughes3efb8412012-03-16 16:09:38 -070073#endif
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080074
75} // namespace art