GRPC C++  1.20.0
completion_queue.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016 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 
32 #ifndef GRPCPP_IMPL_CODEGEN_COMPLETION_QUEUE_H
33 #define GRPCPP_IMPL_CODEGEN_COMPLETION_QUEUE_H
34 
35 #include <grpc/impl/codegen/atm.h>
41 
43 
44 namespace grpc {
45 
46 template <class R>
47 class ClientReader;
48 template <class W>
49 class ClientWriter;
50 template <class W, class R>
51 class ClientReaderWriter;
52 template <class R>
54 template <class W>
56 namespace internal {
57 template <class W, class R>
59 } // namespace internal
60 
61 class Channel;
62 class ChannelInterface;
63 class ClientContext;
64 class CompletionQueue;
65 class Server;
66 class ServerBuilder;
67 class ServerContext;
68 class ServerInterface;
69 
70 namespace internal {
71 class CompletionQueueTag;
72 class RpcMethod;
73 template <class ServiceType, class RequestType, class ResponseType>
74 class RpcMethodHandler;
75 template <class ServiceType, class RequestType, class ResponseType>
77 template <class ServiceType, class RequestType, class ResponseType>
79 template <class ServiceType, class RequestType, class ResponseType>
81 template <class Streamer, bool WriteNeeded>
83 template <StatusCode code>
84 class ErrorMethodHandler;
85 template <class InputMessage, class OutputMessage>
87 template <class Op1, class Op2, class Op3, class Op4, class Op5, class Op6>
88 class CallOpSet;
89 } // namespace internal
90 
92 
98  public:
104  nullptr}) {}
105 
109  explicit CompletionQueue(grpc_completion_queue* take);
110 
113  g_core_codegen_interface->grpc_completion_queue_destroy(cq_);
114  }
115 
117  enum NextStatus {
119  GOT_EVENT,
120  TIMEOUT
122  };
123 
172  bool Next(void** tag, bool* ok) {
173  return (AsyncNextInternal(tag, ok,
174  g_core_codegen_interface->gpr_inf_future(
175  GPR_CLOCK_REALTIME)) != SHUTDOWN);
176  }
177 
189  template <typename T>
190  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
191  TimePoint<T> deadline_tp(deadline);
192  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
193  }
194 
209  template <typename T, typename F>
210  NextStatus DoThenAsyncNext(F&& f, void** tag, bool* ok, const T& deadline) {
211  CompletionQueueTLSCache cache = CompletionQueueTLSCache(this);
212  f();
213  if (cache.Flush(tag, ok)) {
214  return GOT_EVENT;
215  } else {
216  return AsyncNext(tag, ok, deadline);
217  }
218  }
219 
230  void Shutdown();
231 
237  grpc_completion_queue* cq() { return cq_; }
238 
239  protected:
242  cq_ = g_core_codegen_interface->grpc_completion_queue_create(
243  g_core_codegen_interface->grpc_completion_queue_factory_lookup(
244  &attributes),
245  &attributes, NULL);
246  InitialAvalanching(); // reserve this for the future shutdown
247  }
248 
249  private:
250  // Friend synchronous wrappers so that they can access Pluck(), which is
251  // a semi-private API geared towards the synchronous implementation.
252  template <class R>
253  friend class ::grpc::ClientReader;
254  template <class W>
255  friend class ::grpc::ClientWriter;
256  template <class W, class R>
257  friend class ::grpc::ClientReaderWriter;
258  template <class R>
259  friend class ::grpc::ServerReader;
260  template <class W>
261  friend class ::grpc::ServerWriter;
262  template <class W, class R>
263  friend class ::grpc::internal::ServerReaderWriterBody;
264  template <class ServiceType, class RequestType, class ResponseType>
265  friend class ::grpc::internal::RpcMethodHandler;
266  template <class ServiceType, class RequestType, class ResponseType>
267  friend class ::grpc::internal::ClientStreamingHandler;
268  template <class ServiceType, class RequestType, class ResponseType>
269  friend class ::grpc::internal::ServerStreamingHandler;
270  template <class Streamer, bool WriteNeeded>
271  friend class ::grpc::internal::TemplatedBidiStreamingHandler;
272  template <StatusCode code>
273  friend class ::grpc::internal::ErrorMethodHandler;
274  friend class ::grpc::Server;
275  friend class ::grpc::ServerContext;
276  friend class ::grpc::ServerInterface;
277  template <class InputMessage, class OutputMessage>
278  friend class ::grpc::internal::BlockingUnaryCallImpl;
279 
280  // Friends that need access to constructor for callback CQ
281  friend class ::grpc::Channel;
282 
283  // For access to Register/CompleteAvalanching
284  template <class Op1, class Op2, class Op3, class Op4, class Op5, class Op6>
285  friend class ::grpc::internal::CallOpSet;
286 
291  class CompletionQueueTLSCache {
292  public:
293  CompletionQueueTLSCache(CompletionQueue* cq);
294  ~CompletionQueueTLSCache();
295  bool Flush(void** tag, bool* ok);
296 
297  private:
298  CompletionQueue* cq_;
299  bool flushed_;
300  };
301 
302  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
303 
306  bool Pluck(internal::CompletionQueueTag* tag) {
307  auto deadline =
308  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME);
309  while (true) {
310  auto ev = g_core_codegen_interface->grpc_completion_queue_pluck(
311  cq_, tag, deadline, nullptr);
312  bool ok = ev.success != 0;
313  void* ignored = tag;
314  if (tag->FinalizeResult(&ignored, &ok)) {
315  GPR_CODEGEN_ASSERT(ignored == tag);
316  return ok;
317  }
318  }
319  }
320 
329  void TryPluck(internal::CompletionQueueTag* tag) {
330  auto deadline = g_core_codegen_interface->gpr_time_0(GPR_CLOCK_REALTIME);
331  auto ev = g_core_codegen_interface->grpc_completion_queue_pluck(
332  cq_, tag, deadline, nullptr);
333  if (ev.type == GRPC_QUEUE_TIMEOUT) return;
334  bool ok = ev.success != 0;
335  void* ignored = tag;
336  // the tag must be swallowed if using TryPluck
337  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
338  }
339 
345  void TryPluck(internal::CompletionQueueTag* tag, gpr_timespec deadline) {
346  auto ev = g_core_codegen_interface->grpc_completion_queue_pluck(
347  cq_, tag, deadline, nullptr);
348  if (ev.type == GRPC_QUEUE_TIMEOUT || ev.type == GRPC_QUEUE_SHUTDOWN) {
349  return;
350  }
351 
352  bool ok = ev.success != 0;
353  void* ignored = tag;
354  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
355  }
356 
363  void InitialAvalanching() {
364  gpr_atm_rel_store(&avalanches_in_flight_, static_cast<gpr_atm>(1));
365  }
366  void RegisterAvalanching() {
367  gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_,
368  static_cast<gpr_atm>(1));
369  }
370  void CompleteAvalanching() {
371  if (gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_,
372  static_cast<gpr_atm>(-1)) == 1) {
373  g_core_codegen_interface->grpc_completion_queue_shutdown(cq_);
374  }
375  }
376 
377  grpc_completion_queue* cq_; // owned
378 
379  gpr_atm avalanches_in_flight_;
380 };
381 
385  public:
386  bool IsFrequentlyPolled() { return polling_type_ != GRPC_CQ_NON_LISTENING; }
387 
388  protected:
391 
392  private:
400  grpc_cq_polling_type polling_type,
403  GRPC_CQ_CURRENT_VERSION, completion_type, polling_type,
404  shutdown_cb}),
405  polling_type_(polling_type) {}
406 
407  grpc_cq_polling_type polling_type_;
408  friend class ServerBuilder;
409  friend class Server;
410 };
411 
412 } // namespace grpc
413 
414 #endif // GRPCPP_IMPL_CODEGEN_COMPLETION_QUEUE_H
A wrapper class of an application provided server streaming handler.
Definition: byte_buffer.h:47
virtual const grpc_completion_queue_factory * grpc_completion_queue_factory_lookup(const grpc_completion_queue_attributes *attributes)=0
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue.h:82
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:144
virtual void grpc_completion_queue_shutdown(grpc_completion_queue *cq)=0
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
Definition: completion_queue.h:58
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...
Primary implementation of CallOpSetInterface.
Definition: call_op_set.h:828
No event before timeout.
Definition: grpc_types.h:479
int success
If the grpc_completion_type is GRPC_OP_COMPLETE, this field indicates whether the operation was succe...
Definition: grpc_types.h:495
#define GRPC_CQ_CURRENT_VERSION
Definition: grpc_types.h:708
The completion queue has been shutdown and fully-drained.
Definition: completion_queue.h:118
Events are popped out by calling grpc_completion_queue_next() API ONLY.
Definition: grpc_types.h:680
gpr_timespec raw_time()
Definition: time.h:43
bool IsFrequentlyPolled()
Definition: completion_queue.h:386
virtual void grpc_completion_queue_destroy(grpc_completion_queue *cq)=0
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
If you are trying to use CompletionQueue::AsyncNext with a time class that isn&#39;t either gpr_timespec ...
Definition: time.h:40
Definition: grpc_types.h:710
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:237
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:174
Synchronous (blocking) server-side API for doing client-streaming RPCs, where the incoming message st...
Definition: completion_queue.h:53
ServerCompletionQueue()
Default constructor.
Definition: completion_queue.h:390
bool Next(void **tag, bool *ok)
Read from the queue, blocking until an event is available or the queue is shutting down...
Definition: completion_queue.h:172
Descriptor of an RPC method.
Definition: rpc_method.h:29
NextStatus DoThenAsyncNext(F &&f, void **tag, bool *ok, const T &deadline)
EXPERIMENTAL First executes F, then reads from the queue, blocking up to deadline (or the queue&#39;s shu...
Definition: completion_queue.h:210
grpc_cq_completion_type
Specifies the type of APIs to use to pop events from the completion queue.
Definition: grpc_types.h:678
grpc_cq_polling_type
Completion queues internally MAY maintain a set of file descriptors in a structure called &#39;pollset&#39;...
Definition: grpc_types.h:660
CompletionQueue(const grpc_completion_queue_attributes &attributes)
Private constructor of CompletionQueue only visible to friend classes.
Definition: completion_queue.h:241
Shutting down.
Definition: grpc_types.h:477
#define gpr_atm_rel_store(p, value)
Definition: atm_gcc_atomic.h:52
NextStatus AsyncNext(void **tag, bool *ok, const T &deadline)
Read from the queue, blocking up to deadline (or the queue&#39;s shutdown).
Definition: completion_queue.h:190
~CompletionQueue()
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue.h:112
Represents a gRPC server.
Definition: server.h:54
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
virtual grpc_completion_queue * grpc_completion_queue_create(const grpc_completion_queue_factory *factory, const grpc_completion_queue_attributes *attributes, void *reserved)=0
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:76
#define gpr_atm_no_barrier_fetch_add(p, delta)
Definition: atm_gcc_atomic.h:57
Codegen interface for grpc::Channel.
Definition: channel_interface.h:65
A wrapper class of an application provided rpc method handler.
Definition: byte_buffer.h:45
CoreCodegenInterface * g_core_codegen_interface
Definition: call_op_set.h:51
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue.h:117
The completion queue will have an associated pollset and there is no restriction on the type of file ...
Definition: grpc_types.h:663
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:30
Similar to GRPC_CQ_DEFAULT_POLLING except that the completion queues will not contain any &#39;listening ...
Definition: grpc_types.h:668
CompletionQueue()
Default constructor.
Definition: completion_queue.h:101
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:110
Definition: server_interface.h:54
Realtime clock.
Definition: gpr_types.h:36
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs, where the outgoing message stream coming from the server has messages of type W.
Definition: completion_queue.h:55
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:97
Definition: channel_interface.h:44
Interface between the codegen library and the minimal subset of core features required by the generat...
Definition: core_codegen_interface.h:37
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: byte_buffer.h:53
Analogous to struct timespec.
Definition: gpr_types.h:47
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:384
Definition: completion_queue.h:80
struct grpc_completion_queue grpc_completion_queue
Completion Queues enable notification of the completion of asynchronous actions.
Definition: grpc_types.h:56
virtual gpr_timespec gpr_time_0(gpr_clock_type type)=0
EXPERIMENTAL: Specifies an interface class to be used as a tag for callback-based completion queues...
Definition: grpc_types.h:694
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:57
virtual grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag, gpr_timespec deadline, void *reserved)=0
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:44