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 | |
| 7 | #include "gtest/gtest.h" |
| 8 | |
| 9 | #include "aos/linux_code/ipc_lib/aos_sync.h" |
| 10 | #include "aos/common/die.h" |
| 11 | #include "aos/common/util/death_test_log_implementation.h" |
| 12 | #include "aos/common/util/thread.h" |
| 13 | #include "aos/common/time.h" |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 14 | #include "aos/common/queue_testutils.h" |
| 15 | #include "aos/linux_code/ipc_lib/core_lib.h" |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 16 | |
| 17 | namespace aos { |
| 18 | namespace testing { |
| 19 | |
| 20 | class MutexTest : public ::testing::Test { |
| 21 | public: |
| 22 | Mutex test_mutex; |
| 23 | |
| 24 | protected: |
| 25 | void SetUp() override { |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 26 | ::aos::common::testing::EnableTestLogging(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 27 | SetDieTestMode(true); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | typedef MutexTest MutexDeathTest; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 32 | typedef MutexTest MutexLockerTest; |
| 33 | typedef MutexTest MutexLockerDeathTest; |
| 34 | typedef MutexTest IPCMutexLockerTest; |
| 35 | typedef MutexTest IPCMutexLockerDeathTest; |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 36 | |
| 37 | TEST_F(MutexTest, TryLock) { |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 38 | EXPECT_EQ(Mutex::State::kLocked, test_mutex.TryLock()); |
| 39 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex.TryLock()); |
| 40 | |
| 41 | test_mutex.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST_F(MutexTest, Lock) { |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 45 | ASSERT_FALSE(test_mutex.Lock()); |
| 46 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex.TryLock()); |
| 47 | |
| 48 | test_mutex.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST_F(MutexTest, Unlock) { |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 52 | ASSERT_FALSE(test_mutex.Lock()); |
| 53 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex.TryLock()); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 54 | test_mutex.Unlock(); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 55 | EXPECT_EQ(Mutex::State::kLocked, test_mutex.TryLock()); |
| 56 | |
| 57 | test_mutex.Unlock(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Sees what happens with multiple unlocks. |
| 61 | TEST_F(MutexDeathTest, RepeatUnlock) { |
Brian Silverman | 5c201e2 | 2014-06-12 22:40:28 -0700 | [diff] [blame] | 62 | logging::Init(); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 63 | ASSERT_FALSE(test_mutex.Lock()); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 64 | test_mutex.Unlock(); |
| 65 | EXPECT_DEATH( |
| 66 | { |
| 67 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
| 68 | test_mutex.Unlock(); |
| 69 | }, |
| 70 | ".*multiple unlock.*"); |
| 71 | } |
| 72 | |
| 73 | // Sees what happens if you unlock without ever locking (or unlocking) it. |
| 74 | TEST_F(MutexDeathTest, NeverLock) { |
Brian Silverman | 5c201e2 | 2014-06-12 22:40:28 -0700 | [diff] [blame] | 75 | logging::Init(); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 76 | EXPECT_DEATH( |
| 77 | { |
| 78 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
| 79 | test_mutex.Unlock(); |
| 80 | }, |
| 81 | ".*multiple unlock.*"); |
| 82 | } |
| 83 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 84 | // Sees what happens with multiple locks. |
| 85 | TEST_F(MutexDeathTest, RepeatLock) { |
| 86 | EXPECT_DEATH( |
| 87 | { |
| 88 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
| 89 | ASSERT_FALSE(test_mutex.Lock()); |
| 90 | ASSERT_FALSE(test_mutex.Lock()); |
| 91 | }, |
| 92 | ".*multiple lock.*"); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 95 | TEST_F(MutexDeathTest, DestroyLocked) { |
| 96 | EXPECT_DEATH( |
| 97 | { |
| 98 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
| 99 | Mutex new_mutex; |
| 100 | ASSERT_FALSE(new_mutex.Lock()); |
| 101 | }, |
| 102 | ".*destroying locked mutex.*"); |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | namespace { |
| 106 | |
| 107 | class AdderThread : public ::aos::util::Thread { |
| 108 | public: |
| 109 | AdderThread(int *counter, Mutex *mutex, ::aos::time::Time sleep_before_time, |
| 110 | ::aos::time::Time sleep_after_time) |
| 111 | : counter_(counter), |
| 112 | mutex_(mutex), |
| 113 | sleep_before_time_(sleep_before_time), |
| 114 | sleep_after_time_(sleep_after_time) {} |
| 115 | virtual void Run() override { |
| 116 | ::aos::time::SleepFor(sleep_before_time_); |
| 117 | MutexLocker locker(mutex_); |
| 118 | ++(*counter_); |
| 119 | ::aos::time::SleepFor(sleep_after_time_); |
| 120 | } |
| 121 | |
| 122 | private: |
| 123 | int *const counter_; |
| 124 | Mutex *const mutex_; |
| 125 | const ::aos::time::Time sleep_before_time_, sleep_after_time_; |
| 126 | }; |
| 127 | |
| 128 | } // namespace |
| 129 | |
| 130 | // Verifies that ThreadSanitizer understands that a contended mutex establishes |
| 131 | // a happens-before relationship. |
| 132 | TEST_F(MutexTest, ThreadSanitizerContended) { |
| 133 | int counter = 0; |
| 134 | AdderThread threads[2]{ |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 135 | {&counter, &test_mutex, ::aos::time::Time::InSeconds(0.2), |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 136 | ::aos::time::Time::InSeconds(0)}, |
| 137 | {&counter, &test_mutex, ::aos::time::Time::InSeconds(0), |
| 138 | ::aos::time::Time::InSeconds(0)}, }; |
| 139 | for (auto &c : threads) { |
| 140 | c.Start(); |
| 141 | } |
| 142 | for (auto &c : threads) { |
| 143 | c.WaitUntilDone(); |
| 144 | } |
| 145 | EXPECT_EQ(2, counter); |
| 146 | } |
| 147 | |
| 148 | // Verifies that ThreadSanitizer understands that an uncontended mutex |
| 149 | // establishes a happens-before relationship. |
| 150 | TEST_F(MutexTest, ThreadSanitizerUncontended) { |
| 151 | int counter = 0; |
| 152 | AdderThread threads[2]{ |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 153 | {&counter, &test_mutex, ::aos::time::Time::InSeconds(0.2), |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 154 | ::aos::time::Time::InSeconds(0)}, |
| 155 | {&counter, &test_mutex, ::aos::time::Time::InSeconds(0), |
| 156 | ::aos::time::Time::InSeconds(0)}, }; |
| 157 | for (auto &c : threads) { |
| 158 | c.Start(); |
| 159 | } |
| 160 | for (auto &c : threads) { |
| 161 | c.WaitUntilDone(); |
| 162 | } |
| 163 | EXPECT_EQ(2, counter); |
| 164 | } |
| 165 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 166 | TEST_F(MutexLockerTest, Basic) { |
| 167 | { |
| 168 | aos::MutexLocker locker(&test_mutex); |
| 169 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex.TryLock()); |
| 170 | } |
| 171 | EXPECT_EQ(Mutex::State::kLocked, test_mutex.TryLock()); |
| 172 | |
| 173 | test_mutex.Unlock(); |
| 174 | } |
| 175 | |
| 176 | TEST_F(IPCMutexLockerTest, Basic) { |
| 177 | { |
| 178 | aos::IPCMutexLocker locker(&test_mutex); |
| 179 | EXPECT_EQ(Mutex::State::kUnlocked, test_mutex.TryLock()); |
| 180 | EXPECT_FALSE(locker.owner_died()); |
| 181 | } |
| 182 | EXPECT_EQ(Mutex::State::kLocked, test_mutex.TryLock()); |
| 183 | |
| 184 | test_mutex.Unlock(); |
| 185 | } |
| 186 | |
| 187 | TEST_F(IPCMutexLockerDeathTest, NoCheckOwnerDied) { |
| 188 | EXPECT_DEATH({ aos::IPCMutexLocker locker(&test_mutex); }, |
| 189 | "nobody checked if the previous owner of mutex [^ ]+ died.*"); |
| 190 | } |
| 191 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 192 | } // namespace testing |
| 193 | } // namespace aos |