GRPC C++  1.3.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:
107  InitialAvalanching(); // reserve this for the future shutdown
108  }
109 
113  explicit CompletionQueue(grpc_completion_queue* take);
114 
118  }
119 
121  enum NextStatus {
124  TIMEOUT
126  };
127 
138  template <typename T>
139  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
140  TimePoint<T> deadline_tp(deadline);
141  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
142  }
143 
151  bool Next(void** tag, bool* ok) {
152  return (AsyncNextInternal(tag, ok, g_core_codegen_interface->gpr_inf_future(
154  }
155 
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  private:
189  // Friend synchronous wrappers so that they can access Pluck(), which is
190  // a semi-private API geared towards the synchronous implementation.
191  template <class R>
192  friend class ::grpc::ClientReader;
193  template <class W>
194  friend class ::grpc::ClientWriter;
195  template <class W, class R>
196  friend class ::grpc::ClientReaderWriter;
197  template <class R>
198  friend class ::grpc::ServerReader;
199  template <class W>
200  friend class ::grpc::ServerWriter;
201  template <class W, class R>
202  friend class ::grpc::internal::ServerReaderWriterBody;
203  template <class ServiceType, class RequestType, class ResponseType>
204  friend class RpcMethodHandler;
205  template <class ServiceType, class RequestType, class ResponseType>
207  template <class ServiceType, class RequestType, class ResponseType>
209  template <class Streamer, bool WriteNeeded>
211  friend class UnknownMethodHandler;
212  friend class ::grpc::Server;
213  friend class ::grpc::ServerContext;
214  template <class InputMessage, class OutputMessage>
215  friend Status BlockingUnaryCall(ChannelInterface* channel,
216  const RpcMethod& method,
217  ClientContext* context,
218  const InputMessage& request,
219  OutputMessage* result);
220 
221  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
222 
225  bool Pluck(CompletionQueueTag* tag) {
226  auto deadline =
229  cq_, tag, deadline, nullptr);
230  bool ok = ev.success != 0;
231  void* ignored = tag;
232  GPR_CODEGEN_ASSERT(tag->FinalizeResult(&ignored, &ok));
233  GPR_CODEGEN_ASSERT(ignored == tag);
234  // Ignore mutations by FinalizeResult: Pluck returns the C API status
235  return ev.success != 0;
236  }
237 
240  void TryPluck(CompletionQueueTag* tag) {
243  cq_, tag, deadline, nullptr);
244  if (ev.type == GRPC_QUEUE_TIMEOUT) return;
245  bool ok = ev.success != 0;
246  void* ignored = tag;
247  // the tag must be swallowed if using TryPluck
248  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
249  }
250 
251  grpc_completion_queue* cq_; // owned
252 
253  gpr_atm avalanches_in_flight_;
254 };
255 
259  public:
260  bool IsFrequentlyPolled() { return is_frequently_polled_; }
261 
262  private:
263  bool is_frequently_polled_;
264  friend class ServerBuilder;
269  ServerCompletionQueue(bool is_frequently_polled = true)
270  : is_frequently_polled_(is_frequently_polled) {}
271 };
272 
273 } // namespace grpc
274 
275 #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
Definition: completion_queue.h:73
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:122
Got a new event; tag will be filled in with its associated value; ok indicating its success...
Definition: completion_queue.h:123
No event before timeout.
Definition: grpc_types.h:385
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:41
int success
non-zero if the operation was successful, 0 upon failure.
Definition: grpc_types.h:398
The completion queue has been shutdown.
Definition: completion_queue.h:122
gpr_timespec raw_time()
Definition: time.h:57
#define gpr_atm_no_barrier_fetch_add(p, delta)
Definition: atm_gcc_atomic.h:67
bool IsFrequentlyPolled()
Definition: completion_queue.h:260
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
Definition: time.h:54
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:171
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:151
void RegisterAvalanching()
Definition: completion_queue.h:182
Definition: method_handler_impl.h:253
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:139
~CompletionQueue()
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue.h:116
Models a gRPC server.
Definition: server.h:66
Definition: completion_queue.h:76
void Shutdown()
Request the shutdown of the queue.
Codegen interface for grpc::Channel.
Definition: channel_interface.h:64
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:63
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue.h:121
CompletionQueue()
Default constructor.
Definition: completion_queue.h:105
Definition: server_context.h:94
Definition: gpr_types.h:52
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
Definition: completion_queue.h:78
Definition: rpc_method.h:43
#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
Definition: completion_queue.h:80
Did it work? If it didn't, why?
Definition: status.h:45
Definition: gpr_types.h:63
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:258
struct grpc_completion_queue grpc_completion_queue
Completion Queues enable notification of the completion of asynchronous actions.
Definition: grpc_types.h:71
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:68
deadline was reached.
Definition: completion_queue.h:125
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