Add a way to check if we're the only binder thread

libhidl will want to know this when deciding whether to poll or to wait
for a callback.

Bug: 71625413
Test: builds, boots
Change-Id: I9b9b66791951cdcf9a50733c91800d6da8a278a2
diff --git a/IPCThreadState.cpp b/IPCThreadState.cpp
index 71f6023..a93ecab 100644
--- a/IPCThreadState.cpp
+++ b/IPCThreadState.cpp
@@ -539,6 +539,7 @@
     // as that won't work with polling. Also, the caller is responsible
     // for subsequently calling handlePolledCommands()
     mProcess->setThreadPoolConfiguration(1, true /* callerWillJoin */);
+    mIsPollingThread = true;
 
     mOut.writeInt32(BC_ENTER_LOOPER);
     *fd = mProcess->mDriverFD;
@@ -707,7 +708,8 @@
       mMyThreadId(gettid()),
       mStrictModePolicy(0),
       mLastTransactionBinderFlags(0),
-      mIsLooper(false) {
+      mIsLooper(false),
+      mIsPollingThread(false) {
     pthread_setspecific(gTLS, this);
     clearCaller();
     mIn.setDataCapacity(256);
@@ -969,6 +971,10 @@
     return mIsLooper;
 }
 
+bool IPCThreadState::isOnlyBinderThread() {
+    return (mIsLooper && mProcess->mMaxThreads <= 1) || mIsPollingThread;
+}
+
 status_t IPCThreadState::executeCommand(int32_t cmd)
 {
     BHwBinder* obj;
diff --git a/include/hwbinder/IPCThreadState.h b/include/hwbinder/IPCThreadState.h
index 757e09e..43cd6f7 100644
--- a/include/hwbinder/IPCThreadState.h
+++ b/include/hwbinder/IPCThreadState.h
@@ -93,6 +93,8 @@
             void                setTheContextObject(sp<BHwBinder> obj);
 
             bool                isLooperThread();
+            bool                isOnlyBinderThread();
+
 private:
                                 IPCThreadState();
                                 ~IPCThreadState();
@@ -133,6 +135,7 @@
             int32_t             mLastTransactionBinderFlags;
             sp<BHwBinder>         mContextObject;
             bool                mIsLooper;
+            bool mIsPollingThread;
 };
 
 }; // namespace hardware