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