1 // Copyright 2021 gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <grpc/support/port_platform.h>
15 
16 #include "src/core/lib/iomgr/event_engine_shims/closure.h"
17 
18 #include "absl/functional/any_invocable.h"
19 #include "absl/status/status.h"
20 
21 #include <grpc/event_engine/event_engine.h>
22 
23 #include "src/core/lib/iomgr/closure.h"
24 #include "src/core/lib/iomgr/exec_ctx.h"
25 #include "src/core/lib/transport/error_utils.h"
26 
27 namespace grpc_event_engine {
28 namespace experimental {
29 
RunEventEngineClosure(grpc_closure * closure,grpc_error_handle error)30 void RunEventEngineClosure(grpc_closure* closure, grpc_error_handle error) {
31   if (closure == nullptr) {
32     return;
33   }
34   grpc_core::ApplicationCallbackExecCtx app_ctx;
35   grpc_core::ExecCtx exec_ctx;
36 #ifndef NDEBUG
37   closure->scheduled = false;
38   if (grpc_trace_closure.enabled()) {
39     gpr_log(GPR_DEBUG,
40             "EventEngine: running closure %p: created [%s:%d]: %s [%s:%d]",
41             closure, closure->file_created, closure->line_created,
42             closure->run ? "run" : "scheduled", closure->file_initiated,
43             closure->line_initiated);
44   }
45 #endif
46   closure->cb(closure->cb_arg, error);
47 #ifndef NDEBUG
48   if (grpc_trace_closure.enabled()) {
49     gpr_log(GPR_DEBUG, "EventEngine: closure %p finished", closure);
50   }
51 #endif
52 }
53 
GrpcClosureToStatusCallback(grpc_closure * closure)54 absl::AnyInvocable<void(absl::Status)> GrpcClosureToStatusCallback(
55     grpc_closure* closure) {
56   return [closure](absl::Status status) {
57     RunEventEngineClosure(closure, absl_status_to_grpc_error(status));
58   };
59 }
60 
61 }  // namespace experimental
62 }  // namespace grpc_event_engine
63