GRPC C++  1.32.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 <list>
36 
37 #include <grpc/impl/codegen/atm.h>
44 
46 
47 namespace grpc_impl {
48 
49 class Server;
50 template <class R>
51 class ClientReader;
52 template <class W>
53 class ClientWriter;
54 template <class W, class R>
55 class ClientReaderWriter;
56 template <class R>
58 template <class W>
60 class ServerContextBase;
61 namespace internal {
62 template <class W, class R>
64 
65 template <class ServiceType, class RequestType, class ResponseType>
66 class RpcMethodHandler;
67 template <class ServiceType, class RequestType, class ResponseType>
69 template <class ServiceType, class RequestType, class ResponseType>
71 template <class Streamer, bool WriteNeeded>
73 template <::grpc::StatusCode code>
74 class ErrorMethodHandler;
75 } // namespace internal
76 } // namespace grpc_impl
77 namespace grpc {
78 
79 class Channel;
80 class ChannelInterface;
81 class ServerBuilder;
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 
100  public:
106  nullptr}) {}
107 
111  explicit CompletionQueue(grpc_completion_queue* take);
112 
116  }
117 
119  enum NextStatus {
122  TIMEOUT
124  };
125 
174  bool Next(void** tag, bool* ok) {
175  return (AsyncNextInternal(tag, ok,
178  }
179 
191  template <typename T>
192  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
193  ::grpc::TimePoint<T> deadline_tp(deadline);
194  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
195  }
196 
211  template <typename T, typename F>
212  NextStatus DoThenAsyncNext(F&& f, void** tag, bool* ok, const T& deadline) {
213  CompletionQueueTLSCache cache = CompletionQueueTLSCache(this);
214  f();
215  if (cache.Flush(tag, ok)) {
216  return GOT_EVENT;
217  } else {
218  return AsyncNext(tag, ok, deadline);
219  }
220  }
221 
232  void Shutdown();
233 
239  grpc_completion_queue* cq() { return cq_; }
240 
241  protected:
246  &attributes),
247  &attributes, NULL);
248  InitialAvalanching(); // reserve this for the future shutdown
249  }
250 
251  private:
252  // Friends for access to server registration lists that enable checking and
253  // logging on shutdown
254  friend class ::grpc::ServerBuilder;
256 
257  // Friend synchronous wrappers so that they can access Pluck(), which is
258  // a semi-private API geared towards the synchronous implementation.
259  template <class R>
261  template <class W>
263  template <class W, class R>
265  template <class R>
267  template <class W>
269  template <class W, class R>
270  friend class ::grpc_impl::internal::ServerReaderWriterBody;
271  template <class ServiceType, class RequestType, class ResponseType>
273  template <class ServiceType, class RequestType, class ResponseType>
275  template <class ServiceType, class RequestType, class ResponseType>
277  template <class Streamer, bool WriteNeeded>
279  template <::grpc::StatusCode code>
282  friend class ::grpc::ServerInterface;
283  template <class InputMessage, class OutputMessage>
284  friend class ::grpc::internal::BlockingUnaryCallImpl;
285 
286  // Friends that need access to constructor for callback CQ
287  friend class ::grpc::Channel;
288 
289  // For access to Register/CompleteAvalanching
290  template <class Op1, class Op2, class Op3, class Op4, class Op5, class Op6>
291  friend class ::grpc::internal::CallOpSet;
292 
297  class CompletionQueueTLSCache {
298  public:
299  CompletionQueueTLSCache(CompletionQueue* cq);
300  ~CompletionQueueTLSCache();
301  bool Flush(void** tag, bool* ok);
302 
303  private:
304  CompletionQueue* cq_;
305  bool flushed_;
306  };
307 
308  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
309 
312  bool Pluck(::grpc::internal::CompletionQueueTag* tag) {
313  auto deadline =
315  while (true) {
317  cq_, tag, deadline, nullptr);
318  bool ok = ev.success != 0;
319  void* ignored = tag;
320  if (tag->FinalizeResult(&ignored, &ok)) {
321  GPR_CODEGEN_ASSERT(ignored == tag);
322  return ok;
323  }
324  }
325  }
326 
335  void TryPluck(::grpc::internal::CompletionQueueTag* tag) {
336  auto deadline =
339  cq_, tag, deadline, nullptr);
340  if (ev.type == GRPC_QUEUE_TIMEOUT) return;
341  bool ok = ev.success != 0;
342  void* ignored = tag;
343  // the tag must be swallowed if using TryPluck
344  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
345  }
346 
352  void TryPluck(::grpc::internal::CompletionQueueTag* tag,
353  gpr_timespec deadline) {
355  cq_, tag, deadline, nullptr);
356  if (ev.type == GRPC_QUEUE_TIMEOUT || ev.type == GRPC_QUEUE_SHUTDOWN) {
357  return;
358  }
359 
360  bool ok = ev.success != 0;
361  void* ignored = tag;
362  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
363  }
364 
371  void InitialAvalanching() {
372  gpr_atm_rel_store(&avalanches_in_flight_, static_cast<gpr_atm>(1));
373  }
374  void RegisterAvalanching() {
375  gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_,
376  static_cast<gpr_atm>(1));
377  }
378  void CompleteAvalanching() {
379  if (gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_,
380  static_cast<gpr_atm>(-1)) == 1) {
382  }
383  }
384 
385  void RegisterServer(const ::grpc_impl::Server* server) {
386  (void)server;
387 #ifndef NDEBUG
388  grpc::internal::MutexLock l(&server_list_mutex_);
389  server_list_.push_back(server);
390 #endif
391  }
392  void UnregisterServer(const ::grpc_impl::Server* server) {
393  (void)server;
394 #ifndef NDEBUG
395  grpc::internal::MutexLock l(&server_list_mutex_);
396  server_list_.remove(server);
397 #endif
398  }
399  bool ServerListEmpty() const {
400 #ifndef NDEBUG
401  grpc::internal::MutexLock l(&server_list_mutex_);
402  return server_list_.empty();
403 #endif
404  return true;
405  }
406 
407  grpc_completion_queue* cq_; // owned
408 
409  gpr_atm avalanches_in_flight_;
410 
411  // List of servers associated with this CQ. Even though this is only used with
412  // NDEBUG, instantiate it in all cases since otherwise the size will be
413  // inconsistent.
414  mutable grpc::internal::Mutex server_list_mutex_;
415  std::list<const ::grpc_impl::Server*>
416  server_list_ /* GUARDED_BY(server_list_mutex_) */;
417 };
418 
422  public:
423  bool IsFrequentlyPolled() { return polling_type_ != GRPC_CQ_NON_LISTENING; }
424 
425  protected:
428 
429  private:
437  grpc_cq_polling_type polling_type,
440  GRPC_CQ_CURRENT_VERSION, completion_type, polling_type,
441  shutdown_cb}),
442  polling_type_(polling_type) {}
443 
444  grpc_cq_polling_type polling_type_;
445  friend class ::grpc::ServerBuilder;
447 };
448 
449 } // namespace grpc
450 
451 #endif // GRPCPP_IMPL_CODEGEN_COMPLETION_QUEUE_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::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
@ GRPC_CQ_NEXT
Events are popped out by calling grpc_completion_queue_next() API ONLY.
Definition: grpc_types.h:727
time.h
grpc_impl::internal::ClientStreamingHandler
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:68
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:707
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:725
grpc::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.h:192
grpc::ClientWriter
::grpc_impl::ClientWriter< W > ClientWriter
Definition: sync_stream.h:62
GRPC_QUEUE_SHUTDOWN
@ 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
grpc::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.h:212
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
The zero time interval.
grpc::CompletionQueue::CompletionQueue
CompletionQueue(const grpc_completion_queue_attributes &attributes)
Private constructor of CompletionQueue only visible to friend classes.
Definition: completion_queue.h:243
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
@ 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:710
GRPC_CQ_NON_LISTENING
@ GRPC_CQ_NON_LISTENING
Similar to GRPC_CQ_DEFAULT_POLLING except that the completion queues will not contain any 'listening ...
Definition: grpc_types.h:715
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:741
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::CompletionQueue::NextStatus
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue.h:119
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.h:59
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
grpc::CompletionQueue::TIMEOUT
@ TIMEOUT
deadline was reached.
Definition: completion_queue.h:123
grpc::CompletionQueue::cq
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:239
sync.h
grpc_completion_queue_attributes
Definition: grpc_types.h:761
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::ServerCompletionQueue::IsFrequentlyPolled
bool IsFrequentlyPolled()
Definition: completion_queue.h:423
grpc::internal::MutexLock
Definition: sync.h:69
grpc_impl::internal::TemplatedBidiStreamingHandler
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue.h:72
grpc::CompletionQueue::GOT_EVENT
@ GOT_EVENT
Got a new event; tag will be filled in with its associated value; ok indicating its success.
Definition: completion_queue.h:121
grpc_impl::internal::ServerReaderWriterBody
Definition: completion_queue.h:63
GRPC_CQ_CURRENT_VERSION
#define GRPC_CQ_CURRENT_VERSION
Definition: grpc_types.h:759
grpc::CompletionQueue::CompletionQueue
CompletionQueue()
Default constructor.
Definition: completion_queue.h:103
grpc::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:421
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::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.h:57
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:99
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::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue.h:93
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
grpc::internal::Mutex
Definition: sync.h:47
grpc::ServerCompletionQueue::ServerCompletionQueue
ServerCompletionQueue()
Default constructor.
Definition: completion_queue.h:427
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::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
grpc::CompletionQueue::SHUTDOWN
@ SHUTDOWN
The completion queue has been shutdown and fully-drained.
Definition: completion_queue.h:120
GPR_CLOCK_REALTIME
@ 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
@ GRPC_QUEUE_TIMEOUT
No event before timeout.
Definition: grpc_types.h:523
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::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.h:174
grpc::CompletionQueue::~CompletionQueue
~CompletionQueue()
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue.h:114