GRPC C++  1.30.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_IMPL_CODEGEN_CALLBACK_COMMON_H
20 #define GRPCPP_IMPL_CODEGEN_CALLBACK_COMMON_H
21 
22 #include <functional>
23 
30 
31 namespace grpc {
32 namespace internal {
33 
35 // TODO(vjpai): decide whether it is better for this to take a const lvalue
36 // parameter or an rvalue parameter, or if it even matters
37 template <class Func, class... Args>
38 void CatchingCallback(Func&& func, Args&&... args) {
39 #if GRPC_ALLOW_EXCEPTIONS
40  try {
41  func(std::forward<Args>(args)...);
42  } catch (...) {
43  // nothing to return or change here, just don't crash the library
44  }
45 #else // GRPC_ALLOW_EXCEPTIONS
46  func(std::forward<Args>(args)...);
47 #endif // GRPC_ALLOW_EXCEPTIONS
48 }
49 
50 template <class Reactor, class Func, class... Args>
51 Reactor* CatchingReactorGetter(Func&& func, Args&&... args) {
52 #if GRPC_ALLOW_EXCEPTIONS
53  try {
54  return func(std::forward<Args>(args)...);
55  } catch (...) {
56  // fail the RPC, don't crash the library
57  return nullptr;
58  }
59 #else // GRPC_ALLOW_EXCEPTIONS
60  return func(std::forward<Args>(args)...);
61 #endif // GRPC_ALLOW_EXCEPTIONS
62 }
63 
64 // The contract on these tags is that they are single-shot. They must be
65 // constructed and then fired at exactly one point. There is no expectation
66 // that they can be reused without reconstruction.
67 
70  public:
71  // always allocated against a call arena, no memory free required
72  static void operator delete(void* /*ptr*/, std::size_t size) {
74  }
75 
76  // This operator should never be called as the memory should be freed as part
77  // of the arena destruction. It only exists to provide a matching operator
78  // delete to the operator new so that some compilers will not complain (see
79  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
80  // there are no tests catching the compiler warning.
81  static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); }
82 
83  CallbackWithStatusTag(grpc_call* call, std::function<void(Status)> f,
84  CompletionQueueTag* ops)
85  : call_(call), func_(std::move(f)), ops_(ops) {
87  functor_run = &CallbackWithStatusTag::StaticRun;
88  // A client-side callback should never be run inline since they will always
89  // have work to do from the user application. So, set the parent's
90  // inlineable field to false
91  inlineable = false;
92  }
94  Status* status_ptr() { return &status_; }
95 
96  // force_run can not be performed on a tag if operations using this tag
97  // have been sent to PerformOpsOnCall. It is intended for error conditions
98  // that are detected before the operations are internally processed.
99  void force_run(Status s) {
100  status_ = std::move(s);
101  Run(true);
102  }
103 
104  private:
105  grpc_call* call_;
106  std::function<void(Status)> func_;
107  CompletionQueueTag* ops_;
108  Status status_;
109 
110  static void StaticRun(grpc_experimental_completion_queue_functor* cb,
111  int ok) {
112  static_cast<CallbackWithStatusTag*>(cb)->Run(static_cast<bool>(ok));
113  }
114  void Run(bool ok) {
115  void* ignored = ops_;
116 
117  if (!ops_->FinalizeResult(&ignored, &ok)) {
118  // The tag was swallowed
119  return;
120  }
121  GPR_CODEGEN_ASSERT(ignored == ops_);
122 
123  // Last use of func_ or status_, so ok to move them out
124  auto func = std::move(func_);
125  auto status = std::move(status_);
126  func_ = nullptr; // reset to clear this out for sure
127  status_ = Status(); // reset to clear this out for sure
128  CatchingCallback(std::move(func), std::move(status));
130  }
131 };
132 
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 
151  CallbackWithSuccessTag() : call_(nullptr) {}
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;
171  functor_run = &CallbackWithSuccessTag::StaticRun;
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  operator bool() const { return call_ != nullptr; }
193 
194  private:
195  grpc_call* call_;
196  std::function<void(bool)> func_;
197  CompletionQueueTag* ops_;
198 
199  static void StaticRun(grpc_experimental_completion_queue_functor* cb,
200  int ok) {
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) {
214  CatchingCallback(func_, ok);
215  }
216  }
217 };
218 
219 } // namespace internal
220 } // namespace grpc
221 
222 #endif // GRPCPP_IMPL_CODEGEN_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:136
grpc::internal::CallbackWithSuccessTag::~CallbackWithSuccessTag
~CallbackWithSuccessTag()
Definition: callback_common.h:156
grpc_experimental_completion_queue_functor::inlineable
int inlineable
The inlineable member specifies whether this functor can be run inline.
Definition: grpc_types.h:748
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
status.h
grpc::CoreCodegenInterface::grpc_call_ref
virtual void grpc_call_ref(grpc_call *call)=0
grpc::internal::CallbackWithStatusTag::status_ptr
Status * status_ptr()
Definition: callback_common.h:94
config.h
core_codegen_interface.h
grpc::internal::CallbackWithStatusTag
Definition: callback_common.h:68
grpc::internal::CallbackWithStatusTag::CallbackWithStatusTag
CallbackWithStatusTag(grpc_call *call, std::function< void(Status)> f, CompletionQueueTag *ops)
Definition: callback_common.h:83
grpc_types.h
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc::internal::CallbackWithSuccessTag::operator=
CallbackWithSuccessTag & operator=(const CallbackWithSuccessTag &)=delete
grpc_experimental_completion_queue_functor
EXPERIMENTAL: Specifies an interface class to be used as a tag for callback-based completion queues.
Definition: grpc_types.h:739
grpc::internal::CallbackWithSuccessTag::force_run
void force_run(bool ok)
Definition: callback_common.h:189
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc::internal::CallbackWithStatusTag::force_run
void force_run(Status s)
Definition: callback_common.h:99
grpc::internal::CallbackWithSuccessTag::Clear
void Clear()
Definition: callback_common.h:175
grpc::protobuf::util::Status
::google::protobuf::util::Status Status
Definition: config_protobuf.h:90
grpc::internal::CompletionQueueTag
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
grpc::CoreCodegenInterface::grpc_call_unref
virtual void grpc_call_unref(grpc_call *call)=0
grpc::internal::CallbackWithSuccessTag::Set
void Set(grpc_call *call, std::function< void(bool)> f, CompletionQueueTag *ops, bool can_inline)
Definition: callback_common.h:164
channel_interface.h
grpc_experimental_completion_queue_functor::functor_run
void(* functor_run)(struct grpc_experimental_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:744
grpc::internal::CatchingCallback
void CatchingCallback(Func &&func, Args &&... args)
An exception-safe way of invoking a user-specified callback function.
Definition: callback_common.h:38
grpc::internal::CallbackWithSuccessTag::CallbackWithSuccessTag
CallbackWithSuccessTag()
Definition: callback_common.h:151
call.h
std
Definition: async_unary_call_impl.h:301
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::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:93
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
grpc::internal::CatchingReactorGetter
Reactor * CatchingReactorGetter(Func &&func, Args &&... args)
Definition: callback_common.h:51
grpc::internal::CallbackWithSuccessTag::ops
CompletionQueueTag * ops()
Definition: callback_common.h:184
grpc::internal::CallbackWithStatusTag::~CallbackWithStatusTag
~CallbackWithStatusTag()
Definition: callback_common.h:93
GPR_CODEGEN_DEBUG_ASSERT
#define GPR_CODEGEN_DEBUG_ASSERT(x)
Codegen specific version of GPR_DEBUG_ASSERT.
Definition: core_codegen_interface.h:155