GRPC C++  1.66.0
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_SUPPORT_CALLBACK_COMMON_H
20 #define GRPCPP_SUPPORT_CALLBACK_COMMON_H
21 
22 #include <functional>
23 
24 #include "absl/log/absl_check.h"
25 
26 #include <grpc/grpc.h>
27 #include <grpc/impl/grpc_types.h>
28 #include <grpc/support/log.h>
29 #include <grpcpp/impl/call.h>
32 #include <grpcpp/support/config.h>
33 #include <grpcpp/support/status.h>
34 
35 namespace grpc {
36 namespace internal {
37 
39 // TODO(vjpai): decide whether it is better for this to take a const lvalue
40 // parameter or an rvalue parameter, or if it even matters
41 template <class Func, class... Args>
42 void CatchingCallback(Func&& func, Args&&... args) {
43 #if GRPC_ALLOW_EXCEPTIONS
44  try {
45  func(std::forward<Args>(args)...);
46  } catch (...) {
47  // nothing to return or change here, just don't crash the library
48  }
49 #else // GRPC_ALLOW_EXCEPTIONS
50  func(std::forward<Args>(args)...);
51 #endif // GRPC_ALLOW_EXCEPTIONS
52 }
53 
54 template <class Reactor, class Func, class... Args>
55 Reactor* CatchingReactorGetter(Func&& func, Args&&... args) {
56 #if GRPC_ALLOW_EXCEPTIONS
57  try {
58  return func(std::forward<Args>(args)...);
59  } catch (...) {
60  // fail the RPC, don't crash the library
61  return nullptr;
62  }
63 #else // GRPC_ALLOW_EXCEPTIONS
64  return func(std::forward<Args>(args)...);
65 #endif // GRPC_ALLOW_EXCEPTIONS
66 }
67 
68 // The contract on these tags is that they are single-shot. They must be
69 // constructed and then fired at exactly one point. There is no expectation
70 // that they can be reused without reconstruction.
71 
73  public:
74  // always allocated against a call arena, no memory free required
75  static void operator delete(void* /*ptr*/, std::size_t size) {
76  ABSL_CHECK_EQ(size, sizeof(CallbackWithStatusTag));
77  }
78 
79  // This operator should never be called as the memory should be freed as part
80  // of the arena destruction. It only exists to provide a matching operator
81  // delete to the operator new so that some compilers will not complain (see
82  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
83  // there are no tests catching the compiler warning.
84  static void operator delete(void*, void*) { ABSL_CHECK(false); }
85 
86  CallbackWithStatusTag(grpc_call* call, std::function<void(Status)> f,
87  CompletionQueueTag* ops)
88  : call_(call), func_(std::move(f)), ops_(ops) {
89  grpc_call_ref(call);
90  functor_run = &CallbackWithStatusTag::StaticRun;
91  // A client-side callback should never be run inline since they will always
92  // have work to do from the user application. So, set the parent's
93  // inlineable field to false
94  inlineable = false;
95  }
97  Status* status_ptr() { return &status_; }
98 
99  // force_run can not be performed on a tag if operations using this tag
100  // have been sent to PerformOpsOnCall. It is intended for error conditions
101  // that are detected before the operations are internally processed.
102  void force_run(Status s) {
103  status_ = std::move(s);
104  Run(true);
105  }
106 
107  private:
108  grpc_call* call_;
109  std::function<void(Status)> func_;
110  CompletionQueueTag* ops_;
111  Status status_;
112 
113  static void StaticRun(grpc_completion_queue_functor* cb, int ok) {
114  static_cast<CallbackWithStatusTag*>(cb)->Run(static_cast<bool>(ok));
115  }
116  void Run(bool ok) {
117  void* ignored = ops_;
118 
119  if (!ops_->FinalizeResult(&ignored, &ok)) {
120  // The tag was swallowed
121  return;
122  }
123  ABSL_CHECK(ignored == ops_);
124 
125  // Last use of func_ or status_, so ok to move them out
126  auto func = std::move(func_);
127  auto status = std::move(status_);
128  func_ = nullptr; // reset to clear this out for sure
129  status_ = Status(); // reset to clear this out for sure
130  CatchingCallback(std::move(func), std::move(status));
131  grpc_call_unref(call_);
132  }
133 };
134 
139  public:
140  // always allocated against a call arena, no memory free required
141  static void operator delete(void* /*ptr*/, std::size_t size) {
142  ABSL_CHECK_EQ(size, sizeof(CallbackWithSuccessTag));
143  }
144 
145  // This operator should never be called as the memory should be freed as part
146  // of the arena destruction. It only exists to provide a matching operator
147  // delete to the operator new so that some compilers will not complain (see
148  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
149  // there are no tests catching the compiler warning.
150  static void operator delete(void*, void*) { ABSL_CHECK(false); }
151 
152  CallbackWithSuccessTag() : call_(nullptr) {}
153 
156 
158 
159  // Set can only be called on a default-constructed or Clear'ed tag.
160  // It should never be called on a tag that was constructed with arguments
161  // or on a tag that has been Set before unless the tag has been cleared.
162  // can_inline indicates that this particular callback can be executed inline
163  // (without needing a thread hop) and is only used for library-provided server
164  // callbacks.
165  void Set(grpc_call* call, std::function<void(bool)> f,
166  CompletionQueueTag* ops, bool can_inline) {
167  ABSL_CHECK_EQ(call_, nullptr);
168  grpc_call_ref(call);
169  call_ = call;
170  func_ = std::move(f);
171  ops_ = ops;
172  functor_run = &CallbackWithSuccessTag::StaticRun;
173  inlineable = can_inline;
174  }
175 
176  void Clear() {
177  if (call_ != nullptr) {
178  grpc_call* call = call_;
179  call_ = nullptr;
180  func_ = nullptr;
181  grpc_call_unref(call);
182  }
183  }
184 
185  CompletionQueueTag* ops() { return ops_; }
186 
187  // force_run can not be performed on a tag if operations using this tag
188  // have been sent to PerformOpsOnCall. It is intended for error conditions
189  // that are detected before the operations are internally processed.
190  void force_run(bool ok) { Run(ok); }
191 
193  // NOLINTNEXTLINE(google-explicit-constructor)
194  operator bool() const { return call_ != nullptr; }
195 
196  private:
197  grpc_call* call_;
198  std::function<void(bool)> func_;
199  CompletionQueueTag* ops_;
200 
201  static void StaticRun(grpc_completion_queue_functor* cb, int ok) {
202  static_cast<CallbackWithSuccessTag*>(cb)->Run(static_cast<bool>(ok));
203  }
204  void Run(bool ok) {
205  void* ignored = ops_;
206  // Allow a "false" return value from FinalizeResult to silence the
207  // callback, just as it silences a CQ tag in the async cases
208 #ifndef NDEBUG
209  auto* ops = ops_;
210 #endif
211  bool do_callback = ops_->FinalizeResult(&ignored, &ok);
212 #ifndef NDEBUG
213  ABSL_DCHECK(ignored == ops);
214 #endif
215 
216  if (do_callback) {
217  CatchingCallback(func_, ok);
218  }
219  }
220 };
221 
222 } // namespace internal
223 } // namespace grpc
224 
225 #endif // GRPCPP_SUPPORT_CALLBACK_COMMON_H
grpc::internal::CallbackWithSuccessTag
CallbackWithSuccessTag can be reused multiple times, and will be used in this fashion for streaming o...
Definition: callback_common.h:138
grpc::internal::CallbackWithSuccessTag::~CallbackWithSuccessTag
~CallbackWithSuccessTag()
Definition: callback_common.h:157
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::internal::CallbackWithStatusTag::status_ptr
Status * status_ptr()
Definition: callback_common.h:97
status.h
completion_queue_tag.h
grpc::internal::CallbackWithStatusTag
Definition: callback_common.h:72
grpc_call_ref
GRPCAPI void grpc_call_ref(grpc_call *call)
Ref a call.
grpc::internal::CallbackWithStatusTag::CallbackWithStatusTag
CallbackWithStatusTag(grpc_call *call, std::function< void(Status)> f, CompletionQueueTag *ops)
Definition: callback_common.h:86
grpc_types.h
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:34
grpc::internal::CallbackWithSuccessTag::operator=
CallbackWithSuccessTag & operator=(const CallbackWithSuccessTag &)=delete
grpc_completion_queue_functor::functor_run
void(* functor_run)(struct grpc_completion_queue_functor *, int)
The run member specifies a function that will be called when this tag is extracted from the completio...
Definition: grpc_types.h:449
log.h
grpc_call_unref
GRPCAPI void grpc_call_unref(grpc_call *call)
Unref a call.
grpc::internal::CallbackWithSuccessTag::force_run
void force_run(bool ok)
Definition: callback_common.h:190
grpc.h
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:69
grpc::internal::CallbackWithStatusTag::force_run
void force_run(Status s)
Definition: callback_common.h:102
grpc::internal::CallbackWithSuccessTag::Clear
void Clear()
Definition: callback_common.h:176
grpc::internal::CompletionQueueTag
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
grpc::internal::CallbackWithSuccessTag::Set
void Set(grpc_call *call, std::function< void(bool)> f, CompletionQueueTag *ops, bool can_inline)
Definition: callback_common.h:165
channel_interface.h
config.h
grpc::internal::CatchingCallback
void CatchingCallback(Func &&func, Args &&... args)
An exception-safe way of invoking a user-specified callback function.
Definition: callback_common.h:42
grpc::internal::CallbackWithSuccessTag::CallbackWithSuccessTag
CallbackWithSuccessTag()
Definition: callback_common.h:152
std
Definition: async_unary_call.h:407
call.h
grpc::internal::CompletionQueueTag::FinalizeResult
virtual bool FinalizeResult(void **tag, bool *status)=0
FinalizeResult must be called before informing user code that the operation bound to the underlying c...
grpc_completion_queue_functor
Specifies an interface class to be used as a tag for callback-based completion queues.
Definition: grpc_types.h:444
grpc::internal::CatchingReactorGetter
Reactor * CatchingReactorGetter(Func &&func, Args &&... args)
Definition: callback_common.h:55
grpc::protobuf::util::Status
::absl::Status Status
Definition: config_protobuf.h:106
grpc::internal::CallbackWithSuccessTag::ops
CompletionQueueTag * ops()
Definition: callback_common.h:185
grpc::internal::CallbackWithStatusTag::~CallbackWithStatusTag
~CallbackWithStatusTag()
Definition: callback_common.h:96
grpc_completion_queue_functor::inlineable
int inlineable
The inlineable member specifies whether this functor can be run inline.
Definition: grpc_types.h:453