GRPC C++  1.4.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
completion_queue.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
47 #ifndef GRPCXX_IMPL_CODEGEN_COMPLETION_QUEUE_H
48 #define GRPCXX_IMPL_CODEGEN_COMPLETION_QUEUE_H
49 
55 #include <grpc/impl/codegen/atm.h>
56 
58 
59 namespace grpc {
60 
61 template <class R>
62 class ClientReader;
63 template <class W>
64 class ClientWriter;
65 template <class W, class R>
66 class ClientReaderWriter;
67 template <class R>
69 template <class W>
71 namespace internal {
72 template <class W, class R>
74 }
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 ServiceType, class RequestType, class ResponseType>
84 
85 class Channel;
86 class ChannelInterface;
87 class ClientContext;
88 class CompletionQueueTag;
89 class CompletionQueue;
90 class RpcMethod;
91 class Server;
92 class ServerBuilder;
93 class ServerContext;
94 
96 
102  public:
108 
112  explicit CompletionQueue(grpc_completion_queue* take);
113 
117  }
118 
120  enum NextStatus {
123  TIMEOUT
125  };
126 
137  template <typename T>
138  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
139  TimePoint<T> deadline_tp(deadline);
140  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
141  }
142 
150  bool Next(void** tag, bool* ok) {
151  return (AsyncNextInternal(tag, ok, g_core_codegen_interface->gpr_inf_future(
153  }
154 
164  void Shutdown();
165 
171  grpc_completion_queue* cq() { return cq_; }
172 
180  gpr_atm_rel_store(&avalanches_in_flight_, static_cast<gpr_atm>(1));
181  }
183  gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_,
184  static_cast<gpr_atm>(1));
185  };
186  void CompleteAvalanching();
187 
188  protected:
193  &attributes),
194  &attributes, NULL);
195  InitialAvalanching(); // reserve this for the future shutdown
196  }
197 
198  private:
199  // Friend synchronous wrappers so that they can access Pluck(), which is
200  // a semi-private API geared towards the synchronous implementation.
201  template <class R>
202  friend class ::grpc::ClientReader;
203  template <class W>
204  friend class ::grpc::ClientWriter;
205  template <class W, class R>
206  friend class ::grpc::ClientReaderWriter;
207  template <class R>
208  friend class ::grpc::ServerReader;
209  template <class W>
210  friend class ::grpc::ServerWriter;
211  template <class W, class R>
212  friend class ::grpc::internal::ServerReaderWriterBody;
213  template <class ServiceType, class RequestType, class ResponseType>
214  friend class RpcMethodHandler;
215  template <class ServiceType, class RequestType, class ResponseType>
217  template <class ServiceType, class RequestType, class ResponseType>
219  template <class Streamer, bool WriteNeeded>
221  friend class UnknownMethodHandler;
222  friend class ::grpc::Server;
223  friend class ::grpc::ServerContext;
224  template <class InputMessage, class OutputMessage>
225  friend Status BlockingUnaryCall(ChannelInterface* channel,
226  const RpcMethod& method,
227  ClientContext* context,
228  const InputMessage& request,
229  OutputMessage* result);
230 
231  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
232 
235  bool Pluck(CompletionQueueTag* tag) {
236  auto deadline =
239  cq_, tag, deadline, nullptr);
240  bool ok = ev.success != 0;
241  void* ignored = tag;
242  GPR_CODEGEN_ASSERT(tag->FinalizeResult(&ignored, &ok));
243  GPR_CODEGEN_ASSERT(ignored == tag);
244  // Ignore mutations by FinalizeResult: Pluck returns the C API status
245  return ev.success != 0;
246  }
247 
256  void TryPluck(CompletionQueueTag* tag) {
259  cq_, tag, deadline, nullptr);
260  if (ev.type == GRPC_QUEUE_TIMEOUT) return;
261  bool ok = ev.success != 0;
262  void* ignored = tag;
263  // the tag must be swallowed if using TryPluck
264  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
265  }
266 
272  void TryPluck(CompletionQueueTag* tag, gpr_timespec deadline) {
274  cq_, tag, deadline, nullptr);
275  if (ev.type == GRPC_QUEUE_TIMEOUT || ev.type == GRPC_QUEUE_SHUTDOWN) {
276  return;
277  }
278 
279  bool ok = ev.success != 0;
280  void* ignored = tag;
281  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
282  }
283 
284  grpc_completion_queue* cq_; // owned
285 
286  gpr_atm avalanches_in_flight_;
287 };
288 
292  public:
293  bool IsFrequentlyPolled() { return polling_type_ != GRPC_CQ_NON_LISTENING; }
294 
295  private:
296  grpc_cq_polling_type polling_type_;
297  friend class ServerBuilder;
304  GRPC_CQ_CURRENT_VERSION, GRPC_CQ_NEXT, polling_type}),
305  polling_type_(polling_type) {}
306 };
307 
308 } // namespace grpc
309 
310 #endif // GRPCXX_IMPL_CODEGEN_COMPLETION_QUEUE_H
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:40
virtual const grpc_completion_queue_factory * grpc_completion_queue_factory_lookup(const grpc_completion_queue_attributes *attributes)=0
friend Status BlockingUnaryCall(ChannelInterface *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
Wrapper that performs a blocking unary call.
Definition: client_unary_call.h:52
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
virtual bool FinalizeResult(void **tag, bool *status)=0
Called prior to returning from Next(), return value is the status of the operation (return status is ...
Definition: completion_queue.h:73
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:137
Got a new event; tag will be filled in with its associated value; ok indicating its success...
Definition: completion_queue.h:122
No event before timeout.
Definition: grpc_types.h:405
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:41
int success
If the grpc_completion_type is GRPC_OP_COMPLETE, this field indicates whether the operation was succe...
Definition: grpc_types.h:421
#define GRPC_CQ_CURRENT_VERSION
Definition: grpc_types.h:601
The completion queue has been shutdown.
Definition: completion_queue.h:121
Events are popped out by calling grpc_completion_queue_next() API ONLY.
Definition: grpc_types.h:595
gpr_timespec raw_time()
Definition: time.h:56
#define gpr_atm_no_barrier_fetch_add(p, delta)
Definition: atm_gcc_atomic.h:67
bool IsFrequentlyPolled()
Definition: completion_queue.h:293
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:52
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:53
Definition: grpc_types.h:602
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:171
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:168
Synchronous (blocking) server-side API for doing client-streaming RPCs, where the incoming message st...
Definition: completion_queue.h:68
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:150
void RegisterAvalanching()
Definition: completion_queue.h:182
Handle unknown method by returning UNIMPLEMENTED error.
Definition: method_handler_impl.h:253
grpc_cq_polling_type
Completion queues internally MAY maintain a set of file descriptors in a structure called 'pollset'...
Definition: grpc_types.h:575
CompletionQueue(const grpc_completion_queue_attributes &attributes)
Private constructor of CompletionQueue only visible to friend classes.
Definition: completion_queue.h:190
Shutting down.
Definition: grpc_types.h:403
A wrapper class of an application provided bidi-streaming handler.
Definition: method_handler_impl.h:177
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:138
~CompletionQueue()
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue.h:115
Represents a gRPC server.
Definition: server.h:67
A wrapper class of an application provided rpc method handler.
Definition: completion_queue.h:76
void Shutdown()
Request the shutdown of the queue.
virtual grpc_completion_queue * grpc_completion_queue_create(const grpc_completion_queue_factory *factory, const grpc_completion_queue_attributes *attributes, void *reserved)=0
Codegen interface for grpc::Channel.
Definition: channel_interface.h:64
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:64
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue.h:120
The completion queue will have an associated pollset and there is no restriction on the type of file ...
Definition: grpc_types.h:578
Similar to GRPC_CQ_DEFAULT_POLLING except that the completion queues will not contain any 'listening ...
Definition: grpc_types.h:583
CompletionQueue()
Default constructor.
Definition: completion_queue.h:105
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:109
Realtime clock.
Definition: gpr_types.h:51
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:70
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:101
void InitialAvalanching()
Manage state of avalanching operations : completion queue tags that trigger other completion queue op...
Definition: completion_queue.h:179
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:78
Descriptor of an RPC method.
Definition: rpc_method.h:44
#define gpr_atm_rel_store(p, value)
Definition: atm_gcc_atomic.h:62
Interface between the codegen library and the minimal subset of core features required by the generat...
Definition: core_codegen_interface.h:56
A wrapper class of an application provided server streaming handler.
Definition: completion_queue.h:80
Did it work? If it didn't, why?
Definition: status.h:45
Analogous to struct timespec.
Definition: gpr_types.h:62
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:291
struct grpc_completion_queue grpc_completion_queue
Completion Queues enable notification of the completion of asynchronous actions.
Definition: grpc_types.h:72
virtual gpr_timespec gpr_time_0(gpr_clock_type type)=0
Definition: completion_queue.h:82
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:69
deadline was reached.
Definition: completion_queue.h:124
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:49