GRPC C++  1.8.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 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 GRPCXX_IMPL_CODEGEN_COMPLETION_QUEUE_H
33 #define GRPCXX_IMPL_CODEGEN_COMPLETION_QUEUE_H
34 
40 #include <grpc/impl/codegen/atm.h>
41 
43 
44 namespace grpc {
45 
46 template <class R>
47 class ClientReader;
48 template <class W>
49 class ClientWriter;
50 template <class W, class R>
51 class ClientReaderWriter;
52 template <class R>
54 template <class W>
56 namespace internal {
57 template <class W, class R>
59 } // namespace internal
60 
61 class Channel;
62 class ChannelInterface;
63 class ClientContext;
64 class CompletionQueue;
65 class Server;
66 class ServerBuilder;
67 class ServerContext;
68 class ServerInterface;
69 
70 namespace internal {
71 class CompletionQueueTag;
72 class RpcMethod;
73 template <class ServiceType, class RequestType, class ResponseType>
74 class RpcMethodHandler;
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 template <class Streamer, bool WriteNeeded>
84 template <class InputMessage, class OutputMessage>
86 } // namespace internal
87 
89 
95  public:
101 
105  explicit CompletionQueue(grpc_completion_queue* take);
106 
110  }
111 
113  enum NextStatus {
116  TIMEOUT
118  };
119 
133  template <typename T, typename F>
134  NextStatus DoThenAsyncNext(F&& f, void** tag, bool* ok, const T& deadline) {
135  CompletionQueueTLSCache cache = CompletionQueueTLSCache(this);
136  f();
137  if (cache.Flush(tag, ok)) {
138  return GOT_EVENT;
139  } else {
140  return AsyncNext(tag, ok, deadline);
141  }
142  }
143 
154  template <typename T>
155  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
156  TimePoint<T> deadline_tp(deadline);
157  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
158  }
159 
167  bool Next(void** tag, bool* ok) {
168  return (AsyncNextInternal(tag, ok,
171  }
172 
182  void Shutdown();
183 
189  grpc_completion_queue* cq() { return cq_; }
190 
191  protected:
196  &attributes),
197  &attributes, NULL);
198  InitialAvalanching(); // reserve this for the future shutdown
199  }
200 
201  private:
202  // Friend synchronous wrappers so that they can access Pluck(), which is
203  // a semi-private API geared towards the synchronous implementation.
204  template <class R>
205  friend class ::grpc::ClientReader;
206  template <class W>
207  friend class ::grpc::ClientWriter;
208  template <class W, class R>
209  friend class ::grpc::ClientReaderWriter;
210  template <class R>
211  friend class ::grpc::ServerReader;
212  template <class W>
213  friend class ::grpc::ServerWriter;
214  template <class W, class R>
215  friend class ::grpc::internal::ServerReaderWriterBody;
216  template <class ServiceType, class RequestType, class ResponseType>
217  friend class ::grpc::internal::RpcMethodHandler;
218  template <class ServiceType, class RequestType, class ResponseType>
219  friend class ::grpc::internal::ClientStreamingHandler;
220  template <class ServiceType, class RequestType, class ResponseType>
221  friend class ::grpc::internal::ServerStreamingHandler;
222  template <class Streamer, bool WriteNeeded>
223  friend class ::grpc::internal::TemplatedBidiStreamingHandler;
224  friend class ::grpc::internal::UnknownMethodHandler;
225  friend class ::grpc::Server;
226  friend class ::grpc::ServerContext;
227  friend class ::grpc::ServerInterface;
228  template <class InputMessage, class OutputMessage>
229  friend class ::grpc::internal::BlockingUnaryCallImpl;
230 
235  class CompletionQueueTLSCache {
236  public:
237  CompletionQueueTLSCache(CompletionQueue* cq);
238  ~CompletionQueueTLSCache();
239  bool Flush(void** tag, bool* ok);
240 
241  private:
242  CompletionQueue* cq_;
243  bool flushed_;
244  };
245 
246  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
247 
250  bool Pluck(internal::CompletionQueueTag* tag) {
251  auto deadline =
254  cq_, tag, deadline, nullptr);
255  bool ok = ev.success != 0;
256  void* ignored = tag;
257  GPR_CODEGEN_ASSERT(tag->FinalizeResult(&ignored, &ok));
258  GPR_CODEGEN_ASSERT(ignored == tag);
259  // Ignore mutations by FinalizeResult: Pluck returns the C API status
260  return ev.success != 0;
261  }
262 
271  void TryPluck(internal::CompletionQueueTag* tag) {
274  cq_, tag, deadline, nullptr);
275  if (ev.type == GRPC_QUEUE_TIMEOUT) return;
276  bool ok = ev.success != 0;
277  void* ignored = tag;
278  // the tag must be swallowed if using TryPluck
279  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
280  }
281 
287  void TryPluck(internal::CompletionQueueTag* tag, gpr_timespec deadline) {
289  cq_, tag, deadline, nullptr);
290  if (ev.type == GRPC_QUEUE_TIMEOUT || ev.type == GRPC_QUEUE_SHUTDOWN) {
291  return;
292  }
293 
294  bool ok = ev.success != 0;
295  void* ignored = tag;
296  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
297  }
298 
305  void InitialAvalanching() {
306  gpr_atm_rel_store(&avalanches_in_flight_, static_cast<gpr_atm>(1));
307  }
308  void RegisterAvalanching() {
309  gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_,
310  static_cast<gpr_atm>(1));
311  }
312  void CompleteAvalanching();
313 
314  grpc_completion_queue* cq_; // owned
315 
316  gpr_atm avalanches_in_flight_;
317 };
318 
322  public:
323  bool IsFrequentlyPolled() { return polling_type_ != GRPC_CQ_NON_LISTENING; }
324 
325  private:
326  grpc_cq_polling_type polling_type_;
327  friend class ServerBuilder;
334  GRPC_CQ_CURRENT_VERSION, GRPC_CQ_NEXT, polling_type}),
335  polling_type_(polling_type) {}
336 };
337 
338 } // namespace grpc
339 
340 #endif // GRPCXX_IMPL_CODEGEN_COMPLETION_QUEUE_H
A wrapper class of an application provided server streaming handler.
Definition: byte_buffer.h:43
virtual const grpc_completion_queue_factory * grpc_completion_queue_factory_lookup(const grpc_completion_queue_attributes *attributes)=0
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue.h:83
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
Definition: completion_queue.h:58
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:135
Got a new event; tag will be filled in with its associated value; ok indicating its success...
Definition: completion_queue.h:115
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 ...
No event before timeout.
Definition: grpc_types.h:415
Handle unknown method by returning UNIMPLEMENTED error.
Definition: method_handler_impl.h:246
int success
If the grpc_completion_type is GRPC_OP_COMPLETE, this field indicates whether the operation was succe...
Definition: grpc_types.h:431
#define GRPC_CQ_CURRENT_VERSION
Definition: grpc_types.h:624
The completion queue has been shutdown.
Definition: completion_queue.h:114
Events are popped out by calling grpc_completion_queue_next() API ONLY.
Definition: grpc_types.h:618
gpr_timespec raw_time()
Definition: time.h:43
bool IsFrequentlyPolled()
Definition: completion_queue.h:323
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:37
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:40
Definition: grpc_types.h:625
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:189
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:162
Synchronous (blocking) server-side API for doing client-streaming RPCs, where the incoming message st...
Definition: completion_queue.h:53
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:167
Descriptor of an RPC method.
Definition: rpc_method.h:29
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:134
grpc_cq_polling_type
Completion queues internally MAY maintain a set of file descriptors in a structure called 'pollset'...
Definition: grpc_types.h:598
CompletionQueue(const grpc_completion_queue_attributes &attributes)
Private constructor of CompletionQueue only visible to friend classes.
Definition: completion_queue.h:193
Shutting down.
Definition: grpc_types.h:413
#define gpr_atm_rel_store(p, value)
Definition: atm_gcc_atomic.h:52
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:155
~CompletionQueue()
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue.h:108
Represents a gRPC server.
Definition: server.h:52
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
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:76
#define gpr_atm_no_barrier_fetch_add(p, delta)
Definition: atm_gcc_atomic.h:57
Codegen interface for grpc::Channel.
Definition: channel_interface.h:55
A wrapper class of an application provided rpc method handler.
Definition: byte_buffer.h:41
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:46
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue.h:113
The completion queue will have an associated pollset and there is no restriction on the type of file ...
Definition: grpc_types.h:601
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:30
Similar to GRPC_CQ_DEFAULT_POLLING except that the completion queues will not contain any 'listening ...
Definition: grpc_types.h:606
CompletionQueue()
Default constructor.
Definition: completion_queue.h:98
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:96
Definition: server_interface.h:47
Realtime clock.
Definition: gpr_types.h:36
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:55
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:94
Definition: channel_interface.h:43
Interface between the codegen library and the minimal subset of core features required by the generat...
Definition: core_codegen_interface.h:41
Analogous to struct timespec.
Definition: gpr_types.h:47
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:321
Definition: completion_queue.h:80
struct grpc_completion_queue grpc_completion_queue
Completion Queues enable notification of the completion of asynchronous actions.
Definition: grpc_types.h:57
virtual gpr_timespec gpr_time_0(gpr_clock_type type)=0
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:53
deadline was reached.
Definition: completion_queue.h:117
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:34