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 | |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 19 | #include "common_test.h" |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 23 | class MutexTest : public CommonTest {}; |
| 24 | |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 25 | struct MutexTester { |
| 26 | static void AssertDepth(Mutex& mu, uint32_t expected_depth) { |
| 27 | ASSERT_EQ(expected_depth, mu.GetDepth()); |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 28 | |
| 29 | // This test is single-threaded, so we also know _who_ should hold the lock. |
| 30 | if (expected_depth == 0) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 31 | mu.AssertNotHeld(Thread::Current()); |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 32 | } else { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 33 | mu.AssertHeld(Thread::Current()); |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 34 | } |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 35 | } |
| 36 | }; |
| 37 | |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 38 | TEST_F(MutexTest, LockUnlock) { |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 39 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 40 | MutexTester::AssertDepth(mu, 0U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 41 | mu.Lock(Thread::Current()); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 42 | MutexTester::AssertDepth(mu, 1U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 43 | mu.Unlock(Thread::Current()); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 44 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Elliott Hughes | 72d63d4 | 2012-06-18 16:51:20 -0700 | [diff] [blame] | 47 | // 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] | 48 | static void TryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS { |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 49 | Mutex mu("test mutex"); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 50 | MutexTester::AssertDepth(mu, 0U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(mu.TryLock(Thread::Current())); |
Elliott Hughes | 8141405 | 2012-06-18 16:43:50 -0700 | [diff] [blame] | 52 | MutexTester::AssertDepth(mu, 1U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 53 | mu.Unlock(Thread::Current()); |
Elliott Hughes | 8141405 | 2012-06-18 16:43:50 -0700 | [diff] [blame] | 54 | MutexTester::AssertDepth(mu, 0U); |
| 55 | } |
| 56 | |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 57 | TEST_F(MutexTest, TryLockUnlock) { |
Elliott Hughes | 8141405 | 2012-06-18 16:43:50 -0700 | [diff] [blame] | 58 | TryLockUnlockTest(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Elliott Hughes | 72d63d4 | 2012-06-18 16:51:20 -0700 | [diff] [blame] | 61 | // 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] | 62 | static void RecursiveLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 63 | Mutex mu("test mutex", kDefaultMutexLevel, true); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 64 | MutexTester::AssertDepth(mu, 0U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 65 | mu.Lock(Thread::Current()); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 66 | MutexTester::AssertDepth(mu, 1U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 67 | mu.Lock(Thread::Current()); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 68 | MutexTester::AssertDepth(mu, 2U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 69 | mu.Unlock(Thread::Current()); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 70 | MutexTester::AssertDepth(mu, 1U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 71 | mu.Unlock(Thread::Current()); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 72 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 75 | TEST_F(MutexTest, RecursiveLockUnlock) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 76 | RecursiveLockUnlockTest(); |
| 77 | } |
| 78 | |
Elliott Hughes | 72d63d4 | 2012-06-18 16:51:20 -0700 | [diff] [blame] | 79 | // 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] | 80 | static void RecursiveTryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 81 | Mutex mu("test mutex", kDefaultMutexLevel, true); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 82 | MutexTester::AssertDepth(mu, 0U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 83 | ASSERT_TRUE(mu.TryLock(Thread::Current())); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 84 | MutexTester::AssertDepth(mu, 1U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 85 | ASSERT_TRUE(mu.TryLock(Thread::Current())); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 86 | MutexTester::AssertDepth(mu, 2U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 87 | mu.Unlock(Thread::Current()); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 88 | MutexTester::AssertDepth(mu, 1U); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 89 | mu.Unlock(Thread::Current()); |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 90 | MutexTester::AssertDepth(mu, 0U); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 93 | TEST_F(MutexTest, RecursiveTryLockUnlock) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 94 | RecursiveTryLockUnlockTest(); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 95 | } |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 96 | |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 97 | |
| 98 | struct RecursiveLockWait { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 99 | explicit RecursiveLockWait() |
| 100 | : mu("test mutex", kDefaultMutexLevel, true), cv("test condition variable") { |
| 101 | } |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 102 | |
| 103 | static void* Callback(void* arg) { |
| 104 | RecursiveLockWait* state = reinterpret_cast<RecursiveLockWait*>(arg); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 105 | state->mu.Lock(Thread::Current()); |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 106 | state->cv.Signal(); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 107 | state->mu.Unlock(Thread::Current()); |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | Mutex mu; |
| 112 | ConditionVariable cv; |
| 113 | }; |
| 114 | |
| 115 | // GCC has trouble with our mutex tests, so we have to turn off thread safety analysis. |
| 116 | static void RecursiveLockWaitTest() NO_THREAD_SAFETY_ANALYSIS { |
| 117 | RecursiveLockWait state; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 118 | state.mu.Lock(Thread::Current()); |
| 119 | state.mu.Lock(Thread::Current()); |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 120 | |
| 121 | pthread_t pthread; |
| 122 | int pthread_create_result = pthread_create(&pthread, NULL, RecursiveLockWait::Callback, &state); |
| 123 | ASSERT_EQ(0, pthread_create_result); |
| 124 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 125 | state.cv.Wait(Thread::Current(), state.mu); |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 126 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 127 | state.mu.Unlock(Thread::Current()); |
| 128 | state.mu.Unlock(Thread::Current()); |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // This ensures we don't hang when waiting on a recursively locked mutex, |
| 132 | // which is not supported with bare pthread_mutex_t. |
| 133 | TEST_F(MutexTest, RecursiveLockWait) { |
| 134 | RecursiveLockWaitTest(); |
| 135 | } |
| 136 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 137 | TEST_F(MutexTest, SharedLockUnlock) { |
| 138 | ReaderWriterMutex mu("test rwmutex"); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 139 | mu.AssertNotHeld(Thread::Current()); |
| 140 | mu.AssertNotExclusiveHeld(Thread::Current()); |
| 141 | mu.SharedLock(Thread::Current()); |
| 142 | mu.AssertSharedHeld(Thread::Current()); |
| 143 | mu.AssertNotExclusiveHeld(Thread::Current()); |
| 144 | mu.SharedUnlock(Thread::Current()); |
| 145 | mu.AssertNotHeld(Thread::Current()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST_F(MutexTest, ExclusiveLockUnlock) { |
| 149 | ReaderWriterMutex mu("test rwmutex"); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 150 | mu.AssertNotHeld(Thread::Current()); |
| 151 | mu.ExclusiveLock(Thread::Current()); |
| 152 | mu.AssertSharedHeld(Thread::Current()); |
| 153 | mu.AssertExclusiveHeld(Thread::Current()); |
| 154 | mu.ExclusiveUnlock(Thread::Current()); |
| 155 | mu.AssertNotHeld(Thread::Current()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // GCC has trouble with our mutex tests, so we have to turn off thread safety analysis. |
| 159 | static void SharedTryLockUnlockTest() NO_THREAD_SAFETY_ANALYSIS { |
| 160 | ReaderWriterMutex mu("test rwmutex"); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 161 | mu.AssertNotHeld(Thread::Current()); |
| 162 | ASSERT_TRUE(mu.SharedTryLock(Thread::Current())); |
| 163 | mu.AssertSharedHeld(Thread::Current()); |
| 164 | mu.SharedUnlock(Thread::Current()); |
| 165 | mu.AssertNotHeld(Thread::Current()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST_F(MutexTest, SharedTryLockUnlock) { |
| 169 | SharedTryLockUnlockTest(); |
| 170 | } |
| 171 | |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 172 | } // namespace art |