Make CHECK statements work in realtime code

Factor out the RT -> NRT conversion code from glog back to AOS.  And
call it in the CHECK path so we can CHECK without a secondary crash from
making the CHECK statement.

Change-Id: I0a44770bc1bdf523aacb49b6e6ff89c9c0e20b2f
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/third_party/google-glog/src/signalhandler.cc b/third_party/google-glog/src/signalhandler.cc
index 87ed94f..768d72f 100644
--- a/third_party/google-glog/src/signalhandler.cc
+++ b/third_party/google-glog/src/signalhandler.cc
@@ -44,13 +44,22 @@
 #ifdef HAVE_SYS_UCONTEXT_H
 # include <sys/ucontext.h>
 #endif
-#include <dirent.h>
 #include <algorithm>
 
+namespace aos {
+void FatalUnsetRealtimePriority() __attribute__((weak));
+}
+
 _START_GOOGLE_NAMESPACE_
 
 namespace {
 
+void MaybeUnsetRealtime() {
+  if (&aos::FatalUnsetRealtimePriority != nullptr) {
+    aos::FatalUnsetRealtimePriority();
+  }
+}
+
 // We'll install the failure signal handler for these signals.  We could
 // use strsignal() to get signal names, but we don't use it to avoid
 // introducing yet another #ifdef complication.
@@ -309,30 +318,7 @@
       sleep(1);
     }
   }
-
-  {
-    // Put this back on SCHED_OTHER by default.
-    DIR *dirp = opendir("/proc/self/task");
-    if (dirp) {
-      struct dirent *directory_entry;
-      while ((directory_entry = readdir(dirp)) != NULL) {
-        int thread_id = std::atoi(directory_entry->d_name);
-
-        // ignore . and .. which are zeroes for some reason
-        if (thread_id != 0) {
-          struct sched_param param;
-          param.sched_priority = 20;
-          sched_setscheduler(thread_id, SCHED_OTHER, &param);
-        }
-      }
-      closedir(dirp);
-    } else {
-      // Can't get other threads; just lower own priority.
-      struct sched_param param;
-      param.sched_priority = 20;
-      sched_setscheduler(0, SCHED_OTHER, &param);
-    }
-  }
+  MaybeUnsetRealtime();
 
   // This is the first time we enter the signal handler.  We are going to
   // do some interesting stuff from here.