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