GRPC C++  1.70.1
completion_queue.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2015 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_COMPLETION_QUEUE_H
33 #define GRPCPP_COMPLETION_QUEUE_H
34 
35 #include <grpc/grpc.h>
36 #include <grpc/support/atm.h>
37 #include <grpc/support/time.h>
44 #include <grpcpp/impl/sync.h>
45 
46 #include <list>
47 
48 #include "absl/log/absl_check.h"
49 
51 
52 namespace grpc {
53 template <class R>
54 class ClientReader;
55 template <class W>
56 class ClientWriter;
57 template <class W, class R>
58 class ClientReaderWriter;
59 template <class R>
61 template <class W>
63 namespace internal {
64 template <class W, class R>
66 
67 template <class ResponseType>
70  grpc::Status&);
71 template <class ServiceType, class RequestType, class ResponseType,
72  class BaseRequestType, class BaseResponseType>
74 template <class ServiceType, class RequestType, class ResponseType>
76 template <class ServiceType, class RequestType, class ResponseType>
78 template <class Streamer, bool WriteNeeded>
80 template <grpc::StatusCode code>
82 } // namespace internal
83 
84 class Channel;
85 class ChannelInterface;
86 class Server;
87 class ServerBuilder;
88 class ServerContextBase;
89 class ServerInterface;
90 
91 namespace internal {
92 class CompletionQueueTag;
93 class RpcMethod;
94 template <class InputMessage, class OutputMessage>
96 template <class Op1, class Op2, class Op3, class Op4, class Op5, class Op6>
97 class CallOpSet;
98 } // namespace internal
99 
105  public:
111  nullptr}) {}
112 
116  explicit CompletionQueue(grpc_completion_queue* take);
117 
120 
122  enum NextStatus {
125  TIMEOUT
127  };
128 
177  bool Next(void** tag, bool* ok) {
178  // Check return type == GOT_EVENT... cases:
179  // SHUTDOWN - queue has been shutdown, return false.
180  // TIMEOUT - we passed infinity time => queue has been shutdown, return
181  // false.
182  // GOT_EVENT - we actually got an event, return true.
183  return (AsyncNextInternal(tag, ok, gpr_inf_future(GPR_CLOCK_REALTIME)) ==
184  GOT_EVENT);
185  }
186 
198  template <typename T>
199  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
200  grpc::TimePoint<T> deadline_tp(deadline);
201  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
202  }
203 
218  template <typename T, typename F>
219  NextStatus DoThenAsyncNext(F&& f, void** tag, bool* ok, const T& deadline) {
220  CompletionQueueTLSCache cache = CompletionQueueTLSCache(this);
221  f();
222  if (cache.Flush(tag, ok)) {
223  return GOT_EVENT;
224  } else {
225  return AsyncNext(tag, ok, deadline);
226  }
227  }
228 
239  void Shutdown();
240 
246  grpc_completion_queue* cq() { return cq_; }
247 
248  protected:
250  explicit CompletionQueue(const grpc_completion_queue_attributes& attributes) {
252  grpc_completion_queue_factory_lookup(&attributes), &attributes,
253  nullptr);
254  InitialAvalanching(); // reserve this for the future shutdown
255  }
256 
257  private:
258  // Friends for access to server registration lists that enable checking and
259  // logging on shutdown
260  friend class grpc::ServerBuilder;
261  friend class grpc::Server;
262 
263  // Friend synchronous wrappers so that they can access Pluck(), which is
264  // a semi-private API geared towards the synchronous implementation.
265  template <class R>
266  friend class grpc::ClientReader;
267  template <class W>
268  friend class grpc::ClientWriter;
269  template <class W, class R>
271  template <class R>
272  friend class grpc::ServerReader;
273  template <class W>
274  friend class grpc::ServerWriter;
275  template <class W, class R>
277  template <class ResponseType>
280  grpc::Status&);
281  template <class ServiceType, class RequestType, class ResponseType>
283  template <class ServiceType, class RequestType, class ResponseType>
285  template <class Streamer, bool WriteNeeded>
287  template <grpc::StatusCode code>
290  friend class grpc::ServerInterface;
291  template <class InputMessage, class OutputMessage>
293 
294  // Friends that need access to constructor for callback CQ
295  friend class grpc::Channel;
296 
297  // For access to Register/CompleteAvalanching
298  template <class Op1, class Op2, class Op3, class Op4, class Op5, class Op6>
300 
305  class CompletionQueueTLSCache {
306  public:
307  explicit CompletionQueueTLSCache(CompletionQueue* cq);
308  ~CompletionQueueTLSCache();
309  bool Flush(void** tag, bool* ok);
310 
311  private:
312  CompletionQueue* cq_;
313  bool flushed_;
314  };
315 
316  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
317 
320  bool Pluck(grpc::internal::CompletionQueueTag* tag) {
321  auto deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
322  while (true) {
323  auto ev = grpc_completion_queue_pluck(cq_, tag, deadline, nullptr);
324  bool ok = ev.success != 0;
325  void* ignored = tag;
326  if (tag->FinalizeResult(&ignored, &ok)) {
327  ABSL_CHECK(ignored == tag);
328  return ok;
329  }
330  }
331  }
332 
341  void TryPluck(grpc::internal::CompletionQueueTag* tag) {
342  auto deadline = gpr_time_0(GPR_CLOCK_REALTIME);
343  auto ev = grpc_completion_queue_pluck(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  ABSL_CHECK(!tag->FinalizeResult(&ignored, &ok));
349  }
350 
356  void TryPluck(grpc::internal::CompletionQueueTag* tag,
357  gpr_timespec deadline) {
358  auto ev = grpc_completion_queue_pluck(cq_, tag, deadline, nullptr);
359  if (ev.type == GRPC_QUEUE_TIMEOUT || ev.type == GRPC_QUEUE_SHUTDOWN) {
360  return;
361  }
362 
363  bool ok = ev.success != 0;
364  void* ignored = tag;
365  ABSL_CHECK(!tag->FinalizeResult(&ignored, &ok));
366  }
367 
374  void InitialAvalanching() {
375  gpr_atm_rel_store(&avalanches_in_flight_, gpr_atm{1});
376  }
377  void RegisterAvalanching() {
378  gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_, gpr_atm{1});
379  }
380  void CompleteAvalanching() {
381  if (gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_, gpr_atm{-1}) ==
382  1) {
384  }
385  }
386 
387  void RegisterServer(const grpc::Server* server) {
388  (void)server;
389 #ifndef NDEBUG
390  grpc::internal::MutexLock l(&server_list_mutex_);
391  server_list_.push_back(server);
392 #endif
393  }
394  void UnregisterServer(const grpc::Server* server) {
395  (void)server;
396 #ifndef NDEBUG
397  grpc::internal::MutexLock l(&server_list_mutex_);
398  server_list_.remove(server);
399 #endif
400  }
401  bool ServerListEmpty() const {
402 #ifndef NDEBUG
403  grpc::internal::MutexLock l(&server_list_mutex_);
404  return server_list_.empty();
405 #endif
406  return true;
407  }
408 
409  static CompletionQueue* CallbackAlternativeCQ();
410  static void ReleaseCallbackAlternativeCQ(CompletionQueue* cq);
411 
412  grpc_completion_queue* cq_; // owned
413 
414  gpr_atm avalanches_in_flight_;
415 
416  // List of servers associated with this CQ. Even though this is only used with
417  // NDEBUG, instantiate it in all cases since otherwise the size will be
418  // inconsistent.
419  mutable grpc::internal::Mutex server_list_mutex_;
420  std::list<const grpc::Server*>
421  server_list_ /* GUARDED_BY(server_list_mutex_) */;
422 };
423 
427  public:
428  bool IsFrequentlyPolled() { return polling_type_ != GRPC_CQ_NON_LISTENING; }
429 
430  protected:
433 
434  private:
442  grpc_cq_polling_type polling_type,
443  grpc_completion_queue_functor* shutdown_cb)
445  GRPC_CQ_CURRENT_VERSION, completion_type, polling_type,
446  shutdown_cb}),
447  polling_type_(polling_type) {}
448 
449  grpc_cq_polling_type polling_type_;
450  friend class grpc::ServerBuilder;
451  friend class grpc::Server;
452 };
453 
454 } // namespace grpc
455 
456 #endif // GRPCPP_COMPLETION_QUEUE_H
grpc::CompletionQueue::Shutdown
void Shutdown()
Request the shutdown of the queue.
grpc::ClientWriter
Synchronous (blocking) client-side API for doing client-streaming RPCs, where the outgoing message st...
Definition: client_context.h:82
GRPC_CQ_NEXT
@ GRPC_CQ_NEXT
Events are popped out by calling grpc_completion_queue_next() API ONLY.
Definition: grpc_types.h:430
grpc::Server
Represents a gRPC server.
Definition: server.h:57
time.h
rpc_service_method.h
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::internal::CallOpSet
Primary implementation of CallOpSetInterface.
Definition: completion_queue.h:97
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:410
grpc::internal::ErrorMethodHandler
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: completion_queue.h:81
grpc::internal::MethodHandler::HandlerParameter
Definition: rpc_service_method.h:43
grpc::internal::RpcMethodHandler
A wrapper class of an application provided rpc method handler.
Definition: completion_queue.h:73
grpc::ServerWriter
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs,...
Definition: completion_queue.h:62
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:428
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:199
grpc::internal::BlockingUnaryCallImpl
Definition: client_context.h:102
GRPC_QUEUE_SHUTDOWN
@ GRPC_QUEUE_SHUTDOWN
Shutting down.
Definition: grpc_types.h:226
grpc::ServerContextBase
Base class of ServerContext.
Definition: server_context.h:124
completion_queue_tag.h
gpr_atm
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:32
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:219
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
The far future.
grpc_completion_queue_create
GRPCAPI grpc_completion_queue * grpc_completion_queue_create(const grpc_completion_queue_factory *factory, const grpc_completion_queue_attributes *attributes, void *reserved)
Create a completion queue.
grpc::internal::ServerReaderWriterBody
Definition: completion_queue.h:65
grpc::CompletionQueue::CompletionQueue
CompletionQueue(const grpc_completion_queue_attributes &attributes)
Private constructor of CompletionQueue only visible to friend classes.
Definition: completion_queue.h:250
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:34
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:413
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:418
grpc::ClientReader
Synchronous (blocking) client-side API for doing server-streaming RPCs, where the stream of messages ...
Definition: client_context.h:80
grpc::ServerReader
Synchronous (blocking) server-side API for doing client-streaming RPCs, where the incoming message st...
Definition: completion_queue.h:60
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.
gpr_time_0
GPRAPI gpr_timespec gpr_time_0(gpr_clock_type type)
Time constants.
grpc.h
grpc::CompletionQueue::NextStatus
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue.h:122
grpc::TimePoint::raw_time
gpr_timespec raw_time()=delete
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:71
grpc::CompletionQueue::TIMEOUT
@ TIMEOUT
deadline was reached.
Definition: completion_queue.h:126
grpc::CompletionQueue::cq
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:246
sync.h
grpc_completion_queue_attributes
Definition: grpc_types.h:461
grpc::internal::GrpcLibrary
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:32
grpc::internal::CompletionQueueTag
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
grpc_completion_queue_pluck
GRPCAPI grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag, gpr_timespec deadline, void *reserved)
Blocks until an event with tag 'tag' is available, the completion queue is being shutdown or deadline...
grpc::internal::ServerStreamingHandler
A wrapper class of an application provided server streaming handler.
Definition: completion_queue.h:77
grpc::ServerCompletionQueue::IsFrequentlyPolled
bool IsFrequentlyPolled()
Definition: completion_queue.h:428
grpc::internal::MutexLock
Definition: sync.h:80
grpc_library.h
grpc::internal::ClientStreamingHandler
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:75
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:124
grpc_completion_queue_destroy
GRPCAPI void grpc_completion_queue_destroy(grpc_completion_queue *cq)
Destroy a completion queue.
grpc::internal::TemplatedBidiStreamingHandler
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue.h:79
GRPC_CQ_CURRENT_VERSION
#define GRPC_CQ_CURRENT_VERSION
Definition: grpc_types.h:459
grpc::CompletionQueue::CompletionQueue
CompletionQueue()
Default constructor.
Definition: completion_queue.h:108
grpc::CompletionQueue::~CompletionQueue
~CompletionQueue() override
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue.h:119
grpc::ClientReaderWriter
Synchronous (blocking) client-side API for bi-directional streaming RPCs, where the outgoing message ...
Definition: client_context.h:84
grpc::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:426
grpc_completion_queue
struct grpc_completion_queue grpc_completion_queue
Completion Queues enable notification of the completion of asynchronous actions.
Definition: grpc_types.h:57
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_completion_queue_shutdown
GRPCAPI void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Begin destruction of a completion queue.
grpc_completion_queue_functor
Specifies an interface class to be used as a tag for callback-based completion queues.
Definition: grpc_types.h:443
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:104
grpc::Channel
Channels represent a connection to an endpoint. Created by CreateChannel.
Definition: channel.h:54
gpr_atm_rel_store
#define gpr_atm_rel_store(p, value)
Definition: atm_gcc_atomic.h:40
grpc::internal::Mutex
Definition: sync.h:57
grpc::internal::RpcMethod
Descriptor of an RPC method.
Definition: rpc_method.h:29
grpc::ServerCompletionQueue::ServerCompletionQueue
ServerCompletionQueue()
Default constructor.
Definition: completion_queue.h:432
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:88
grpc::ServerInterface
Definition: server_interface.h:60
atm.h
gpr_timespec
Analogous to struct timespec.
Definition: time.h:47
grpc::internal::UnaryRunHandlerHelper
void UnaryRunHandlerHelper(const grpc::internal::MethodHandler::HandlerParameter &, ResponseType *, grpc::Status &)
A helper function with reduced templating to do the common work needed to actually send the server re...
Definition: method_handler.h:59
grpc::CompletionQueue::SHUTDOWN
@ SHUTDOWN
The completion queue has been shutdown and fully-drained.
Definition: completion_queue.h:123
sync.h
GRPC_QUEUE_TIMEOUT
@ GRPC_QUEUE_TIMEOUT
No event before timeout.
Definition: grpc_types.h:228
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Realtime clock.
Definition: time.h:36
grpc::TimePoint
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:40
time.h
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:177
gpr_atm_no_barrier_fetch_add
#define gpr_atm_no_barrier_fetch_add(p, delta)
Definition: atm_gcc_atomic.h:45