From b335256444349361cf406b14bc846611b7249056 Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 4 Aug 2015 14:42:06 -0700 Subject: Add AsyncNotifyWhenDone --- src/cpp/server/server_context.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index cf19556e7a..0d09519b28 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -57,9 +57,12 @@ class ServerContext::CompletionOp GRPC_FINAL : public CallOpSetInterface { bool CheckCancelled(CompletionQueue* cq); + void set_tag(void* tag) { tag_ = tag; } + void Unref(); private: + void* tag_; grpc::mutex mu_; int refs_; bool finalized_; @@ -90,18 +93,24 @@ void ServerContext::CompletionOp::FillOps(grpc_op* ops, size_t* nops) { bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { grpc::unique_lock lock(mu_); finalized_ = true; + bool ret = false; + if (tag_) { + *tag = tag_; + ret = true; + } if (!*status) cancelled_ = 1; if (--refs_ == 0) { lock.unlock(); delete this; } - return false; + return ret; } // ServerContext body ServerContext::ServerContext() : completion_op_(nullptr), + async_notify_when_done_tag_(nullptr), call_(nullptr), cq_(nullptr), sent_initial_metadata_(false) {} @@ -109,6 +118,7 @@ ServerContext::ServerContext() ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata* metadata, size_t metadata_count) : completion_op_(nullptr), + async_notify_when_done_tag_(nullptr), deadline_(deadline), call_(nullptr), cq_(nullptr), @@ -133,6 +143,7 @@ ServerContext::~ServerContext() { void ServerContext::BeginCompletionOp(Call* call) { GPR_ASSERT(!completion_op_); completion_op_ = new CompletionOp(); + completion_op_->set_tag(async_notify_when_done_tag_); call->PerformOps(completion_op_); } -- cgit v1.2.3 From d45a26ed06dfeafa41f49d17fe42a2f637ad6742 Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 4 Aug 2015 16:36:22 -0700 Subject: allow null tag --- include/grpc++/server_context.h | 6 +++++- src/cpp/server/server_context.cc | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/include/grpc++/server_context.h b/include/grpc++/server_context.h index 03d2f0d128..23273f43e6 100644 --- a/include/grpc++/server_context.h +++ b/include/grpc++/server_context.h @@ -128,7 +128,10 @@ class ServerContext { // Async only. Has to be called before the rpc starts. // Returns the tag in completion queue when the rpc finishes. // IsCancelled() can then be called to check whether the rpc was cancelled. - void AsyncNotifyWhenDone(void* tag) { async_notify_when_done_tag_ = tag; } + void AsyncNotifyWhenDone(void* tag) { + has_notify_when_done_tag_ = true; + async_notify_when_done_tag_ = tag; + } private: friend class ::grpc::testing::InteropContextInspector; @@ -170,6 +173,7 @@ class ServerContext { void set_call(grpc_call* call); CompletionOp* completion_op_; + bool has_notify_when_done_tag_; void* async_notify_when_done_tag_; gpr_timespec deadline_; diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index 0d09519b28..04373397f9 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -50,18 +50,22 @@ namespace grpc { class ServerContext::CompletionOp GRPC_FINAL : public CallOpSetInterface { public: // initial refs: one in the server context, one in the cq - CompletionOp() : refs_(2), finalized_(false), cancelled_(0) {} + CompletionOp() : has_tag_(false), tag_(nullptr), refs_(2), finalized_(false), cancelled_(0) {} void FillOps(grpc_op* ops, size_t* nops) GRPC_OVERRIDE; bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE; bool CheckCancelled(CompletionQueue* cq); - void set_tag(void* tag) { tag_ = tag; } + void set_tag(void* tag) { + has_tag_ = true; + tag_ = tag; + } void Unref(); private: + bool has_tag_; void* tag_; grpc::mutex mu_; int refs_; @@ -94,7 +98,7 @@ bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { grpc::unique_lock lock(mu_); finalized_ = true; bool ret = false; - if (tag_) { + if (has_tag_) { *tag = tag_; ret = true; } @@ -110,6 +114,7 @@ bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { ServerContext::ServerContext() : completion_op_(nullptr), + has_notify_when_done_tag_(false), async_notify_when_done_tag_(nullptr), call_(nullptr), cq_(nullptr), @@ -118,6 +123,7 @@ ServerContext::ServerContext() ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata* metadata, size_t metadata_count) : completion_op_(nullptr), + has_notify_when_done_tag_(false), async_notify_when_done_tag_(nullptr), deadline_(deadline), call_(nullptr), @@ -143,7 +149,9 @@ ServerContext::~ServerContext() { void ServerContext::BeginCompletionOp(Call* call) { GPR_ASSERT(!completion_op_); completion_op_ = new CompletionOp(); - completion_op_->set_tag(async_notify_when_done_tag_); + if (has_notify_when_done_tag_) { + completion_op_->set_tag(async_notify_when_done_tag_); + } call->PerformOps(completion_op_); } -- cgit v1.2.3