Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015-2016 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 18 | #include "src/core/lib/surface/completion_queue.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 23 | #include <grpc/support/alloc.h> |
| 24 | #include <grpc/support/atm.h> |
| 25 | #include <grpc/support/log.h> |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 26 | #include <grpc/support/string_util.h> |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 27 | #include <grpc/support/time.h> |
| 28 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 29 | #include "src/core/lib/iomgr/pollset.h" |
| 30 | #include "src/core/lib/iomgr/timer.h" |
| 31 | #include "src/core/lib/profiling/timers.h" |
Sree Kuchibhotla | fe5f235 | 2017-04-18 13:46:10 -0700 | [diff] [blame] | 32 | #include "src/core/lib/support/spinlock.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 33 | #include "src/core/lib/support/string.h" |
| 34 | #include "src/core/lib/surface/api_trace.h" |
| 35 | #include "src/core/lib/surface/call.h" |
| 36 | #include "src/core/lib/surface/event_string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | |
Craig Tiller | 3b65436 | 2017-05-04 08:11:17 -0700 | [diff] [blame] | 38 | grpc_tracer_flag grpc_trace_operation_failures = GRPC_TRACER_INITIALIZER(false); |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 39 | #ifndef NDEBUG |
Craig Tiller | 3b65436 | 2017-05-04 08:11:17 -0700 | [diff] [blame] | 40 | grpc_tracer_flag grpc_trace_pending_tags = GRPC_TRACER_INITIALIZER(false); |
ncteisen | d39010e | 2017-06-08 17:08:07 -0700 | [diff] [blame] | 41 | grpc_tracer_flag grpc_trace_cq_refcount = GRPC_TRACER_INITIALIZER(false); |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 42 | #endif |
Craig Tiller | a286b04 | 2016-06-13 15:20:39 +0000 | [diff] [blame] | 43 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 44 | typedef struct { |
Craig Tiller | d0a8ae1 | 2016-02-18 08:01:19 -0800 | [diff] [blame] | 45 | grpc_pollset_worker **worker; |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 46 | void *tag; |
| 47 | } plucker; |
| 48 | |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 49 | typedef struct { |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 50 | bool can_get_pollset; |
Craig Tiller | 11c5832 | 2017-04-12 08:21:17 -0700 | [diff] [blame] | 51 | bool can_listen; |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 52 | size_t (*size)(void); |
| 53 | void (*init)(grpc_pollset *pollset, gpr_mu **mu); |
| 54 | grpc_error *(*kick)(grpc_pollset *pollset, |
| 55 | grpc_pollset_worker *specific_worker); |
| 56 | grpc_error *(*work)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 57 | grpc_pollset_worker **worker, gpr_timespec now, |
| 58 | gpr_timespec deadline); |
| 59 | void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 60 | grpc_closure *closure); |
Craig Tiller | 890b584 | 2017-04-24 19:33:53 +0000 | [diff] [blame] | 61 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset); |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 62 | } cq_poller_vtable; |
| 63 | |
| 64 | typedef struct non_polling_worker { |
| 65 | gpr_cv cv; |
| 66 | bool kicked; |
| 67 | struct non_polling_worker *next; |
| 68 | struct non_polling_worker *prev; |
| 69 | } non_polling_worker; |
| 70 | |
| 71 | typedef struct { |
| 72 | gpr_mu mu; |
| 73 | non_polling_worker *root; |
| 74 | grpc_closure *shutdown; |
| 75 | } non_polling_poller; |
| 76 | |
| 77 | static size_t non_polling_poller_size(void) { |
| 78 | return sizeof(non_polling_poller); |
| 79 | } |
| 80 | |
| 81 | static void non_polling_poller_init(grpc_pollset *pollset, gpr_mu **mu) { |
| 82 | non_polling_poller *npp = (non_polling_poller *)pollset; |
| 83 | gpr_mu_init(&npp->mu); |
| 84 | *mu = &npp->mu; |
| 85 | } |
| 86 | |
Craig Tiller | 956920d | 2017-04-24 13:09:12 -0700 | [diff] [blame] | 87 | static void non_polling_poller_destroy(grpc_exec_ctx *exec_ctx, |
| 88 | grpc_pollset *pollset) { |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 89 | non_polling_poller *npp = (non_polling_poller *)pollset; |
| 90 | gpr_mu_destroy(&npp->mu); |
| 91 | } |
| 92 | |
| 93 | static grpc_error *non_polling_poller_work(grpc_exec_ctx *exec_ctx, |
| 94 | grpc_pollset *pollset, |
| 95 | grpc_pollset_worker **worker, |
| 96 | gpr_timespec now, |
| 97 | gpr_timespec deadline) { |
| 98 | non_polling_poller *npp = (non_polling_poller *)pollset; |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 99 | if (npp->shutdown) return GRPC_ERROR_NONE; |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 100 | non_polling_worker w; |
| 101 | gpr_cv_init(&w.cv); |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 102 | if (worker != NULL) *worker = (grpc_pollset_worker *)&w; |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 103 | if (npp->root == NULL) { |
| 104 | npp->root = w.next = w.prev = &w; |
| 105 | } else { |
| 106 | w.next = npp->root; |
| 107 | w.prev = w.next->prev; |
| 108 | w.next->prev = w.prev->next = &w; |
| 109 | } |
| 110 | w.kicked = false; |
| 111 | while (!npp->shutdown && !w.kicked && !gpr_cv_wait(&w.cv, &npp->mu, deadline)) |
| 112 | ; |
| 113 | if (&w == npp->root) { |
| 114 | npp->root = w.next; |
| 115 | if (&w == npp->root) { |
| 116 | if (npp->shutdown) { |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 117 | GRPC_CLOSURE_SCHED(exec_ctx, npp->shutdown, GRPC_ERROR_NONE); |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 118 | } |
| 119 | npp->root = NULL; |
| 120 | } |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 121 | } |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 122 | w.next->prev = w.prev; |
| 123 | w.prev->next = w.next; |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 124 | gpr_cv_destroy(&w.cv); |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 125 | if (worker != NULL) *worker = NULL; |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 126 | return GRPC_ERROR_NONE; |
| 127 | } |
| 128 | |
| 129 | static grpc_error *non_polling_poller_kick( |
| 130 | grpc_pollset *pollset, grpc_pollset_worker *specific_worker) { |
| 131 | non_polling_poller *p = (non_polling_poller *)pollset; |
| 132 | if (specific_worker == NULL) specific_worker = (grpc_pollset_worker *)p->root; |
| 133 | if (specific_worker != NULL) { |
| 134 | non_polling_worker *w = (non_polling_worker *)specific_worker; |
| 135 | if (!w->kicked) { |
| 136 | w->kicked = true; |
| 137 | gpr_cv_signal(&w->cv); |
| 138 | } |
| 139 | } |
| 140 | return GRPC_ERROR_NONE; |
| 141 | } |
| 142 | |
| 143 | static void non_polling_poller_shutdown(grpc_exec_ctx *exec_ctx, |
| 144 | grpc_pollset *pollset, |
| 145 | grpc_closure *closure) { |
| 146 | non_polling_poller *p = (non_polling_poller *)pollset; |
| 147 | GPR_ASSERT(closure != NULL); |
| 148 | p->shutdown = closure; |
| 149 | if (p->root == NULL) { |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 150 | GRPC_CLOSURE_SCHED(exec_ctx, closure, GRPC_ERROR_NONE); |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 151 | } else { |
| 152 | non_polling_worker *w = p->root; |
| 153 | do { |
| 154 | gpr_cv_signal(&w->cv); |
| 155 | w = w->next; |
| 156 | } while (w != p->root); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static const cq_poller_vtable g_poller_vtable_by_poller_type[] = { |
| 161 | /* GRPC_CQ_DEFAULT_POLLING */ |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 162 | {.can_get_pollset = true, |
Craig Tiller | 11c5832 | 2017-04-12 08:21:17 -0700 | [diff] [blame] | 163 | .can_listen = true, |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 164 | .size = grpc_pollset_size, |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 165 | .init = grpc_pollset_init, |
| 166 | .kick = grpc_pollset_kick, |
| 167 | .work = grpc_pollset_work, |
| 168 | .shutdown = grpc_pollset_shutdown, |
| 169 | .destroy = grpc_pollset_destroy}, |
| 170 | /* GRPC_CQ_NON_LISTENING */ |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 171 | {.can_get_pollset = true, |
Craig Tiller | 11c5832 | 2017-04-12 08:21:17 -0700 | [diff] [blame] | 172 | .can_listen = false, |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 173 | .size = grpc_pollset_size, |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 174 | .init = grpc_pollset_init, |
| 175 | .kick = grpc_pollset_kick, |
| 176 | .work = grpc_pollset_work, |
| 177 | .shutdown = grpc_pollset_shutdown, |
| 178 | .destroy = grpc_pollset_destroy}, |
| 179 | /* GRPC_CQ_NON_POLLING */ |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 180 | {.can_get_pollset = false, |
Craig Tiller | 11c5832 | 2017-04-12 08:21:17 -0700 | [diff] [blame] | 181 | .can_listen = false, |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 182 | .size = non_polling_poller_size, |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 183 | .init = non_polling_poller_init, |
| 184 | .kick = non_polling_poller_kick, |
| 185 | .work = non_polling_poller_work, |
| 186 | .shutdown = non_polling_poller_shutdown, |
| 187 | .destroy = non_polling_poller_destroy}, |
| 188 | }; |
| 189 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 190 | typedef struct cq_vtable { |
| 191 | grpc_cq_completion_type cq_completion_type; |
| 192 | size_t (*size)(); |
| 193 | void (*begin_op)(grpc_completion_queue *cc, void *tag); |
| 194 | void (*end_op)(grpc_exec_ctx *exec_ctx, grpc_completion_queue *cc, void *tag, |
| 195 | grpc_error *error, |
| 196 | void (*done)(grpc_exec_ctx *exec_ctx, void *done_arg, |
| 197 | grpc_cq_completion *storage), |
| 198 | void *done_arg, grpc_cq_completion *storage); |
| 199 | grpc_event (*next)(grpc_completion_queue *cc, gpr_timespec deadline, |
| 200 | void *reserved); |
| 201 | grpc_event (*pluck)(grpc_completion_queue *cc, void *tag, |
| 202 | gpr_timespec deadline, void *reserved); |
| 203 | } cq_vtable; |
| 204 | |
| 205 | /* Queue that holds the cq_completion_events. Internally uses gpr_mpscq queue |
| 206 | * (a lockfree multiproducer single consumer queue). It uses a queue_lock |
| 207 | * to support multiple consumers. |
| 208 | * Only used in completion queues whose completion_type is GRPC_CQ_NEXT */ |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 209 | typedef struct grpc_cq_event_queue { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 210 | /* Spinlock to serialize consumers i.e pop() operations */ |
Sree Kuchibhotla | fe5f235 | 2017-04-18 13:46:10 -0700 | [diff] [blame] | 211 | gpr_spinlock queue_lock; |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 212 | |
| 213 | gpr_mpscq queue; |
| 214 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 215 | /* A lazy counter of number of items in the queue. This is NOT atomically |
| 216 | incremented/decremented along with push/pop operations and hence is only |
| 217 | eventually consistent */ |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 218 | gpr_atm num_queue_items; |
| 219 | } grpc_cq_event_queue; |
| 220 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 221 | /* TODO: sreek Refactor this based on the completion_type. Put completion-type |
| 222 | * specific data in a different structure (and co-allocate memory for it along |
| 223 | * with completion queue + pollset )*/ |
| 224 | typedef struct cq_data { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 225 | gpr_mu *mu; |
Sree Kuchibhotla | 7a4e5b4 | 2017-02-17 09:28:46 -0800 | [diff] [blame] | 226 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 227 | /** Completed events for completion-queues of type GRPC_CQ_PLUCK */ |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 228 | grpc_cq_completion completed_head; |
| 229 | grpc_cq_completion *completed_tail; |
Sree Kuchibhotla | 5461a8b | 2017-04-10 09:52:40 -0700 | [diff] [blame] | 230 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 231 | /** Completed events for completion-queues of type GRPC_CQ_NEXT */ |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 232 | grpc_cq_event_queue queue; |
Sree Kuchibhotla | 5461a8b | 2017-04-10 09:52:40 -0700 | [diff] [blame] | 233 | |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 234 | /** Number of pending events (+1 if we're not shutdown) */ |
| 235 | gpr_refcount pending_events; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 236 | |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 237 | /** Once owning_refs drops to zero, we will destroy the cq */ |
Craig Tiller | 5717a98 | 2015-04-27 12:01:49 -0700 | [diff] [blame] | 238 | gpr_refcount owning_refs; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 239 | |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 240 | /** Counter of how many things have ever been queued on this completion queue |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 241 | useful for avoiding locks to check the queue */ |
| 242 | gpr_atm things_queued_ever; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 243 | |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 244 | /** 0 initially, 1 once we've begun shutting down */ |
Sree Kuchibhotla | 94aff9e | 2017-04-10 10:25:03 -0700 | [diff] [blame] | 245 | gpr_atm shutdown; |
Craig Tiller | f5fd4ba | 2015-03-02 18:01:21 +0000 | [diff] [blame] | 246 | int shutdown_called; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 247 | |
Craig Tiller | b56975c | 2015-06-15 10:11:16 -0700 | [diff] [blame] | 248 | int is_server_cq; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 249 | |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 250 | int num_pluckers; |
Yuxuan Li | 87827e0 | 2017-05-05 14:12:42 -0700 | [diff] [blame] | 251 | int num_polls; |
Craig Tiller | 489df07 | 2015-08-01 16:15:45 -0700 | [diff] [blame] | 252 | plucker pluckers[GRPC_MAX_COMPLETION_QUEUE_PLUCKERS]; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 253 | grpc_closure pollset_shutdown_done; |
| 254 | |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 255 | #ifndef NDEBUG |
| 256 | void **outstanding_tags; |
| 257 | size_t outstanding_tag_count; |
| 258 | size_t outstanding_tag_capacity; |
| 259 | #endif |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 260 | } cq_data; |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 261 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 262 | /* Completion queue structure */ |
| 263 | struct grpc_completion_queue { |
| 264 | cq_data data; |
| 265 | const cq_vtable *vtable; |
| 266 | const cq_poller_vtable *poller_vtable; |
| 267 | }; |
| 268 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 269 | /* Forward declarations */ |
| 270 | static void cq_finish_shutdown(grpc_exec_ctx *exec_ctx, |
| 271 | grpc_completion_queue *cc); |
| 272 | |
| 273 | static size_t cq_size(grpc_completion_queue *cc); |
| 274 | |
| 275 | static void cq_begin_op(grpc_completion_queue *cc, void *tag); |
| 276 | |
| 277 | static void cq_end_op_for_next(grpc_exec_ctx *exec_ctx, |
| 278 | grpc_completion_queue *cc, void *tag, |
| 279 | grpc_error *error, |
| 280 | void (*done)(grpc_exec_ctx *exec_ctx, |
| 281 | void *done_arg, |
| 282 | grpc_cq_completion *storage), |
| 283 | void *done_arg, grpc_cq_completion *storage); |
| 284 | |
| 285 | static void cq_end_op_for_pluck(grpc_exec_ctx *exec_ctx, |
| 286 | grpc_completion_queue *cc, void *tag, |
| 287 | grpc_error *error, |
| 288 | void (*done)(grpc_exec_ctx *exec_ctx, |
| 289 | void *done_arg, |
| 290 | grpc_cq_completion *storage), |
| 291 | void *done_arg, grpc_cq_completion *storage); |
| 292 | |
| 293 | static grpc_event cq_next(grpc_completion_queue *cc, gpr_timespec deadline, |
| 294 | void *reserved); |
| 295 | |
| 296 | static grpc_event cq_pluck(grpc_completion_queue *cc, void *tag, |
| 297 | gpr_timespec deadline, void *reserved); |
| 298 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 299 | /* Completion queue vtables based on the completion-type */ |
| 300 | static const cq_vtable g_cq_vtable[] = { |
| 301 | /* GRPC_CQ_NEXT */ |
| 302 | {.cq_completion_type = GRPC_CQ_NEXT, |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 303 | .size = cq_size, |
| 304 | .begin_op = cq_begin_op, |
| 305 | .end_op = cq_end_op_for_next, |
| 306 | .next = cq_next, |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 307 | .pluck = NULL}, |
| 308 | /* GRPC_CQ_PLUCK */ |
| 309 | {.cq_completion_type = GRPC_CQ_PLUCK, |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 310 | .size = cq_size, |
| 311 | .begin_op = cq_begin_op, |
| 312 | .end_op = cq_end_op_for_pluck, |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 313 | .next = NULL, |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 314 | .pluck = cq_pluck}, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 315 | }; |
| 316 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 317 | #define POLLSET_FROM_CQ(cq) ((grpc_pollset *)(cq + 1)) |
Craig Tiller | 40945c7 | 2016-05-16 17:15:24 -0700 | [diff] [blame] | 318 | #define CQ_FROM_POLLSET(ps) (((grpc_completion_queue *)ps) - 1) |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 319 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 320 | grpc_tracer_flag grpc_cq_pluck_trace = GRPC_TRACER_INITIALIZER(true); |
| 321 | grpc_tracer_flag grpc_cq_event_timeout_trace = GRPC_TRACER_INITIALIZER(true); |
murgatroid99 | 9f6a805 | 2016-06-15 17:39:03 -0700 | [diff] [blame] | 322 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 323 | #define GRPC_SURFACE_TRACE_RETURNED_EVENT(cq, event) \ |
| 324 | if (GRPC_TRACER_ON(grpc_api_trace) && \ |
| 325 | (GRPC_TRACER_ON(grpc_cq_pluck_trace) || \ |
| 326 | (event)->type != GRPC_QUEUE_TIMEOUT)) { \ |
| 327 | char *_ev = grpc_event_string(event); \ |
| 328 | gpr_log(GPR_INFO, "RETURN_EVENT[%p]: %s", cq, _ev); \ |
| 329 | gpr_free(_ev); \ |
murgatroid99 | 9f6a805 | 2016-06-15 17:39:03 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 332 | static void on_pollset_shutdown_done(grpc_exec_ctx *exec_ctx, void *cc, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 333 | grpc_error *error); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 334 | |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 335 | static void cq_event_queue_init(grpc_cq_event_queue *q) { |
| 336 | gpr_mpscq_init(&q->queue); |
Sree Kuchibhotla | fe5f235 | 2017-04-18 13:46:10 -0700 | [diff] [blame] | 337 | q->queue_lock = GPR_SPINLOCK_INITIALIZER; |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 338 | gpr_atm_no_barrier_store(&q->num_queue_items, 0); |
| 339 | } |
| 340 | |
| 341 | static void cq_event_queue_destroy(grpc_cq_event_queue *q) { |
| 342 | gpr_mpscq_destroy(&q->queue); |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static void cq_event_queue_push(grpc_cq_event_queue *q, grpc_cq_completion *c) { |
| 346 | gpr_mpscq_push(&q->queue, (gpr_mpscq_node *)c); |
| 347 | gpr_atm_no_barrier_fetch_add(&q->num_queue_items, 1); |
| 348 | } |
| 349 | |
| 350 | static grpc_cq_completion *cq_event_queue_pop(grpc_cq_event_queue *q) { |
Sree Kuchibhotla | fe5f235 | 2017-04-18 13:46:10 -0700 | [diff] [blame] | 351 | grpc_cq_completion *c = NULL; |
| 352 | if (gpr_spinlock_trylock(&q->queue_lock)) { |
| 353 | c = (grpc_cq_completion *)gpr_mpscq_pop(&q->queue); |
| 354 | gpr_spinlock_unlock(&q->queue_lock); |
| 355 | } |
| 356 | |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 357 | if (c) { |
| 358 | gpr_atm_no_barrier_fetch_add(&q->num_queue_items, -1); |
| 359 | } |
| 360 | |
| 361 | return c; |
| 362 | } |
| 363 | |
| 364 | /* Note: The counter is not incremented/decremented atomically with push/pop. |
| 365 | * The count is only eventually consistent */ |
| 366 | static long cq_event_queue_num_items(grpc_cq_event_queue *q) { |
Sree Kuchibhotla | fe5f235 | 2017-04-18 13:46:10 -0700 | [diff] [blame] | 367 | return (long)gpr_atm_no_barrier_load(&q->num_queue_items); |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 370 | static size_t cq_size(grpc_completion_queue *cc) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 371 | /* Size of the completion queue and the size of the pollset whose memory is |
| 372 | allocated right after that of completion queue */ |
| 373 | return sizeof(grpc_completion_queue) + cc->poller_vtable->size(); |
| 374 | } |
| 375 | |
Sree Kuchibhotla | 2abbf8a | 2017-03-21 17:31:03 -0700 | [diff] [blame] | 376 | grpc_completion_queue *grpc_completion_queue_create_internal( |
| 377 | grpc_cq_completion_type completion_type, |
| 378 | grpc_cq_polling_type polling_type) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 379 | grpc_completion_queue *cc; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 380 | |
Sree Kuchibhotla | 2abbf8a | 2017-03-21 17:31:03 -0700 | [diff] [blame] | 381 | GPR_TIMER_BEGIN("grpc_completion_queue_create_internal", 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 382 | |
Sree Kuchibhotla | 2abbf8a | 2017-03-21 17:31:03 -0700 | [diff] [blame] | 383 | GRPC_API_TRACE( |
| 384 | "grpc_completion_queue_create_internal(completion_type=%d, " |
| 385 | "polling_type=%d)", |
| 386 | 2, (completion_type, polling_type)); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 387 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 388 | const cq_vtable *vtable = &g_cq_vtable[completion_type]; |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 389 | const cq_poller_vtable *poller_vtable = |
| 390 | &g_poller_vtable_by_poller_type[polling_type]; |
| 391 | |
| 392 | cc = gpr_zalloc(sizeof(grpc_completion_queue) + poller_vtable->size()); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 393 | cq_data *cqd = &cc->data; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 394 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 395 | cc->vtable = vtable; |
Craig Tiller | 2f40ff4 | 2017-04-11 16:01:19 -0700 | [diff] [blame] | 396 | cc->poller_vtable = poller_vtable; |
Sree Kuchibhotla | 321881d | 2017-02-27 11:25:28 -0800 | [diff] [blame] | 397 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 398 | poller_vtable->init(POLLSET_FROM_CQ(cc), &cc->data.mu); |
| 399 | |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 400 | #ifndef NDEBUG |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 401 | cqd->outstanding_tags = NULL; |
| 402 | cqd->outstanding_tag_capacity = 0; |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 403 | #endif |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 404 | |
| 405 | /* Initial ref is dropped by grpc_completion_queue_shutdown */ |
| 406 | gpr_ref_init(&cqd->pending_events, 1); |
| 407 | /* One for destroy(), one for pollset_shutdown */ |
| 408 | gpr_ref_init(&cqd->owning_refs, 2); |
| 409 | cqd->completed_tail = &cqd->completed_head; |
| 410 | cqd->completed_head.next = (uintptr_t)cqd->completed_tail; |
| 411 | gpr_atm_no_barrier_store(&cqd->shutdown, 0); |
| 412 | cqd->shutdown_called = 0; |
| 413 | cqd->is_server_cq = 0; |
| 414 | cqd->num_pluckers = 0; |
Yuxuan Li | 6ca796d | 2017-05-15 14:15:34 -0700 | [diff] [blame] | 415 | cqd->num_polls = 0; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 416 | gpr_atm_no_barrier_store(&cqd->things_queued_ever, 0); |
| 417 | #ifndef NDEBUG |
| 418 | cqd->outstanding_tag_count = 0; |
| 419 | #endif |
| 420 | cq_event_queue_init(&cqd->queue); |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 421 | GRPC_CLOSURE_INIT(&cqd->pollset_shutdown_done, on_pollset_shutdown_done, cc, |
Craig Tiller | d465456 | 2017-01-03 08:45:56 -0800 | [diff] [blame] | 422 | grpc_schedule_on_exec_ctx); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 423 | |
Sree Kuchibhotla | 2abbf8a | 2017-03-21 17:31:03 -0700 | [diff] [blame] | 424 | GPR_TIMER_END("grpc_completion_queue_create_internal", 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 425 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 426 | return cc; |
| 427 | } |
| 428 | |
Sree Kuchibhotla | 321881d | 2017-02-27 11:25:28 -0800 | [diff] [blame] | 429 | grpc_cq_completion_type grpc_get_cq_completion_type(grpc_completion_queue *cc) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 430 | return cc->vtable->cq_completion_type; |
Sree Kuchibhotla | 321881d | 2017-02-27 11:25:28 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Yuxuan Li | 85d3a53 | 2017-05-08 06:18:52 +0000 | [diff] [blame] | 433 | int grpc_get_cq_poll_num(grpc_completion_queue *cc) { |
Yuxuan Li | 87827e0 | 2017-05-05 14:12:42 -0700 | [diff] [blame] | 434 | int cur_num_polls; |
Yuxuan Li | 6ca796d | 2017-05-15 14:15:34 -0700 | [diff] [blame] | 435 | gpr_mu_lock(cc->data.mu); |
| 436 | cur_num_polls = cc->data.num_polls; |
| 437 | gpr_mu_unlock(cc->data.mu); |
Yuxuan Li | 87827e0 | 2017-05-05 14:12:42 -0700 | [diff] [blame] | 438 | return cur_num_polls; |
Yuxuan Li | 999ac15 | 2017-05-03 21:36:36 -0700 | [diff] [blame] | 439 | } |
| 440 | |
ncteisen | d39010e | 2017-06-08 17:08:07 -0700 | [diff] [blame] | 441 | #ifndef NDEBUG |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 442 | void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason, |
| 443 | const char *file, int line) { |
Sree Kuchibhotla | 60b282e | 2017-04-25 12:57:29 -0700 | [diff] [blame] | 444 | cq_data *cqd = &cc->data; |
ncteisen | d39010e | 2017-06-08 17:08:07 -0700 | [diff] [blame] | 445 | if (GRPC_TRACER_ON(grpc_trace_cq_refcount)) { |
| 446 | gpr_atm val = gpr_atm_no_barrier_load(&cqd->owning_refs.count); |
ncteisen | 3bc2558 | 2017-06-09 10:35:35 -0700 | [diff] [blame^] | 447 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 448 | "CQ:%p ref %" PRIdPTR " -> %" PRIdPTR " %s", cc, val, val + 1, |
| 449 | reason); |
ncteisen | d39010e | 2017-06-08 17:08:07 -0700 | [diff] [blame] | 450 | } |
Craig Tiller | 463f237 | 2015-05-28 16:16:15 -0700 | [diff] [blame] | 451 | #else |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 452 | void grpc_cq_internal_ref(grpc_completion_queue *cc) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 453 | cq_data *cqd = &cc->data; |
Craig Tiller | 463f237 | 2015-05-28 16:16:15 -0700 | [diff] [blame] | 454 | #endif |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 455 | gpr_ref(&cqd->owning_refs); |
Craig Tiller | 5717a98 | 2015-04-27 12:01:49 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 458 | static void on_pollset_shutdown_done(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 459 | grpc_error *error) { |
Craig Tiller | 5717a98 | 2015-04-27 12:01:49 -0700 | [diff] [blame] | 460 | grpc_completion_queue *cc = arg; |
Craig Tiller | f840110 | 2017-04-17 09:47:28 -0700 | [diff] [blame] | 461 | GRPC_CQ_INTERNAL_UNREF(exec_ctx, cc, "pollset_destroy"); |
Craig Tiller | 5717a98 | 2015-04-27 12:01:49 -0700 | [diff] [blame] | 462 | } |
| 463 | |
ncteisen | d39010e | 2017-06-08 17:08:07 -0700 | [diff] [blame] | 464 | #ifndef NDEBUG |
ncteisen | 3bc2558 | 2017-06-09 10:35:35 -0700 | [diff] [blame^] | 465 | void grpc_cq_internal_unref(grpc_exec_ctx *exec_ctx, grpc_completion_queue *cc, |
| 466 | const char *reason, const char *file, int line) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 467 | cq_data *cqd = &cc->data; |
ncteisen | d39010e | 2017-06-08 17:08:07 -0700 | [diff] [blame] | 468 | if (GRPC_TRACER_ON(grpc_trace_cq_refcount)) { |
| 469 | gpr_atm val = gpr_atm_no_barrier_load(&cqd->owning_refs.count); |
ncteisen | 3bc2558 | 2017-06-09 10:35:35 -0700 | [diff] [blame^] | 470 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 471 | "CQ:%p unref %" PRIdPTR " -> %" PRIdPTR " %s", cc, val, val - 1, |
| 472 | reason); |
ncteisen | d39010e | 2017-06-08 17:08:07 -0700 | [diff] [blame] | 473 | } |
Craig Tiller | 463f237 | 2015-05-28 16:16:15 -0700 | [diff] [blame] | 474 | #else |
Craig Tiller | f840110 | 2017-04-17 09:47:28 -0700 | [diff] [blame] | 475 | void grpc_cq_internal_unref(grpc_exec_ctx *exec_ctx, |
| 476 | grpc_completion_queue *cc) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 477 | cq_data *cqd = &cc->data; |
Craig Tiller | 463f237 | 2015-05-28 16:16:15 -0700 | [diff] [blame] | 478 | #endif |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 479 | if (gpr_unref(&cqd->owning_refs)) { |
| 480 | GPR_ASSERT(cqd->completed_head.next == (uintptr_t)&cqd->completed_head); |
Craig Tiller | 890b584 | 2017-04-24 19:33:53 +0000 | [diff] [blame] | 481 | cc->poller_vtable->destroy(exec_ctx, POLLSET_FROM_CQ(cc)); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 482 | cq_event_queue_destroy(&cqd->queue); |
Craig Tiller | 07e8fb1 | 2017-02-09 21:36:24 -0800 | [diff] [blame] | 483 | #ifndef NDEBUG |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 484 | gpr_free(cqd->outstanding_tags); |
Craig Tiller | 07e8fb1 | 2017-02-09 21:36:24 -0800 | [diff] [blame] | 485 | #endif |
| 486 | gpr_free(cc); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 487 | } |
Craig Tiller | 5717a98 | 2015-04-27 12:01:49 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 490 | static void cq_begin_op(grpc_completion_queue *cc, void *tag) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 491 | cq_data *cqd = &cc->data; |
Craig Tiller | 402acf6 | 2015-08-05 10:43:10 -0700 | [diff] [blame] | 492 | #ifndef NDEBUG |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 493 | gpr_mu_lock(cqd->mu); |
| 494 | GPR_ASSERT(!cqd->shutdown_called); |
| 495 | if (cqd->outstanding_tag_count == cqd->outstanding_tag_capacity) { |
| 496 | cqd->outstanding_tag_capacity = |
| 497 | GPR_MAX(4, 2 * cqd->outstanding_tag_capacity); |
| 498 | cqd->outstanding_tags = |
| 499 | gpr_realloc(cqd->outstanding_tags, sizeof(*cqd->outstanding_tags) * |
| 500 | cqd->outstanding_tag_capacity); |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 501 | } |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 502 | cqd->outstanding_tags[cqd->outstanding_tag_count++] = tag; |
| 503 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | 402acf6 | 2015-08-05 10:43:10 -0700 | [diff] [blame] | 504 | #endif |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 505 | gpr_ref(&cqd->pending_events); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 508 | void grpc_cq_begin_op(grpc_completion_queue *cc, void *tag) { |
| 509 | cc->vtable->begin_op(cc, tag); |
| 510 | } |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 511 | |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 512 | #ifndef NDEBUG |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 513 | static void cq_check_tag(grpc_completion_queue *cc, void *tag, bool lock_cq) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 514 | cq_data *cqd = &cc->data; |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 515 | int found = 0; |
| 516 | if (lock_cq) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 517 | gpr_mu_lock(cqd->mu); |
Craig Tiller | cfc8ae1 | 2016-05-10 20:49:54 -0700 | [diff] [blame] | 518 | } |
Craig Tiller | ce1f19e | 2015-10-09 16:16:04 -0700 | [diff] [blame] | 519 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 520 | for (int i = 0; i < (int)cqd->outstanding_tag_count; i++) { |
| 521 | if (cqd->outstanding_tags[i] == tag) { |
| 522 | cqd->outstanding_tag_count--; |
| 523 | GPR_SWAP(void *, cqd->outstanding_tags[i], |
| 524 | cqd->outstanding_tags[cqd->outstanding_tag_count]); |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 525 | found = 1; |
| 526 | break; |
| 527 | } |
| 528 | } |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 529 | |
| 530 | if (lock_cq) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 531 | gpr_mu_unlock(cqd->mu); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 534 | GPR_ASSERT(found); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 535 | } |
| 536 | #else |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 537 | static void cq_check_tag(grpc_completion_queue *cc, void *tag, bool lock_cq) {} |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 538 | #endif |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 539 | |
| 540 | /* Queue a GRPC_OP_COMPLETED operation to a completion queue (with a completion |
| 541 | * type of GRPC_CQ_NEXT) */ |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 542 | static void cq_end_op_for_next(grpc_exec_ctx *exec_ctx, |
| 543 | grpc_completion_queue *cc, void *tag, |
| 544 | grpc_error *error, |
| 545 | void (*done)(grpc_exec_ctx *exec_ctx, |
| 546 | void *done_arg, |
| 547 | grpc_cq_completion *storage), |
| 548 | void *done_arg, grpc_cq_completion *storage) { |
| 549 | GPR_TIMER_BEGIN("cq_end_op_for_next", 0); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 550 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 551 | if (GRPC_TRACER_ON(grpc_api_trace) || |
| 552 | (GRPC_TRACER_ON(grpc_trace_operation_failures) && |
| 553 | error != GRPC_ERROR_NONE)) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 554 | const char *errmsg = grpc_error_string(error); |
| 555 | GRPC_API_TRACE( |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 556 | "cq_end_op_for_next(exec_ctx=%p, cc=%p, tag=%p, error=%s, " |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 557 | "done=%p, done_arg=%p, storage=%p)", |
| 558 | 7, (exec_ctx, cc, tag, errmsg, done, done_arg, storage)); |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 559 | if (GRPC_TRACER_ON(grpc_trace_operation_failures) && |
| 560 | error != GRPC_ERROR_NONE) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 561 | gpr_log(GPR_ERROR, "Operation failed: tag=%p, error=%s", tag, errmsg); |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 562 | } |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | cq_data *cqd = &cc->data; |
| 566 | int is_success = (error == GRPC_ERROR_NONE); |
| 567 | |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 568 | storage->tag = tag; |
| 569 | storage->done = done; |
| 570 | storage->done_arg = done_arg; |
Sree Kuchibhotla | 453c611 | 2017-04-11 23:25:42 -0700 | [diff] [blame] | 571 | storage->next = (uintptr_t)(is_success); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 572 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 573 | cq_check_tag(cc, tag, true); /* Used in debug builds only */ |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 574 | |
| 575 | /* Add the completion to the queue */ |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 576 | cq_event_queue_push(&cqd->queue, storage); |
| 577 | gpr_atm_no_barrier_fetch_add(&cqd->things_queued_ever, 1); |
Sree Kuchibhotla | 5461a8b | 2017-04-10 09:52:40 -0700 | [diff] [blame] | 578 | |
Sree Kuchibhotla | 8e36845 | 2017-04-25 15:45:02 -0700 | [diff] [blame] | 579 | gpr_mu_lock(cqd->mu); |
yang-g | 67cda0a | 2017-05-17 17:23:47 -0700 | [diff] [blame] | 580 | |
| 581 | int shutdown = gpr_unref(&cqd->pending_events); |
Sree Kuchibhotla | 8e36845 | 2017-04-25 15:45:02 -0700 | [diff] [blame] | 582 | if (!shutdown) { |
Sree Kuchibhotla | a72d79b | 2017-04-24 19:45:20 -0700 | [diff] [blame] | 583 | grpc_error *kick_error = cc->poller_vtable->kick(POLLSET_FROM_CQ(cc), NULL); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 584 | gpr_mu_unlock(cqd->mu); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 585 | |
Craig Tiller | 4f1d0f3 | 2016-05-06 17:12:37 -0700 | [diff] [blame] | 586 | if (kick_error != GRPC_ERROR_NONE) { |
| 587 | const char *msg = grpc_error_string(kick_error); |
| 588 | gpr_log(GPR_ERROR, "Kick failed: %s", msg); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 589 | |
Craig Tiller | 4f1d0f3 | 2016-05-06 17:12:37 -0700 | [diff] [blame] | 590 | GRPC_ERROR_UNREF(kick_error); |
| 591 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 592 | } else { |
Sree Kuchibhotla | 8e36845 | 2017-04-25 15:45:02 -0700 | [diff] [blame] | 593 | cq_finish_shutdown(exec_ctx, cc); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 594 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 595 | } |
Craig Tiller | ce1f19e | 2015-10-09 16:16:04 -0700 | [diff] [blame] | 596 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 597 | GPR_TIMER_END("cq_end_op_for_next", 0); |
Craig Tiller | cae4b1b | 2016-05-10 09:11:09 -0700 | [diff] [blame] | 598 | |
| 599 | GRPC_ERROR_UNREF(error); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 602 | /* Queue a GRPC_OP_COMPLETED operation to a completion queue (with a completion |
| 603 | * type of GRPC_CQ_PLUCK) */ |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 604 | static void cq_end_op_for_pluck(grpc_exec_ctx *exec_ctx, |
| 605 | grpc_completion_queue *cc, void *tag, |
| 606 | grpc_error *error, |
| 607 | void (*done)(grpc_exec_ctx *exec_ctx, |
| 608 | void *done_arg, |
| 609 | grpc_cq_completion *storage), |
| 610 | void *done_arg, grpc_cq_completion *storage) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 611 | cq_data *cqd = &cc->data; |
| 612 | int is_success = (error == GRPC_ERROR_NONE); |
| 613 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 614 | GPR_TIMER_BEGIN("cq_end_op_for_pluck", 0); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 615 | |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 616 | if (GRPC_TRACER_ON(grpc_api_trace) || |
| 617 | (GRPC_TRACER_ON(grpc_trace_operation_failures) && |
| 618 | error != GRPC_ERROR_NONE)) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 619 | const char *errmsg = grpc_error_string(error); |
| 620 | GRPC_API_TRACE( |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 621 | "cq_end_op_for_pluck(exec_ctx=%p, cc=%p, tag=%p, error=%s, " |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 622 | "done=%p, done_arg=%p, storage=%p)", |
| 623 | 7, (exec_ctx, cc, tag, errmsg, done, done_arg, storage)); |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 624 | if (GRPC_TRACER_ON(grpc_trace_operation_failures) && |
| 625 | error != GRPC_ERROR_NONE) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 626 | gpr_log(GPR_ERROR, "Operation failed: tag=%p, error=%s", tag, errmsg); |
| 627 | } |
| 628 | } |
| 629 | |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 630 | storage->tag = tag; |
| 631 | storage->done = done; |
| 632 | storage->done_arg = done_arg; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 633 | storage->next = ((uintptr_t)&cqd->completed_head) | ((uintptr_t)(is_success)); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 634 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 635 | gpr_mu_lock(cqd->mu); |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 636 | cq_check_tag(cc, tag, false); /* Used in debug builds only */ |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 637 | |
| 638 | /* Add to the list of completions */ |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 639 | gpr_atm_no_barrier_fetch_add(&cqd->things_queued_ever, 1); |
| 640 | cqd->completed_tail->next = |
| 641 | ((uintptr_t)storage) | (1u & (uintptr_t)cqd->completed_tail->next); |
| 642 | cqd->completed_tail = storage; |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 643 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 644 | int shutdown = gpr_unref(&cqd->pending_events); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 645 | if (!shutdown) { |
| 646 | grpc_pollset_worker *pluck_worker = NULL; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 647 | for (int i = 0; i < cqd->num_pluckers; i++) { |
| 648 | if (cqd->pluckers[i].tag == tag) { |
| 649 | pluck_worker = *cqd->pluckers[i].worker; |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | grpc_error *kick_error = |
Sree Kuchibhotla | a72d79b | 2017-04-24 19:45:20 -0700 | [diff] [blame] | 655 | cc->poller_vtable->kick(POLLSET_FROM_CQ(cc), pluck_worker); |
| 656 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 657 | gpr_mu_unlock(cqd->mu); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 658 | |
| 659 | if (kick_error != GRPC_ERROR_NONE) { |
| 660 | const char *msg = grpc_error_string(kick_error); |
| 661 | gpr_log(GPR_ERROR, "Kick failed: %s", msg); |
| 662 | |
| 663 | GRPC_ERROR_UNREF(kick_error); |
| 664 | } |
Sree Kuchibhotla | 5461a8b | 2017-04-10 09:52:40 -0700 | [diff] [blame] | 665 | } else { |
Sree Kuchibhotla | 8e36845 | 2017-04-25 15:45:02 -0700 | [diff] [blame] | 666 | cq_finish_shutdown(exec_ctx, cc); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 667 | gpr_mu_unlock(cqd->mu); |
Sree Kuchibhotla | 5461a8b | 2017-04-10 09:52:40 -0700 | [diff] [blame] | 668 | } |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 669 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 670 | GPR_TIMER_END("cq_end_op_for_pluck", 0); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 671 | |
| 672 | GRPC_ERROR_UNREF(error); |
Sree Kuchibhotla | 5461a8b | 2017-04-10 09:52:40 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 675 | void grpc_cq_end_op(grpc_exec_ctx *exec_ctx, grpc_completion_queue *cc, |
| 676 | void *tag, grpc_error *error, |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 677 | void (*done)(grpc_exec_ctx *exec_ctx, void *done_arg, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 678 | grpc_cq_completion *storage), |
| 679 | void *done_arg, grpc_cq_completion *storage) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 680 | cc->vtable->end_op(exec_ctx, cc, tag, error, done, done_arg, storage); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 681 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 682 | |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 683 | typedef struct { |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 684 | gpr_atm last_seen_things_queued_ever; |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 685 | grpc_completion_queue *cq; |
| 686 | gpr_timespec deadline; |
| 687 | grpc_cq_completion *stolen_completion; |
| 688 | void *tag; /* for pluck */ |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 689 | bool first_loop; |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 690 | } cq_is_finished_arg; |
| 691 | |
| 692 | static bool cq_is_next_finished(grpc_exec_ctx *exec_ctx, void *arg) { |
| 693 | cq_is_finished_arg *a = arg; |
| 694 | grpc_completion_queue *cq = a->cq; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 695 | cq_data *cqd = &cq->data; |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 696 | GPR_ASSERT(a->stolen_completion == NULL); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 697 | |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 698 | gpr_atm current_last_seen_things_queued_ever = |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 699 | gpr_atm_no_barrier_load(&cqd->things_queued_ever); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 700 | |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 701 | if (current_last_seen_things_queued_ever != a->last_seen_things_queued_ever) { |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 702 | a->last_seen_things_queued_ever = |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 703 | gpr_atm_no_barrier_load(&cqd->things_queued_ever); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 704 | |
| 705 | /* Pop a cq_completion from the queue. Returns NULL if the queue is empty |
| 706 | * might return NULL in some cases even if the queue is not empty; but that |
| 707 | * is ok and doesn't affect correctness. Might effect the tail latencies a |
| 708 | * bit) */ |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 709 | a->stolen_completion = cq_event_queue_pop(&cqd->queue); |
Sree Kuchibhotla | 078a340 | 2017-04-11 21:23:03 -0700 | [diff] [blame] | 710 | if (a->stolen_completion != NULL) { |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 711 | return true; |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 712 | } |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 713 | } |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 714 | return !a->first_loop && |
| 715 | gpr_time_cmp(a->deadline, gpr_now(a->deadline.clock_type)) < 0; |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 718 | #ifndef NDEBUG |
| 719 | static void dump_pending_tags(grpc_completion_queue *cc) { |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 720 | if (!GRPC_TRACER_ON(grpc_trace_pending_tags)) return; |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 721 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 722 | cq_data *cqd = &cc->data; |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 723 | |
| 724 | gpr_strvec v; |
| 725 | gpr_strvec_init(&v); |
| 726 | gpr_strvec_add(&v, gpr_strdup("PENDING TAGS:")); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 727 | gpr_mu_lock(cqd->mu); |
| 728 | for (size_t i = 0; i < cqd->outstanding_tag_count; i++) { |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 729 | char *s; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 730 | gpr_asprintf(&s, " %p", cqd->outstanding_tags[i]); |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 731 | gpr_strvec_add(&v, s); |
| 732 | } |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 733 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 734 | char *out = gpr_strvec_flatten(&v, NULL); |
| 735 | gpr_strvec_destroy(&v); |
| 736 | gpr_log(GPR_DEBUG, "%s", out); |
| 737 | gpr_free(out); |
| 738 | } |
Craig Tiller | 49c644c | 2016-08-19 13:52:23 -0700 | [diff] [blame] | 739 | #else |
| 740 | static void dump_pending_tags(grpc_completion_queue *cc) {} |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 741 | #endif |
| 742 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 743 | static grpc_event cq_next(grpc_completion_queue *cc, gpr_timespec deadline, |
| 744 | void *reserved) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 745 | grpc_event ret; |
Craig Tiller | 69f90e6 | 2015-08-06 08:32:35 -0700 | [diff] [blame] | 746 | gpr_timespec now; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 747 | cq_data *cqd = &cc->data; |
Sree Kuchibhotla | 321881d | 2017-02-27 11:25:28 -0800 | [diff] [blame] | 748 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 749 | GPR_TIMER_BEGIN("grpc_completion_queue_next", 0); |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 750 | |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 751 | GRPC_API_TRACE( |
| 752 | "grpc_completion_queue_next(" |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 753 | "cc=%p, " |
Ken Payson | 62a6c92 | 2016-06-24 11:53:54 -0700 | [diff] [blame] | 754 | "deadline=gpr_timespec { tv_sec: %" PRId64 |
| 755 | ", tv_nsec: %d, clock_type: %d }, " |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 756 | "reserved=%p)", |
Sree Kuchibhotla | 5bec133 | 2017-03-24 10:03:22 -0700 | [diff] [blame] | 757 | 5, (cc, deadline.tv_sec, deadline.tv_nsec, (int)deadline.clock_type, |
| 758 | reserved)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 759 | GPR_ASSERT(!reserved); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 760 | |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 761 | dump_pending_tags(cc); |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 762 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 763 | deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC); |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 764 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 765 | GRPC_CQ_INTERNAL_REF(cc, "next"); |
Sree Kuchibhotla | a72d79b | 2017-04-24 19:45:20 -0700 | [diff] [blame] | 766 | |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 767 | cq_is_finished_arg is_finished_arg = { |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 768 | .last_seen_things_queued_ever = |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 769 | gpr_atm_no_barrier_load(&cqd->things_queued_ever), |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 770 | .cq = cc, |
| 771 | .deadline = deadline, |
| 772 | .stolen_completion = NULL, |
| 773 | .tag = NULL, |
| 774 | .first_loop = true}; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 775 | grpc_exec_ctx exec_ctx = |
| 776 | GRPC_EXEC_CTX_INITIALIZER(0, cq_is_next_finished, &is_finished_arg); |
Sree Kuchibhotla | 94aff9e | 2017-04-10 10:25:03 -0700 | [diff] [blame] | 777 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 778 | for (;;) { |
Sree Kuchibhotla | 55d0b49 | 2017-04-12 17:33:50 -0700 | [diff] [blame] | 779 | gpr_timespec iteration_deadline = deadline; |
| 780 | |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 781 | if (is_finished_arg.stolen_completion != NULL) { |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 782 | grpc_cq_completion *c = is_finished_arg.stolen_completion; |
| 783 | is_finished_arg.stolen_completion = NULL; |
| 784 | ret.type = GRPC_OP_COMPLETE; |
| 785 | ret.success = c->next & 1u; |
| 786 | ret.tag = c->tag; |
| 787 | c->done(&exec_ctx, c->done_arg, c); |
| 788 | break; |
| 789 | } |
Sree Kuchibhotla | 94aff9e | 2017-04-10 10:25:03 -0700 | [diff] [blame] | 790 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 791 | grpc_cq_completion *c = cq_event_queue_pop(&cqd->queue); |
Sree Kuchibhotla | 94aff9e | 2017-04-10 10:25:03 -0700 | [diff] [blame] | 792 | |
| 793 | if (c != NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 794 | ret.type = GRPC_OP_COMPLETE; |
| 795 | ret.success = c->next & 1u; |
| 796 | ret.tag = c->tag; |
| 797 | c->done(&exec_ctx, c->done_arg, c); |
| 798 | break; |
Sree Kuchibhotla | 55d0b49 | 2017-04-12 17:33:50 -0700 | [diff] [blame] | 799 | } else { |
| 800 | /* If c == NULL it means either the queue is empty OR in an transient |
| 801 | inconsistent state. If it is the latter, we shold do a 0-timeout poll |
| 802 | so that the thread comes back quickly from poll to make a second |
| 803 | attempt at popping. Not doing this can potentially deadlock this thread |
| 804 | forever (if the deadline is infinity) */ |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 805 | if (cq_event_queue_num_items(&cqd->queue) > 0) { |
Sree Kuchibhotla | 55d0b49 | 2017-04-12 17:33:50 -0700 | [diff] [blame] | 806 | iteration_deadline = gpr_time_0(GPR_CLOCK_MONOTONIC); |
| 807 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 808 | } |
Sree Kuchibhotla | 94aff9e | 2017-04-10 10:25:03 -0700 | [diff] [blame] | 809 | |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 810 | if (gpr_atm_no_barrier_load(&cqd->shutdown)) { |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 811 | /* Before returning, check if the queue has any items left over (since |
| 812 | gpr_mpscq_pop() can sometimes return NULL even if the queue is not |
| 813 | empty. If so, keep retrying but do not return GRPC_QUEUE_SHUTDOWN */ |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 814 | if (cq_event_queue_num_items(&cqd->queue) > 0) { |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 815 | /* Go to the beginning of the loop. No point doing a poll because |
| 816 | (cc->shutdown == true) is only possible when there is no pending work |
| 817 | (i.e cc->pending_events == 0) and any outstanding grpc_cq_completion |
| 818 | events are already queued on this cq */ |
| 819 | continue; |
| 820 | } |
| 821 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 822 | memset(&ret, 0, sizeof(ret)); |
| 823 | ret.type = GRPC_QUEUE_SHUTDOWN; |
| 824 | break; |
| 825 | } |
Sree Kuchibhotla | 94aff9e | 2017-04-10 10:25:03 -0700 | [diff] [blame] | 826 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 827 | now = gpr_now(GPR_CLOCK_MONOTONIC); |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 828 | if (!is_finished_arg.first_loop && gpr_time_cmp(now, deadline) >= 0) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 829 | memset(&ret, 0, sizeof(ret)); |
| 830 | ret.type = GRPC_QUEUE_TIMEOUT; |
Craig Tiller | 49c644c | 2016-08-19 13:52:23 -0700 | [diff] [blame] | 831 | dump_pending_tags(cc); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 832 | break; |
| 833 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 834 | |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 835 | /* The main polling work happens in grpc_pollset_work */ |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 836 | gpr_mu_lock(cqd->mu); |
Yuxuan Li | 6ca796d | 2017-05-15 14:15:34 -0700 | [diff] [blame] | 837 | cqd->num_polls++; |
Sree Kuchibhotla | a72d79b | 2017-04-24 19:45:20 -0700 | [diff] [blame] | 838 | grpc_error *err = cc->poller_vtable->work(&exec_ctx, POLLSET_FROM_CQ(cc), |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 839 | NULL, now, iteration_deadline); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 840 | gpr_mu_unlock(cqd->mu); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 841 | |
Sree Kuchibhotla | 94aff9e | 2017-04-10 10:25:03 -0700 | [diff] [blame] | 842 | if (err != GRPC_ERROR_NONE) { |
| 843 | const char *msg = grpc_error_string(err); |
| 844 | gpr_log(GPR_ERROR, "Completion queue next failed: %s", msg); |
| 845 | |
| 846 | GRPC_ERROR_UNREF(err); |
| 847 | memset(&ret, 0, sizeof(ret)); |
| 848 | ret.type = GRPC_QUEUE_TIMEOUT; |
| 849 | dump_pending_tags(cc); |
| 850 | break; |
Craig Tiller | ccdea19 | 2016-02-16 08:06:46 -0800 | [diff] [blame] | 851 | } |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 852 | is_finished_arg.first_loop = false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 853 | } |
Sree Kuchibhotla | 94aff9e | 2017-04-10 10:25:03 -0700 | [diff] [blame] | 854 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 855 | GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret); |
Craig Tiller | f840110 | 2017-04-17 09:47:28 -0700 | [diff] [blame] | 856 | GRPC_CQ_INTERNAL_UNREF(&exec_ctx, cc, "next"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 857 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 858 | GPR_ASSERT(is_finished_arg.stolen_completion == NULL); |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 859 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 860 | GPR_TIMER_END("grpc_completion_queue_next", 0); |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 861 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 862 | return ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 865 | grpc_event grpc_completion_queue_next(grpc_completion_queue *cc, |
| 866 | gpr_timespec deadline, void *reserved) { |
| 867 | return cc->vtable->next(cc, deadline, reserved); |
| 868 | } |
| 869 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 870 | static int add_plucker(grpc_completion_queue *cc, void *tag, |
Craig Tiller | d0a8ae1 | 2016-02-18 08:01:19 -0800 | [diff] [blame] | 871 | grpc_pollset_worker **worker) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 872 | cq_data *cqd = &cc->data; |
| 873 | if (cqd->num_pluckers == GRPC_MAX_COMPLETION_QUEUE_PLUCKERS) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 874 | return 0; |
| 875 | } |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 876 | cqd->pluckers[cqd->num_pluckers].tag = tag; |
| 877 | cqd->pluckers[cqd->num_pluckers].worker = worker; |
| 878 | cqd->num_pluckers++; |
Craig Tiller | 791e78a | 2015-08-01 16:20:17 -0700 | [diff] [blame] | 879 | return 1; |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 882 | static void del_plucker(grpc_completion_queue *cc, void *tag, |
Craig Tiller | d0a8ae1 | 2016-02-18 08:01:19 -0800 | [diff] [blame] | 883 | grpc_pollset_worker **worker) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 884 | cq_data *cqd = &cc->data; |
| 885 | for (int i = 0; i < cqd->num_pluckers; i++) { |
| 886 | if (cqd->pluckers[i].tag == tag && cqd->pluckers[i].worker == worker) { |
| 887 | cqd->num_pluckers--; |
| 888 | GPR_SWAP(plucker, cqd->pluckers[i], cqd->pluckers[cqd->num_pluckers]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 889 | return; |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 890 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 891 | } |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 892 | GPR_UNREACHABLE_CODE(return ); |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 895 | static bool cq_is_pluck_finished(grpc_exec_ctx *exec_ctx, void *arg) { |
| 896 | cq_is_finished_arg *a = arg; |
| 897 | grpc_completion_queue *cq = a->cq; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 898 | cq_data *cqd = &cq->data; |
| 899 | |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 900 | GPR_ASSERT(a->stolen_completion == NULL); |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 901 | gpr_atm current_last_seen_things_queued_ever = |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 902 | gpr_atm_no_barrier_load(&cqd->things_queued_ever); |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 903 | if (current_last_seen_things_queued_ever != a->last_seen_things_queued_ever) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 904 | gpr_mu_lock(cqd->mu); |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 905 | a->last_seen_things_queued_ever = |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 906 | gpr_atm_no_barrier_load(&cqd->things_queued_ever); |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 907 | grpc_cq_completion *c; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 908 | grpc_cq_completion *prev = &cqd->completed_head; |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 909 | while ((c = (grpc_cq_completion *)(prev->next & ~(uintptr_t)1)) != |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 910 | &cqd->completed_head) { |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 911 | if (c->tag == a->tag) { |
| 912 | prev->next = (prev->next & (uintptr_t)1) | (c->next & ~(uintptr_t)1); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 913 | if (c == cqd->completed_tail) { |
| 914 | cqd->completed_tail = prev; |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 915 | } |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 916 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 917 | a->stolen_completion = c; |
| 918 | return true; |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 919 | } |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 920 | prev = c; |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 921 | } |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 922 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 923 | } |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 924 | return !a->first_loop && |
| 925 | gpr_time_cmp(a->deadline, gpr_now(a->deadline.clock_type)) < 0; |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 928 | static grpc_event cq_pluck(grpc_completion_queue *cc, void *tag, |
Sree Kuchibhotla | 1eabdab | 2017-04-26 18:52:51 -0700 | [diff] [blame] | 929 | gpr_timespec deadline, void *reserved) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 930 | grpc_event ret; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 931 | grpc_cq_completion *c; |
| 932 | grpc_cq_completion *prev; |
Craig Tiller | d0a8ae1 | 2016-02-18 08:01:19 -0800 | [diff] [blame] | 933 | grpc_pollset_worker *worker = NULL; |
Craig Tiller | 4c06b82 | 2015-08-06 08:41:31 -0700 | [diff] [blame] | 934 | gpr_timespec now; |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 935 | cq_data *cqd = &cc->data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 936 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 937 | GPR_TIMER_BEGIN("grpc_completion_queue_pluck", 0); |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 938 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 939 | if (GRPC_TRACER_ON(grpc_cq_pluck_trace)) { |
murgatroid99 | 9f6a805 | 2016-06-15 17:39:03 -0700 | [diff] [blame] | 940 | GRPC_API_TRACE( |
| 941 | "grpc_completion_queue_pluck(" |
| 942 | "cc=%p, tag=%p, " |
murgatroid99 | b2edc6c | 2016-06-27 14:24:18 -0700 | [diff] [blame] | 943 | "deadline=gpr_timespec { tv_sec: %" PRId64 |
| 944 | ", tv_nsec: %d, clock_type: %d }, " |
murgatroid99 | 9f6a805 | 2016-06-15 17:39:03 -0700 | [diff] [blame] | 945 | "reserved=%p)", |
Sree Kuchibhotla | 5bec133 | 2017-03-24 10:03:22 -0700 | [diff] [blame] | 946 | 6, (cc, tag, deadline.tv_sec, deadline.tv_nsec, |
| 947 | (int)deadline.clock_type, reserved)); |
murgatroid99 | 9f6a805 | 2016-06-15 17:39:03 -0700 | [diff] [blame] | 948 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 949 | GPR_ASSERT(!reserved); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 950 | |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 951 | dump_pending_tags(cc); |
Craig Tiller | 4e41e36 | 2016-08-19 13:12:54 -0700 | [diff] [blame] | 952 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 953 | deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC); |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 954 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 955 | GRPC_CQ_INTERNAL_REF(cc, "pluck"); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 956 | gpr_mu_lock(cqd->mu); |
Craig Tiller | aef3a79 | 2016-08-24 15:13:53 -0700 | [diff] [blame] | 957 | cq_is_finished_arg is_finished_arg = { |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 958 | .last_seen_things_queued_ever = |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 959 | gpr_atm_no_barrier_load(&cqd->things_queued_ever), |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 960 | .cq = cc, |
| 961 | .deadline = deadline, |
| 962 | .stolen_completion = NULL, |
| 963 | .tag = tag, |
| 964 | .first_loop = true}; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 965 | grpc_exec_ctx exec_ctx = |
| 966 | GRPC_EXEC_CTX_INITIALIZER(0, cq_is_pluck_finished, &is_finished_arg); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 967 | for (;;) { |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 968 | if (is_finished_arg.stolen_completion != NULL) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 969 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | 5f70fc6 | 2016-08-04 16:00:00 -0700 | [diff] [blame] | 970 | c = is_finished_arg.stolen_completion; |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 971 | is_finished_arg.stolen_completion = NULL; |
| 972 | ret.type = GRPC_OP_COMPLETE; |
| 973 | ret.success = c->next & 1u; |
| 974 | ret.tag = c->tag; |
| 975 | c->done(&exec_ctx, c->done_arg, c); |
| 976 | break; |
| 977 | } |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 978 | prev = &cqd->completed_head; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 979 | while ((c = (grpc_cq_completion *)(prev->next & ~(uintptr_t)1)) != |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 980 | &cqd->completed_head) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 981 | if (c->tag == tag) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 982 | prev->next = (prev->next & (uintptr_t)1) | (c->next & ~(uintptr_t)1); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 983 | if (c == cqd->completed_tail) { |
| 984 | cqd->completed_tail = prev; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 985 | } |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 986 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 987 | ret.type = GRPC_OP_COMPLETE; |
| 988 | ret.success = c->next & 1u; |
| 989 | ret.tag = c->tag; |
| 990 | c->done(&exec_ctx, c->done_arg, c); |
| 991 | goto done; |
| 992 | } |
| 993 | prev = c; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 994 | } |
Sree Kuchibhotla | 8e36845 | 2017-04-25 15:45:02 -0700 | [diff] [blame] | 995 | if (gpr_atm_no_barrier_load(&cqd->shutdown)) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 996 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 997 | memset(&ret, 0, sizeof(ret)); |
| 998 | ret.type = GRPC_QUEUE_SHUTDOWN; |
| 999 | break; |
| 1000 | } |
| 1001 | if (!add_plucker(cc, tag, &worker)) { |
| 1002 | gpr_log(GPR_DEBUG, |
| 1003 | "Too many outstanding grpc_completion_queue_pluck calls: maximum " |
| 1004 | "is %d", |
| 1005 | GRPC_MAX_COMPLETION_QUEUE_PLUCKERS); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 1006 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1007 | memset(&ret, 0, sizeof(ret)); |
| 1008 | /* TODO(ctiller): should we use a different result here */ |
| 1009 | ret.type = GRPC_QUEUE_TIMEOUT; |
Craig Tiller | 49c644c | 2016-08-19 13:52:23 -0700 | [diff] [blame] | 1010 | dump_pending_tags(cc); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1011 | break; |
| 1012 | } |
| 1013 | now = gpr_now(GPR_CLOCK_MONOTONIC); |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 1014 | if (!is_finished_arg.first_loop && gpr_time_cmp(now, deadline) >= 0) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1015 | del_plucker(cc, tag, &worker); |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 1016 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1017 | memset(&ret, 0, sizeof(ret)); |
| 1018 | ret.type = GRPC_QUEUE_TIMEOUT; |
Craig Tiller | 49c644c | 2016-08-19 13:52:23 -0700 | [diff] [blame] | 1019 | dump_pending_tags(cc); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1020 | break; |
| 1021 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1022 | |
Yuxuan Li | 6ca796d | 2017-05-15 14:15:34 -0700 | [diff] [blame] | 1023 | cqd->num_polls++; |
Craig Tiller | c357179 | 2017-05-02 12:33:38 -0700 | [diff] [blame] | 1024 | grpc_error *err = cc->poller_vtable->work(&exec_ctx, POLLSET_FROM_CQ(cc), |
| 1025 | &worker, now, deadline); |
| 1026 | if (err != GRPC_ERROR_NONE) { |
| 1027 | del_plucker(cc, tag, &worker); |
Sree Kuchibhotla | 467ad20 | 2017-05-11 13:15:07 -0700 | [diff] [blame] | 1028 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | c357179 | 2017-05-02 12:33:38 -0700 | [diff] [blame] | 1029 | const char *msg = grpc_error_string(err); |
Sree Kuchibhotla | 467ad20 | 2017-05-11 13:15:07 -0700 | [diff] [blame] | 1030 | gpr_log(GPR_ERROR, "Completion queue pluck failed: %s", msg); |
Craig Tiller | 4f1d0f3 | 2016-05-06 17:12:37 -0700 | [diff] [blame] | 1031 | |
Craig Tiller | c357179 | 2017-05-02 12:33:38 -0700 | [diff] [blame] | 1032 | GRPC_ERROR_UNREF(err); |
| 1033 | memset(&ret, 0, sizeof(ret)); |
| 1034 | ret.type = GRPC_QUEUE_TIMEOUT; |
| 1035 | dump_pending_tags(cc); |
| 1036 | break; |
Craig Tiller | ccdea19 | 2016-02-16 08:06:46 -0800 | [diff] [blame] | 1037 | } |
Craig Tiller | 2c4043b | 2016-09-05 14:50:16 -0700 | [diff] [blame] | 1038 | is_finished_arg.first_loop = false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1039 | del_plucker(cc, tag, &worker); |
| 1040 | } |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1041 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1042 | GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret); |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1043 | GRPC_CQ_INTERNAL_UNREF(&exec_ctx, cc, "pluck"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1044 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 2f42fde | 2016-08-04 15:13:18 -0700 | [diff] [blame] | 1045 | GPR_ASSERT(is_finished_arg.stolen_completion == NULL); |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 1046 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 1047 | GPR_TIMER_END("grpc_completion_queue_pluck", 0); |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 1048 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1049 | return ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1052 | grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag, |
| 1053 | gpr_timespec deadline, void *reserved) { |
| 1054 | return cc->vtable->pluck(cc, tag, deadline, reserved); |
| 1055 | } |
Sree Kuchibhotla | 736dd90 | 2017-04-25 18:26:53 -0700 | [diff] [blame] | 1056 | |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1057 | /* Finishes the completion queue shutdown. This means that there are no more |
| 1058 | completion events / tags expected from the completion queue |
| 1059 | - Must be called under completion queue lock |
| 1060 | - Must be called only once in completion queue's lifetime |
| 1061 | - grpc_completion_queue_shutdown() MUST have been called before calling |
| 1062 | this function */ |
| 1063 | static void cq_finish_shutdown(grpc_exec_ctx *exec_ctx, |
| 1064 | grpc_completion_queue *cc) { |
| 1065 | cq_data *cqd = &cc->data; |
Sree Kuchibhotla | 8e36845 | 2017-04-25 15:45:02 -0700 | [diff] [blame] | 1066 | |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1067 | GPR_ASSERT(cqd->shutdown_called); |
| 1068 | GPR_ASSERT(!gpr_atm_no_barrier_load(&cqd->shutdown)); |
| 1069 | gpr_atm_no_barrier_store(&cqd->shutdown, 1); |
Sree Kuchibhotla | 8e36845 | 2017-04-25 15:45:02 -0700 | [diff] [blame] | 1070 | |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1071 | cc->poller_vtable->shutdown(exec_ctx, POLLSET_FROM_CQ(cc), |
| 1072 | &cqd->pollset_shutdown_done); |
| 1073 | } |
| 1074 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1075 | /* Shutdown simply drops a ref that we reserved at creation time; if we drop |
| 1076 | to zero here, then enter shutdown mode and wake up any waiters */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1077 | void grpc_completion_queue_shutdown(grpc_completion_queue *cc) { |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1078 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1079 | GPR_TIMER_BEGIN("grpc_completion_queue_shutdown", 0); |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1080 | GRPC_API_TRACE("grpc_completion_queue_shutdown(cc=%p)", 1, (cc)); |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1081 | cq_data *cqd = &cc->data; |
| 1082 | |
| 1083 | gpr_mu_lock(cqd->mu); |
| 1084 | if (cqd->shutdown_called) { |
Sree Kuchibhotla | c2134c3 | 2017-04-25 12:10:04 -0700 | [diff] [blame] | 1085 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1086 | GPR_TIMER_END("grpc_completion_queue_shutdown", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1087 | return; |
| 1088 | } |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1089 | cqd->shutdown_called = 1; |
| 1090 | if (gpr_unref(&cqd->pending_events)) { |
| 1091 | cq_finish_shutdown(&exec_ctx, cc); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1092 | } |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1093 | gpr_mu_unlock(cqd->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1094 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1095 | GPR_TIMER_END("grpc_completion_queue_shutdown", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1098 | void grpc_completion_queue_destroy(grpc_completion_queue *cc) { |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1099 | GRPC_API_TRACE("grpc_completion_queue_destroy(cc=%p)", 1, (cc)); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1100 | GPR_TIMER_BEGIN("grpc_completion_queue_destroy", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1101 | grpc_completion_queue_shutdown(cc); |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1102 | |
| 1103 | /* TODO (sreek): This should not ideally be here. Refactor it into the |
| 1104 | * cq_vtable (perhaps have a create/destroy methods in the cq vtable) */ |
| 1105 | if (cc->vtable->cq_completion_type == GRPC_CQ_NEXT) { |
| 1106 | GPR_ASSERT(cq_event_queue_num_items(&cc->data.queue) == 0); |
Sree Kuchibhotla | d7a1b8f | 2017-04-11 19:43:07 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1109 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 1110 | GRPC_CQ_INTERNAL_UNREF(&exec_ctx, cc, "destroy"); |
| 1111 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1112 | GPR_TIMER_END("grpc_completion_queue_destroy", 0); |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1115 | grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) { |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 1116 | return cc->poller_vtable->can_get_pollset ? POLLSET_FROM_CQ(cc) : NULL; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 1117 | } |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1118 | |
Craig Tiller | 40945c7 | 2016-05-16 17:15:24 -0700 | [diff] [blame] | 1119 | grpc_completion_queue *grpc_cq_from_pollset(grpc_pollset *ps) { |
| 1120 | return CQ_FROM_POLLSET(ps); |
| 1121 | } |
| 1122 | |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1123 | void grpc_cq_mark_server_cq(grpc_completion_queue *cc) { |
| 1124 | cc->data.is_server_cq = 1; |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Craig Tiller | 11c5832 | 2017-04-12 08:21:17 -0700 | [diff] [blame] | 1127 | bool grpc_cq_is_server_cq(grpc_completion_queue *cc) { |
Sree Kuchibhotla | 8ac5c6d | 2017-05-11 14:13:11 -0700 | [diff] [blame] | 1128 | return cc->data.is_server_cq; |
Craig Tiller | 11c5832 | 2017-04-12 08:21:17 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | bool grpc_cq_can_listen(grpc_completion_queue *cc) { |
| 1132 | return cc->poller_vtable->can_listen; |
| 1133 | } |