Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2016. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 8 | #include "HAL/cpp/priority_mutex.h" |
| 9 | #include "TestBench.h" |
| 10 | |
| 11 | #include "gtest/gtest.h" |
| 12 | #include <atomic> |
| 13 | #include <condition_variable> |
| 14 | #include <thread> |
| 15 | |
| 16 | namespace wpilib { |
| 17 | namespace testing { |
| 18 | |
| 19 | using std::chrono::system_clock; |
| 20 | |
| 21 | // Threading primitive used to notify many threads that a condition is now true. |
| 22 | // The condition can not be cleared. |
| 23 | class Notification { |
| 24 | public: |
| 25 | // Efficiently waits until the Notification has been notified once. |
| 26 | void Wait() { |
| 27 | std::unique_lock<priority_mutex> lock(m_mutex); |
| 28 | while (!m_set) { |
| 29 | m_condition.wait(lock); |
| 30 | } |
| 31 | } |
| 32 | // Sets the condition to true, and wakes all waiting threads. |
| 33 | void Notify() { |
| 34 | std::lock_guard<priority_mutex> lock(m_mutex); |
| 35 | m_set = true; |
| 36 | m_condition.notify_all(); |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | // priority_mutex used for the notification and to protect the bit. |
| 41 | priority_mutex m_mutex; |
| 42 | // Condition for threads to sleep on. |
| 43 | std::condition_variable_any m_condition; |
| 44 | // Bool which is true when the notification has been notified. |
| 45 | bool m_set = false; |
| 46 | }; |
| 47 | |
| 48 | void SetProcessorAffinity(int core_id) { |
| 49 | cpu_set_t cpuset; |
| 50 | CPU_ZERO(&cpuset); |
| 51 | CPU_SET(core_id, &cpuset); |
| 52 | |
| 53 | pthread_t current_thread = pthread_self(); |
| 54 | ASSERT_TRUE( |
| 55 | pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset) == 0); |
| 56 | } |
| 57 | |
| 58 | void SetThreadRealtimePriorityOrDie(int priority) { |
| 59 | struct sched_param param; |
| 60 | // Set realtime priority for this thread |
| 61 | param.sched_priority = priority + sched_get_priority_min(SCHED_RR); |
| 62 | ASSERT_TRUE(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m) == 0) |
| 63 | << ": Failed to set scheduler priority."; |
| 64 | } |
| 65 | |
| 66 | // This thread holds the mutex and spins until signaled to release it and stop. |
| 67 | template <typename MutexType> |
| 68 | class LowPriorityThread { |
| 69 | public: |
| 70 | LowPriorityThread(MutexType *mutex) |
| 71 | : m_mutex(mutex), m_hold_mutex(1), m_success(0) {} |
| 72 | |
| 73 | void operator()() { |
| 74 | SetProcessorAffinity(0); |
| 75 | SetThreadRealtimePriorityOrDie(20); |
| 76 | m_mutex->lock(); |
| 77 | m_started.Notify(); |
| 78 | while (m_hold_mutex.test_and_set()) {} |
| 79 | m_mutex->unlock(); |
| 80 | m_success.store(1); |
| 81 | } |
| 82 | |
| 83 | void WaitForStartup() { m_started.Wait(); } |
| 84 | void release_mutex() { m_hold_mutex.clear(); } |
| 85 | bool success() { return m_success.load(); } |
| 86 | |
| 87 | private: |
| 88 | // priority_mutex to grab and release. |
| 89 | MutexType *m_mutex; |
| 90 | Notification m_started; |
| 91 | // Atomic type used to signal when the thread should unlock the mutex. |
| 92 | // Using a mutex to protect something could cause other issues, and a delay |
| 93 | // between setting and reading isn't a problem as long as the set is atomic. |
| 94 | std::atomic_flag m_hold_mutex; |
| 95 | std::atomic<int> m_success; |
| 96 | }; |
| 97 | |
| 98 | // This thread spins forever until signaled to stop. |
| 99 | class BusyWaitingThread { |
| 100 | public: |
| 101 | BusyWaitingThread() : m_run(1), m_success(0) {} |
| 102 | |
| 103 | void operator()() { |
| 104 | SetProcessorAffinity(0); |
| 105 | SetThreadRealtimePriorityOrDie(21); |
| 106 | system_clock::time_point start_time = system_clock::now(); |
| 107 | m_started.Notify(); |
| 108 | while (m_run.test_and_set()) { |
| 109 | // Have the busy waiting thread time out after a while. If it times out, |
| 110 | // the test failed. |
| 111 | if (system_clock::now() - start_time > std::chrono::milliseconds(50)) { |
| 112 | return; |
| 113 | } |
| 114 | } |
| 115 | m_success.store(1); |
| 116 | } |
| 117 | |
| 118 | void quit() { m_run.clear(); } |
| 119 | void WaitForStartup() { m_started.Wait(); } |
| 120 | bool success() { return m_success.load(); } |
| 121 | |
| 122 | private: |
| 123 | // Use an atomic type to signal if the thread should be running or not. A |
| 124 | // mutex could affect the scheduler, which isn't worth it. A delay between |
| 125 | // setting and reading the new value is fine. |
| 126 | std::atomic_flag m_run; |
| 127 | |
| 128 | Notification m_started; |
| 129 | |
| 130 | std::atomic<int> m_success; |
| 131 | }; |
| 132 | |
| 133 | // This thread starts up, grabs the mutex, and then exits. |
| 134 | template <typename MutexType> |
| 135 | class HighPriorityThread { |
| 136 | public: |
| 137 | HighPriorityThread(MutexType *mutex) : m_mutex(mutex), m_success(0) {} |
| 138 | |
| 139 | void operator()() { |
| 140 | SetProcessorAffinity(0); |
| 141 | SetThreadRealtimePriorityOrDie(22); |
| 142 | m_started.Notify(); |
| 143 | m_mutex->lock(); |
| 144 | m_success.store(1); |
| 145 | } |
| 146 | |
| 147 | void WaitForStartup() { m_started.Wait(); } |
| 148 | bool success() { return m_success.load(); } |
| 149 | |
| 150 | private: |
| 151 | Notification m_started; |
| 152 | MutexType *m_mutex; |
| 153 | std::atomic<int> m_success; |
| 154 | }; |
| 155 | |
| 156 | // Class to test a MutexType to see if it solves the priority inheritance |
| 157 | // problem. |
| 158 | // |
| 159 | // To run the test, we need 3 threads, and then 1 thread to kick the test off. |
| 160 | // The threads must all run on the same core, otherwise they wouldn't starve |
| 161 | // eachother. The threads and their roles are as follows: |
| 162 | // |
| 163 | // Low priority thread: |
| 164 | // Holds a lock that the high priority thread needs, and releases it upon |
| 165 | // request. |
| 166 | // Medium priority thread: |
| 167 | // Hogs the processor so that the low priority thread will never run if it's |
| 168 | // priority doesn't get bumped. |
| 169 | // High priority thread: |
| 170 | // Starts up and then goes to grab the lock that the low priority thread has. |
| 171 | // |
| 172 | // Control thread: |
| 173 | // Sets the deadlock up so that it will happen 100% of the time by making sure |
| 174 | // that each thread in order gets to the point that it needs to be at to cause |
| 175 | // the deadlock. |
| 176 | template <typename MutexType> |
| 177 | class InversionTestRunner { |
| 178 | public: |
| 179 | void operator()() { |
| 180 | // This thread must run at the highest priority or it can't coordinate the |
| 181 | // inversion. This means that it can't busy wait or everything could |
| 182 | // starve. |
| 183 | SetThreadRealtimePriorityOrDie(23); |
| 184 | |
| 185 | MutexType m; |
| 186 | |
| 187 | // Start the lowest priority thread and wait until it holds the lock. |
| 188 | LowPriorityThread<MutexType> low(&m); |
| 189 | std::thread low_thread(std::ref(low)); |
| 190 | low.WaitForStartup(); |
| 191 | |
| 192 | // Start the busy waiting thread and let it get to the loop. |
| 193 | BusyWaitingThread busy; |
| 194 | std::thread busy_thread(std::ref(busy)); |
| 195 | busy.WaitForStartup(); |
| 196 | |
| 197 | // Start the high priority thread and let it become blocked on the lock. |
| 198 | HighPriorityThread<MutexType> high(&m); |
| 199 | std::thread high_thread(std::ref(high)); |
| 200 | high.WaitForStartup(); |
| 201 | // Startup and locking the mutex in the high priority thread aren't atomic, |
| 202 | // but pretty close. Wait a bit to let it happen. |
| 203 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 204 | |
| 205 | // Release the mutex in the low priority thread. If done right, everything |
| 206 | // should finish now. |
| 207 | low.release_mutex(); |
| 208 | |
| 209 | // Wait for everything to finish and compute success. |
| 210 | high_thread.join(); |
| 211 | busy.quit(); |
| 212 | busy_thread.join(); |
| 213 | low_thread.join(); |
| 214 | m_success = low.success() && busy.success() && high.success(); |
| 215 | } |
| 216 | |
| 217 | bool success() { return m_success; } |
| 218 | |
| 219 | private: |
| 220 | bool m_success = false; |
| 221 | }; |
| 222 | |
| 223 | //TODO: Fix roborio permissions to run as root. |
| 224 | |
| 225 | // Priority inversion test. |
| 226 | TEST(MutexTest, DISABLED_PriorityInversionTest) { |
| 227 | InversionTestRunner<priority_mutex> runner; |
| 228 | std::thread runner_thread(std::ref(runner)); |
| 229 | runner_thread.join(); |
| 230 | EXPECT_TRUE(runner.success()); |
| 231 | } |
| 232 | |
| 233 | // Verify that the non-priority inversion mutex doesn't pass the test. |
| 234 | TEST(MutexTest, DISABLED_StdMutexPriorityInversionTest) { |
| 235 | InversionTestRunner<std::mutex> runner; |
| 236 | std::thread runner_thread(std::ref(runner)); |
| 237 | runner_thread.join(); |
| 238 | EXPECT_FALSE(runner.success()); |
| 239 | } |
| 240 | |
| 241 | // Smoke test to make sure that mutexes lock and unlock. |
| 242 | TEST(MutexTest, TryLock) { |
| 243 | priority_mutex m; |
| 244 | m.lock(); |
| 245 | EXPECT_FALSE(m.try_lock()); |
| 246 | m.unlock(); |
| 247 | EXPECT_TRUE(m.try_lock()); |
| 248 | } |
| 249 | |
| 250 | // Priority inversion test. |
| 251 | TEST(MutexTest, DISABLED_ReentrantPriorityInversionTest) { |
| 252 | InversionTestRunner<priority_recursive_mutex> runner; |
| 253 | std::thread runner_thread(std::ref(runner)); |
| 254 | runner_thread.join(); |
| 255 | EXPECT_TRUE(runner.success()); |
| 256 | } |
| 257 | |
| 258 | // Smoke test to make sure that mutexes lock and unlock. |
| 259 | TEST(MutexTest, ReentrantTryLock) { |
| 260 | priority_recursive_mutex m; |
| 261 | m.lock(); |
| 262 | EXPECT_TRUE(m.try_lock()); |
| 263 | m.unlock(); |
| 264 | EXPECT_TRUE(m.try_lock()); |
| 265 | } |
| 266 | |
| 267 | } // namespace testing |
| 268 | } // namespace wpilib |