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 | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 16 | #include "aos/common/queue_testutils.h" |
| 17 | #include "aos/linux_code/ipc_lib/core_lib.h" |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 18 | |
| 19 | namespace aos { |
| 20 | namespace testing { |
| 21 | |
| 22 | class MutexTest : public ::testing::Test { |
| 23 | public: |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 24 | Mutex test_mutex_; |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 25 | |
| 26 | protected: |
| 27 | void SetUp() override { |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 28 | ::aos::common::testing::EnableTestLogging(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 29 | SetDieTestMode(true); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | typedef MutexTest MutexDeathTest; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 34 | typedef MutexTest MutexLockerTest; |
| 35 | typedef MutexTest MutexLockerDeathTest; |
| 36 | typedef MutexTest IPCMutexLockerTest; |
| 37 | typedef MutexTest IPCMutexLockerDeathTest; |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 38 | typedef MutexTest IPCRecursiveMutexLockerTest; |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 39 | |
| 40 | TEST_F(MutexTest, TryLock) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 41 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
| 42 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 43 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 44 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST_F(MutexTest, Lock) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 48 | ASSERT_FALSE(test_mutex_.Lock()); |
| 49 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 50 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 51 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST_F(MutexTest, Unlock) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 55 | ASSERT_FALSE(test_mutex_.Lock()); |
| 56 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex_.TryLock()); |
| 57 | test_mutex_.Unlock(); |
| 58 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 59 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 60 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Sees what happens with multiple unlocks. |
| 64 | TEST_F(MutexDeathTest, RepeatUnlock) { |
Brian Silverman | 5c201e2 | 2014-06-12 22:40:28 -0700 | [diff] [blame] | 65 | logging::Init(); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 66 | ASSERT_FALSE(test_mutex_.Lock()); |
| 67 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 68 | EXPECT_DEATH( |
| 69 | { |
| 70 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 71 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 72 | }, |
| 73 | ".*multiple unlock.*"); |
| 74 | } |
| 75 | |
| 76 | // Sees what happens if you unlock without ever locking (or unlocking) it. |
| 77 | TEST_F(MutexDeathTest, NeverLock) { |
Brian Silverman | 5c201e2 | 2014-06-12 22:40:28 -0700 | [diff] [blame] | 78 | logging::Init(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 79 | EXPECT_DEATH( |
| 80 | { |
| 81 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 82 | test_mutex_.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 83 | }, |
| 84 | ".*multiple unlock.*"); |
| 85 | } |
| 86 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 87 | // Sees what happens with multiple locks. |
| 88 | TEST_F(MutexDeathTest, RepeatLock) { |
| 89 | EXPECT_DEATH( |
| 90 | { |
| 91 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 92 | ASSERT_FALSE(test_mutex_.Lock()); |
| 93 | ASSERT_FALSE(test_mutex_.Lock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 94 | }, |
| 95 | ".*multiple lock.*"); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 98 | TEST_F(MutexDeathTest, DestroyLocked) { |
| 99 | EXPECT_DEATH( |
| 100 | { |
| 101 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
| 102 | Mutex new_mutex; |
| 103 | ASSERT_FALSE(new_mutex.Lock()); |
| 104 | }, |
| 105 | ".*destroying locked mutex.*"); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | namespace { |
| 109 | |
| 110 | class AdderThread : public ::aos::util::Thread { |
| 111 | public: |
| 112 | AdderThread(int *counter, Mutex *mutex, ::aos::time::Time sleep_before_time, |
| 113 | ::aos::time::Time sleep_after_time) |
| 114 | : counter_(counter), |
| 115 | mutex_(mutex), |
| 116 | sleep_before_time_(sleep_before_time), |
| 117 | sleep_after_time_(sleep_after_time) {} |
| 118 | virtual void Run() override { |
| 119 | ::aos::time::SleepFor(sleep_before_time_); |
| 120 | MutexLocker locker(mutex_); |
| 121 | ++(*counter_); |
| 122 | ::aos::time::SleepFor(sleep_after_time_); |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | int *const counter_; |
| 127 | Mutex *const mutex_; |
| 128 | const ::aos::time::Time sleep_before_time_, sleep_after_time_; |
| 129 | }; |
| 130 | |
| 131 | } // namespace |
| 132 | |
| 133 | // Verifies that ThreadSanitizer understands that a contended mutex establishes |
| 134 | // a happens-before relationship. |
| 135 | TEST_F(MutexTest, ThreadSanitizerContended) { |
| 136 | int counter = 0; |
| 137 | AdderThread threads[2]{ |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 138 | {&counter, &test_mutex_, ::aos::time::Time::InSeconds(0.2), |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 139 | ::aos::time::Time::InSeconds(0)}, |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 140 | {&counter, &test_mutex_, ::aos::time::Time::InSeconds(0), |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 141 | ::aos::time::Time::InSeconds(0)}, }; |
| 142 | for (auto &c : threads) { |
| 143 | c.Start(); |
| 144 | } |
| 145 | for (auto &c : threads) { |
| 146 | c.WaitUntilDone(); |
| 147 | } |
| 148 | EXPECT_EQ(2, counter); |
| 149 | } |
| 150 | |
Brian Silverman | 119b3b1 | 2015-03-29 17:26:05 -0400 | [diff] [blame] | 151 | // Verifiers that ThreadSanitizer understands how a mutex works. |
| 152 | // For some reason this used to fail when the other tests didn't... |
| 153 | TEST_F(MutexTest, ThreadSanitizerMutexLocker) { |
| 154 | int counter = 0; |
| 155 | ::std::thread thread([&counter, this]() { |
| 156 | for (int i = 0; i < 1000; ++i) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 157 | MutexLocker locker(&test_mutex_); |
Brian Silverman | 119b3b1 | 2015-03-29 17:26:05 -0400 | [diff] [blame] | 158 | ++counter; |
| 159 | } |
| 160 | }); |
| 161 | for (int i = 0; i < 1000; ++i) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 162 | MutexLocker locker(&test_mutex_); |
Brian Silverman | 119b3b1 | 2015-03-29 17:26:05 -0400 | [diff] [blame] | 163 | --counter; |
| 164 | } |
| 165 | thread.join(); |
| 166 | EXPECT_EQ(0, counter); |
| 167 | } |
| 168 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 169 | // Verifies that ThreadSanitizer understands that an uncontended mutex |
| 170 | // establishes a happens-before relationship. |
| 171 | TEST_F(MutexTest, ThreadSanitizerUncontended) { |
| 172 | int counter = 0; |
| 173 | AdderThread threads[2]{ |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 174 | {&counter, &test_mutex_, ::aos::time::Time::InSeconds(0.2), |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 175 | ::aos::time::Time::InSeconds(0)}, |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 176 | {&counter, &test_mutex_, ::aos::time::Time::InSeconds(0), |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 177 | ::aos::time::Time::InSeconds(0)}, }; |
| 178 | for (auto &c : threads) { |
| 179 | c.Start(); |
| 180 | } |
| 181 | for (auto &c : threads) { |
| 182 | c.WaitUntilDone(); |
| 183 | } |
| 184 | EXPECT_EQ(2, counter); |
| 185 | } |
| 186 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 187 | namespace { |
| 188 | |
| 189 | class LockerThread : public util::Thread { |
| 190 | public: |
| 191 | LockerThread(Mutex *mutex, bool lock, bool unlock) |
| 192 | : mutex_(mutex), lock_(lock), unlock_(unlock) {} |
| 193 | |
| 194 | private: |
| 195 | virtual void Run() override { |
| 196 | if (lock_) ASSERT_FALSE(mutex_->Lock()); |
| 197 | if (unlock_) mutex_->Unlock(); |
| 198 | } |
| 199 | |
| 200 | Mutex *const mutex_; |
| 201 | const bool lock_, unlock_; |
| 202 | }; |
| 203 | |
| 204 | } // namespace |
| 205 | |
| 206 | // Makes sure that we don't SIGSEGV or something with multiple threads. |
| 207 | TEST_F(MutexTest, MultiThreadedLock) { |
| 208 | LockerThread t(&test_mutex_, true, true); |
| 209 | t.Start(); |
| 210 | ASSERT_FALSE(test_mutex_.Lock()); |
| 211 | test_mutex_.Unlock(); |
| 212 | t.Join(); |
| 213 | } |
| 214 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 215 | TEST_F(MutexLockerTest, Basic) { |
| 216 | { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 217 | aos::MutexLocker locker(&test_mutex_); |
| 218 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 219 | } |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 220 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 221 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 222 | test_mutex_.Unlock(); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST_F(IPCMutexLockerTest, Basic) { |
| 226 | { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 227 | aos::IPCMutexLocker locker(&test_mutex_); |
| 228 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 229 | EXPECT_FALSE(locker.owner_died()); |
| 230 | } |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 231 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 232 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 233 | test_mutex_.Unlock(); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 236 | // Tests what happens when the caller doesn't check if the previous owner died |
| 237 | // with an IPCMutexLocker. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 238 | TEST_F(IPCMutexLockerDeathTest, NoCheckOwnerDied) { |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 239 | EXPECT_DEATH({ aos::IPCMutexLocker locker(&test_mutex_); }, |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 240 | "nobody checked if the previous owner of mutex [^ ]+ died.*"); |
| 241 | } |
| 242 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame^] | 243 | TEST_F(IPCRecursiveMutexLockerTest, Basic) { |
| 244 | { |
| 245 | aos::IPCRecursiveMutexLocker locker(&test_mutex_); |
| 246 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex_.TryLock()); |
| 247 | EXPECT_FALSE(locker.owner_died()); |
| 248 | } |
| 249 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
| 250 | |
| 251 | test_mutex_.Unlock(); |
| 252 | } |
| 253 | |
| 254 | // Tests actually locking a mutex recursively with IPCRecursiveMutexLocker. |
| 255 | TEST_F(IPCRecursiveMutexLockerTest, RecursiveLock) { |
| 256 | { |
| 257 | aos::IPCRecursiveMutexLocker locker(&test_mutex_); |
| 258 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex_.TryLock()); |
| 259 | { |
| 260 | aos::IPCRecursiveMutexLocker locker(&test_mutex_); |
| 261 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex_.TryLock()); |
| 262 | EXPECT_FALSE(locker.owner_died()); |
| 263 | } |
| 264 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex_.TryLock()); |
| 265 | EXPECT_FALSE(locker.owner_died()); |
| 266 | } |
| 267 | EXPECT_EQ(Mutex::State::kLocked, test_mutex_.TryLock()); |
| 268 | |
| 269 | test_mutex_.Unlock(); |
| 270 | } |
| 271 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 272 | } // namespace testing |
| 273 | } // namespace aos |