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 
19 #ifndef GRPC_SRC_CORE_LIB_SURFACE_COMPLETION_QUEUE_H
20 #define GRPC_SRC_CORE_LIB_SURFACE_COMPLETION_QUEUE_H
21 
22 // Internal API for completion queues
23 
24 #include <grpc/support/port_platform.h>
25 
26 #include <stdint.h>
27 
28 #include <grpc/grpc.h>
29 
30 #include "src/core/lib/debug/trace.h"
31 #include "src/core/lib/gprpp/manual_constructor.h"
32 #include "src/core/lib/gprpp/mpscq.h"
33 #include "src/core/lib/iomgr/error.h"
34 #include "src/core/lib/iomgr/iomgr_fwd.h"
35 
36 // These trace flags default to 1. The corresponding lines are only traced
37 // if grpc_api_trace is also truthy
38 extern grpc_core::TraceFlag grpc_cq_pluck_trace;
39 extern grpc_core::TraceFlag grpc_trace_operation_failures;
40 extern grpc_core::DebugOnlyTraceFlag grpc_trace_pending_tags;
41 extern grpc_core::DebugOnlyTraceFlag grpc_trace_cq_refcount;
42 
43 typedef struct grpc_cq_completion {
44   grpc_core::ManualConstructor<
45       grpc_core::MultiProducerSingleConsumerQueue::Node>
46       node;
47 
48   /// user supplied tag
49   void* tag;
50   /// done callback - called when this queue element is no longer
51   /// needed by the completion queue
52   void (*done)(void* done_arg, struct grpc_cq_completion* c);
53   void* done_arg;
54   /// next pointer; low bit is used to indicate success or not
55   uintptr_t next;
56 } grpc_cq_completion;
57 
58 #ifndef NDEBUG
59 void grpc_cq_internal_ref(grpc_completion_queue* cq, const char* reason,
60                           const char* file, int line);
61 void grpc_cq_internal_unref(grpc_completion_queue* cq, const char* reason,
62                             const char* file, int line);
63 #define GRPC_CQ_INTERNAL_REF(cq, reason) \
64   grpc_cq_internal_ref(cq, reason, __FILE__, __LINE__)
65 #define GRPC_CQ_INTERNAL_UNREF(cq, reason) \
66   grpc_cq_internal_unref(cq, reason, __FILE__, __LINE__)
67 #else
68 void grpc_cq_internal_ref(grpc_completion_queue* cq);
69 void grpc_cq_internal_unref(grpc_completion_queue* cq);
70 #define GRPC_CQ_INTERNAL_REF(cq, reason) grpc_cq_internal_ref(cq)
71 #define GRPC_CQ_INTERNAL_UNREF(cq, reason) grpc_cq_internal_unref(cq)
72 #endif
73 
74 // Flag that an operation is beginning: the completion channel will not finish
75 // shutdown until a corrensponding grpc_cq_end_* call is made.
76 // \a tag is currently used only in debug builds. Return true on success, and
77 // false if completion_queue has been shutdown.
78 bool grpc_cq_begin_op(grpc_completion_queue* cq, void* tag);
79 
80 // Queue a GRPC_OP_COMPLETED operation; tag must correspond to the tag passed to
81 // grpc_cq_begin_op
82 void grpc_cq_end_op(grpc_completion_queue* cq, void* tag,
83                     grpc_error_handle error,
84                     void (*done)(void* done_arg, grpc_cq_completion* storage),
85                     void* done_arg, grpc_cq_completion* storage,
86                     bool internal = false);
87 
88 grpc_pollset* grpc_cq_pollset(grpc_completion_queue* cq);
89 
90 bool grpc_cq_can_listen(grpc_completion_queue* cq);
91 
92 grpc_cq_completion_type grpc_get_cq_completion_type(grpc_completion_queue* cq);
93 
94 int grpc_get_cq_poll_num(grpc_completion_queue* cq);
95 
96 grpc_completion_queue* grpc_completion_queue_create_internal(
97     grpc_cq_completion_type completion_type, grpc_cq_polling_type polling_type,
98     grpc_completion_queue_functor* shutdown_callback);
99 
100 #endif  // GRPC_SRC_CORE_LIB_SURFACE_COMPLETION_QUEUE_H
101