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