AudioFlinger: Boost priority on a separate thread

Ensures no contention between priority boost
and audio patch configuration, since priority boost
requires waiting for the SchedulingPolicyService
to be available which may increase boot time.

Test: Spatializer continues to work
Bug: 297322674
Change-Id: Ieed64fbff98eecb6e039f3afd1b661514a227334
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index df0b576..c4b41fd 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -85,6 +85,7 @@
 #include <utils/Trace.h>
 
 #include <fcntl.h>
+#include <future>
 #include <linux/futex.h>
 #include <math.h>
 #include <memory>
@@ -3899,15 +3900,25 @@
 {
     aflog::setThreadWriter(mNBLogWriter.get());
 
+    std::future<void> priorityBoostFuture; // joined on dtor; this is a one-shot boost.
     if (mType == SPATIALIZER) {
         const pid_t tid = getTid();
         if (tid == -1) {  // odd: we are here, we must be a running thread.
             ALOGW("%s: Cannot update Spatializer mixer thread priority, no tid", __func__);
         } else {
-            const int priorityBoost = requestSpatializerPriority(getpid(), tid);
-            if (priorityBoost > 0) {
-                stream()->setHalThreadPriority(priorityBoost);
-            }
+            // We launch the priority boost request in a separate thread because
+            // the SchedulingPolicyService may not be available during early
+            // boot time, with a wait causing boot delay.
+            // There is also a PrioConfigEvent that does this, but it will also
+            // block other config events.  This command should be able
+            // to run concurrent with other stream commands.
+            priorityBoostFuture = std::async(std::launch::async,
+                    [tid, output_sp = stream()]() {
+                const int priorityBoost = requestSpatializerPriority(getpid(), tid);
+                if (priorityBoost > 0) {
+                    output_sp->setHalThreadPriority(priorityBoost);
+                }
+            });
         }
     }