Up priorities of some of threads.

Specifically:
-Set RT priorities on the 2020 wpilib_interface threads.
-Set RT priorities on some of the threads spawned by the HAL in
 wpilib_interface.
-Catch both spi0 and spi1 in the ADIS16470 driver, although I'm guessing
 that only spi0 really matters.

 Also, make the ShmEventLoop name propagate through to the thread name
 so that we have sane thread names on more things.

Change-Id: I4d986ba2eb19d90a0a56b40e0ec9766300a95076
diff --git a/aos/events/shm_event_loop.cc b/aos/events/shm_event_loop.cc
index bdee4f2..8824181 100644
--- a/aos/events/shm_event_loop.cc
+++ b/aos/events/shm_event_loop.cc
@@ -766,6 +766,7 @@
 
   ReserveEvents();
 
+  aos::SetCurrentThreadName(name_.substr(0, 16));
   // Now, all the callbacks are setup.  Lock everything into memory and go RT.
   if (priority_ != 0) {
     ::aos::InitRT();
@@ -847,6 +848,11 @@
   priority_ = priority;
 }
 
+void ShmEventLoop::set_name(const std::string_view name) {
+  name_ = std::string(name);
+  UpdateTimingReport();
+}
+
 pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); }
 
 }  // namespace aos
diff --git a/aos/events/shm_event_loop.h b/aos/events/shm_event_loop.h
index bb4ad77..c888e57 100644
--- a/aos/events/shm_event_loop.h
+++ b/aos/events/shm_event_loop.h
@@ -62,10 +62,7 @@
 
   void SetRuntimeRealtimePriority(int priority) override;
 
-  void set_name(const std::string_view name) override {
-    name_ = std::string(name);
-    UpdateTimingReport();
-  }
+  void set_name(const std::string_view name) override;
   const std::string_view name() const override { return name_; }
   const Node *node() const override { return node_; }
 
diff --git a/aos/realtime.cc b/aos/realtime.cc
index 60f712b..9f7e810 100644
--- a/aos/realtime.cc
+++ b/aos/realtime.cc
@@ -116,10 +116,11 @@
       << ": sched_setscheduler(0, SCHED_OTHER, 0) failed";
 }
 
