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 "src/core/lib/iomgr/event_engine_shims/closure.h"
15
16 #include <grpc/event_engine/event_engine.h>
17 #include <grpc/support/port_platform.h>
18
19 #include "absl/functional/any_invocable.h"
20 #include "absl/log/log.h"
21 #include "absl/status/status.h"
22 #include "src/core/lib/iomgr/closure.h"
23 #include "src/core/lib/iomgr/exec_ctx.h"
24 #include "src/core/lib/transport/error_utils.h"
25
26 namespace grpc_event_engine {
27 namespace experimental {
28
RunEventEngineClosure(grpc_closure * closure,grpc_error_handle error)29 void RunEventEngineClosure(grpc_closure* closure, grpc_error_handle error) {
30 if (closure == nullptr) {
31 return;
32 }
33 grpc_core::ApplicationCallbackExecCtx app_ctx;
34 grpc_core::ExecCtx exec_ctx;
35 #ifndef NDEBUG
36 closure->scheduled = false;
37 GRPC_TRACE_VLOG(closure, 2)
38 << "EventEngine: running closure " << closure << ": created ["
39 << closure->file_created << ":" << closure->line_created
40 << "]: " << (closure->run ? "run" : "scheduled") << " ["
41 << closure->file_initiated << ":" << closure->line_initiated << "]";
42 #endif
43 closure->cb(closure->cb_arg, error);
44 #ifndef NDEBUG
45 GRPC_TRACE_VLOG(closure, 2)
46 << "EventEngine: closure " << closure << " finished";
47 #endif
48 }
49
GrpcClosureToStatusCallback(grpc_closure * closure)50 absl::AnyInvocable<void(absl::Status)> GrpcClosureToStatusCallback(
51 grpc_closure* closure) {
52 return [closure](absl::Status status) {
53 RunEventEngineClosure(closure, absl_status_to_grpc_error(status));
54 };
55 }
56
57 } // namespace experimental
58 } // namespace grpc_event_engine
59