[binder] Implicitly initialize the binder pool (#27261)

pull/27278/head
Ta-Wei Tu 3 years ago committed by GitHub
parent e20e26f0d4
commit 3a5e844b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      examples/android/binder/java/io/grpc/binder/cpp/exampleserver/native.cc
  2. 34
      src/core/ext/transport/binder/server/binder_server.cc
  3. 8
      src/core/ext/transport/binder/server/binder_server.h
  4. 10
      test/core/transport/binder/end2end/binder_server_test.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());

@ -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<std::string, void*>* g_endpoint_binder_pool = nullptr;
static absl::flat_hash_map<std::string, void*>* 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<std::string, void*>();
}
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<std::string, void*>();
}
(*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;
}

@ -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<std::string, void*>* 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);

@ -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

Loading…
Cancel
Save