From 3b2bc2deb1a249fd6d260adb50c39064ef5f9c2c Mon Sep 17 00:00:00 2001 From: Sree Kuchibhotla Date: Thu, 29 Sep 2016 17:53:29 -0700 Subject: [PATCH] tsan failures --- src/cpp/rpcmanager/grpc_rpc_manager.cc | 33 ++++++++++++++------------ 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/cpp/rpcmanager/grpc_rpc_manager.cc b/src/cpp/rpcmanager/grpc_rpc_manager.cc index 58b337da634..2299dbdcd38 100644 --- a/src/cpp/rpcmanager/grpc_rpc_manager.cc +++ b/src/cpp/rpcmanager/grpc_rpc_manager.cc @@ -59,12 +59,14 @@ GrpcRpcManager::GrpcRpcManager(int min_pollers, int max_pollers) : shutdown_(false), num_pollers_(0), min_pollers_(min_pollers), - max_pollers_(max_pollers == -1 ? INT_MAX: max_pollers), + max_pollers_(max_pollers == -1 ? INT_MAX : max_pollers), num_threads_(0) {} GrpcRpcManager::~GrpcRpcManager() { - std::unique_lock lock(mu_); - GPR_ASSERT(num_threads_ == 0); + { + std::unique_lock lock(mu_); + GPR_ASSERT(num_threads_ == 0); + } CleanupCompletedThreads(); } @@ -87,8 +89,16 @@ bool GrpcRpcManager::IsShutdown() { } void GrpcRpcManager::MarkAsCompleted(GrpcRpcManagerThread* thd) { - std::unique_lock lock(list_mu_); - completed_threads_.push_back(thd); + { + std::unique_lock list_lock(list_mu_); + completed_threads_.push_back(thd); + } + + grpc::unique_lock lock(mu_); + num_threads_--; + if (num_threads_ == 0) { + shutdown_cv_.notify_one(); + } } void GrpcRpcManager::CleanupCompletedThreads() { @@ -169,17 +179,10 @@ void GrpcRpcManager::MainWorkLoop() { } } while (MaybeContinueAsPoller()); - // If we are here, either GrpcRpcManager is shutting down or it already has - // enough threads. In both cases, current thread can be terminated - { - grpc::unique_lock lock(mu_); - num_threads_--; - if (num_threads_ == 0) { - shutdown_cv_.notify_one(); - } - } - CleanupCompletedThreads(); + + // If we are here, either GrpcRpcManager is shutting down or it already has + // enough threads. } } // namespace grpc