-void SetCurrentThreadName(const ::std::string &name) {
+void SetCurrentThreadName(const std::string_view name) {
   CHECK_LE(name.size(), 16u) << ": thread name '" << name << "' too long";
   VLOG(1) << "This thread is changing to '" << name << "'";
-  PCHECK(prctl(PR_SET_NAME, name.c_str()) == 0);
+  std::string string_name(name);
+  PCHECK(prctl(PR_SET_NAME, string_name.c_str()) == 0);
   if (&logging::internal::ReloadThreadName != nullptr) {
     logging::internal::ReloadThreadName();
   }
diff --git a/aos/realtime.h b/aos/realtime.h
index bc80ee5..20df979 100644
--- a/aos/realtime.h
+++ b/aos/realtime.h
@@ -1,7 +1,7 @@
 #ifndef AOS_REALTIME_H_
 #define AOS_REALTIME_H_
 
-#include <string>
+#include <string_view>
 
 namespace aos {
 
@@ -16,7 +16,7 @@
 // Sets the name of the current thread.
 // This will displayed by `top -H`, dump_rtprio, and show up in logs.
 // name can have a maximum of 16 characters.
-void SetCurrentThreadName(const ::std::string &name);
+void SetCurrentThreadName(const std::string_view name);
 
 // Sets the current thread's realtime priority.
 void SetCurrentThreadRealtimePriority(int priority);
diff --git a/frc971/wpilib/ADIS16470.cc b/frc971/wpilib/ADIS16470.cc
index b7cce1e..c42e964 100644
--- a/frc971/wpilib/ADIS16470.cc
+++ b/frc971/wpilib/ADIS16470.cc
@@ -196,6 +196,9 @@
   PCHECK(
       system("ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p "
              "33") == 0);
+  PCHECK(
+      system("ps -ef | grep '\\[spi1\\]' | awk '{print $1}' | xargs chrt -f -p "
+             "33") == 0);
 
   event_loop_->OnRun([this]() { BeginInitialization(); });
 }
diff --git a/frc971/wpilib/ahal/BUILD b/frc971/wpilib/ahal/BUILD
index 3f06ff8..44341b7 100644
--- a/frc971/wpilib/ahal/BUILD
+++ b/frc971/wpilib/ahal/BUILD
@@ -18,6 +18,7 @@
     visibility = ["//third_party:__pkg__"],
     deps = [
         "//aos:make_unique",
+        "//aos:realtime",
         "//aos/logging",
         "//third_party:wpilib_hal",
     ],
diff --git a/frc971/wpilib/ahal/RobotBase.h b/frc971/wpilib/ahal/RobotBase.h
index 24e2898..cf1ed4c 100644
--- a/frc971/wpilib/ahal/RobotBase.h
+++ b/frc971/wpilib/ahal/RobotBase.h
@@ -11,6 +11,7 @@
 #include <iostream>
 #include <thread>
 
+#include "aos/realtime.h"
 #include "hal/HAL.h"
 #include "frc971/wpilib/ahal/Base.h"
 
@@ -34,11 +35,15 @@
 #else
 #define START_ROBOT_CLASS(_ClassName_)                                       \
   int main(int argc, char *argv[]) {                                         \
-    ::aos::InitGoogle(&argc, &argv);                                         \
+    aos::InitGoogle(&argc, &argv);                                           \
+    /* HAL_Initialize spawns several threads, including the CAN drivers.  */ \
+    /* Go to realtime so that the child threads are RT.                   */ \
+    aos::SetCurrentThreadRealtimePriority(10);                               \
     if (!HAL_Initialize(500, 0)) {                                           \
       std::cerr << "FATAL ERROR: HAL could not be initialized" << std::endl; \
       return -1;                                                             \
     }                                                                        \
+    aos::UnsetCurrentThreadRealtimePriority();                               \
     HAL_Report(HALUsageReporting::kResourceType_Language,                    \
                HALUsageReporting::kLanguage_CPlusPlus);                      \
     static _ClassName_ robot;                                                \
diff --git a/frc971/wpilib/joystick_sender.cc b/frc971/wpilib/joystick_sender.cc
index 2fec111..c614cfe 100644
--- a/frc971/wpilib/joystick_sender.cc
+++ b/frc971/wpilib/joystick_sender.cc
@@ -14,16 +14,16 @@
 
 using aos::Joystick;
 
-JoystickSender::JoystickSender(::aos::EventLoop *event_loop)
+JoystickSender::JoystickSender(::aos::ShmEventLoop *event_loop)
     : event_loop_(event_loop),
       joystick_state_sender_(
           event_loop_->MakeSender<::aos::JoystickState>("/aos")),
       team_id_(::aos::network::GetTeamNumber()) {
   event_loop_->SetRuntimeRealtimePriority(29);
+  event_loop->set_name("joystick_sender");
 
   event_loop_->OnRun([this]() {
     frc::DriverStation *const ds = &frc::DriverStation::GetInstance();
-    ::aos::SetCurrentThreadName("DSReader");
 
     // TODO(Brian): Fix the potential deadlock when stopping here (condition
     // variable / mutex needs to get exposed all the way out or something).
diff --git a/frc971/wpilib/joystick_sender.h b/frc971/wpilib/joystick_sender.h
index e2609e8..6622d85 100644
--- a/frc971/wpilib/joystick_sender.h
+++ b/frc971/wpilib/joystick_sender.h
@@ -3,7 +3,7 @@
 
 #include <atomic>
 
-#include "aos/events/event_loop.h"
+#include "aos/events/shm_event_loop.h"
 #include "aos/robot_state/joystick_state_generated.h"
 
 namespace frc971 {
@@ -11,10 +11,10 @@
 
 class JoystickSender {
  public:
-  JoystickSender(::aos::EventLoop *event_loop);
+  JoystickSender(::aos::ShmEventLoop *event_loop);
 
  private:
-  ::aos::EventLoop *event_loop_;
+  ::aos::ShmEventLoop *event_loop_;
   ::aos::Sender<::aos::JoystickState> joystick_state_sender_;
   const uint16_t team_id_;
 };
diff --git a/y2020/BUILD b/y2020/BUILD
index b044f53..53ccb8e 100644
--- a/y2020/BUILD
+++ b/y2020/BUILD
@@ -72,7 +72,6 @@
         "//frc971/wpilib:sensor_reader",
         "//frc971/wpilib:wpilib_interface",
         "//frc971/wpilib:wpilib_robot_base",
-        "//third_party:phoenix",
         "//third_party:wpilib",
         "//y2020/control_loops/superstructure:superstructure_output_fbs",
         "//y2020/control_loops/superstructure:superstructure_position_fbs",
diff --git a/y2020/wpilib_interface.cc b/y2020/wpilib_interface.cc
index a363775..4989390 100644
--- a/y2020/wpilib_interface.cc
+++ b/y2020/wpilib_interface.cc
@@ -10,7 +10,6 @@
 #include <mutex>
 #include <thread>
 
-#include "ctre/phoenix/CANifier.h"
 #include "frc971/wpilib/ahal/AnalogInput.h"
 #include "frc971/wpilib/ahal/Counter.h"
 #include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
@@ -31,7 +30,6 @@
 #include "aos/util/log_interval.h"
 #include "aos/util/phased_loop.h"
 #include "aos/util/wrapping_counter.h"
-#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
 #include "frc971/autonomous/auto_mode_generated.h"
 #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
 #include "frc971/wpilib/ADIS16470.h"
@@ -241,6 +239,7 @@
 
     // Thread 4.
     ::aos::ShmEventLoop output_event_loop(&config.message());
+    output_event_loop.set_name("output_writer");
     ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
     drivetrain_writer.set_left_controller0(
         ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);