Use the first available CPU instead of 0 and 1

This makes the test work in docker containers.

Change-Id: I21a93e59434182ba50ef8c9ea45e24dc0e79a617
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/events/event_loop_param_test.cc b/aos/events/event_loop_param_test.cc
index 4a298f0..b74a3fe 100644
--- a/aos/events/event_loop_param_test.cc
+++ b/aos/events/event_loop_param_test.cc
@@ -780,11 +780,23 @@
 
 // Verify that SetRuntimeAffinity fails while running.
 TEST_P(AbstractEventLoopDeathTest, SetRuntimeAffinity) {
+  const cpu_set_t available = GetCurrentThreadAffinity();
+  int first_cpu = -1;
+  for (int i = 0; i < CPU_SETSIZE; ++i) {
+    if (CPU_ISSET(i, &available)) {
+      first_cpu = i;
+      break;
+      continue;
+    }
+  }
+  CHECK_NE(first_cpu, -1) << ": Default affinity has no CPUs?";
+
   auto loop = MakePrimary();
   // Confirm that runtime priority calls work when not running.
-  loop->SetRuntimeAffinity(MakeCpusetFromCpus({0}));
+  loop->SetRuntimeAffinity(MakeCpusetFromCpus({first_cpu}));
 
-  loop->OnRun([&]() { loop->SetRuntimeAffinity(MakeCpusetFromCpus({1})); });
+  loop->OnRun(
+      [&]() { loop->SetRuntimeAffinity(MakeCpusetFromCpus({first_cpu})); });
 
   EXPECT_DEATH(Run(), "Cannot set affinity while running");
 }
diff --git a/aos/realtime.cc b/aos/realtime.cc
index dc53a37..c58e2e6 100644
--- a/aos/realtime.cc
+++ b/aos/realtime.cc
@@ -176,6 +176,12 @@
   }
 }
 
+cpu_set_t GetCurrentThreadAffinity() {
+  cpu_set_t result;
+  PCHECK(sched_getaffinity(0, sizeof(result), &result) == 0);
+  return result;
+}
+
 void SetCurrentThreadRealtimePriority(int priority) {
   if (FLAGS_skip_realtime_scheduler) {
     LOG(WARNING) << "Ignoring request to switch to the RT scheduler due to "
diff --git a/aos/realtime.h b/aos/realtime.h
index fb49cac..be28e85 100644
--- a/aos/realtime.h
+++ b/aos/realtime.h
@@ -2,6 +2,7 @@
 #define AOS_REALTIME_H_
 
 #include <sched.h>
+
 #include <string_view>
 
 #include "glog/logging.h"
@@ -37,6 +38,9 @@
   return result;
 }
 
+// Returns the current thread's CPU affinity.
+cpu_set_t GetCurrentThreadAffinity();
+
 // Sets the current thread's scheduling affinity.
 void SetCurrentThreadAffinity(const cpu_set_t &cpuset);