GRPC C++  1.0.0
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/time.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 template <class W, class R>
73 template <class ServiceType, class RequestType, class ResponseType>
75 template <class ServiceType, class RequestType, class ResponseType>
77 template <class ServiceType, class RequestType, class ResponseType>
79 template <class ServiceType, class RequestType, class ResponseType>
82 
83 class Channel;
84 class ChannelInterface;
85 class ClientContext;
86 class CompletionQueueTag;
87 class CompletionQueue;
88 class RpcMethod;
89 class Server;
90 class ServerBuilder;
91 class ServerContext;
92 
94 
98  public:
102  cq_ = g_core_codegen_interface->grpc_completion_queue_create(nullptr);
103  }
104 
108  explicit CompletionQueue(grpc_completion_queue* take);
109 
112  g_core_codegen_interface->grpc_completion_queue_destroy(cq_);
113  }
114 
116  enum NextStatus {
119  TIMEOUT
121  };
122 
133  template <typename T>
134  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
135  TimePoint<T> deadline_tp(deadline);
136  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
137  }
138 
146  bool Next(void** tag, bool* ok) {
147  return (AsyncNextInternal(tag, ok, g_core_codegen_interface->gpr_inf_future(
149  }
150 
158  void Shutdown();
159 
165  grpc_completion_queue* cq() { return cq_; }
166 
167  private:
168  // Friend synchronous wrappers so that they can access Pluck(), which is
169  // a semi-private API geared towards the synchronous implementation.
170  template <class R>
171  friend class ::grpc::ClientReader;
172  template <class W>
173  friend class ::grpc::ClientWriter;
174  template <class W, class R>
175  friend class ::grpc::ClientReaderWriter;
176  template <class R>
177  friend class ::grpc::ServerReader;
178  template <class W>
179  friend class ::grpc::ServerWriter;
180  template <class W, class R>
181  friend class ::grpc::ServerReaderWriter;
182  template <class ServiceType, class RequestType, class ResponseType>
183  friend class RpcMethodHandler;
184  template <class ServiceType, class RequestType, class ResponseType>
186  template <class ServiceType, class RequestType, class ResponseType>
188  template <class ServiceType, class RequestType, class ResponseType>
189  friend class BidiStreamingHandler;
190  friend class UnknownMethodHandler;
191  friend class ::grpc::Server;
192  friend class ::grpc::ServerContext;
193  template <class InputMessage, class OutputMessage>
194  friend Status BlockingUnaryCall(ChannelInterface* channel,
195  const RpcMethod& method,
196  ClientContext* context,
197  const InputMessage& request,
198  OutputMessage* result);
199 
200  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
201 
204  bool Pluck(CompletionQueueTag* tag) {
205  auto deadline =
206  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME);
207  auto ev = g_core_codegen_interface->grpc_completion_queue_pluck(
208  cq_, tag, deadline, nullptr);
209  bool ok = ev.success != 0;
210  void* ignored = tag;
211  GPR_CODEGEN_ASSERT(tag->FinalizeResult(&ignored, &ok));
212  GPR_CODEGEN_ASSERT(ignored == tag);
213  // Ignore mutations by FinalizeResult: Pluck returns the C API status
214  return ev.success != 0;
215  }
216 
219  void TryPluck(CompletionQueueTag* tag) {
220  auto deadline = gpr_time_0(GPR_CLOCK_REALTIME);
221  auto ev = g_core_codegen_interface->grpc_completion_queue_pluck(
222  cq_, tag, deadline, nullptr);
223  if (ev.type == GRPC_QUEUE_TIMEOUT) return;
224  bool ok = ev.success != 0;
225  void* ignored = tag;
226  // the tag must be swallowed if using TryPluck
227  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
228  }
229 
230  grpc_completion_queue* cq_; // owned
231 };
232 
236  public:
237  bool IsFrequentlyPolled() { return is_frequently_polled_; }
238 
239  private:
240  bool is_frequently_polled_;
241  friend class ServerBuilder;
246  ServerCompletionQueue(bool is_frequently_polled = true)
247  : is_frequently_polled_(is_frequently_polled) {}
248 };
249 
250 } // namespace grpc
251 
252 #endif // GRPCXX_IMPL_CODEGEN_COMPLETION_QUEUE_H
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:40
friend Status BlockingUnaryCall(ChannelInterface *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
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
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:97
Got a new event; tag will be filled in with its associated value; ok indicating its success...
Definition: completion_queue.h:118
No event before timeout.
Definition: grpc_types.h:250
int success
non-zero if the operation was successful, 0 upon failure.
Definition: grpc_types.h:263
The completion queue has been shutdown.
Definition: completion_queue.h:117
GPRAPI gpr_timespec gpr_time_0(gpr_clock_type type)
gpr_timespec raw_time()
Definition: time.h:57
bool IsFrequentlyPolled()
Definition: completion_queue.h:237
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:53
Definition: time.h:54
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:165
Definition: client_context.h:154
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:146
Definition: method_handler_impl.h:206
Definition: time.h:54
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:134
~CompletionQueue()
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue.h:111
Models a gRPC server.
Definition: server.h:67
Definition: completion_queue.h:74
void Shutdown()
Request the shutdown of the queue.
Definition: alarm.h:48
Codegen interface for grpc::Channel.
Definition: channel_interface.h:64
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:62
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue.h:116
CompletionQueue()
Default constructor.
Definition: completion_queue.h:101
Definition: server_context.h:91
Definition: completion_queue.h:70
A thin wrapper around grpc_completion_queue (see / src/core/surface/completion_queue.h).
Definition: completion_queue.h:97
Definition: completion_queue.h:76
Definition: rpc_method.h:43
Server-side interface for bi-directional streaming.
Definition: completion_queue.h:72
Interface between the codegen library and the minimal subset of core features required by the generat...
Definition: core_codegen_interface.h:50
Definition: completion_queue.h:78
Did it work? If it didn&#39;t, why?
Definition: status.h:45
Definition: time.h:63
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:235
struct grpc_completion_queue grpc_completion_queue
Completion Queues enable notification of the completion of asynchronous actions.
Definition: grpc_types.h:48
Definition: completion_queue.h:80
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:61
deadline was reached.
Definition: completion_queue.h:120
virtual grpc_completion_queue * grpc_completion_queue_create(void *reserved)=0
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