2 * Copyright 2019 gRPC authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <grpcpp/impl/codegen/server_callback.h>
20 #include "src/core/lib/iomgr/closure.h"
21 #include "src/core/lib/iomgr/exec_ctx.h"
22 #include "src/core/lib/iomgr/executor.h"
27 void ServerCallbackCall::ScheduleOnDone(bool inline_ondone) {
31 // Unlike other uses of closure, do not Ref or Unref here since at this
32 // point, all the Ref'fing and Unref'fing is done for this call.
33 grpc_core::ExecCtx exec_ctx;
34 struct ClosureWithArg {
36 ServerCallbackCall* call;
37 explicit ClosureWithArg(ServerCallbackCall* call_arg) : call(call_arg) {
38 GRPC_CLOSURE_INIT(&closure,
39 [](void* void_arg, grpc_error*) {
41 static_cast<ClosureWithArg*>(void_arg);
42 arg->call->CallOnDone();
45 this, grpc_schedule_on_exec_ctx);
48 ClosureWithArg* arg = new ClosureWithArg(this);
49 grpc_core::Executor::Run(&arg->closure, GRPC_ERROR_NONE);
53 void ServerCallbackCall::CallOnCancel(ServerReactor* reactor) {
54 if (reactor->InternalInlineable()) {
57 // Ref to make sure that the closure executes before the whole call gets
58 // destructed, and Unref within the closure.
60 grpc_core::ExecCtx exec_ctx;
61 struct ClosureWithArg {
63 ServerCallbackCall* call;
64 ServerReactor* reactor;
65 ClosureWithArg(ServerCallbackCall* call_arg, ServerReactor* reactor_arg)
66 : call(call_arg), reactor(reactor_arg) {
67 GRPC_CLOSURE_INIT(&closure,
68 [](void* void_arg, grpc_error*) {
70 static_cast<ClosureWithArg*>(void_arg);
71 arg->reactor->OnCancel();
72 arg->call->MaybeDone();
75 this, grpc_schedule_on_exec_ctx);
78 ClosureWithArg* arg = new ClosureWithArg(this, reactor);
79 grpc_core::Executor::Run(&arg->closure, GRPC_ERROR_NONE);
83 } // namespace internal