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