diff options
author | 2016-01-26 16:45:40 -0800 | |
---|---|---|
committer | 2016-01-26 16:45:40 -0800 | |
commit | 8b193943b6111dbc3ac4bcda76bc6a7cbf1d1e9e (patch) | |
tree | c18870bb4595254570887ec3580f673e687d925a | |
parent | 6a25656fece0f644239cf79a80348455a1a7c754 (diff) | |
parent | 64616496e2500bc99eeb52035e85a38ae8bb7244 (diff) |
Merge pull request #4904 from ctiller/c++
Alternative ownership for global callbacks
-rw-r--r-- | include/grpc++/server.h | 2 | ||||
-rw-r--r-- | src/cpp/server/server.cc | 16 |
2 files changed, 10 insertions, 8 deletions
diff --git a/include/grpc++/server.h b/include/grpc++/server.h index 644e66e6e0..c9371ba649 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -304,6 +304,8 @@ class Server GRPC_FINAL : public GrpcLibrary, private CallHook { int num_running_cb_; grpc::condition_variable callback_cv_; + std::shared_ptr<GlobalCallbacks> global_callbacks_; + std::list<SyncRequest>* sync_methods_; std::unique_ptr<RpcServiceMethod> unknown_method_; bool has_generic_service_; diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index 458fb322b1..3bf9f3fa0f 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -58,13 +58,12 @@ class DefaultGlobalCallbacks GRPC_FINAL : public Server::GlobalCallbacks { void PostSynchronousRequest(ServerContext* context) GRPC_OVERRIDE {} }; -static Server::GlobalCallbacks* g_callbacks = nullptr; +static std::shared_ptr<Server::GlobalCallbacks> g_callbacks = nullptr; static gpr_once g_once_init_callbacks = GPR_ONCE_INIT; static void InitGlobalCallbacks() { if (g_callbacks == nullptr) { - static DefaultGlobalCallbacks default_global_callbacks; - g_callbacks = &default_global_callbacks; + g_callbacks.reset(new DefaultGlobalCallbacks()); } } @@ -235,12 +234,12 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { } } - void Run() { + void Run(std::shared_ptr<GlobalCallbacks> global_callbacks) { ctx_.BeginCompletionOp(&call_); - g_callbacks->PreSynchronousRequest(&ctx_); + global_callbacks->PreSynchronousRequest(&ctx_); method_->handler()->RunHandler(MethodHandler::HandlerParameter( &call_, &ctx_, request_payload_, call_.max_message_size())); - g_callbacks->PostSynchronousRequest(&ctx_); + global_callbacks->PostSynchronousRequest(&ctx_); request_payload_ = nullptr; void* ignored_tag; bool ignored_ok; @@ -288,6 +287,7 @@ Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, thread_pool_(thread_pool), thread_pool_owned_(thread_pool_owned) { gpr_once_init(&g_once_init_callbacks, InitGlobalCallbacks); + global_callbacks_ = g_callbacks; grpc_server_register_completion_queue(server_, cq_.cq(), nullptr); } @@ -312,7 +312,7 @@ Server::~Server() { void Server::SetGlobalCallbacks(GlobalCallbacks* callbacks) { GPR_ASSERT(g_callbacks == nullptr); GPR_ASSERT(callbacks != nullptr); - g_callbacks = callbacks; + g_callbacks.reset(callbacks); } bool Server::RegisterService(const grpc::string* host, RpcService* service) { @@ -570,7 +570,7 @@ void Server::RunRpc() { } } GPR_TIMER_SCOPE("cd.Run()", 0); - cd.Run(); + cd.Run(global_callbacks_); } } |