John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/mutex/mutex.h" |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 2 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 3 | #include <math.h> |
| 4 | #include <pthread.h> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 5 | #include <sched.h> |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 6 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 7 | #include <chrono> |
Brian Silverman | 119b3b1 | 2015-03-29 17:26:05 -0400 | [diff] [blame] | 8 | #include <thread> |
| 9 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
| 11 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 12 | #include "aos/die.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 13 | #include "aos/ipc_lib/aos_sync.h" |
| 14 | #include "aos/ipc_lib/core_lib.h" |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 15 | #include "aos/testing/test_logging.h" |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 16 | #include "aos/testing/test_shm.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "aos/time/time.h" |
| 18 | #include "aos/util/death_test_log_implementation.h" |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 19 | |
| 20 | namespace aos { |
| 21 | namespace testing { |
| 22 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 23 | namespace chrono = ::std::chrono; |
| 24 | namespace this_thread = ::std::this_thread; |
| 25 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 26 | class MutexTest : public ::testing::Test { |
| 27 | public: |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 28 | Mutex test_mutex_; |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 29 | |
| 30 | protected: |
| 31 | void SetUp() override { |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 32 | ::aos::testing::EnableTestLogging(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 33 | SetDieTestMode(true); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | typedef MutexTest MutexDeathTest; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 38 | typedef MutexTest MutexLockerTest; |
| 39 | typedef MutexTest MutexLockerDeathTest; |
| 40 | typedef MutexTest IPCMutexLockerTest; |
| 41 | typedef MutexTest IPCMutexLockerDeathTest; |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 42 | typedef MutexTest IPCRecursiveMutexLockerTest; |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 43 | |
| 44 | TEST_F(MutexTest, TryLock) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 45 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 46 | EXPECT_EQ(Mutex::State::kLockFailed, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 47 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 48 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST_F(MutexTest, Lock) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 52 | ASSERT_FALSE(test_mutex_.Lock()); |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 53 | EXPECT_EQ(Mutex::State::kLockFailed, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 54 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 55 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | TEST_F(MutexTest, Unlock) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 59 | ASSERT_FALSE(test_mutex_.Lock()); |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 60 | EXPECT_EQ(Mutex::State::kLockFailed, test_mutex_.TryLock()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 61 | test_mutex_.Unlock(); |
| 62 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 63 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 64 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Sees what happens with multiple unlocks. |
| 68 | TEST_F(MutexDeathTest, RepeatUnlock) { |
Brian Silverman | 5c201e2 | 2014-06-12 22:40:28 -0700 | [diff] [blame] | 69 | logging::Init(); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 70 | ASSERT_FALSE(test_mutex_.Lock()); |
| 71 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 72 | EXPECT_DEATH( |
| 73 | { |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 74 | logging::SetImplementation(new util::DeathTestLogImplementation()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 75 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 76 | }, |
| 77 | ".*multiple unlock.*"); |
| 78 | } |
| 79 | |
| 80 | // Sees what happens if you unlock without ever locking (or unlocking) it. |
| 81 | TEST_F(MutexDeathTest, NeverLock) { |
Brian Silverman | 5c201e2 | 2014-06-12 22:40:28 -0700 | [diff] [blame] | 82 | logging::Init(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 83 | EXPECT_DEATH( |
| 84 | { |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 85 | logging::SetImplementation(new util::DeathTestLogImplementation()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 86 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 87 | }, |
| 88 | ".*multiple unlock.*"); |
| 89 | } |
| 90 | |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 91 | // Tests that locking a mutex multiple times from the same thread fails nicely. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 92 | TEST_F(MutexDeathTest, RepeatLock) { |
| 93 | EXPECT_DEATH( |
| 94 | { |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 95 | logging::SetImplementation(new util::DeathTestLogImplementation()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 96 | ASSERT_FALSE(test_mutex_.Lock()); |
| 97 | ASSERT_FALSE(test_mutex_.Lock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 98 | }, |
| 99 | ".*multiple lock.*"); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 102 | // Tests that Lock behaves correctly when the previous owner exits with the lock |
| 103 | // held (which is the same as dying any other way). |
| 104 | TEST_F(MutexTest, OwnerDiedDeathLock) { |
| 105 | testing::TestSharedMemory my_shm; |
| 106 | Mutex *mutex = |
| 107 | static_cast<Mutex *>(shm_malloc_aligned(sizeof(Mutex), alignof(Mutex))); |
| 108 | new (mutex) Mutex(); |
| 109 | |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 110 | std::thread thread([&]() { |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 111 | ASSERT_FALSE(mutex->Lock()); |
| 112 | }); |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 113 | thread.join(); |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 114 | EXPECT_TRUE(mutex->Lock()); |
| 115 | |
| 116 | mutex->Unlock(); |
| 117 | mutex->~Mutex(); |
| 118 | } |
| 119 | |
| 120 | // Tests that TryLock behaves correctly when the previous owner dies. |
| 121 | TEST_F(MutexTest, OwnerDiedDeathTryLock) { |
| 122 | testing::TestSharedMemory my_shm; |
| 123 | Mutex *mutex = |
| 124 | static_cast<Mutex *>(shm_malloc_aligned(sizeof(Mutex), alignof(Mutex))); |
| 125 | new (mutex) Mutex(); |
| 126 | |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 127 | std::thread thread([&]() { |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 128 | ASSERT_FALSE(mutex->Lock()); |
| 129 | }); |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 130 | thread.join(); |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 131 | EXPECT_EQ(Mutex::State::kOwnerDied, mutex->TryLock()); |
| 132 | |
| 133 | mutex->Unlock(); |
| 134 | mutex->~Mutex(); |
| 135 | } |
| 136 | |
| 137 | // TODO(brians): Test owner dying by being SIGKILLed and SIGTERMed. |
| 138 | |
| 139 | // This sequence of mutex operations used to mess up the robust list and cause |
| 140 | // one of the mutexes to not get owner-died like it should. |
| 141 | TEST_F(MutexTest, DontCorruptRobustList) { |
| 142 | // I think this was the allocator lock in the original failure. |
| 143 | Mutex mutex1; |
| 144 | // This one should get owner-died afterwards (iff the kernel accepts the |
| 145 | // robust list and uses it). I think it was the task_death_notification lock |
| 146 | // in the original failure. |
| 147 | Mutex mutex2; |
| 148 | |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 149 | std::thread thread([&]() { |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 150 | ASSERT_FALSE(mutex1.Lock()); |
| 151 | ASSERT_FALSE(mutex2.Lock()); |
| 152 | mutex1.Unlock(); |
| 153 | }); |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 154 | thread.join(); |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 155 | |
| 156 | EXPECT_EQ(Mutex::State::kLocked, mutex1.TryLock()); |
| 157 | EXPECT_EQ(Mutex::State::kOwnerDied, mutex2.TryLock()); |
| 158 | |
| 159 | mutex1.Unlock(); |
| 160 | mutex2.Unlock(); |
| 161 | } |
| 162 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 163 | // Verifies that ThreadSanitizer understands that a contended mutex establishes |
| 164 | // a happens-before relationship. |
| 165 | TEST_F(MutexTest, ThreadSanitizerContended) { |
| 166 | int counter = 0; |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 167 | std::thread thread1([this, &counter]() { |
| 168 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 169 | MutexLocker locker(&test_mutex_); |
| 170 | ++counter; |
| 171 | }); |
| 172 | std::thread thread2([this, &counter]() { |
| 173 | MutexLocker locker(&test_mutex_); |
| 174 | ++counter; |
| 175 | }); |
| 176 | thread1.join(); |
| 177 | thread2.join(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 178 | EXPECT_EQ(2, counter); |
| 179 | } |
| 180 | |
Brian Silverman | 119b3b1 | 2015-03-29 17:26:05 -0400 | [diff] [blame] | 181 | // Verifiers that ThreadSanitizer understands how a mutex works. |
| 182 | // For some reason this used to fail when the other tests didn't... |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 183 | // The loops make it fail more reliably when it's going to. |
Brian Silverman | 119b3b1 | 2015-03-29 17:26:05 -0400 | [diff] [blame] | 184 | TEST_F(MutexTest, ThreadSanitizerMutexLocker) { |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 185 | for (int i = 0; i < 100; ++i) { |
| 186 | int counter = 0; |
| 187 | ::std::thread thread([&counter, this]() { |
| 188 | for (int i = 0; i < 300; ++i) { |
| 189 | MutexLocker locker(&test_mutex_); |
| 190 | ++counter; |
| 191 | } |
| 192 | }); |
| 193 | for (int i = 0; i < 300; ++i) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 194 | MutexLocker locker(&test_mutex_); |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 195 | --counter; |
Brian Silverman | 119b3b1 | 2015-03-29 17:26:05 -0400 | [diff] [blame] | 196 | } |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 197 | thread.join(); |
| 198 | EXPECT_EQ(0, counter); |
Brian Silverman | 119b3b1 | 2015-03-29 17:26:05 -0400 | [diff] [blame] | 199 | } |
Brian Silverman | 119b3b1 | 2015-03-29 17:26:05 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 202 | // Verifies that ThreadSanitizer understands that an uncontended mutex |
| 203 | // establishes a happens-before relationship. |
| 204 | TEST_F(MutexTest, ThreadSanitizerUncontended) { |
| 205 | int counter = 0; |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 206 | std::thread thread1([this, &counter]() { |
| 207 | MutexLocker locker(&test_mutex_); |
| 208 | ++counter; |
| 209 | }); |
| 210 | std::thread thread2([this, &counter]() { |
| 211 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 212 | MutexLocker locker(&test_mutex_); |
| 213 | ++counter; |
| 214 | }); |
| 215 | thread1.join(); |
| 216 | thread2.join(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 217 | EXPECT_EQ(2, counter); |
| 218 | } |
| 219 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 220 | // Makes sure that we don't SIGSEGV or something with multiple threads. |
| 221 | TEST_F(MutexTest, MultiThreadedLock) { |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 222 | std::thread thread([this] { |
| 223 | ASSERT_FALSE(test_mutex_.Lock()); |
| 224 | test_mutex_.Unlock(); |
| 225 | }); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 226 | ASSERT_FALSE(test_mutex_.Lock()); |
| 227 | test_mutex_.Unlock(); |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 228 | thread.join(); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 231 | TEST_F(MutexLockerTest, Basic) { |
| 232 | { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 233 | aos::MutexLocker locker(&test_mutex_); |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 234 | EXPECT_EQ(Mutex::State::kLockFailed, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 235 | } |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 236 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 237 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 238 | test_mutex_.Unlock(); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 241 | // Tests that MutexLocker behaves correctly when the previous owner dies. |
| 242 | TEST_F(MutexLockerDeathTest, OwnerDied) { |
| 243 | testing::TestSharedMemory my_shm; |
| 244 | Mutex *mutex = |
| 245 | static_cast<Mutex *>(shm_malloc_aligned(sizeof(Mutex), alignof(Mutex))); |
| 246 | new (mutex) Mutex(); |
| 247 | |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 248 | std::thread thread([&]() { |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 249 | ASSERT_FALSE(mutex->Lock()); |
| 250 | }); |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 251 | thread.join(); |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 252 | EXPECT_DEATH( |
| 253 | { |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 254 | logging::SetImplementation(new util::DeathTestLogImplementation()); |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 255 | MutexLocker locker(mutex); |
| 256 | }, |
| 257 | ".*previous owner of mutex [^ ]+ died.*"); |
| 258 | |
| 259 | mutex->~Mutex(); |
| 260 | } |
| 261 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 262 | TEST_F(IPCMutexLockerTest, Basic) { |
| 263 | { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 264 | aos::IPCMutexLocker locker(&test_mutex_); |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 265 | EXPECT_EQ(Mutex::State::kLockFailed, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 266 | EXPECT_FALSE(locker.owner_died()); |
| 267 | } |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 268 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 269 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 270 | test_mutex_.Unlock(); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 273 | // Tests what happens when the caller doesn't check if the previous owner died |
| 274 | // with an IPCMutexLocker. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 275 | TEST_F(IPCMutexLockerDeathTest, NoCheckOwnerDied) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 276 | EXPECT_DEATH({ aos::IPCMutexLocker locker(&test_mutex_); }, |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 277 | "nobody checked if the previous owner of mutex [^ ]+ died.*"); |
| 278 | } |
| 279 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 280 | TEST_F(IPCRecursiveMutexLockerTest, Basic) { |
| 281 | { |
| 282 | aos::IPCRecursiveMutexLocker locker(&test_mutex_); |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 283 | EXPECT_EQ(Mutex::State::kLockFailed, test_mutex_.TryLock()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 284 | EXPECT_FALSE(locker.owner_died()); |
| 285 | } |
| 286 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
| 287 | |
| 288 | test_mutex_.Unlock(); |
| 289 | } |
| 290 | |
| 291 | // Tests actually locking a mutex recursively with IPCRecursiveMutexLocker. |
| 292 | TEST_F(IPCRecursiveMutexLockerTest, RecursiveLock) { |
| 293 | { |
| 294 | aos::IPCRecursiveMutexLocker locker(&test_mutex_); |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 295 | EXPECT_EQ(Mutex::State::kLockFailed, test_mutex_.TryLock()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 296 | { |
| 297 | aos::IPCRecursiveMutexLocker locker(&test_mutex_); |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 298 | EXPECT_EQ(Mutex::State::kLockFailed, test_mutex_.TryLock()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 299 | EXPECT_FALSE(locker.owner_died()); |
| 300 | } |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 301 | EXPECT_EQ(Mutex::State::kLockFailed, test_mutex_.TryLock()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 302 | EXPECT_FALSE(locker.owner_died()); |
| 303 | } |
| 304 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
| 305 | |
| 306 | test_mutex_.Unlock(); |
| 307 | } |
| 308 | |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 309 | // Tests that IPCMutexLocker behaves correctly when the previous owner dies. |
| 310 | TEST_F(IPCMutexLockerTest, OwnerDied) { |
| 311 | testing::TestSharedMemory my_shm; |
| 312 | Mutex *mutex = |
| 313 | static_cast<Mutex *>(shm_malloc_aligned(sizeof(Mutex), alignof(Mutex))); |
| 314 | new (mutex) Mutex(); |
| 315 | |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 316 | std::thread thread([&]() { |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 317 | ASSERT_FALSE(mutex->Lock()); |
| 318 | }); |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame^] | 319 | thread.join(); |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 320 | { |
| 321 | aos::IPCMutexLocker locker(mutex); |
| 322 | EXPECT_EQ(Mutex::State::kLockFailed, mutex->TryLock()); |
| 323 | EXPECT_TRUE(locker.owner_died()); |
| 324 | } |
| 325 | EXPECT_EQ(Mutex::State::kLocked, mutex->TryLock()); |
| 326 | |
| 327 | mutex->Unlock(); |
| 328 | mutex->~Mutex(); |
| 329 | } |
| 330 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 331 | } // namespace testing |
| 332 | } // namespace aos |