From 059e10447534eeb2e43b5d4cb1f1d29afdb591ee Mon Sep 17 00:00:00 2001 From: Soheil Hassas Yeganeh Date: Sat, 2 Mar 2019 23:25:18 -0500 Subject: [PATCH] Use grpc_core::RefCount for ServerContext. --- src/cpp/server/server_context.cc | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index e116cf6e7d2..d38b46822ae 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -32,6 +32,7 @@ #include #include +#include "src/core/lib/gprpp/ref_counted.h" #include "src/core/lib/surface/call.h" namespace grpc { @@ -116,13 +117,7 @@ class ServerContext::CompletionOp final : public internal::CallOpSetInterface { done_intercepting_ = true; if (!has_tag_) { /* We don't have a tag to return. */ - std::unique_lock lock(mu_); - if (--refs_ == 0) { - lock.unlock(); - grpc_call* call = call_.call(); - delete this; - grpc_call_unref(call); - } + Unref(); return; } /* Start a dummy op so that we can return the tag */ @@ -142,8 +137,8 @@ class ServerContext::CompletionOp final : public internal::CallOpSetInterface { bool has_tag_; void* tag_; void* core_cq_tag_; + grpc_core::RefCount refs_; std::mutex mu_; - int refs_; bool finalized_; int cancelled_; // This is an int (not bool) because it is passed to core bool done_intercepting_; @@ -151,9 +146,7 @@ class ServerContext::CompletionOp final : public internal::CallOpSetInterface { }; void ServerContext::CompletionOp::Unref() { - std::unique_lock lock(mu_); - if (--refs_ == 0) { - lock.unlock(); + if (refs_.Unref()) { grpc_call* call = call_.call(); delete this; grpc_call_unref(call); @@ -183,12 +176,7 @@ bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { *tag = tag_; ret = true; } - if (--refs_ == 0) { - lock.unlock(); - grpc_call* call = call_.call(); - delete this; - grpc_call_unref(call); - } + Unref(); return ret; } finalized_ = true; @@ -220,13 +208,7 @@ bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { *tag = tag_; ret = true; } - lock.lock(); - if (--refs_ == 0) { - lock.unlock(); - grpc_call* call = call_.call(); - delete this; - grpc_call_unref(call); - } + Unref(); return ret; } /* There are interceptors to be run. Return false for now */