callback_common.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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 GRPCPP_IMPL_CODEGEN_CALLBACK_COMMON_H
20 #define GRPCPP_IMPL_CODEGEN_CALLBACK_COMMON_H
21 
22 // IWYU pragma: private
23 
24 #include <functional>
25 
33 
34 namespace grpc {
35 namespace internal {
36 
38 // TODO(vjpai): decide whether it is better for this to take a const lvalue
39 // parameter or an rvalue parameter, or if it even matters
40 template <class Func, class... Args>
42 #if GRPC_ALLOW_EXCEPTIONS
43  try {
44  func(std::forward<Args>(args)...);
45  } catch (...) {
46  // nothing to return or change here, just don't crash the library
47  }
48 #else // GRPC_ALLOW_EXCEPTIONS
49  func(std::forward<Args>(args)...);
50 #endif // GRPC_ALLOW_EXCEPTIONS
51 }
52 
53 template <class Reactor, class Func, class... Args>
55 #if GRPC_ALLOW_EXCEPTIONS
56  try {
57  return func(std::forward<Args>(args)...);
58  } catch (...) {
59  // fail the RPC, don't crash the library
60  return nullptr;
61  }
62 #else // GRPC_ALLOW_EXCEPTIONS
63  return func(std::forward<Args>(args)...);
64 #endif // GRPC_ALLOW_EXCEPTIONS
65 }
66 
67 // The contract on these tags is that they are single-shot. They must be
68 // constructed and then fired at exactly one point. There is no expectation
69 // that they can be reused without reconstruction.
70 
72  public:
73  // always allocated against a call arena, no memory free required
74  static void operator delete(void* /*ptr*/, std::size_t size) {
76  }
77 
78  // This operator should never be called as the memory should be freed as part
79  // of the arena destruction. It only exists to provide a matching operator
80  // delete to the operator new so that some compilers will not complain (see
81  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
82  // there are no tests catching the compiler warning.
83  static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); }
84 
87  : call_(call), func_(std::move(f)), ops_(ops) {
90  // A client-side callback should never be run inline since they will always
91  // have work to do from the user application. So, set the parent's
92  // inlineable field to false
93  inlineable = false;
94  }
96  Status* status_ptr() { return &status_; }
97 
98  // force_run can not be performed on a tag if operations using this tag
99  // have been sent to PerformOpsOnCall. It is intended for error conditions
100  // that are detected before the operations are internally processed.
101  void force_run(Status s) {
102  status_ = std::move(s);
103  Run(true);
104  }
105 
106  private:
111 
113  static_cast<CallbackWithStatusTag*>(cb)->Run(static_cast<bool>(ok));
114  }
115  void Run(bool ok) {
116  void* ignored = ops_;
117 
118  if (!ops_->FinalizeResult(&ignored, &ok)) {
119  // The tag was swallowed
120  return;
121  }
122  GPR_CODEGEN_ASSERT(ignored == ops_);
123 
124  // Last use of func_ or status_, so ok to move them out
125  auto func = std::move(func_);
126  auto status = std::move(status_);
127  func_ = nullptr; // reset to clear this out for sure
128  status_ = Status(); // reset to clear this out for sure
131  }
132 };
133 
138  public:
139  // always allocated against a call arena, no memory free required
140  static void operator delete(void* /*ptr*/, std::size_t size) {
142  }
143 
144  // This operator should never be called as the memory should be freed as part
145  // of the arena destruction. It only exists to provide a matching operator
146  // delete to the operator new so that some compilers will not complain (see
147  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
148  // there are no tests catching the compiler warning.
149  static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); }
150 
152 
155 
157 
158  // Set can only be called on a default-constructed or Clear'ed tag.
159  // It should never be called on a tag that was constructed with arguments
160  // or on a tag that has been Set before unless the tag has been cleared.
161  // can_inline indicates that this particular callback can be executed inline
162  // (without needing a thread hop) and is only used for library-provided server
163  // callbacks.
164  void Set(grpc_call* call, std::function<void(bool)> f,
165  CompletionQueueTag* ops, bool can_inline) {
166  GPR_CODEGEN_ASSERT(call_ == nullptr);
168  call_ = call;
169  func_ = std::move(f);
170  ops_ = ops;
172  inlineable = can_inline;
173  }
174 
175  void Clear() {
176  if (call_ != nullptr) {
177  grpc_call* call = call_;
178  call_ = nullptr;
179  func_ = nullptr;
181  }
182  }
183 
184  CompletionQueueTag* ops() { return ops_; }
185 
186  // force_run can not be performed on a tag if operations using this tag
187  // have been sent to PerformOpsOnCall. It is intended for error conditions
188  // that are detected before the operations are internally processed.
189  void force_run(bool ok) { Run(ok); }
190 
192  /* NOLINTNEXTLINE(google-explicit-constructor) */
193  operator bool() const { return call_ != nullptr; }
194 
195  private:
197  std::function<void(bool)> func_;
199 
201  static_cast<CallbackWithSuccessTag*>(cb)->Run(static_cast<bool>(ok));
202  }
203  void Run(bool ok) {
204  void* ignored = ops_;
205  // Allow a "false" return value from FinalizeResult to silence the
206  // callback, just as it silences a CQ tag in the async cases
207 #ifndef NDEBUG
208  auto* ops = ops_;
209 #endif
210  bool do_callback = ops_->FinalizeResult(&ignored, &ok);
211  GPR_CODEGEN_DEBUG_ASSERT(ignored == ops);
212 
213  if (do_callback) {
215  }
216  }
217 };
218 
219 } // namespace internal
220 } // namespace grpc
221 
222 #endif // GRPCPP_IMPL_CODEGEN_CALLBACK_COMMON_H
grpc::internal::CallbackWithStatusTag
Definition: callback_common.h:71
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
grpc::internal::CallbackWithStatusTag::status_ptr
Status * status_ptr()
Definition: callback_common.h:96
bool
bool
Definition: setup_once.h:312
grpc
Definition: grpcpp/alarm.h:33
grpc::internal::CallbackWithStatusTag::force_run
void force_run(Status s)
Definition: callback_common.h:101
grpc::internal::CallbackWithSuccessTag::Clear
void Clear()
Definition: callback_common.h:175
grpc::internal::CallbackWithSuccessTag::~CallbackWithSuccessTag
~CallbackWithSuccessTag()
Definition: callback_common.h:156
grpc::internal::CallbackWithSuccessTag::func_
std::function< void(bool)> func_
Definition: callback_common.h:197
grpc::internal::CallbackWithSuccessTag::call_
grpc_call * call_
Definition: callback_common.h:196
core_codegen_interface.h
grpc::internal::CallbackWithSuccessTag::Run
void Run(bool ok)
Definition: callback_common.h:203
grpc::internal::CallbackWithStatusTag::StaticRun
static void StaticRun(grpc_completion_queue_functor *cb, int ok)
Definition: callback_common.h:112
config.h
call
FilterStackCall * call
Definition: call.cc:750
grpc::internal::CallbackWithStatusTag::Run
void Run(bool ok)
Definition: callback_common.h:115
grpc::internal::CallbackWithSuccessTag::ops
CompletionQueueTag * ops()
Definition: callback_common.h:184
grpc_types.h
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: include/grpcpp/impl/codegen/completion_queue.h:98
grpc::internal::CallbackWithStatusTag::func_
std::function< void(Status)> func_
Definition: callback_common.h:108
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_completion_queue_functor::functor_run
void(* functor_run)(struct grpc_completion_queue_functor *, int)
Definition: grpc_types.h:778
hpack_encoder_fixtures::Args
Args({0, 16384})
grpc::internal::CallbackWithSuccessTag::CallbackWithSuccessTag
CallbackWithSuccessTag()
Definition: callback_common.h:151
absl::random_internal_nanobenchmark::Func
FuncOutput(*)(const void *, FuncInput) Func
Definition: abseil-cpp/absl/random/internal/nanobenchmark.h:68
grpc::internal::CallbackWithSuccessTag
Definition: callback_common.h:137
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
grpc::internal::CallbackWithSuccessTag::StaticRun
static void StaticRun(grpc_completion_queue_functor *cb, int ok)
Definition: callback_common.h:200
completion_queue_tag.h
grpc::internal::CompletionQueueTag::FinalizeResult
virtual bool FinalizeResult(void **tag, bool *status)=0
grpc::internal::CallbackWithStatusTag::ops_
CompletionQueueTag * ops_
Definition: callback_common.h:109
status.h
grpc::internal::CallbackWithSuccessTag::ops_
CompletionQueueTag * ops_
Definition: callback_common.h:198
func
const EVP_CIPHER *(* func)(void)
Definition: cipher_extra.c:73
grpc::internal::CallbackWithStatusTag::CallbackWithStatusTag
CallbackWithStatusTag(grpc_call *call, std::function< void(Status)> f, CompletionQueueTag *ops)
Definition: callback_common.h:85
channel_interface.h
grpc::internal::CallbackWithSuccessTag::Set
void Set(grpc_call *call, std::function< void(bool)> f, CompletionQueueTag *ops, bool can_inline)
Definition: callback_common.h:164
grpc::internal::CatchingCallback
void CatchingCallback(Func &&func, Args &&... args)
An exception-safe way of invoking a user-specified callback function.
Definition: callback_common.h:41
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::internal::CallbackWithSuccessTag::force_run
void force_run(bool ok)
Definition: callback_common.h:189
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
ok
bool ok
Definition: async_end2end_test.cc:197
grpc_completion_queue_functor
Definition: grpc_types.h:773
grpc::internal::CallbackWithStatusTag::~CallbackWithStatusTag
~CallbackWithStatusTag()
Definition: callback_common.h:95
grpc::CoreCodegenInterface::grpc_call_ref
virtual void grpc_call_ref(grpc_call *call)=0
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: grpcpp/impl/codegen/core_codegen_interface.h:151
grpc::internal::CallbackWithStatusTag::call_
grpc_call * call_
Definition: callback_common.h:107
internal
Definition: benchmark/test/output_test_helper.cc:20
grpc::internal::CallbackWithStatusTag::status_
Status status_
Definition: callback_common.h:110
grpc::internal::CompletionQueueTag
An interface allowing implementors to process and filter event tags.
Definition: grpcpp/impl/codegen/completion_queue_tag.h:28
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
grpc::internal::CatchingReactorGetter
Reactor * CatchingReactorGetter(Func &&func, Args &&... args)
Definition: callback_common.h:54
ops
static grpc_op ops[6]
Definition: test/core/fling/client.cc:39
grpc::CoreCodegenInterface::grpc_call_unref
virtual void grpc_call_unref(grpc_call *call)=0
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
grpc::internal::CallbackWithSuccessTag::operator=
CallbackWithSuccessTag & operator=(const CallbackWithSuccessTag &)=delete
grpc_completion_queue_functor::inlineable
int inlineable
Definition: grpc_types.h:782
GPR_CODEGEN_DEBUG_ASSERT
#define GPR_CODEGEN_DEBUG_ASSERT(x)
Codegen specific version of GPR_DEBUG_ASSERT.
Definition: grpcpp/impl/codegen/core_codegen_interface.h:160
call.h


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