Make a CommonPool for host builds

CommonPool uses some APIs that are only available on Android builds for
the setting up of the threads in the constructor. This creates
CommonPoolBase as a superclass of CommonPool to contain a setupThread
method. We provide two implementations of CommonPoolBase, one for
Android which copies the setup performed originally in CommonPool.cpp,
and one version for host which removes the calls to Android specific
APIs.
In addition, this adds a way to unblock the threads in the destructor of
CommonPool, as the destructor would otherwise get stuck if ever called.

Bug: 322360037
Test: libhwui tests
Change-Id: I26d67d36a2d5946c56bde375fd2810b5e395193f
diff --git a/libs/hwui/Android.bp b/libs/hwui/Android.bp
index 33830f1..014b841 100644
--- a/libs/hwui/Android.bp
+++ b/libs/hwui/Android.bp
@@ -547,6 +547,7 @@
         "hwui/MinikinUtils.cpp",
         "hwui/PaintImpl.cpp",
         "hwui/Typeface.cpp",
+        "thread/CommonPool.cpp",
         "utils/Blur.cpp",
         "utils/Color.cpp",
         "utils/LinearAllocator.cpp",
@@ -623,7 +624,6 @@
                 "renderthread/RenderThread.cpp",
                 "renderthread/HintSessionWrapper.cpp",
                 "service/GraphicsStatsService.cpp",
-                "thread/CommonPool.cpp",
                 "utils/GLUtils.cpp",
                 "utils/NdkUtils.cpp",
                 "AutoBackendTextureRelease.cpp",
diff --git a/libs/hwui/platform/android/thread/CommonPoolBase.h b/libs/hwui/platform/android/thread/CommonPoolBase.h
new file mode 100644
index 0000000..8f836b6
--- /dev/null
+++ b/libs/hwui/platform/android/thread/CommonPoolBase.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef FRAMEWORKS_BASE_COMMONPOOLBASE_H
+#define FRAMEWORKS_BASE_COMMONPOOLBASE_H
+
+#include <sys/resource.h>
+
+#include "renderthread/RenderThread.h"
+
+namespace android {
+namespace uirenderer {
+
+class CommonPoolBase {
+    PREVENT_COPY_AND_ASSIGN(CommonPoolBase);
+
+protected:
+    CommonPoolBase() {}
+
+    void setupThread(int i, std::mutex& mLock, std::vector<int>& tids,
+                     std::vector<std::condition_variable>& tidConditionVars) {
+        std::array<char, 20> name{"hwuiTask"};
+        snprintf(name.data(), name.size(), "hwuiTask%d", i);
+        auto self = pthread_self();
+        pthread_setname_np(self, name.data());
+        {
+            std::unique_lock lock(mLock);
+            tids[i] = pthread_gettid_np(self);
+            tidConditionVars[i].notify_one();
+        }
+        setpriority(PRIO_PROCESS, 0, PRIORITY_FOREGROUND);
+        auto startHook = renderthread::RenderThread::getOnStartHook();
+        if (startHook) {
+            startHook(name.data());
+        }
+    }
+
+    bool supportsTid() { return true; }
+};
+
+}  // namespace uirenderer
+}  // namespace android
+
+#endif  // FRAMEWORKS_BASE_COMMONPOOLBASE_H
diff --git a/libs/hwui/platform/host/thread/CommonPoolBase.h b/libs/hwui/platform/host/thread/CommonPoolBase.h
new file mode 100644
index 0000000..cd09101
--- /dev/null
+++ b/libs/hwui/platform/host/thread/CommonPoolBase.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef FRAMEWORKS_BASE_COMMONPOOLBASE_H
+#define FRAMEWORKS_BASE_COMMONPOOLBASE_H
+
+#include <condition_variable>
+#include <mutex>
+#include <vector>
+
+#include "renderthread/RenderThread.h"
+
+namespace android {
+namespace uirenderer {
+
+class CommonPoolBase {
+    PREVENT_COPY_AND_ASSIGN(CommonPoolBase);
+
+protected:
+    CommonPoolBase() {}
+
+    void setupThread(int i, std::mutex& mLock, std::vector<int>& tids,
+                     std::vector<std::condition_variable>& tidConditionVars) {
+        std::array<char, 20> name{"hwuiTask"};
+        snprintf(name.data(), name.size(), "hwuiTask%d", i);
+        {
+            std::unique_lock lock(mLock);
+            tids[i] = -1;
+            tidConditionVars[i].notify_one();
+        }
+        auto startHook = renderthread::RenderThread::getOnStartHook();
+        if (startHook) {
+            startHook(name.data());
+        }
+    }
+
+    bool supportsTid() { return false; }
+};
+
+}  // namespace uirenderer
+}  // namespace android
+
+#endif  // FRAMEWORKS_BASE_COMMONPOOLBASE_H
diff --git a/libs/hwui/thread/CommonPool.cpp b/libs/hwui/thread/CommonPool.cpp
index dc92f9f..6c0c30f 100644
--- a/libs/hwui/thread/CommonPool.cpp
+++ b/libs/hwui/thread/CommonPool.cpp
@@ -16,16 +16,14 @@
 
 #include "CommonPool.h"
 
