Move serviceCounter outside template function
static variables inside template functions behave differently i.e.
each instantiation of function template has its own copy of
local static variables. This defeats the purpose of the counter
since the hal will exit even when there are connected services
in the hal-process.
Move the static variable declaration outside the template function
so that all instances of registerLazyPassthroughImplementation
share the same copy of serviceCounter.
Test: register multiple lazy services in the same hal process.
The hal-process should exit only when there are no connected services.
BUG: 130208323
Copyright (c) 2019 Qualcomm Innovation Center, Inc. All Rights Reserved.
Change-Id: Ie2d66a75e9aa376bcc298fcdcfa70b10da4732e8
diff --git a/transport/include/hidl/LegacySupport.h b/transport/include/hidl/LegacySupport.h
index 1e983eb..0a9feee 100644
--- a/transport/include/hidl/LegacySupport.h
+++ b/transport/include/hidl/LegacySupport.h
@@ -91,6 +91,15 @@
return defaultPassthroughServiceImplementation<Interface>("default", maxThreads);
}
+// Make LazyServiceRegistrar static so that multiple calls to
+// registerLazyPassthroughServiceImplementation work as expected: each HAL is registered and the
+// process only exits once all HALs have 0 clients.
+static inline std::shared_ptr<LazyServiceRegistrar> getOrCreateLazyServiceRegistrar() {
+ using android::hardware::LazyServiceRegistrar;
+ static auto serviceCounter(std::make_shared<LazyServiceRegistrar>());
+ return serviceCounter;
+}
+
/**
* Registers a passthrough service implementation that exits when there are 0 clients.
*
@@ -102,15 +111,9 @@
template <class Interface>
__attribute__((warn_unused_result)) status_t registerLazyPassthroughServiceImplementation(
const std::string& name = "default") {
- // Make LazyServiceRegistrar static so that multiple calls to
- // registerLazyPassthroughServiceImplementation work as expected: each HAL is registered and the
- // process only exits once all HALs have 0 clients.
- using android::hardware::LazyServiceRegistrar;
- static auto serviceCounter(std::make_shared<LazyServiceRegistrar>());
-
return details::registerPassthroughServiceImplementation<Interface>(
[](const sp<Interface>& service, const std::string& name) {
- return serviceCounter->registerService(service, name);
+ return getOrCreateLazyServiceRegistrar()->registerService(service, name);
},
name);
}