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