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