GRPC C++  1.30.0
completion_queue_impl.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_IMPL_H
33 #define GRPCPP_IMPL_CODEGEN_COMPLETION_QUEUE_IMPL_H
34 
35 #include <list>
36 
37 #include <grpc/impl/codegen/atm.h>
44 
46 
47 namespace grpc_impl {
48 
49 class Channel;
50 class Server;
51 class ServerBuilder;
52 template <class R>
53 class ClientReader;
54 template <class W>
55 class ClientWriter;
56 template <class W, class R>
57 class ClientReaderWriter;
58 template <class R>
60 template <class W>
62 class ServerContextBase;
63 namespace internal {
64 template <class W, class R>
66 
67 template <class ServiceType, class RequestType, class ResponseType>
68 class RpcMethodHandler;
69 template <class ServiceType, class RequestType, class ResponseType>
71 template <class ServiceType, class RequestType, class ResponseType>
73 template <class Streamer, bool WriteNeeded>
75 template <::grpc::StatusCode code>
76 class ErrorMethodHandler;
77 } // namespace internal
78 } // namespace grpc_impl
79 namespace grpc {
80 
81 class ChannelInterface;
82 class ServerInterface;
83 
84 namespace internal {
85 class CompletionQueueTag;
86 class RpcMethod;
87 template <class InputMessage, class OutputMessage>
88 class BlockingUnaryCallImpl;
89 template <class Op1, class Op2, class Op3, class Op4, class Op5, class Op6>
90 class CallOpSet;
91 } // namespace internal
92 
94 
95 } // namespace grpc
96 
97 namespace grpc_impl {
98 
104  public:
110  nullptr}) {}
111 
115  explicit CompletionQueue(grpc_completion_queue* take);
116 
120  }
121 
123  enum NextStatus {
126  TIMEOUT
128  };
129 
178  bool Next(void** tag, bool* ok) {
179  return (AsyncNextInternal(tag, ok,
182  }
183 
195  template <typename T>
196  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
197  ::grpc::TimePoint<T> deadline_tp(deadline);
198  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
199  }
200 
215  template <typename T, typename F>
216  NextStatus DoThenAsyncNext(F&& f, void** tag, bool* ok, const T& deadline) {
217  CompletionQueueTLSCache cache = CompletionQueueTLSCache(this);
218  f();
219  if (cache.Flush(tag, ok)) {
220  return GOT_EVENT;
221  } else {
222  return AsyncNext(tag, ok, deadline);
223  }
224  }
225 
236  void Shutdown();
237 
243  grpc_completion_queue* cq() { return cq_; }
244 
245  protected:
250  &attributes),
251  &attributes, NULL);
252  InitialAvalanching(); // reserve this for the future shutdown
253  }
254 
255  private:
256  // Friends for access to server registration lists that enable checking and
257  // logging on shutdown
260 
261  // Friend synchronous wrappers so that they can access Pluck(), which is
262  // a semi-private API geared towards the synchronous implementation.
263  template <class R>
265  template <class W>
267  template <class W, class R>
269  template <class R>
271  template <class W>
273  template <class W, class R>
274  friend class ::grpc_impl::internal::ServerReaderWriterBody;
275  template <class ServiceType, class RequestType, class ResponseType>
277  template <class ServiceType, class RequestType, class ResponseType>
279  template <class ServiceType, class RequestType, class ResponseType>
281  template <class Streamer, bool WriteNeeded>
283  template <::grpc::StatusCode code>
286  friend class ::grpc::ServerInterface;
287  template <class InputMessage, class OutputMessage>
288  friend class ::grpc::internal::BlockingUnaryCallImpl;
289 
290  // Friends that need access to constructor for callback CQ
292 
293  // For access to Register/CompleteAvalanching
294  template <class Op1, class Op2, class Op3, class Op4, class Op5, class Op6>
295  friend class ::grpc::internal::CallOpSet;
296 
301  class CompletionQueueTLSCache {
302  public:
303  CompletionQueueTLSCache(CompletionQueue* cq);
304  ~CompletionQueueTLSCache();
305  bool Flush(void** tag, bool* ok);
306 
307  private:
308  CompletionQueue* cq_;
309  bool flushed_;
310  };
311 
312  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
313 
316  bool Pluck(::grpc::internal::CompletionQueueTag* tag) {
317  auto deadline =
319  while (true) {
321  cq_, tag, deadline, nullptr);
322  bool ok = ev.success != 0;
323  void* ignored = tag;
324  if (tag->FinalizeResult(&ignored, &ok)) {
325  GPR_CODEGEN_ASSERT(ignored == tag);
326  return ok;
327  }
328  }
329  }
330 
339  void TryPluck(::grpc::internal::CompletionQueueTag* tag) {
340  auto deadline =
343  cq_, tag, deadline, nullptr);
344  if (ev.type == GRPC_QUEUE_TIMEOUT) return;
345  bool ok = ev.success != 0;
346  void* ignored = tag;
347  // the tag must be swallowed if using TryPluck
348  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
349  }
350 
356  void TryPluck(::grpc::internal::CompletionQueueTag* tag,
357  gpr_timespec deadline) {
359  cq_, tag, deadline, nullptr);
360  if (ev.type == GRPC_QUEUE_TIMEOUT || ev.type == GRPC_QUEUE_SHUTDOWN) {
361  return;
362  }
363 
364  bool ok = ev.success != 0;
365  void* ignored = tag;
366  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
367  }
368 
375  void InitialAvalanching() {
376  gpr_atm_rel_store(&avalanches_in_flight_, static_cast<gpr_atm>(1));
377  }
378  void RegisterAvalanching() {
379  gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_,
380  static_cast<gpr_atm>(1));
381  }
382  void CompleteAvalanching() {
383  if (gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_,
384  static_cast<gpr_atm>(-1)) == 1) {
386  }
387  }
388 
389  void RegisterServer(const Server* server) {
390  (void)server;
391 #ifndef NDEBUG
392  grpc::internal::MutexLock l(&server_list_mutex_);
393  server_list_.push_back(server);
394 #endif
395  }
396  void UnregisterServer(const Server* server) {
397  (void)server;
398 #ifndef NDEBUG
399  grpc::internal::MutexLock l(&server_list_mutex_);
400  server_list_.remove(server);
401 #endif
402  }
403  bool ServerListEmpty() const {
404 #ifndef NDEBUG
405  grpc::internal::MutexLock l(&server_list_mutex_);
406  return server_list_.empty();
407 #endif
408  return true;
409  }
410 
411  grpc_completion_queue* cq_; // owned
412 
413  gpr_atm avalanches_in_flight_;
414 
415  // List of servers associated with this CQ. Even though this is only used with
416  // NDEBUG, instantiate it in all cases since otherwise the size will be
417  // inconsistent.
418  mutable grpc::internal::Mutex server_list_mutex_;
419  std::list<const Server*> server_list_ /* GUARDED_BY(server_list_mutex_) */;
420 };
421 
425  public:
426  bool IsFrequentlyPolled() { return polling_type_ != GRPC_CQ_NON_LISTENING; }
427 
428  protected:
431 
432  private:
440  grpc_cq_polling_type polling_type,
443  GRPC_CQ_CURRENT_VERSION, completion_type, polling_type,
444  shutdown_cb}),
445  polling_type_(polling_type) {}
446 
447  grpc_cq_polling_type polling_type_;
450 };
451 
452 } // namespace grpc_impl
453 
454 #endif // GRPCPP_IMPL_CODEGEN_COMPLETION_QUEUE_IMPL_H
grpc::GrpcLibraryCodegen
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
grpc_impl::internal::ErrorMethodHandler
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: byte_buffer.h:44
grpc::internal::RpcMethodHandler
::grpc_impl::internal::RpcMethodHandler< ServiceType, RequestType, ResponseType > RpcMethodHandler
Definition: method_handler.h:36
atm.h
grpc_impl::CompletionQueue::Shutdown
void Shutdown()
Request the shutdown of the queue.
grpc::CoreCodegenInterface::gpr_time_0
virtual gpr_timespec gpr_time_0(gpr_clock_type type)=0
grpc::CoreCodegenInterface::grpc_completion_queue_create
virtual grpc_completion_queue * grpc_completion_queue_create(const grpc_completion_queue_factory *factory, const grpc_completion_queue_attributes *attributes, void *reserved)=0
GRPC_CQ_NEXT
Events are popped out by calling grpc_completion_queue_next() API ONLY.
Definition: grpc_types.h:725
time.h
grpc_impl::internal::ClientStreamingHandler
A wrapper class of an application provided client streaming handler.
Definition: completion_queue_impl.h:70
grpc::internal::TemplatedBidiStreamingHandler
::grpc_impl::internal::TemplatedBidiStreamingHandler< Streamer, WriteNeeded > TemplatedBidiStreamingHandler
Definition: method_handler.h:50
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
status.h
grpc_cq_polling_type
grpc_cq_polling_type
Completion queues internally MAY maintain a set of file descriptors in a structure called 'pollset'.
Definition: grpc_types.h:705
grpc_cq_completion_type
grpc_cq_completion_type
Specifies the type of APIs to use to pop events from the completion queue.
Definition: grpc_types.h:723
grpc::ClientWriter
::grpc_impl::ClientWriter< W > ClientWriter
Definition: sync_stream.h:62
GRPC_QUEUE_SHUTDOWN
Shutting down.
Definition: grpc_types.h:521
core_codegen_interface.h
grpc_impl::ServerContextBase
Base class of ServerContext. Experimental until callback API is final.
Definition: server_context_impl.h:123
grpc::Server
::grpc_impl::Server Server
Definition: server.h:26
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
The zero time interval.
grpc_impl::CompletionQueue::~CompletionQueue
~CompletionQueue()
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue_impl.h:118
grpc_impl::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue_impl.h:424
grpc::ServerWriter
::grpc_impl::ServerWriter< W > ServerWriter
Definition: sync_stream.h:81
grpc::CoreCodegenInterface::gpr_inf_future
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
grpc::internal::ServerStreamingHandler
::grpc_impl::internal::ServerStreamingHandler< ServiceType, RequestType, ResponseType > ServerStreamingHandler
Definition: method_handler.h:46
GRPC_CQ_DEFAULT_POLLING
The completion queue will have an associated pollset and there is no restriction on the type of file ...
Definition: grpc_types.h:708
GRPC_CQ_NON_LISTENING
Similar to GRPC_CQ_DEFAULT_POLLING except that the completion queues will not contain any 'listening ...
Definition: grpc_types.h:713
grpc_impl::CompletionQueue::CompletionQueue
CompletionQueue()
Default constructor.
Definition: completion_queue_impl.h:107
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_completion_queue_factory_lookup
const GRPCAPI grpc_completion_queue_factory * grpc_completion_queue_factory_lookup(const grpc_completion_queue_attributes *attributes)
Returns the completion queue factory based on the attributes.
grpc_impl::ServerCompletionQueue::IsFrequentlyPolled
bool IsFrequentlyPolled()
Definition: completion_queue_impl.h:426
grpc::TimePoint::raw_time
gpr_timespec raw_time()=delete
completion_queue_tag.h
grpc_impl::ServerWriter
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs,...
Definition: completion_queue_impl.h:61
grpc::ServerReader
::grpc_impl::ServerReader< R > ServerReader
Definition: sync_stream.h:75
grpc_impl::internal::RpcMethodHandler
A wrapper class of an application provided rpc method handler.
Definition: byte_buffer.h:40
gpr_atm_rel_store
#define gpr_atm_rel_store(p, value)
Definition: atm_gcc_atomic.h:52
gpr_atm_no_barrier_fetch_add
#define gpr_atm_no_barrier_fetch_add(p, delta)
Definition: atm_gcc_atomic.h:57
sync.h
grpc_completion_queue_attributes
Definition: grpc_types.h:759
grpc_impl::CompletionQueue::Next
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_impl.h:178
grpc::experimental::ServerContextBase
::grpc_impl::ServerContextBase ServerContextBase
Definition: server_context.h:36
grpc::internal::CompletionQueueTag
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
grpc::CoreCodegenInterface
Interface between the codegen library and the minimal subset of core features required by the generat...
Definition: core_codegen_interface.h:38
grpc_library.h
gpr_atm
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:30
grpc_impl::CompletionQueue::AsyncNext
NextStatus AsyncNext(void **tag, bool *ok, const T &deadline)
Read from the queue, blocking up to deadline (or the queue's shutdown).
Definition: completion_queue_impl.h:196
grpc::internal::MutexLock
Definition: sync.h:69
grpc_impl::internal::TemplatedBidiStreamingHandler
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue_impl.h:74
grpc_impl::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue_impl.h:103
grpc_impl::internal::ServerReaderWriterBody
Definition: completion_queue_impl.h:65
GRPC_CQ_CURRENT_VERSION
#define GRPC_CQ_CURRENT_VERSION
Definition: grpc_types.h:757
grpc_completion_queue
struct grpc_completion_queue grpc_completion_queue
Completion Queues enable notification of the completion of asynchronous actions.
Definition: grpc_types.h:56
grpc::ClientReader
::grpc_impl::ClientReader< R > ClientReader
Definition: sync_stream.h:56
grpc_impl::CompletionQueue::TIMEOUT
deadline was reached.
Definition: completion_queue_impl.h:127
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::CoreCodegenInterface::grpc_completion_queue_destroy
virtual void grpc_completion_queue_destroy(grpc_completion_queue *cq)=0
grpc_impl::ServerReader
Synchronous (blocking) server-side API for doing client-streaming RPCs, where the incoming message st...
Definition: completion_queue_impl.h:59
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc::ClientReaderWriter
::grpc_impl::ClientReaderWriter< W, R > ClientReaderWriter
Definition: sync_stream.h:69
grpc_impl::CompletionQueue::GOT_EVENT
Got a new event; tag will be filled in with its associated value; ok indicating its success.
Definition: completion_queue_impl.h:125
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_impl::CompletionQueue::cq
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue_impl.h:243
grpc::internal::Mutex
Definition: sync.h:47
grpc::CoreCodegenInterface::grpc_completion_queue_pluck
virtual grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag, gpr_timespec deadline, void *reserved)=0
gpr_timespec
Analogous to struct timespec.
Definition: gpr_types.h:47
grpc_impl::CompletionQueue::NextStatus
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue_impl.h:123
grpc::internal::ErrorMethodHandler
::grpc_impl::internal::ErrorMethodHandler< code > ErrorMethodHandler
Definition: method_handler.h:62
grpc::CoreCodegenInterface::grpc_completion_queue_shutdown
virtual void grpc_completion_queue_shutdown(grpc_completion_queue *cq)=0
GPR_CLOCK_REALTIME
Realtime clock.
Definition: gpr_types.h:36
grpc_event::success
int success
If the grpc_completion_type is GRPC_OP_COMPLETE, this field indicates whether the operation was succe...
Definition: grpc_types.h:539
GRPC_QUEUE_TIMEOUT
No event before timeout.
Definition: grpc_types.h:523
grpc_impl::ServerCompletionQueue::ServerCompletionQueue
ServerCompletionQueue()
Default constructor.
Definition: completion_queue_impl.h:430
grpc::internal::ClientStreamingHandler
::grpc_impl::internal::ClientStreamingHandler< ServiceType, RequestType, ResponseType > ClientStreamingHandler
Definition: method_handler.h:41
grpc_impl::internal::ServerStreamingHandler
A wrapper class of an application provided server streaming handler.
Definition: byte_buffer.h:42
grpc::TimePoint
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:40
grpc::ServerBuilder
::grpc_impl::ServerBuilder ServerBuilder
Definition: server_builder.h:26
grpc::Channel
::grpc_impl::Channel Channel
Definition: channel.h:26
grpc_impl::CompletionQueue::CompletionQueue
CompletionQueue(const grpc_completion_queue_attributes &attributes)
Private constructor of CompletionQueue only visible to friend classes.
Definition: completion_queue_impl.h:247
grpc_impl::CompletionQueue::SHUTDOWN
The completion queue has been shutdown and fully-drained.
Definition: completion_queue_impl.h:124
grpc_impl::CompletionQueue::DoThenAsyncNext
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's shu...
Definition: completion_queue_impl.h:216