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