Debian Codegraph @

src/core/lib/iomgr/exec_ctx.cc

raw
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 <grpc/support/log.h>
24 #include <grpc/support/sync.h>
25
26 #include "src/core/lib/iomgr/combiner.h"
27 #include "src/core/lib/iomgr/error.h"
28
29 static void exec_ctx_run(grpc_closure* closure) {
30 #ifndef NDEBUG
31 closure->scheduled = false;
32 if (grpc_trace_closure.enabled()) {
33 gpr_log(GPR_DEBUG, "running closure %p: created [%s:%d]: %s [%s:%d]",
34 closure, closure->file_created, closure->line_created,
35 closure->run ? "run" : "scheduled", closure->file_initiated,
36 closure->line_initiated);
37 }
38 #endif
39 grpc_error_handle error =
40 grpc_core::internal::StatusMoveFromHeapPtr(closure->error_data.error);
41 closure->error_data.error = 0;
42 closure->cb(closure->cb_arg, std::move(error));
43 #ifndef NDEBUG
44 if (grpc_trace_closure.enabled()) {
45 gpr_log(GPR_DEBUG, "closure %p finished", closure);
46 }
47 #endif
48 }
49
50 static void exec_ctx_sched(grpc_closure* closure) {
51 grpc_closure_list_append(grpc_core::ExecCtx::Get()->closure_list(), closure);
52 }
53
54 namespace grpc_core {
55
56 thread_local ExecCtx* ExecCtx::exec_ctx_;
57 thread_local ApplicationCallbackExecCtx*
58 ApplicationCallbackExecCtx::callback_exec_ctx_;
59
60 bool ExecCtx::Flush() {
61 bool did_something = false;
62 for (;;) {
63 if (!grpc_closure_list_empty(closure_list_)) {
64 grpc_closure* c = closure_list_.head;
65 closure_list_.head = closure_list_.tail = nullptr;
66 while (c != nullptr) {
67 grpc_closure* next = c->next_data.next;
68 did_something = true;
69 exec_ctx_run(c);
70 c = next;
71 }
72 } else if (!grpc_combiner_continue_exec_ctx()) {
73 break;
74 }
75 }
76 GPR_ASSERT(combiner_data_.active_combiner == nullptr);
77 return did_something;
78 }
79
80 void ExecCtx::Run(const DebugLocation& location, grpc_closure* closure,
81 grpc_error_handle error) {
82 (void)location;
83 if (closure == nullptr) {
84 return;
85 }
86 #ifndef NDEBUG
87 if (closure->scheduled) {
88 gpr_log(GPR_ERROR,
89 "Closure already scheduled. (closure: %p, created: [%s:%d], "
90 "previously scheduled at: [%s: %d], newly scheduled at [%s: %d]",
91 closure, closure->file_created, closure->line_created,
92 closure->file_initiated, closure->line_initiated, location.file(),
93 location.line());
94 abort();
95 }
96 closure->scheduled = true;
97 closure->file_initiated = location.file();
98 closure->line_initiated = location.line();
99 closure->run = false;
100 GPR_ASSERT(closure->cb != nullptr);
101 #endif
102 closure->error_data.error = internal::StatusAllocHeapPtr(error);
103 exec_ctx_sched(closure);
104 }
105
106 void ExecCtx::RunList(const DebugLocation& location, grpc_closure_list* list) {
107 (void)location;
108 grpc_closure* c = list->head;
109 while (c != nullptr) {
110 grpc_closure* next = c->next_data.next;
111 #ifndef NDEBUG
112 if (c->scheduled) {
113 gpr_log(GPR_ERROR,
114 "Closure already scheduled. (closure: %p, created: [%s:%d], "
115 "previously scheduled at: [%s: %d], newly scheduled at [%s:%d]",
116 c, c->file_created, c->line_created, c->file_initiated,
117 c->line_initiated, location.file(), location.line());
118 abort();
119 }
120 c->scheduled = true;
121 c->file_initiated = location.file();
122 c->line_initiated = location.line();
123 c->run = false;
124 GPR_ASSERT(c->cb != nullptr);
125 #endif
126 exec_ctx_sched(c);
127 c = next;
128 }
129 list->head = list->tail = nullptr;
130 }
131
132 } // namespace grpc_core