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 | |
| 45 | TEST(Mutex, TryLockUnlock) { |
| 46 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 47 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 48 | mu.TryLock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 49 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 50 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 51 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST(Mutex, RecursiveLockUnlock) { |
| 55 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 56 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 57 | mu.Lock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 58 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 59 | mu.Lock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 60 | MutexTester::AssertDepth(mu, 2U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 61 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 62 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 63 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 64 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST(Mutex, RecursiveTryLockUnlock) { |
| 68 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 69 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 70 | mu.TryLock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 71 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 72 | mu.TryLock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 73 | MutexTester::AssertDepth(mu, 2U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 74 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 75 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 76 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 77 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 78 | } |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 79 | |
| 80 | } // namespace art |