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