Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/surface/completion_queue.h" |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
| 38 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 39 | #include "src/core/iomgr/pollset.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 40 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include "src/core/surface/call.h" |
| 42 | #include "src/core/surface/event_string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include "src/core/surface/surface_trace.h" |
| 44 | #include <grpc/support/alloc.h> |
| 45 | #include <grpc/support/atm.h> |
| 46 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
| 48 | #define NUM_TAG_BUCKETS 31 |
| 49 | |
| 50 | /* A single event: extends grpc_event to form a linked list with a destruction |
| 51 | function (on_finish) that is hidden from outside this module */ |
| 52 | typedef struct event { |
| 53 | grpc_event base; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | struct event *queue_next; |
| 55 | struct event *queue_prev; |
| 56 | struct event *bucket_next; |
| 57 | struct event *bucket_prev; |
| 58 | } event; |
| 59 | |
| 60 | /* Completion queue structure */ |
| 61 | struct grpc_completion_queue { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 62 | /* TODO(ctiller): see if this can be removed */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | int allow_polling; |
| 64 | |
| 65 | /* When refs drops to zero, we are in shutdown mode, and will be destroyable |
| 66 | once all queued events are drained */ |
| 67 | gpr_refcount refs; |
Craig Tiller | 5717a98 | 2015-04-27 12:01:49 -0700 | [diff] [blame] | 68 | /* Once owning_refs drops to zero, we will destroy the cq */ |
| 69 | gpr_refcount owning_refs; |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 70 | /* the set of low level i/o things that concern this cq */ |
| 71 | grpc_pollset pollset; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | /* 0 initially, 1 once we've begun shutting down */ |
| 73 | int shutdown; |
Craig Tiller | f5fd4ba | 2015-03-02 18:01:21 +0000 | [diff] [blame] | 74 | int shutdown_called; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | /* Head of a linked list of queued events (prev points to the last element) */ |
| 76 | event *queue; |
| 77 | /* Fixed size chained hash table of events for pluck() */ |
| 78 | event *buckets[NUM_TAG_BUCKETS]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 81 | grpc_completion_queue *grpc_completion_queue_create(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | grpc_completion_queue *cc = gpr_malloc(sizeof(grpc_completion_queue)); |
| 83 | memset(cc, 0, sizeof(*cc)); |
| 84 | /* Initial ref is dropped by grpc_completion_queue_shutdown */ |
| 85 | gpr_ref_init(&cc->refs, 1); |
Craig Tiller | 5717a98 | 2015-04-27 12:01:49 -0700 | [diff] [blame] | 86 | gpr_ref_init(&cc->owning_refs, 1); |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 87 | grpc_pollset_init(&cc->pollset); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | cc->allow_polling = 1; |
| 89 | return cc; |
| 90 | } |
| 91 | |
Craig Tiller | 5717a98 | 2015-04-27 12:01:49 -0700 | [diff] [blame] | 92 | void grpc_cq_internal_ref(grpc_completion_queue *cc) { |
| 93 | gpr_ref(&cc->owning_refs); |
| 94 | } |
| 95 | |
| 96 | static void on_pollset_destroy_done(void *arg) { |
| 97 | grpc_completion_queue *cc = arg; |
| 98 | grpc_pollset_destroy(&cc->pollset); |
| 99 | gpr_free(cc); |
| 100 | } |
| 101 | |
| 102 | void grpc_cq_internal_unref(grpc_completion_queue *cc) { |
| 103 | if (gpr_unref(&cc->owning_refs)) { |
| 104 | GPR_ASSERT(cc->queue == NULL); |
| 105 | grpc_pollset_shutdown(&cc->pollset, on_pollset_destroy_done, cc); |
| 106 | } |
| 107 | } |
| 108 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | void grpc_completion_queue_dont_poll_test_only(grpc_completion_queue *cc) { |
| 110 | cc->allow_polling = 0; |
| 111 | } |
| 112 | |
| 113 | /* Create and append an event to the queue. Returns the event so that its data |
| 114 | members can be filled in. |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 115 | Requires GRPC_POLLSET_MU(&cc->pollset) locked. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 116 | static event *add_locked(grpc_completion_queue *cc, grpc_completion_type type, |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 117 | void *tag, grpc_call *call) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | event *ev = gpr_malloc(sizeof(event)); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 119 | gpr_uintptr bucket = ((gpr_uintptr)tag) % NUM_TAG_BUCKETS; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | ev->base.type = type; |
| 121 | ev->base.tag = tag; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | if (cc->queue == NULL) { |
| 123 | cc->queue = ev->queue_next = ev->queue_prev = ev; |
| 124 | } else { |
| 125 | ev->queue_next = cc->queue; |
| 126 | ev->queue_prev = cc->queue->queue_prev; |
| 127 | ev->queue_next->queue_prev = ev->queue_prev->queue_next = ev; |
| 128 | } |
| 129 | if (cc->buckets[bucket] == NULL) { |
| 130 | cc->buckets[bucket] = ev->bucket_next = ev->bucket_prev = ev; |
| 131 | } else { |
| 132 | ev->bucket_next = cc->buckets[bucket]; |
| 133 | ev->bucket_prev = cc->buckets[bucket]->bucket_prev; |
| 134 | ev->bucket_next->bucket_prev = ev->bucket_prev->bucket_next = ev; |
| 135 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 136 | gpr_cv_broadcast(GRPC_POLLSET_CV(&cc->pollset)); |
| 137 | grpc_pollset_kick(&cc->pollset); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | return ev; |
| 139 | } |
| 140 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 141 | void grpc_cq_begin_op(grpc_completion_queue *cc, grpc_call *call) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | gpr_ref(&cc->refs); |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 143 | if (call) GRPC_CALL_INTERNAL_REF(call, "cq"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* Signal the end of an operation - if this is the last waiting-to-be-queued |
| 147 | event, then enter shutdown mode */ |
| 148 | static void end_op_locked(grpc_completion_queue *cc, |
| 149 | grpc_completion_type type) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 150 | if (gpr_unref(&cc->refs)) { |
| 151 | GPR_ASSERT(!cc->shutdown); |
Craig Tiller | f5fd4ba | 2015-03-02 18:01:21 +0000 | [diff] [blame] | 152 | GPR_ASSERT(cc->shutdown_called); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 153 | cc->shutdown = 1; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 154 | gpr_cv_broadcast(GRPC_POLLSET_CV(&cc->pollset)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 158 | void grpc_cq_end_op(grpc_completion_queue *cc, void *tag, grpc_call *call, |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 159 | int success) { |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 160 | event *ev; |
| 161 | gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset)); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 162 | ev = add_locked(cc, GRPC_OP_COMPLETE, tag, call); |
| 163 | ev->base.success = success; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 164 | end_op_locked(cc, GRPC_OP_COMPLETE); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 165 | gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); |
| 166 | } |
| 167 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 168 | /* Create a GRPC_QUEUE_SHUTDOWN event without queuing it anywhere */ |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 169 | static event *create_shutdown_event(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | event *ev = gpr_malloc(sizeof(event)); |
| 171 | ev->base.type = GRPC_QUEUE_SHUTDOWN; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 172 | ev->base.tag = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 173 | return ev; |
| 174 | } |
| 175 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 176 | grpc_event grpc_completion_queue_next(grpc_completion_queue *cc, |
| 177 | gpr_timespec deadline) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | event *ev = NULL; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 179 | grpc_event ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 180 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 181 | gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | for (;;) { |
| 183 | if (cc->queue != NULL) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 184 | gpr_uintptr bucket; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 185 | ev = cc->queue; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 186 | bucket = ((gpr_uintptr)ev->base.tag) % NUM_TAG_BUCKETS; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | cc->queue = ev->queue_next; |
| 188 | ev->queue_next->queue_prev = ev->queue_prev; |
| 189 | ev->queue_prev->queue_next = ev->queue_next; |
| 190 | ev->bucket_next->bucket_prev = ev->bucket_prev; |
| 191 | ev->bucket_prev->bucket_next = ev->bucket_next; |
| 192 | if (ev == cc->buckets[bucket]) { |
| 193 | cc->buckets[bucket] = ev->bucket_next; |
| 194 | if (ev == cc->buckets[bucket]) { |
| 195 | cc->buckets[bucket] = NULL; |
| 196 | } |
| 197 | } |
| 198 | if (cc->queue == ev) { |
| 199 | cc->queue = NULL; |
| 200 | } |
| 201 | break; |
| 202 | } |
| 203 | if (cc->shutdown) { |
| 204 | ev = create_shutdown_event(); |
| 205 | break; |
| 206 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 207 | if (cc->allow_polling && grpc_pollset_work(&cc->pollset, deadline)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 208 | continue; |
| 209 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 210 | if (gpr_cv_wait(GRPC_POLLSET_CV(&cc->pollset), |
| 211 | GRPC_POLLSET_MU(&cc->pollset), deadline)) { |
| 212 | gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 213 | memset(&ret, 0, sizeof(ret)); |
| 214 | ret.type = GRPC_QUEUE_TIMEOUT; |
| 215 | return ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 218 | gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 219 | ret = ev->base; |
| 220 | gpr_free(ev); |
| 221 | GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret); |
| 222 | return ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static event *pluck_event(grpc_completion_queue *cc, void *tag) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 226 | gpr_uintptr bucket = ((gpr_uintptr)tag) % NUM_TAG_BUCKETS; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 227 | event *ev = cc->buckets[bucket]; |
| 228 | if (ev == NULL) return NULL; |
| 229 | do { |
| 230 | if (ev->base.tag == tag) { |
| 231 | ev->queue_next->queue_prev = ev->queue_prev; |
| 232 | ev->queue_prev->queue_next = ev->queue_next; |
| 233 | ev->bucket_next->bucket_prev = ev->bucket_prev; |
| 234 | ev->bucket_prev->bucket_next = ev->bucket_next; |
| 235 | if (ev == cc->buckets[bucket]) { |
| 236 | cc->buckets[bucket] = ev->bucket_next; |
| 237 | if (ev == cc->buckets[bucket]) { |
| 238 | cc->buckets[bucket] = NULL; |
| 239 | } |
| 240 | } |
| 241 | if (cc->queue == ev) { |
| 242 | cc->queue = ev->queue_next; |
| 243 | if (cc->queue == ev) { |
| 244 | cc->queue = NULL; |
| 245 | } |
| 246 | } |
| 247 | return ev; |
| 248 | } |
| 249 | ev = ev->bucket_next; |
| 250 | } while (ev != cc->buckets[bucket]); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 254 | grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag, |
| 255 | gpr_timespec deadline) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 256 | event *ev = NULL; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 257 | grpc_event ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 258 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 259 | gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 260 | for (;;) { |
| 261 | if ((ev = pluck_event(cc, tag))) { |
| 262 | break; |
| 263 | } |
| 264 | if (cc->shutdown) { |
| 265 | ev = create_shutdown_event(); |
| 266 | break; |
| 267 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 268 | if (cc->allow_polling && grpc_pollset_work(&cc->pollset, deadline)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 269 | continue; |
| 270 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 271 | if (gpr_cv_wait(GRPC_POLLSET_CV(&cc->pollset), |
| 272 | GRPC_POLLSET_MU(&cc->pollset), deadline)) { |
| 273 | gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 274 | memset(&ret, 0, sizeof(ret)); |
| 275 | ret.type = GRPC_QUEUE_TIMEOUT; |
| 276 | return ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 277 | } |
| 278 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 279 | gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 280 | ret = ev->base; |
| 281 | gpr_free(ev); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 282 | GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ev->base); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame^] | 283 | return ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Shutdown simply drops a ref that we reserved at creation time; if we drop |
| 287 | to zero here, then enter shutdown mode and wake up any waiters */ |
| 288 | void grpc_completion_queue_shutdown(grpc_completion_queue *cc) { |
Craig Tiller | f5fd4ba | 2015-03-02 18:01:21 +0000 | [diff] [blame] | 289 | gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset)); |
| 290 | cc->shutdown_called = 1; |
| 291 | gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); |
| 292 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 293 | if (gpr_unref(&cc->refs)) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 294 | gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 295 | GPR_ASSERT(!cc->shutdown); |
| 296 | cc->shutdown = 1; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 297 | gpr_cv_broadcast(GRPC_POLLSET_CV(&cc->pollset)); |
| 298 | gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 302 | void grpc_completion_queue_destroy(grpc_completion_queue *cc) { |
Craig Tiller | 5717a98 | 2015-04-27 12:01:49 -0700 | [diff] [blame] | 303 | grpc_cq_internal_unref(cc); |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 304 | } |
| 305 | |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 306 | grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) { |
| 307 | return &cc->pollset; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 308 | } |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 309 | |
| 310 | void grpc_cq_hack_spin_pollset(grpc_completion_queue *cc) { |
| 311 | gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset)); |
| 312 | grpc_pollset_kick(&cc->pollset); |
Craig Tiller | c02c1d8 | 2015-04-07 16:21:55 -0700 | [diff] [blame] | 313 | grpc_pollset_work(&cc->pollset, |
| 314 | gpr_time_add(gpr_now(), gpr_time_from_millis(100))); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 315 | gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); |
| 316 | } |