call_combiner.h
Go to the documentation of this file.
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 #ifndef GRPC_CORE_LIB_IOMGR_CALL_COMBINER_H
20 #define GRPC_CORE_LIB_IOMGR_CALL_COMBINER_H
21 
23 
24 #include <stddef.h>
25 
26 #include "absl/container/inlined_vector.h"
27 
28 #include <grpc/support/atm.h>
29 
36 
37 // A simple, lock-free mechanism for serializing activity related to a
38 // single call. This is similar to a combiner but is more lightweight.
39 //
40 // It requires the callback (or, in the common case where the callback
41 // actually kicks off a chain of callbacks, the last callback in that
42 // chain) to explicitly indicate (by calling GRPC_CALL_COMBINER_STOP())
43 // when it is done with the action that was kicked off by the original
44 // callback.
45 
46 namespace grpc_core {
47 
49 
50 class CallCombiner {
51  public:
52  CallCombiner();
53  ~CallCombiner();
54 
55 #ifndef NDEBUG
56 #define GRPC_CALL_COMBINER_START(call_combiner, closure, error, reason) \
57  (call_combiner)->Start((closure), (error), __FILE__, __LINE__, (reason))
58 #define GRPC_CALL_COMBINER_STOP(call_combiner, reason) \
59  (call_combiner)->Stop(__FILE__, __LINE__, (reason))
62  int line, const char* reason);
64  void Stop(const char* file, int line, const char* reason);
65 #else
66 #define GRPC_CALL_COMBINER_START(call_combiner, closure, error, reason) \
67  (call_combiner)->Start((closure), (error), (reason))
68 #define GRPC_CALL_COMBINER_STOP(call_combiner, reason) \
69  (call_combiner)->Stop((reason))
72  const char* reason);
74  void Stop(const char* reason);
75 #endif
76 
99 
102 
103  private:
105 #ifdef GRPC_TSAN_ENABLED
106  static void TsanClosure(void* arg, grpc_error_handle error);
107 #endif
108 
109  gpr_atm size_ = 0; // size_t, num closures in queue or currently executing
111  // Either 0 (if not cancelled and no cancellation closure set),
112  // a grpc_closure* (if the lowest bit is 0),
113  // or a grpc_error_handle (if the lowest bit is 1).
115 #ifdef GRPC_TSAN_ENABLED
116  // A fake ref-counted lock that is kept alive after the destruction of
117  // grpc_call_combiner, when we are running the original closure.
118  //
119  // Ideally we want to lock and unlock the call combiner as a pointer, when the
120  // callback is called. However, original_closure is free to trigger
121  // anything on the call combiner (including destruction of grpc_call).
122  // Thus, we need a ref-counted structure that can outlive the call combiner.
123  struct TsanLock : public RefCounted<TsanLock, NonPolymorphicRefCount> {
124  TsanLock() { TSAN_ANNOTATE_RWLOCK_CREATE(&taken); }
125  ~TsanLock() { TSAN_ANNOTATE_RWLOCK_DESTROY(&taken); }
126  // To avoid double-locking by the same thread, we should acquire/release
127  // the lock only when taken is false. On each acquire taken must be set to
128  // true.
129  std::atomic<bool> taken{false};
130  };
131  RefCountedPtr<TsanLock> tsan_lock_ = MakeRefCounted<TsanLock>();
132  grpc_closure tsan_closure_;
133  grpc_closure* original_closure_;
134 #endif
135 };
136 
137 // Helper for running a list of closures in a call combiner.
138 //
139 // Each callback running in the call combiner will eventually be
140 // returned to the surface, at which point the surface will yield the
141 // call combiner. So when we are running in the call combiner and have
142 // more than one callback to return to the surface, we need to re-enter
143 // the call combiner for all but one of those callbacks.
145  public:
147 
148  // Adds a closure to the list. The closure must eventually result in
149  // the call combiner being yielded.
150  void Add(grpc_closure* closure, grpc_error_handle error, const char* reason) {
151  closures_.emplace_back(closure, error, reason);
152  }
153 
154  // Runs all closures in the call combiner and yields the call combiner.
155  //
156  // All but one of the closures in the list will be scheduled via
157  // GRPC_CALL_COMBINER_START(), and the remaining closure will be
158  // scheduled via ExecCtx::Run(), which will eventually result
159  // in yielding the call combiner. If the list is empty, then the call
160  // combiner will be yielded immediately.
161  void RunClosures(CallCombiner* call_combiner) {
162  if (closures_.empty()) {
163  GRPC_CALL_COMBINER_STOP(call_combiner, "no closures to schedule");
164  return;
165  }
166  for (size_t i = 1; i < closures_.size(); ++i) {
167  auto& closure = closures_[i];
168  GRPC_CALL_COMBINER_START(call_combiner, closure.closure, closure.error,
169  closure.reason);
170  }
173  "CallCombinerClosureList executing closure while already "
174  "holding call_combiner %p: closure=%p error=%s reason=%s",
175  call_combiner, closures_[0].closure,
177  closures_[0].reason);
178  }
179  // This will release the call combiner.
181  closures_.clear();
182  }
183 
184  // Runs all closures in the call combiner, but does NOT yield the call
185  // combiner. All closures will be scheduled via GRPC_CALL_COMBINER_START().
187  for (size_t i = 0; i < closures_.size(); ++i) {
188  auto& closure = closures_[i];
189  GRPC_CALL_COMBINER_START(call_combiner, closure.closure, closure.error,
190  closure.reason);
191  }
192  closures_.clear();
193  }
194 
195  size_t size() const { return closures_.size(); }
196 
197  private:
201  const char* reason;
202 
204  const char* reason)
206  };
207 
208  // There are generally a maximum of 6 closures to run in the call
209  // combiner, one for each pending op.
211 };
212 
213 } // namespace grpc_core
214 
215 #endif /* GRPC_CORE_LIB_IOMGR_CALL_COMBINER_H */
grpc_core::CallCombinerClosureList
Definition: call_combiner.h:144
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc_core::CallCombiner
Definition: call_combiner.h:50
mpscq.h
grpc_core::CallCombiner::SetNotifyOnCancel
void SetNotifyOnCancel(grpc_closure *closure)
Definition: call_combiner.cc:212
TSAN_ANNOTATE_RWLOCK_DESTROY
#define TSAN_ANNOTATE_RWLOCK_DESTROY(addr)
Definition: src/core/lib/iomgr/dynamic_annotations.h:61
grpc_core
Definition: call_metric_recorder.h:31
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_core::MultiProducerSingleConsumerQueue
Definition: mpscq.h:35
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
closure.h
TSAN_ANNOTATE_RWLOCK_CREATE
#define TSAN_ANNOTATE_RWLOCK_CREATE(addr)
Definition: src/core/lib/iomgr/dynamic_annotations.h:60
grpc_core::CallCombiner::Start
void Start(grpc_closure *closure, grpc_error_handle error, const char *file, int line, const char *reason)
Starts processing closure.
Definition: call_combiner.cc:123
GRPC_TRACE_FLAG_ENABLED
#define GRPC_TRACE_FLAG_ENABLED(f)
Definition: debug/trace.h:114
grpc_core::CallCombiner::CallCombiner
CallCombiner()
Definition: call_combiner.cc:52
grpc_core::CallCombinerClosureList::size
size_t size() const
Definition: call_combiner.h:195
grpc_core::CallCombinerClosureList::CallCombinerClosure::CallCombinerClosure
CallCombinerClosure(grpc_closure *closure, grpc_error_handle error, const char *reason)
Definition: call_combiner.h:203
grpc_core::CallCombiner::Cancel
void Cancel(grpc_error_handle error)
Indicates that the call has been cancelled.
Definition: call_combiner.cc:255
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
grpc_core::CallCombiner::ScheduleClosure
void ScheduleClosure(grpc_closure *closure, grpc_error_handle error)
Definition: call_combiner.cc:103
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
grpc_core::CallCombiner::size_
gpr_atm size_
Definition: call_combiner.h:109
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
GRPC_CALL_COMBINER_STOP
#define GRPC_CALL_COMBINER_STOP(call_combiner, reason)
Definition: call_combiner.h:58
grpc_core::CallCombiner::cancel_state_
gpr_atm cancel_state_
Definition: call_combiner.h:114
arg
Definition: cmdline.cc:40
grpc_core::CallCombiner::Stop
void Stop(const char *file, int line, const char *reason)
Yields the call combiner to the next closure in the queue, if any.
Definition: call_combiner.cc:163
grpc_core::CallCombiner::~CallCombiner
~CallCombiner()
Definition: call_combiner.cc:61
grpc_core::RefCounted
Definition: ref_counted.h:280
GRPC_CALL_COMBINER_START
#define GRPC_CALL_COMBINER_START(call_combiner, closure, error, reason)
Definition: call_combiner.h:56
grpc_core::TraceFlag
Definition: debug/trace.h:63
grpc_core::CallCombinerClosureList::RunClosures
void RunClosures(CallCombiner *call_combiner)
Definition: call_combiner.h:161
grpc_core::CallCombinerClosureList::CallCombinerClosure::error
grpc_error_handle error
Definition: call_combiner.h:200
gpr_atm
intptr_t gpr_atm
Definition: impl/codegen/atm_gcc_atomic.h:32
ref_counted.h
grpc_core::CallCombinerClosureList::Add
void Add(grpc_closure *closure, grpc_error_handle error, const char *reason)
Definition: call_combiner.h:150
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
grpc_core::CallCombinerClosureList::CallCombinerClosure::closure
grpc_closure * closure
Definition: call_combiner.h:199
grpc_core::CallCombiner::queue_
MultiProducerSingleConsumerQueue queue_
Definition: call_combiner.h:110
grpc_core::CallCombinerClosureList::RunClosuresWithoutYielding
void RunClosuresWithoutYielding(CallCombiner *call_combiner)
Definition: call_combiner.h:186
regen-readme.line
line
Definition: regen-readme.py:30
exec_ctx.h
grpc_core::grpc_call_combiner_trace
DebugOnlyTraceFlag grpc_call_combiner_trace(false, "call_combiner")
Definition: call_combiner.h:48
closure
Definition: proxy.cc:59
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
ref_counted_ptr.h
grpc_core::CallCombinerClosureList::closures_
absl::InlinedVector< CallCombinerClosure, 6 > closures_
Definition: call_combiner.h:210
grpc_core::CallCombinerClosureList::CallCombinerClosure::reason
const char * reason
Definition: call_combiner.h:201
grpc_core::CallCombinerClosureList::CallCombinerClosureList
CallCombinerClosureList()
Definition: call_combiner.h:146
grpc_core::CallCombinerClosureList::CallCombinerClosure
Definition: call_combiner.h:198
atm.h
absl::InlinedVector
Definition: abseil-cpp/absl/container/inlined_vector.h:69
grpc_error
Definition: error_internal.h:42
dynamic_annotations.h
grpc_closure
Definition: closure.h:56
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:42