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 | #if !defined(__APPLE__) |
| 24 | struct MutexTester { |
| 25 | static void AssertDepth(Mutex& mu, uint32_t expected_depth) { |
| 26 | ASSERT_EQ(expected_depth, mu.GetDepth()); |
| 27 | } |
| 28 | }; |
| 29 | |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 30 | TEST(Mutex, LockUnlock) { |
| 31 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 32 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 33 | mu.Lock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 34 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 35 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 36 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | TEST(Mutex, TryLockUnlock) { |
| 40 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 41 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 42 | mu.TryLock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 43 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 44 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 45 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | TEST(Mutex, RecursiveLockUnlock) { |
| 49 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 50 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 51 | mu.Lock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 52 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 53 | mu.Lock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 54 | MutexTester::AssertDepth(mu, 2U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 55 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 56 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 57 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 58 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST(Mutex, RecursiveTryLockUnlock) { |
| 62 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 63 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 64 | mu.TryLock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 65 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 66 | mu.TryLock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 67 | MutexTester::AssertDepth(mu, 2U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 68 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 69 | MutexTester::AssertDepth(mu, 1U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 70 | mu.Unlock(); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 71 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 72 | } |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame^] | 73 | #endif |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 74 | |
| 75 | } // namespace art |