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