-#include <sys/resource.h>
 #include <utils/Trace.h>
-#include "renderthread/RenderThread.h"
 
 #include <array>
 
 namespace android {
 namespace uirenderer {
 
-CommonPool::CommonPool() {
+CommonPool::CommonPool() : CommonPoolBase() {
     ATRACE_CALL();
 
     CommonPool* pool = this;
@@ -36,22 +34,7 @@
     // Create 2 workers
     for (int i = 0; i < THREAD_COUNT; i++) {
         std::thread worker([pool, i, &mLock, &tids, &tidConditionVars] {
-            {
-                std::array<char, 20> name{"hwuiTask"};
-                snprintf(name.data(), name.size(), "hwuiTask%d", i);
-                auto self = pthread_self();
-                pthread_setname_np(self, name.data());
-                {
-                    std::unique_lock lock(mLock);
-                    tids[i] = pthread_gettid_np(self);
-                    tidConditionVars[i].notify_one();
-                }
-                setpriority(PRIO_PROCESS, 0, PRIORITY_FOREGROUND);
-                auto startHook = renderthread::RenderThread::getOnStartHook();
-                if (startHook) {
-                    startHook(name.data());
-                }
-            }
+            pool->setupThread(i, mLock, tids, tidConditionVars);
             pool->workerLoop();
         });
         worker.detach();
@@ -64,7 +47,9 @@
             }
         }
     }
-    mWorkerThreadIds = std::move(tids);
+    if (pool->supportsTid()) {
+        mWorkerThreadIds = std::move(tids);
+    }
 }
 
 CommonPool& CommonPool::instance() {
@@ -95,7 +80,7 @@
 
 void CommonPool::workerLoop() {
     std::unique_lock lock(mLock);
-    while (true) {
+    while (!mIsStopping) {
         if (!mWorkQueue.hasWork()) {
             mWaitingThreads++;
             mCondition.wait(lock);
diff --git a/libs/hwui/thread/CommonPool.h b/libs/hwui/thread/CommonPool.h
index 74f852b..0c025b4 100644
--- a/libs/hwui/thread/CommonPool.h
+++ b/libs/hwui/thread/CommonPool.h
@@ -17,8 +17,6 @@
 #ifndef FRAMEWORKS_BASE_COMMONPOOL_H
 #define FRAMEWORKS_BASE_COMMONPOOL_H
 
-#include "utils/Macros.h"
-
 #include <log/log.h>
 
 #include <condition_variable>
@@ -27,6 +25,9 @@
 #include <mutex>
 #include <vector>
 
+#include "thread/CommonPoolBase.h"
+#include "utils/Macros.h"
+
 namespace android {
 namespace uirenderer {
 
@@ -73,7 +74,7 @@
     int mTail = 0;
 };
 
-class CommonPool {
+class CommonPool : private CommonPoolBase {
     PREVENT_COPY_AND_ASSIGN(CommonPool);
 
 public:
@@ -107,7 +108,10 @@
     static CommonPool& instance();
 
     CommonPool();
-    ~CommonPool() {}
+    ~CommonPool() {
+        mIsStopping = true;
+        mCondition.notify_all();
+    }
 
     void enqueue(Task&&);
     void doWaitForIdle();
@@ -120,6 +124,7 @@
     std::condition_variable mCondition;
     int mWaitingThreads = 0;
     ArrayQueue<Task, QUEUE_SIZE> mWorkQueue;
+    std::atomic_bool mIsStopping = false;
 };
 
 }  // namespace uirenderer