diff --git a/examples/android/binder/java/io/grpc/binder/cpp/exampleserver/native.cc b/examples/android/binder/java/io/grpc/binder/cpp/exampleserver/native.cc index 346af6aa435..c875b609222 100644 --- a/examples/android/binder/java/io/grpc/binder/cpp/exampleserver/native.cc +++ b/examples/android/binder/java/io/grpc/binder/cpp/exampleserver/native.cc @@ -58,7 +58,6 @@ Java_io_grpc_binder_cpp_exampleserver_ExportedEndpointService_init_1grpc_1server grpc::ServerBuilder server_builder; server_builder.RegisterService(&service); - grpc_endpoint_binder_pool_init(); server_builder.AddListeningPort( "binder://example.service", grpc::experimental::BinderServerCredentials()); diff --git a/src/core/ext/transport/binder/server/binder_server.cc b/src/core/ext/transport/binder/server/binder_server.cc index ffd33dff852..6d783a08e40 100644 --- a/src/core/ext/transport/binder/server/binder_server.cc +++ b/src/core/ext/transport/binder/server/binder_server.cc @@ -47,34 +47,40 @@ void RemoveEndpointBinder(const std::string& service) { } // namespace experimental } // namespace grpc -grpc_core::Mutex* g_endpoint_binder_pool_mu = nullptr; -absl::flat_hash_map* g_endpoint_binder_pool = nullptr; +static absl::flat_hash_map* g_endpoint_binder_pool = + nullptr; -void grpc_endpoint_binder_pool_init() { - g_endpoint_binder_pool_mu = new grpc_core::Mutex(); - g_endpoint_binder_pool = new absl::flat_hash_map(); -} +namespace { -void grpc_endpoint_binder_pool_shutdown() { - g_endpoint_binder_pool_mu->Lock(); - delete g_endpoint_binder_pool; - g_endpoint_binder_pool_mu->Unlock(); - delete g_endpoint_binder_pool_mu; +grpc_core::Mutex* GetBinderPoolMutex() { + static grpc_core::Mutex* mu = new grpc_core::Mutex(); + return mu; } +} // namespace + void grpc_add_endpoint_binder(const std::string& service, void* endpoint_binder) { - grpc_core::MutexLock lock(g_endpoint_binder_pool_mu); + grpc_core::MutexLock lock(GetBinderPoolMutex()); + if (g_endpoint_binder_pool == nullptr) { + g_endpoint_binder_pool = new absl::flat_hash_map(); + } (*g_endpoint_binder_pool)[service] = endpoint_binder; } void grpc_remove_endpoint_binder(const std::string& service) { - grpc_core::MutexLock lock(g_endpoint_binder_pool_mu); + grpc_core::MutexLock lock(GetBinderPoolMutex()); + if (g_endpoint_binder_pool == nullptr) { + return; + } g_endpoint_binder_pool->erase(service); } void* grpc_get_endpoint_binder(const std::string& service) { - grpc_core::MutexLock lock(g_endpoint_binder_pool_mu); + grpc_core::MutexLock lock(GetBinderPoolMutex()); + if (g_endpoint_binder_pool == nullptr) { + return nullptr; + } auto iter = g_endpoint_binder_pool->find(service); return iter == g_endpoint_binder_pool->end() ? nullptr : iter->second; } diff --git a/src/core/ext/transport/binder/server/binder_server.h b/src/core/ext/transport/binder/server/binder_server.h index 9ec1a06afad..c02429f3d10 100644 --- a/src/core/ext/transport/binder/server/binder_server.h +++ b/src/core/ext/transport/binder/server/binder_server.h @@ -41,14 +41,6 @@ void RemoveEndpointBinder(const std::string& service); } // namespace experimental } // namespace grpc -extern grpc_core::Mutex* g_endpoint_binder_pool_mu; -extern absl::flat_hash_map* g_endpoint_binder_pool; - -// TODO(waynetu): Can these two functions be called in grpc_init() and -// grpc_shutdown()? -void grpc_endpoint_binder_pool_init(); -void grpc_endpoint_binder_pool_shutdown(); - void grpc_add_endpoint_binder(const std::string& service, void* endpoint_binder); void grpc_remove_endpoint_binder(const std::string& service); diff --git a/test/core/transport/binder/end2end/binder_server_test.cc b/test/core/transport/binder/end2end/binder_server_test.cc index f5b5774a4e0..e70d0522f3e 100644 --- a/test/core/transport/binder/end2end/binder_server_test.cc +++ b/test/core/transport/binder/end2end/binder_server_test.cc @@ -89,14 +89,8 @@ class BinderServerTest : public ::testing::Test { ~BinderServerTest() override { delete grpc_binder::end2end_testing::g_transaction_processor; } - static void SetUpTestSuite() { - grpc_init(); - grpc_endpoint_binder_pool_init(); - } - static void TearDownTestSuite() { - grpc_endpoint_binder_pool_shutdown(); - grpc_shutdown(); - } + static void SetUpTestSuite() { grpc_init(); } + static void TearDownTestSuite() { grpc_shutdown(); } }; #ifndef GPR_ANDROID