GRPC C++  1.17.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 template <class Func, class... Args>
36 void CatchingCallback(Func&& func, Args&&... args) {
37 #if GRPC_ALLOW_EXCEPTIONS
38  try {
39  func(std::forward<Args>(args)...);
40  } catch (...) {
41  // nothing to return or change here, just don't crash the library
42  }
43 #else // GRPC_ALLOW_EXCEPTIONS
44  func(std::forward<Args>(args)...);
45 #endif // GRPC_ALLOW_EXCEPTIONS
46 }
47 
48 // The contract on these tags is that they are single-shot. They must be
49 // constructed and then fired at exactly one point. There is no expectation
50 // that they can be reused without reconstruction.
51 
54  public:
55  // always allocated against a call arena, no memory free required
56  static void operator delete(void* ptr, std::size_t size) {
57  assert(size == sizeof(CallbackWithStatusTag));
58  }
59 
60  // This operator should never be called as the memory should be freed as part
61  // of the arena destruction. It only exists to provide a matching operator
62  // delete to the operator new so that some compilers will not complain (see
63  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
64  // there are no tests catching the compiler warning.
65  static void operator delete(void*, void*) { assert(0); }
66 
67  CallbackWithStatusTag(grpc_call* call, std::function<void(Status)> f,
68  CompletionQueueTag* ops)
69  : call_(call), func_(std::move(f)), ops_(ops) {
71  functor_run = &CallbackWithStatusTag::StaticRun;
72  }
74  Status* status_ptr() { return &status_; }
75 
76  // force_run can not be performed on a tag if operations using this tag
77  // have been sent to PerformOpsOnCall. It is intended for error conditions
78  // that are detected before the operations are internally processed.
79  void force_run(Status s) {
80  status_ = std::move(s);
81  Run(true);
82  }
83 
84  private:
85  grpc_call* call_;
86  std::function<void(Status)> func_;
87  CompletionQueueTag* ops_;
88  Status status_;
89 
90  static void StaticRun(grpc_experimental_completion_queue_functor* cb,
91  int ok) {
92  static_cast<CallbackWithStatusTag*>(cb)->Run(static_cast<bool>(ok));
93  }
94  void Run(bool ok) {
95  void* ignored = ops_;
96 
97  if (!ops_->FinalizeResult(&ignored, &ok)) {
98  // The tag was swallowed
99  return;
100  }
101  GPR_CODEGEN_ASSERT(ignored == ops_);
102 
103  // Last use of func_ or status_, so ok to move them out
104  auto func = std::move(func_);
105  auto status = std::move(status_);
106  func_ = nullptr; // reset to clear this out for sure
107  status_ = Status(); // reset to clear this out for sure
108  CatchingCallback(std::move(func), std::move(status));
110  }
111 };
112 
118  public:
119  // always allocated against a call arena, no memory free required
120  static void operator delete(void* ptr, std::size_t size) {
121  assert(size == sizeof(CallbackWithSuccessTag));
122  }
123 
124  // This operator should never be called as the memory should be freed as part
125  // of the arena destruction. It only exists to provide a matching operator
126  // delete to the operator new so that some compilers will not complain (see
127  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
128  // there are no tests catching the compiler warning.
129  static void operator delete(void*, void*) { assert(0); }
130 
131  CallbackWithSuccessTag() : call_(nullptr) {}
132 
133  CallbackWithSuccessTag(grpc_call* call, std::function<void(bool)> f,
134  CompletionQueueTag* ops) {
135  Set(call, f, ops);
136  }
137 
139  CallbackWithSuccessTag& operator=(const CallbackWithSuccessTag&) = delete;
140 
141  ~CallbackWithSuccessTag() { Clear(); }
142 
143  // Set can only be called on a default-constructed or Clear'ed tag.
144  // It should never be called on a tag that was constructed with arguments
145  // or on a tag that has been Set before unless the tag has been cleared.
146  void Set(grpc_call* call, std::function<void(bool)> f,
147  CompletionQueueTag* ops) {
148  call_ = call;
149  func_ = std::move(f);
150  ops_ = ops;
152  functor_run = &CallbackWithSuccessTag::StaticRun;
153  }
154 
155  void Clear() {
156  if (call_ != nullptr) {
157  func_ = nullptr;
158  grpc_call* call = call_;
159  call_ = nullptr;
161  }
162  }
163 
164  CompletionQueueTag* ops() { return ops_; }
165 
166  // force_run can not be performed on a tag if operations using this tag
167  // have been sent to PerformOpsOnCall. It is intended for error conditions
168  // that are detected before the operations are internally processed.
169  void force_run(bool ok) { Run(ok); }
170 
172  operator bool() const { return call_ != nullptr; }
173 
174  private:
175  grpc_call* call_;
176  std::function<void(bool)> func_;
177  CompletionQueueTag* ops_;
178 
179  static void StaticRun(grpc_experimental_completion_queue_functor* cb,
180  int ok) {
181  static_cast<CallbackWithSuccessTag*>(cb)->Run(static_cast<bool>(ok));
182  }
183  void Run(bool ok) {
184  void* ignored = ops_;
185  bool new_ok = ok;
186  // Allow a "false" return value from FinalizeResult to silence the
187  // callback, just as it silences a CQ tag in the async cases
188  bool do_callback = ops_->FinalizeResult(&ignored, &new_ok);
189  GPR_CODEGEN_ASSERT(ignored == ops_);
190 
191  if (do_callback) {
192  CatchingCallback(func_, ok);
193  }
194  }
195 };
196 
197 } // namespace internal
198 } // namespace grpc
199 
200 #endif // GRPCPP_IMPL_CODEGEN_CALLBACK_COMMON_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:141
void force_run(bool ok)
Definition: callback_common.h:169
virtual void grpc_call_ref(grpc_call *call)=0
CallbackWithSuccessTag(grpc_call *call, std::function< void(bool)> f, CompletionQueueTag *ops)
Definition: callback_common.h:133
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
virtual bool FinalizeResult(void **tag, bool *status)=0
FinalizeResult must be called before informing user code that the operation bound to the underlying c...
virtual void grpc_call_unref(grpc_call *call)=0
CompletionQueueTag * ops()
Definition: callback_common.h:164
Definition: async_unary_call.h:304
::google::protobuf::util::Status Status
Definition: config_protobuf.h:93
void Set(grpc_call *call, std::function< void(bool)> f, CompletionQueueTag *ops)
Definition: callback_common.h:146
~CallbackWithSuccessTag()
Definition: callback_common.h:141
CallbackWithStatusTag(grpc_call *call, std::function< void(Status)> f, CompletionQueueTag *ops)
Definition: callback_common.h:67
Status * status_ptr()
Definition: callback_common.h:74
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:33
void force_run(Status s)
Definition: callback_common.h:79
~CallbackWithStatusTag()
Definition: callback_common.h:73
CoreCodegenInterface * g_core_codegen_interface
Definition: call_op_set.h:50
CallbackWithSuccessTag can be reused multiple times, and will be used in this fashion for streaming o...
Definition: callback_common.h:116
Did it work? If it didn&#39;t, why?
Definition: status.h:31
void Clear()
Definition: callback_common.h:155
void CatchingCallback(Func &&func, Args &&... args)
An exception-safe way of invoking a user-specified callback function.
Definition: callback_common.h:36
Definition: callback_common.h:52
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:678
EXPERIMENTAL: Specifies an interface class to be used as a tag for callback-based completion queues...
Definition: grpc_types.h:673
CallbackWithSuccessTag()
Definition: callback_common.h:131