Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 16 | |
| 17 | #include "mutex.h" |
| 18 | |
| 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | namespace art { |
| 22 | |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 23 | struct MutexTester { |
| 24 | static void AssertDepth(Mutex& mu, uint32_t expected_depth) { |
| 25 | ASSERT_EQ(expected_depth, mu.GetDepth()); |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 26 | |
| 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 Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 33 | } |
| 34 | }; |
| 35 | |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 36 | TEST(Mutex, LockUnlock) { |
| 37 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 38 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 39 | mu.Lock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 40 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 41 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 42 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Elliott Hughes | 72d63d4 | 2012-06-18 16:51:20 -0700 | [diff] [blame^] | 45 | // GCC has trouble with our mutex tests, so we have to turn off thread safety analysis. |
Elliott Hughes | 8141405 | 2012-06-18 16:43:50 -0700 | [diff] [blame] | 46 | static void TryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS { |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 47 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 48 | MutexTester::AssertDepth(mu, 0U); |
Elliott Hughes | 8141405 | 2012-06-18 16:43:50 -0700 | [diff] [blame] | 49 | ASSERT_TRUE(mu.TryLock()); |
| 50 | MutexTester::AssertDepth(mu, 1U); |
| 51 | mu.Unlock(); |
| 52 | MutexTester::AssertDepth(mu, 0U); |
| 53 | } |
| 54 | |
| 55 | TEST(Mutex, TryLockUnlock) { |
| 56 | TryLockUnlockTest(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Elliott Hughes | 72d63d4 | 2012-06-18 16:51:20 -0700 | [diff] [blame^] | 59 | // GCC has trouble with our mutex tests, so we have to turn off thread safety analysis. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 60 | static 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 Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 68 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 69 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 70 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(Mutex, RecursiveLockUnlock) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 74 | RecursiveLockUnlockTest(); |
| 75 | } |
| 76 | |
Elliott Hughes | 72d63d4 | 2012-06-18 16:51:20 -0700 | [diff] [blame^] | 77 | // GCC has trouble with our mutex tests, so we have to turn off thread safety analysis. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 78 | static void RecursiveTryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS { |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 79 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 80 | MutexTester::AssertDepth(mu, 0U); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 81 | ASSERT_TRUE(mu.TryLock()); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 82 | MutexTester::AssertDepth(mu, 1U); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 83 | ASSERT_TRUE(mu.TryLock()); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 84 | MutexTester::AssertDepth(mu, 2U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 85 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 86 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 87 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 88 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | TEST(Mutex, RecursiveTryLockUnlock) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 92 | RecursiveTryLockUnlockTest(); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 93 | } |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 94 | |
| 95 | } // namespace art |