GRPC C++  1.6.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 }
60 template <class ServiceType, class RequestType, class ResponseType>
62 template <class ServiceType, class RequestType, class ResponseType>
64 template <class ServiceType, class RequestType, class ResponseType>
66 template <class ServiceType, class RequestType, class ResponseType>
69 
70 class Channel;
71 class ChannelInterface;
72 class ClientContext;
73 class CompletionQueueTag;
74 class CompletionQueue;
75 class RpcMethod;
76 class Server;
77 class ServerBuilder;
78 class ServerContext;
79 
81 
87  public:
93 
97  explicit CompletionQueue(grpc_completion_queue* take);
98 
102  }
103 
105  enum NextStatus {
108  TIMEOUT
110  };
111 
122  template <typename T>
123  NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
124  TimePoint<T> deadline_tp(deadline);
125  return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
126  }
127 
135  bool Next(void** tag, bool* ok) {
136  return (AsyncNextInternal(tag, ok, g_core_codegen_interface->gpr_inf_future(
138  }
139 
149  void Shutdown();
150 
156  grpc_completion_queue* cq() { return cq_; }
157 
165  gpr_atm_rel_store(&avalanches_in_flight_, static_cast<gpr_atm>(1));
166  }
168  gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_,
169  static_cast<gpr_atm>(1));
170  }
171  void CompleteAvalanching();
172 
173  protected:
178  &attributes),
179  &attributes, NULL);
180  InitialAvalanching(); // reserve this for the future shutdown
181  }
182 
183  private:
184  // Friend synchronous wrappers so that they can access Pluck(), which is
185  // a semi-private API geared towards the synchronous implementation.
186  template <class R>
187  friend class ::grpc::ClientReader;
188  template <class W>
189  friend class ::grpc::ClientWriter;
190  template <class W, class R>
191  friend class ::grpc::ClientReaderWriter;
192  template <class R>
193  friend class ::grpc::ServerReader;
194  template <class W>
195  friend class ::grpc::ServerWriter;
196  template <class W, class R>
197  friend class ::grpc::internal::ServerReaderWriterBody;
198  template <class ServiceType, class RequestType, class ResponseType>
199  friend class RpcMethodHandler;
200  template <class ServiceType, class RequestType, class ResponseType>
202  template <class ServiceType, class RequestType, class ResponseType>
204  template <class Streamer, bool WriteNeeded>
206  friend class UnknownMethodHandler;
207  friend class ::grpc::Server;
208  friend class ::grpc::ServerContext;
209  template <class InputMessage, class OutputMessage>
210  friend Status BlockingUnaryCall(ChannelInterface* channel,
211  const RpcMethod& method,
212  ClientContext* context,
213  const InputMessage& request,
214  OutputMessage* result);
215 
216  NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline);
217 
220  bool Pluck(CompletionQueueTag* tag) {
221  auto deadline =
224  cq_, tag, deadline, nullptr);
225  bool ok = ev.success != 0;
226  void* ignored = tag;
227  GPR_CODEGEN_ASSERT(tag->FinalizeResult(&ignored, &ok));
228  GPR_CODEGEN_ASSERT(ignored == tag);
229  // Ignore mutations by FinalizeResult: Pluck returns the C API status
230  return ev.success != 0;
231  }
232 
241  void TryPluck(CompletionQueueTag* tag) {
244  cq_, tag, deadline, nullptr);
245  if (ev.type == GRPC_QUEUE_TIMEOUT) return;
246  bool ok = ev.success != 0;
247  void* ignored = tag;
248  // the tag must be swallowed if using TryPluck
249  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
250  }
251 
257  void TryPluck(CompletionQueueTag* tag, gpr_timespec deadline) {
259  cq_, tag, deadline, nullptr);
260  if (ev.type == GRPC_QUEUE_TIMEOUT || ev.type == GRPC_QUEUE_SHUTDOWN) {
261  return;
262  }
263 
264  bool ok = ev.success != 0;
265  void* ignored = tag;
266  GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok));
267  }
268 
269  grpc_completion_queue* cq_; // owned
270 
271  gpr_atm avalanches_in_flight_;
272 };
273 
277  public:
278  bool IsFrequentlyPolled() { return polling_type_ != GRPC_CQ_NON_LISTENING; }
279 
280  private:
281  grpc_cq_polling_type polling_type_;
282  friend class ServerBuilder;
289  GRPC_CQ_CURRENT_VERSION, GRPC_CQ_NEXT, polling_type}),
290  polling_type_(polling_type) {}
291 };
292 
293 } // namespace grpc
294 
295 #endif // GRPCXX_IMPL_CODEGEN_COMPLETION_QUEUE_H
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:25
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:37
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:58
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:126
Got a new event; tag will be filled in with its associated value; ok indicating its success...
Definition: completion_queue.h:107
No event before timeout.
Definition: grpc_types.h:404
int success
If the grpc_completion_type is GRPC_OP_COMPLETE, this field indicates whether the operation was succe...
Definition: grpc_types.h:420
#define GRPC_CQ_CURRENT_VERSION
Definition: grpc_types.h:600
The completion queue has been shutdown.
Definition: completion_queue.h:106
Events are popped out by calling grpc_completion_queue_next() API ONLY.
Definition: grpc_types.h:594
gpr_timespec raw_time()
Definition: time.h:41
bool IsFrequentlyPolled()
Definition: completion_queue.h:278
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:38
Definition: grpc_types.h:601
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:156
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:153
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:135
void RegisterAvalanching()
Definition: completion_queue.h:167
Handle unknown method by returning UNIMPLEMENTED error.
Definition: method_handler_impl.h:244
grpc_cq_polling_type
Completion queues internally MAY maintain a set of file descriptors in a structure called 'pollset'...
Definition: grpc_types.h:574
CompletionQueue(const grpc_completion_queue_attributes &attributes)
Private constructor of CompletionQueue only visible to friend classes.
Definition: completion_queue.h:175
Shutting down.
Definition: grpc_types.h:402
#define gpr_atm_rel_store(p, value)
Definition: atm_gcc_atomic.h:47
A wrapper class of an application provided bidi-streaming handler.
Definition: method_handler_impl.h:165
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:123
~CompletionQueue()
Destructor. Destroys the owned wrapped completion queue / instance.
Definition: completion_queue.h:100
Represents a gRPC server.
Definition: server.h:52
A wrapper class of an application provided rpc method handler.
Definition: completion_queue.h:61
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
#define gpr_atm_no_barrier_fetch_add(p, delta)
Definition: atm_gcc_atomic.h:52
Codegen interface for grpc::Channel.
Definition: channel_interface.h:49
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:49
NextStatus
Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
Definition: completion_queue.h:105
The completion queue will have an associated pollset and there is no restriction on the type of file ...
Definition: grpc_types.h:577
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:26
Similar to GRPC_CQ_DEFAULT_POLLING except that the completion queues will not contain any 'listening ...
Definition: grpc_types.h:582
CompletionQueue()
Default constructor.
Definition: completion_queue.h:90
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:95
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:86
void InitialAvalanching()
Manage state of avalanching operations : completion queue tags that trigger other completion queue op...
Definition: completion_queue.h:164
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:63
Descriptor of an RPC method.
Definition: rpc_method.h:29
Interface between the codegen library and the minimal subset of core features required by the generat...
Definition: core_codegen_interface.h:41
A wrapper class of an application provided server streaming handler.
Definition: completion_queue.h:65
Did it work? If it didn't, why?
Definition: status.h:30
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:276
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
Definition: completion_queue.h:67
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:54
deadline was reached.
Definition: completion_queue.h:109
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