Debian Codegraph @

src/core/lib/iomgr/call_combiner.cc

raw
1 /*
2 *
3 * Copyright 2017 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/call_combiner.h"
22
23 #include <inttypes.h>
24
25 #include <grpc/support/log.h>
26
27 #include "src/core/lib/debug/stats.h"
28 #include "src/core/lib/debug/stats_data.h"
29
30 namespace grpc_core {
31
32 DebugOnlyTraceFlag grpc_call_combiner_trace(false, "call_combiner");
33
34 namespace {
35
36 // grpc_error LSB can be used
37 constexpr intptr_t kErrorBit = 1;
38
39 grpc_error_handle DecodeCancelStateError(gpr_atm cancel_state) {
40 if (cancel_state & kErrorBit) {
41 return internal::StatusGetFromHeapPtr(cancel_state & ~kErrorBit);
42 }
43 return absl::OkStatus();
44 }
45
46 } // namespace
47
48 CallCombiner::CallCombiner() {
49 gpr_atm_no_barrier_store(&cancel_state_, 0);
50 gpr_atm_no_barrier_store(&size_, 0);
51 #ifdef GRPC_TSAN_ENABLED
52 GRPC_CLOSURE_INIT(&tsan_closure_, TsanClosure, this,
53 grpc_schedule_on_exec_ctx);
54 #endif
55 }
56
57 CallCombiner::~CallCombiner() {
58 if (cancel_state_ & kErrorBit) {
59 internal::StatusFreeHeapPtr(cancel_state_ & ~kErrorBit);
60 }
61 }
62
63 #ifdef GRPC_TSAN_ENABLED
64 void CallCombiner::TsanClosure(void* arg, grpc_error_handle error) {
65 CallCombiner* self = static_cast<CallCombiner*>(arg);
66 // We ref-count the lock, and check if it's already taken.
67 // If it was taken, we should do nothing. Otherwise, we will mark it as
68 // locked. Note that if two different threads try to do this, only one of
69 // them will be able to mark the lock as acquired, while they both run their
70 // callbacks. In such cases (which should never happen for call_combiner),
71 // TSAN will correctly produce an error.
72 //
73 // TODO(soheil): This only covers the callbacks scheduled by
74 // CallCombiner::Start() and CallCombiner::Stop().
75 // If in the future, a callback gets scheduled using other
76 // mechanisms, we will need to add APIs to externally lock
77 // call combiners.
78 RefCountedPtr<TsanLock> lock = self->tsan_lock_;
79 bool prev = false;
80 if (lock->taken.compare_exchange_strong(prev, true)) {
81 TSAN_ANNOTATE_RWLOCK_ACQUIRED(&lock->taken, true);
82 } else {
83 lock.reset();
84 }
85 Closure::Run(DEBUG_LOCATION, self->original_closure_, error);
86 if (lock != nullptr) {
87 TSAN_ANNOTATE_RWLOCK_RELEASED(&lock->taken, true);
88 bool prev = true;
89 GPR_ASSERT(lock->taken.compare_exchange_strong(prev, false));
90 }
91 }
92 #endif
93
94 void CallCombiner::ScheduleClosure(grpc_closure* closure,
95 grpc_error_handle error) {
96 #ifdef GRPC_TSAN_ENABLED
97 original_closure_ = closure;
98 ExecCtx::Run(DEBUG_LOCATION, &tsan_closure_, error);
99 #else
100 ExecCtx::Run(DEBUG_LOCATION, closure, error);
101 #endif
102 }
103
104 #ifndef NDEBUG
105 #define DEBUG_ARGS const char *file, int line,
106 #define DEBUG_FMT_STR "%s:%d: "
107 #define DEBUG_FMT_ARGS , file, line
108 #else
109 #define DEBUG_ARGS
110 #define DEBUG_FMT_STR
111 #define DEBUG_FMT_ARGS
112 #endif
113
114 void CallCombiner::Start(grpc_closure* closure, grpc_error_handle error,
115 DEBUG_ARGS const char* reason) {
116 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
117 gpr_log(GPR_INFO,
118 "==> CallCombiner::Start() [%p] closure=%p [" DEBUG_FMT_STR
119 "%s] error=%s",
120 this, closure DEBUG_FMT_ARGS, reason,
121 StatusToString(error).c_str());
122 }
123 size_t prev_size =
124 static_cast<size_t>(gpr_atm_full_fetch_add(&size_, (gpr_atm)1));
125 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
126 gpr_log(GPR_INFO, " size: %" PRIdPTR " -> %" PRIdPTR, prev_size,
127 prev_size + 1);
128 }
129 if (prev_size == 0) {
130 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
131 gpr_log(GPR_INFO, " EXECUTING IMMEDIATELY");
132 }
133 // Queue was empty, so execute this closure immediately.
134 ScheduleClosure(closure, error);
135 } else {
136 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
137 gpr_log(GPR_INFO, " QUEUING");
138 }
139 // Queue was not empty, so add closure to queue.
140 closure->error_data.error = internal::StatusAllocHeapPtr(error);
141 queue_.Push(
142 reinterpret_cast<MultiProducerSingleConsumerQueue::Node*>(closure));
143 }
144 }
145
146 void CallCombiner::Stop(DEBUG_ARGS const char* reason) {
147 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
148 gpr_log(GPR_INFO, "==> CallCombiner::Stop() [%p] [" DEBUG_FMT_STR "%s]",
149 this DEBUG_FMT_ARGS, reason);
150 }
151 size_t prev_size =
152 static_cast<size_t>(gpr_atm_full_fetch_add(&size_, (gpr_atm)-1));
153 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
154 gpr_log(GPR_INFO, " size: %" PRIdPTR " -> %" PRIdPTR, prev_size,
155 prev_size - 1);
156 }
157 GPR_ASSERT(prev_size >= 1);
158 if (prev_size > 1) {
159 while (true) {
160 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
161 gpr_log(GPR_INFO, " checking queue");
162 }
163 bool empty;
164 grpc_closure* closure =
165 reinterpret_cast<grpc_closure*>(queue_.PopAndCheckEnd(&empty));
166 if (closure == nullptr) {
167 // This can happen either due to a race condition within the mpscq
168 // code or because of a race with Start().
169 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
170 gpr_log(GPR_INFO, " queue returned no result; checking again");
171 }
172 continue;
173 }
174 grpc_error_handle error =
175 internal::StatusMoveFromHeapPtr(closure->error_data.error);
176 closure->error_data.error = 0;
177 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
178 gpr_log(GPR_INFO, " EXECUTING FROM QUEUE: closure=%p error=%s",
179 closure, StatusToString(error).c_str());
180 }
181 ScheduleClosure(closure, error);
182 break;
183 }
184 } else if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
185 gpr_log(GPR_INFO, " queue empty");
186 }
187 }
188
189 void CallCombiner::SetNotifyOnCancel(grpc_closure* closure) {
190 while (true) {
191 // Decode original state.
192 gpr_atm original_state = gpr_atm_acq_load(&cancel_state_);
193 grpc_error_handle original_error = DecodeCancelStateError(original_state);
194 // If error is set, invoke the cancellation closure immediately.
195 // Otherwise, store the new closure.
196 if (!original_error.ok()) {
197 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
198 gpr_log(GPR_INFO,
199 "call_combiner=%p: scheduling notify_on_cancel callback=%p "
200 "for pre-existing cancellation",
201 this, closure);
202 }
203 ExecCtx::Run(DEBUG_LOCATION, closure, original_error);
204 break;
205 } else {
206 if (gpr_atm_full_cas(&cancel_state_, original_state,
207 reinterpret_cast<gpr_atm>(closure))) {
208 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
209 gpr_log(GPR_INFO, "call_combiner=%p: setting notify_on_cancel=%p",
210 this, closure);
211 }
212 // If we replaced an earlier closure, invoke the original
213 // closure with absl::OkStatus(). This allows callers to clean
214 // up any resources they may be holding for the callback.
215 if (original_state != 0) {
216 closure = reinterpret_cast<grpc_closure*>(original_state);
217 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
218 gpr_log(GPR_INFO,
219 "call_combiner=%p: scheduling old cancel callback=%p", this,
220 closure);
221 }
222 ExecCtx::Run(DEBUG_LOCATION, closure, absl::OkStatus());
223 }
224 break;
225 }
226 }
227 // cas failed, try again.
228 }
229 }
230
231 void CallCombiner::Cancel(grpc_error_handle error) {
232 intptr_t status_ptr = internal::StatusAllocHeapPtr(error);
233 gpr_atm new_state = kErrorBit | status_ptr;
234 while (true) {
235 gpr_atm original_state = gpr_atm_acq_load(&cancel_state_);
236 grpc_error_handle original_error = DecodeCancelStateError(original_state);
237 if (!original_error.ok()) {
238 internal::StatusFreeHeapPtr(status_ptr);
239 break;
240 }
241 if (gpr_atm_full_cas(&cancel_state_, original_state, new_state)) {
242 if (original_state != 0) {
243 grpc_closure* notify_on_cancel =
244 reinterpret_cast<grpc_closure*>(original_state);
245 if (GRPC_TRACE_FLAG_ENABLED(grpc_call_combiner_trace)) {
246 gpr_log(GPR_INFO,
247 "call_combiner=%p: scheduling notify_on_cancel callback=%p",
248 this, notify_on_cancel);
249 }
250 ExecCtx::Run(DEBUG_LOCATION, notify_on_cancel, error);
251 }
252 break;
253 }
254 // cas failed, try again.
255 }
256 }
257
258 } // namespace grpc_core