Transport threadpool configuration.
Servers can specify the number of threads they'd like to
have for handling incoming RPC calls, as well as whether
they'd like to join the threadpool themselves, by calling:
configureRpcThreadpool(numThreads, true /* callerWillJoin */);
This method *must* be called before interacting with any
HIDL services, including the servicemanager through
IFoo::getService / IFoo.registerAsService().
If the server indicated it wanted to join, it should do so
as soon as it can with:
joinRpcThreadpool();
This allows a server full flexibility:
- Without any of these calls, a threadpool of size 1 will be
started, and the main thread won't be a part of it.
- If the server wants a single-threaded RPC pool with its own
main thread for handling incoming RPC transactions, it can call
configureRpcThreadpool(1, true) followed by joinRpcThreadpool().
- If the server wants a multi-threaded RPC pool, it can call
configureRpcThreadpool(5, join) followed by joinRpcThreadpool()
if join was set to true.
Bug: 31226656
Test: mma, hidl_test
Change-Id: I9a3c68ebbe34ea9f14cdae48ca9908d05012c3f2
diff --git a/transport/include/hidl/HidlBinderSupport.h b/transport/include/hidl/HidlBinderSupport.h
index 13b67fd..08989e2 100644
--- a/transport/include/hidl/HidlBinderSupport.h
+++ b/transport/include/hidl/HidlBinderSupport.h
@@ -23,7 +23,9 @@
#include <hidl/MQDescriptor.h>
#include <hidl/Static.h>
#include <hwbinder/IBinder.h>
+#include <hwbinder/IPCThreadState.h>
#include <hwbinder/Parcel.h>
+#include <hwbinder/ProcessState.h>
#include <android/hidl/base/1.0/BnBase.h>
// Defines functions for hidl_string, hidl_version, Status, hidl_vec, MQDescriptor,
// etc. to interact with Parcel.
@@ -353,6 +355,14 @@
}
}
+inline void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
+ ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/);
+}
+
+inline void joinBinderRpcThreadpool() {
+ IPCThreadState::self()->joinThreadPool();
+}
+
} // namespace hardware
} // namespace android
diff --git a/transport/include/hidl/HidlTransportSupport.h b/transport/include/hidl/HidlTransportSupport.h
index 190227f..eabb75d 100644
--- a/transport/include/hidl/HidlTransportSupport.h
+++ b/transport/include/hidl/HidlTransportSupport.h
@@ -24,6 +24,35 @@
namespace android {
namespace hardware {
+/* Configures the threadpool used for handling incoming RPC calls in this process.
+ *
+ * This method MUST be called before interacting with any HIDL interfaces,
+ * including the IFoo::getService and IFoo::registerAsService methods.
+ *
+ * @param maxThreads maximum number of threads in this process
+ * @param callerWillJoin whether the caller will join the threadpool later.
+ *
+ * Note that maxThreads must include the caller thread if callerWillJoin is true;
+ *
+ * If you want to create a threadpool of 5 threads, without the caller ever joining:
+ * configureRpcThreadPool(5, false);
+ * If you want to create a threadpool of 1 thread, with the caller joining:
+ * configureRpcThreadPool(1, true); // transport won't launch any threads by itself
+ *
+ */
+inline void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
+ // TODO(b/32756130) this should be transport-dependent
+ configureBinderRpcThreadpool(maxThreads, callerWillJoin);
+}
+
+/* Joins a threadpool that you configured earlier with
+ * configureRpcThreadPool(x, true);
+ */
+inline void joinRpcThreadpool() {
+ // TODO(b/32756130) this should be transport-dependent
+ joinBinderRpcThreadpool();
+}
+
// cast the interface IParent to IChild.
// Return nullptr if parent is null or any failure.
template<typename IChild, typename IParent, typename BpChild, typename BpParent>
diff --git a/transport/include/hidl/LegacySupport.h b/transport/include/hidl/LegacySupport.h
index 2ff1785..8fd1795 100644
--- a/transport/include/hidl/LegacySupport.h
+++ b/transport/include/hidl/LegacySupport.h
@@ -14,10 +14,9 @@
* limitations under the License.
*/
+#include <hidl/HidlTransportSupport.h>
+#include <sys/wait.h>
#include <utils/Log.h>
-
-#include <hwbinder/IPCThreadState.h>
-#include <hwbinder/ProcessState.h>
#include <utils/Errors.h>
#include <utils/StrongPointer.h>
@@ -53,27 +52,16 @@
}
/**
- * Launches the RPC threadpool. This method never returns.
- *
- * Return value is exit status.
- */
-inline int launchRpcServer(size_t maxThreads) {
- ProcessState::self()->setThreadPoolMaxThreadCount(maxThreads);
- ProcessState::self()->startThreadPool();
- IPCThreadState::self()->joinThreadPool();
-
- return 0;
-}
-
-/**
* Creates default passthrough service implementation. This method never returns.
*
* Return value is exit status.
*/
template<class Interface>
-int defaultPassthroughServiceImplementation(std::string name) {
+int defaultPassthroughServiceImplementation(std::string name, size_t maxThreads = 1) {
+ configureRpcThreadpool(maxThreads, true);
registerPassthroughServiceImplementation<Interface>(name);
- return launchRpcServer(0);
+ joinRpcThreadpool();
+ return 0;
}
} // namespace hardware
diff --git a/transport/memory/1.0/default/service.cpp b/transport/memory/1.0/default/service.cpp
index 8ea8b8a..0302e51 100644
--- a/transport/memory/1.0/default/service.cpp
+++ b/transport/memory/1.0/default/service.cpp
@@ -4,17 +4,18 @@
#include <android-base/logging.h>
#include <android/hidl/memory/1.0/IAllocator.h>
-#include <hwbinder/IPCThreadState.h>
-#include <hwbinder/ProcessState.h>
+#include <hidl/HidlTransportSupport.h>
-using android::hardware::IPCThreadState;
-using android::hardware::ProcessState;
+using android::hardware::configureRpcThreadpool;
+using android::hardware::joinRpcThreadpool;
using android::hidl::memory::V1_0::IAllocator;
using android::hidl::memory::V1_0::implementation::AshmemAllocator;
using android::sp;
using android::status_t;
int main() {
+ configureRpcThreadpool(1, true /* callerWillJoin */);
+
sp<IAllocator> allocator = new AshmemAllocator();
status_t status = allocator->registerAsService("ashmem");
@@ -23,9 +24,7 @@
LOG(FATAL) << "Unable to register allocator service: " << status;
}
- ProcessState::self()->setThreadPoolMaxThreadCount(0);
- ProcessState::self()->startThreadPool();
- IPCThreadState::self()->joinThreadPool();
+ joinRpcThreadpool();
return -1;
}