1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/iomgr/exec_ctx.h"
22
23 #include "absl/strings/str_format.h"
24
25 #include <grpc/support/log.h>
26 #include <grpc/support/sync.h>
27
28 #include "src/core/lib/gprpp/crash.h"
29 #include "src/core/lib/iomgr/combiner.h"
30 #include "src/core/lib/iomgr/error.h"
31
exec_ctx_run(grpc_closure * closure)32 static void exec_ctx_run(grpc_closure* closure) {
33 #ifndef NDEBUG
34 closure->scheduled = false;
35 if (grpc_trace_closure.enabled()) {
36 gpr_log(GPR_DEBUG, "running closure %p: created [%s:%d]: %s [%s:%d]",
37 closure, closure->file_created, closure->line_created,
38 closure->run ? "run" : "scheduled", closure->file_initiated,
39 closure->line_initiated);
40 }
41 #endif
42 grpc_error_handle error =
43 grpc_core::internal::StatusMoveFromHeapPtr(closure->error_data.error);
44 closure->error_data.error = 0;
45 closure->cb(closure->cb_arg, std::move(error));
46 #ifndef NDEBUG
47 if (grpc_trace_closure.enabled()) {
48 gpr_log(GPR_DEBUG, "closure %p finished", closure);
49 }
50 #endif
51 }
52
exec_ctx_sched(grpc_closure * closure)53 static void exec_ctx_sched(grpc_closure* closure) {
54 grpc_closure_list_append(grpc_core::ExecCtx::Get()->closure_list(), closure);
55 }
56
57 namespace grpc_core {
58
59 thread_local ExecCtx* ExecCtx::exec_ctx_;
60 thread_local ApplicationCallbackExecCtx*
61 ApplicationCallbackExecCtx::callback_exec_ctx_;
62
Flush()63 bool ExecCtx::Flush() {
64 bool did_something = false;
65 for (;;) {
66 if (!grpc_closure_list_empty(closure_list_)) {
67 grpc_closure* c = closure_list_.head;
68 closure_list_.head = closure_list_.tail = nullptr;
69 while (c != nullptr) {
70 grpc_closure* next = c->next_data.next;
71 did_something = true;
72 exec_ctx_run(c);
73 c = next;
74 }
75 } else if (!grpc_combiner_continue_exec_ctx()) {
76 break;
77 }
78 }
79 GPR_ASSERT(combiner_data_.active_combiner == nullptr);
80 return did_something;
81 }
82
Run(const DebugLocation & location,grpc_closure * closure,grpc_error_handle error)83 void ExecCtx::Run(const DebugLocation& location, grpc_closure* closure,
84 grpc_error_handle error) {
85 (void)location;
86 if (closure == nullptr) {
87 return;
88 }
89 #ifndef NDEBUG
90 if (closure->scheduled) {
91 Crash(absl::StrFormat(
92 "Closure already scheduled. (closure: %p, created: [%s:%d], "
93 "previously scheduled at: [%s: %d], newly scheduled at [%s: %d]",
94 closure, closure->file_created, closure->line_created,
95 closure->file_initiated, closure->line_initiated, location.file(),
96 location.line()));
97 }
98 closure->scheduled = true;
99 closure->file_initiated = location.file();
100 closure->line_initiated = location.line();
101 closure->run = false;
102 GPR_ASSERT(closure->cb != nullptr);
103 #endif
104 closure->error_data.error = internal::StatusAllocHeapPtr(error);
105 exec_ctx_sched(closure);
106 }
107
RunList(const DebugLocation & location,grpc_closure_list * list)108 void ExecCtx::RunList(const DebugLocation& location, grpc_closure_list* list) {
109 (void)location;
110 grpc_closure* c = list->head;
111 while (c != nullptr) {
112 grpc_closure* next = c->next_data.next;
113 #ifndef NDEBUG
114 if (c->scheduled) {
115 Crash(absl::StrFormat(
116 "Closure already scheduled. (closure: %p, created: [%s:%d], "
117 "previously scheduled at: [%s: %d], newly scheduled at [%s:%d]",
118 c, c->file_created, c->line_created, c->file_initiated,
119 c->line_initiated, location.file(), location.line()));
120 }
121 c->scheduled = true;
122 c->file_initiated = location.file();
123 c->line_initiated = location.line();
124 c->run = false;
125 GPR_ASSERT(c->cb != nullptr);
126 #endif
127 exec_ctx_sched(c);
128 c = next;
129 }
130 list->head = list->tail = nullptr;
131 }
132
133 } // namespace grpc_core
134