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