3 * Copyright 2015-2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_LIB_SURFACE_COMPLETION_QUEUE_H
20 #define GRPC_CORE_LIB_SURFACE_COMPLETION_QUEUE_H
22 /* Internal API for completion queues */
24 #include <grpc/support/port_platform.h>
26 #include <grpc/grpc.h>
27 #include "src/core/lib/debug/trace.h"
28 #include "src/core/lib/gprpp/abstract.h"
29 #include "src/core/lib/iomgr/pollset.h"
31 /* These trace flags default to 1. The corresponding lines are only traced
32 if grpc_api_trace is also truthy */
33 extern grpc_core::TraceFlag grpc_cq_pluck_trace;
34 extern grpc_core::TraceFlag grpc_trace_operation_failures;
35 extern grpc_core::DebugOnlyTraceFlag grpc_trace_pending_tags;
36 extern grpc_core::DebugOnlyTraceFlag grpc_trace_cq_refcount;
38 typedef struct grpc_cq_completion {
41 /** user supplied tag */
43 /** done callback - called when this queue element is no longer
44 needed by the completion queue */
45 void (*done)(void* done_arg, struct grpc_cq_completion* c);
47 /** next pointer; low bit is used to indicate success or not */
52 void grpc_cq_internal_ref(grpc_completion_queue* cc, const char* reason,
53 const char* file, int line);
54 void grpc_cq_internal_unref(grpc_completion_queue* cc, const char* reason,
55 const char* file, int line);
56 #define GRPC_CQ_INTERNAL_REF(cc, reason) \
57 grpc_cq_internal_ref(cc, reason, __FILE__, __LINE__)
58 #define GRPC_CQ_INTERNAL_UNREF(cc, reason) \
59 grpc_cq_internal_unref(cc, reason, __FILE__, __LINE__)
61 void grpc_cq_internal_ref(grpc_completion_queue* cc);
62 void grpc_cq_internal_unref(grpc_completion_queue* cc);
63 #define GRPC_CQ_INTERNAL_REF(cc, reason) grpc_cq_internal_ref(cc)
64 #define GRPC_CQ_INTERNAL_UNREF(cc, reason) grpc_cq_internal_unref(cc)
67 /* Initializes global variables used by completion queues */
68 void grpc_cq_global_init();
70 /* Flag that an operation is beginning: the completion channel will not finish
71 shutdown until a corrensponding grpc_cq_end_* call is made.
72 \a tag is currently used only in debug builds. Return true on success, and
73 false if completion_queue has been shutdown. */
74 bool grpc_cq_begin_op(grpc_completion_queue* cc, void* tag);
76 /* Queue a GRPC_OP_COMPLETED operation; tag must correspond to the tag passed to
78 void grpc_cq_end_op(grpc_completion_queue* cc, void* tag, grpc_error* error,
79 void (*done)(void* done_arg, grpc_cq_completion* storage),
80 void* done_arg, grpc_cq_completion* storage,
81 bool internal = false);
83 grpc_pollset* grpc_cq_pollset(grpc_completion_queue* cc);
85 bool grpc_cq_can_listen(grpc_completion_queue* cc);
87 grpc_cq_completion_type grpc_get_cq_completion_type(grpc_completion_queue* cc);
89 int grpc_get_cq_poll_num(grpc_completion_queue* cc);
91 grpc_completion_queue* grpc_completion_queue_create_internal(
92 grpc_cq_completion_type completion_type, grpc_cq_polling_type polling_type,
93 grpc_experimental_completion_queue_functor* shutdown_callback);
95 #endif /* GRPC_CORE_LIB_SURFACE_COMPLETION_QUEUE_H */