Add SetRuntimeRealtimePriority

This lets us move the concept of priority into the event loop rather
than trying to manage it elsewhere.  Also add tests to confirm I got
them all.

Change-Id: Iba2651778e8d1eb88dedf71d0efdf05dfc999f2d
diff --git a/aos/events/shm-event-loop.cc b/aos/events/shm-event-loop.cc
index 11d9312..ee71b4c 100644
--- a/aos/events/shm-event-loop.cc
+++ b/aos/events/shm-event-loop.cc
@@ -6,6 +6,7 @@
 #include <chrono>
 #include <stdexcept>
 
+#include "aos/init.h"
 #include "aos/logging/logging.h"
 #include "aos/queue.h"
 
@@ -122,9 +123,13 @@
   }
 
   void Run() {
+    thread_state_->MaybeSetCurrentThreadRealtimePriority();
     thread_state_->WaitForStart();
 
-    if (!thread_state_->is_running()) return;
+    if (!thread_state_->is_running()) {
+      ::aos::UnsetCurrentThreadRealtimePriority();
+      return;
+    }
 
     const void *msg = nullptr;
     while (true) {
@@ -143,6 +148,7 @@
     }
 
     queue_->FreeMessage(msg);
+    ::aos::UnsetCurrentThreadRealtimePriority();
   }
 
  private:
@@ -180,6 +186,7 @@
   }
 
   void Run() {
+    thread_state_->MaybeSetCurrentThreadRealtimePriority();
     thread_state_->WaitForStart();
 
     while (true) {
@@ -196,6 +203,7 @@
         if (!thread_state_->is_running()) break;
       }
     }
+    ::aos::UnsetCurrentThreadRealtimePriority();
   }
 
  private:
@@ -256,11 +264,13 @@
 }
 
 void ShmEventLoop::Run() {
+  thread_state_->MaybeSetCurrentThreadRealtimePriority();
   set_is_running(true);
   for (const auto &run : on_run_) run();
   // TODO(austin): epoll event loop in main thread (if needed), and async safe
   // quit handler.
   thread_state_->Run();
+  ::aos::UnsetCurrentThreadRealtimePriority();
 }
 
 void ShmEventLoop::ThreadState::Run() {
@@ -275,6 +285,12 @@
   }
 }
 
+void ShmEventLoop::ThreadState::MaybeSetCurrentThreadRealtimePriority() {
+  if (priority_ != -1) {
+    ::aos::SetCurrentThreadRealtimePriority(priority_);
+  }
+}
+
 void ShmEventLoop::ThreadState::WaitForStart() {
   MutexLocker locker(&mutex_);
   while (!(loop_running_ || loop_finished_)) {