blob: 49dfc3c0e1c7ca49502629f48f9594b1529fcab4 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
ctiller58393c22015-01-07 14:03:30 -080039#include "src/core/iomgr/pollset.h"
Craig Tiller485d7762015-01-23 12:54:05 -080040#include "src/core/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include "src/core/surface/call.h"
42#include "src/core/surface/event_string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#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 Nobleb7ebd3b2014-11-26 16:33:03 -080047
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070048typedef struct {
49 grpc_pollset_worker *worker;
50 void *tag;
51} plucker;
52
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053/* Completion queue structure */
54struct grpc_completion_queue {
Craig Tiller97fc6a32015-07-08 15:31:35 -070055 /** completed events */
56 grpc_cq_completion completed_head;
57 grpc_cq_completion *completed_tail;
58 /** Number of pending events (+1 if we're not shutdown) */
59 gpr_refcount pending_events;
60 /** Once owning_refs drops to zero, we will destroy the cq */
Craig Tiller5717a982015-04-27 12:01:49 -070061 gpr_refcount owning_refs;
Craig Tiller97fc6a32015-07-08 15:31:35 -070062 /** the set of low level i/o things that concern this cq */
ctillerd79b4862014-12-17 16:36:59 -080063 grpc_pollset pollset;
Craig Tiller97fc6a32015-07-08 15:31:35 -070064 /** 0 initially, 1 once we've begun shutting down */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065 int shutdown;
Craig Tillerf5fd4ba2015-03-02 18:01:21 +000066 int shutdown_called;
Craig Tillerb56975c2015-06-15 10:11:16 -070067 int is_server_cq;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070068 int num_pluckers;
Craig Tiller489df072015-08-01 16:15:45 -070069 plucker pluckers[GRPC_MAX_COMPLETION_QUEUE_PLUCKERS];
Craig Tillerdfff1b82015-09-21 14:39:57 -070070 grpc_closure pollset_destroy_done;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071};
72
Craig Tillerdfff1b82015-09-21 14:39:57 -070073static void on_pollset_destroy_done(void *cc, int success,
74 grpc_call_list *call_list);
75
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +020076grpc_completion_queue *grpc_completion_queue_create(void *reserved) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077 grpc_completion_queue *cc = gpr_malloc(sizeof(grpc_completion_queue));
Nicolas "Pixel" Noble45992882015-07-29 23:52:14 +020078 GPR_ASSERT(!reserved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079 memset(cc, 0, sizeof(*cc));
80 /* Initial ref is dropped by grpc_completion_queue_shutdown */
Craig Tiller97fc6a32015-07-08 15:31:35 -070081 gpr_ref_init(&cc->pending_events, 1);
Craig Tiller70730b42015-05-22 14:42:38 -070082 /* One for destroy(), one for pollset_shutdown */
83 gpr_ref_init(&cc->owning_refs, 2);
ctillerd79b4862014-12-17 16:36:59 -080084 grpc_pollset_init(&cc->pollset);
Craig Tiller97fc6a32015-07-08 15:31:35 -070085 cc->completed_tail = &cc->completed_head;
Craig Tiller12cf5372015-07-09 13:48:11 -070086 cc->completed_head.next = (gpr_uintptr)cc->completed_tail;
Craig Tillerdfff1b82015-09-21 14:39:57 -070087 grpc_closure_init(&cc->pollset_destroy_done, on_pollset_destroy_done, cc);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088 return cc;
89}
90
Craig Tiller463f2372015-05-28 16:16:15 -070091#ifdef GRPC_CQ_REF_COUNT_DEBUG
Nicolas "Pixel" Nobleaaf39ab2015-06-24 23:22:53 +020092void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason,
93 const char *file, int line) {
Craig Tiller12cf5372015-07-09 13:48:11 -070094 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p ref %d -> %d %s", cc,
95 (int)cc->owning_refs.count, (int)cc->owning_refs.count + 1, reason);
Craig Tiller463f2372015-05-28 16:16:15 -070096#else
Craig Tiller5717a982015-04-27 12:01:49 -070097void grpc_cq_internal_ref(grpc_completion_queue *cc) {
Craig Tiller463f2372015-05-28 16:16:15 -070098#endif
Craig Tiller5717a982015-04-27 12:01:49 -070099 gpr_ref(&cc->owning_refs);
100}
101
Craig Tillerdfff1b82015-09-21 14:39:57 -0700102static void on_pollset_destroy_done(void *arg, int success,
103 grpc_call_list *call_list) {
Craig Tiller5717a982015-04-27 12:01:49 -0700104 grpc_completion_queue *cc = arg;
Craig Tiller463f2372015-05-28 16:16:15 -0700105 GRPC_CQ_INTERNAL_UNREF(cc, "pollset_destroy");
Craig Tiller5717a982015-04-27 12:01:49 -0700106}
107
Craig Tiller463f2372015-05-28 16:16:15 -0700108#ifdef GRPC_CQ_REF_COUNT_DEBUG
Nicolas "Pixel" Nobleaaf39ab2015-06-24 23:22:53 +0200109void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason,
110 const char *file, int line) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700111 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p unref %d -> %d %s", cc,
112 (int)cc->owning_refs.count, (int)cc->owning_refs.count - 1, reason);
Craig Tiller463f2372015-05-28 16:16:15 -0700113#else
Craig Tiller5717a982015-04-27 12:01:49 -0700114void grpc_cq_internal_unref(grpc_completion_queue *cc) {
Craig Tiller463f2372015-05-28 16:16:15 -0700115#endif
Craig Tiller5717a982015-04-27 12:01:49 -0700116 if (gpr_unref(&cc->owning_refs)) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700117 GPR_ASSERT(cc->completed_head.next == (gpr_uintptr)&cc->completed_head);
Craig Tiller70730b42015-05-22 14:42:38 -0700118 grpc_pollset_destroy(&cc->pollset);
119 gpr_free(cc);
Craig Tiller5717a982015-04-27 12:01:49 -0700120 }
121}
122
Craig Tiller97fc6a32015-07-08 15:31:35 -0700123void grpc_cq_begin_op(grpc_completion_queue *cc) {
Craig Tiller402acf62015-08-05 10:43:10 -0700124#ifndef NDEBUG
125 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
126 GPR_ASSERT(!cc->shutdown_called);
127 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
128#endif
Craig Tiller97fc6a32015-07-08 15:31:35 -0700129 gpr_ref(&cc->pending_events);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130}
131
132/* Signal the end of an operation - if this is the last waiting-to-be-queued
133 event, then enter shutdown mode */
Craig Tiller97fc6a32015-07-08 15:31:35 -0700134/* Queue a GRPC_OP_COMPLETED operation */
Craig Tiller12cf5372015-07-09 13:48:11 -0700135void grpc_cq_end_op(grpc_completion_queue *cc, void *tag, int success,
Craig Tillerdfff1b82015-09-21 14:39:57 -0700136 void (*done)(void *done_arg, grpc_cq_completion *storage,
137 grpc_call_list *call_list),
138 void *done_arg, grpc_cq_completion *storage,
139 grpc_call_list *call_list) {
Craig Tiller304048c2015-07-17 11:19:58 -0700140 int shutdown;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700141 int i;
142 grpc_pollset_worker *pluck_worker;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700143
144 storage->tag = tag;
145 storage->done = done;
146 storage->done_arg = done_arg;
Craig Tiller12cf5372015-07-09 13:48:11 -0700147 storage->next =
148 ((gpr_uintptr)&cc->completed_head) | ((gpr_uintptr)(success != 0));
Craig Tiller97fc6a32015-07-08 15:31:35 -0700149
Craig Tiller304048c2015-07-17 11:19:58 -0700150 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
151 shutdown = gpr_unref(&cc->pending_events);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700152 if (!shutdown) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700153 cc->completed_tail->next =
154 ((gpr_uintptr)storage) | (1u & (gpr_uintptr)cc->completed_tail->next);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700155 cc->completed_tail = storage;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700156 pluck_worker = NULL;
157 for (i = 0; i < cc->num_pluckers; i++) {
158 if (cc->pluckers[i].tag == tag) {
159 pluck_worker = cc->pluckers[i].worker;
160 break;
161 }
162 }
163 grpc_pollset_kick(&cc->pollset, pluck_worker);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700164 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
165 } else {
Craig Tiller12cf5372015-07-09 13:48:11 -0700166 cc->completed_tail->next =
167 ((gpr_uintptr)storage) | (1u & (gpr_uintptr)cc->completed_tail->next);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700168 cc->completed_tail = storage;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169 GPR_ASSERT(!cc->shutdown);
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000170 GPR_ASSERT(cc->shutdown_called);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800171 cc->shutdown = 1;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700172 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tillerdfff1b82015-09-21 14:39:57 -0700173 grpc_pollset_shutdown(&cc->pollset, &cc->pollset_destroy_done, call_list);
Craig Tiller6f2d6472015-05-22 21:34:15 -0700174 }
Craig Tillercce17ac2015-01-20 09:29:28 -0800175}
176
Craig Tiller64be9f72015-05-04 14:53:51 -0700177grpc_event grpc_completion_queue_next(grpc_completion_queue *cc,
Craig Tiller8f7bff72015-08-17 13:23:14 -0700178 gpr_timespec deadline, void *reserved) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700179 grpc_event ret;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700180 grpc_pollset_worker worker;
Craig Tiller69f90e62015-08-06 08:32:35 -0700181 int first_loop = 1;
182 gpr_timespec now;
Craig Tillerdfff1b82015-09-21 14:39:57 -0700183 grpc_call_list call_list = GRPC_CALL_LIST_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800184
Nicolas "Pixel" Noble45992882015-07-29 23:52:14 +0200185 GPR_ASSERT(!reserved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800186
Craig Tiller6a7626c2015-07-19 22:21:41 -0700187 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
188
Craig Tiller463f2372015-05-28 16:16:15 -0700189 GRPC_CQ_INTERNAL_REF(cc, "next");
ctiller58393c22015-01-07 14:03:30 -0800190 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800191 for (;;) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700192 if (cc->completed_tail != &cc->completed_head) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700193 grpc_cq_completion *c = (grpc_cq_completion *)cc->completed_head.next;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700194 cc->completed_head.next = c->next & ~(gpr_uintptr)1;
195 if (c == cc->completed_tail) {
196 cc->completed_tail = &cc->completed_head;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197 }
Craig Tiller97fc6a32015-07-08 15:31:35 -0700198 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
199 ret.type = GRPC_OP_COMPLETE;
200 ret.success = c->next & 1u;
201 ret.tag = c->tag;
Craig Tillerdfff1b82015-09-21 14:39:57 -0700202 c->done(c->done_arg, c, &call_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800203 break;
204 }
205 if (cc->shutdown) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700206 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
207 memset(&ret, 0, sizeof(ret));
208 ret.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 break;
210 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700211 now = gpr_now(GPR_CLOCK_MONOTONIC);
212 if (!first_loop && gpr_time_cmp(now, deadline) >= 0) {
ctiller58393c22015-01-07 14:03:30 -0800213 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700214 memset(&ret, 0, sizeof(ret));
215 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700216 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800217 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700218 first_loop = 0;
Craig Tillerdfff1b82015-09-21 14:39:57 -0700219 grpc_pollset_work(&cc->pollset, &worker, now, deadline, &call_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800220 }
Craig Tiller64be9f72015-05-04 14:53:51 -0700221 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700222 GRPC_CQ_INTERNAL_UNREF(cc, "next");
Craig Tillerdfff1b82015-09-21 14:39:57 -0700223 grpc_call_list_run(&call_list);
Craig Tiller64be9f72015-05-04 14:53:51 -0700224 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225}
226
Craig Tiller791e78a2015-08-01 16:20:17 -0700227static int add_plucker(grpc_completion_queue *cc, void *tag,
228 grpc_pollset_worker *worker) {
229 if (cc->num_pluckers == GRPC_MAX_COMPLETION_QUEUE_PLUCKERS) {
230 return 0;
231 }
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700232 cc->pluckers[cc->num_pluckers].tag = tag;
233 cc->pluckers[cc->num_pluckers].worker = worker;
234 cc->num_pluckers++;
Craig Tiller791e78a2015-08-01 16:20:17 -0700235 return 1;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700236}
237
238static void del_plucker(grpc_completion_queue *cc, void *tag,
239 grpc_pollset_worker *worker) {
240 int i;
241 for (i = 0; i < cc->num_pluckers; i++) {
242 if (cc->pluckers[i].tag == tag && cc->pluckers[i].worker == worker) {
243 cc->num_pluckers--;
244 GPR_SWAP(plucker, cc->pluckers[i], cc->pluckers[cc->num_pluckers]);
245 return;
246 }
247 }
248 gpr_log(GPR_ERROR, "should never reach here");
249 abort();
250}
251
Craig Tiller64be9f72015-05-04 14:53:51 -0700252grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag,
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200253 gpr_timespec deadline, void *reserved) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700254 grpc_event ret;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700255 grpc_cq_completion *c;
256 grpc_cq_completion *prev;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700257 grpc_pollset_worker worker;
Craig Tiller4c06b822015-08-06 08:41:31 -0700258 gpr_timespec now;
Craig Tiller69f90e62015-08-06 08:32:35 -0700259 int first_loop = 1;
Craig Tillerdfff1b82015-09-21 14:39:57 -0700260 grpc_call_list call_list = GRPC_CALL_LIST_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800261
Nicolas "Pixel" Noble45992882015-07-29 23:52:14 +0200262 GPR_ASSERT(!reserved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800263
Craig Tiller6a7626c2015-07-19 22:21:41 -0700264 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
265
Craig Tiller463f2372015-05-28 16:16:15 -0700266 GRPC_CQ_INTERNAL_REF(cc, "pluck");
ctiller58393c22015-01-07 14:03:30 -0800267 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800268 for (;;) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700269 prev = &cc->completed_head;
Craig Tiller12cf5372015-07-09 13:48:11 -0700270 while ((c = (grpc_cq_completion *)(prev->next & ~(gpr_uintptr)1)) !=
271 &cc->completed_head) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700272 if (c->tag == tag) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700273 prev->next =
274 (prev->next & (gpr_uintptr)1) | (c->next & ~(gpr_uintptr)1);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700275 if (c == cc->completed_tail) {
276 cc->completed_tail = prev;
277 }
278 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
279 ret.type = GRPC_OP_COMPLETE;
280 ret.success = c->next & 1u;
281 ret.tag = c->tag;
Craig Tillerdfff1b82015-09-21 14:39:57 -0700282 c->done(c->done_arg, c, &call_list);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700283 goto done;
284 }
285 prev = c;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800286 }
287 if (cc->shutdown) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700288 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
289 memset(&ret, 0, sizeof(ret));
290 ret.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800291 break;
292 }
Craig Tiller791e78a2015-08-01 16:20:17 -0700293 if (!add_plucker(cc, tag, &worker)) {
Craig Tiller8f7bff72015-08-17 13:23:14 -0700294 gpr_log(GPR_DEBUG,
295 "Too many outstanding grpc_completion_queue_pluck calls: maximum "
296 "is %d",
Craig Tiller791e78a2015-08-01 16:20:17 -0700297 GRPC_MAX_COMPLETION_QUEUE_PLUCKERS);
298 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
299 memset(&ret, 0, sizeof(ret));
300 /* TODO(ctiller): should we use a different result here */
301 ret.type = GRPC_QUEUE_TIMEOUT;
302 break;
303 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700304 now = gpr_now(GPR_CLOCK_MONOTONIC);
305 if (!first_loop && gpr_time_cmp(now, deadline) >= 0) {
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700306 del_plucker(cc, tag, &worker);
ctiller58393c22015-01-07 14:03:30 -0800307 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700308 memset(&ret, 0, sizeof(ret));
309 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700310 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800311 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700312 first_loop = 0;
Craig Tillerdfff1b82015-09-21 14:39:57 -0700313 grpc_pollset_work(&cc->pollset, &worker, now, deadline, &call_list);
Craig Tillerd5689302015-08-06 13:27:22 -0700314 del_plucker(cc, tag, &worker);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800315 }
Craig Tiller97fc6a32015-07-08 15:31:35 -0700316done:
Craig Tiller4751fb12015-05-15 14:42:54 -0700317 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700318 GRPC_CQ_INTERNAL_UNREF(cc, "pluck");
Craig Tillerdfff1b82015-09-21 14:39:57 -0700319 grpc_call_list_run(&call_list);
Craig Tiller64be9f72015-05-04 14:53:51 -0700320 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321}
322
323/* Shutdown simply drops a ref that we reserved at creation time; if we drop
324 to zero here, then enter shutdown mode and wake up any waiters */
325void grpc_completion_queue_shutdown(grpc_completion_queue *cc) {
Craig Tillerdfff1b82015-09-21 14:39:57 -0700326 grpc_call_list call_list = GRPC_CALL_LIST_INIT;
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000327 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller91c069c2015-05-29 11:32:10 -0700328 if (cc->shutdown_called) {
329 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
330 return;
331 }
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000332 cc->shutdown_called = 1;
333 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
334
Craig Tiller97fc6a32015-07-08 15:31:35 -0700335 if (gpr_unref(&cc->pending_events)) {
ctiller58393c22015-01-07 14:03:30 -0800336 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800337 GPR_ASSERT(!cc->shutdown);
338 cc->shutdown = 1;
ctiller58393c22015-01-07 14:03:30 -0800339 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tillerdfff1b82015-09-21 14:39:57 -0700340 grpc_pollset_shutdown(&cc->pollset, &cc->pollset_destroy_done, &call_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800341 }
Craig Tillerdfff1b82015-09-21 14:39:57 -0700342 grpc_call_list_run(&call_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343}
344
David Klempnerb5056612015-02-24 14:22:50 -0800345void grpc_completion_queue_destroy(grpc_completion_queue *cc) {
Craig Tiller91c069c2015-05-29 11:32:10 -0700346 grpc_completion_queue_shutdown(cc);
Craig Tiller463f2372015-05-28 16:16:15 -0700347 GRPC_CQ_INTERNAL_UNREF(cc, "destroy");
David Klempnerb5056612015-02-24 14:22:50 -0800348}
349
ctillerd79b4862014-12-17 16:36:59 -0800350grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) {
351 return &cc->pollset;
Craig Tiller190d3602015-02-18 09:23:38 -0800352}
Craig Tilleraec96aa2015-04-07 14:32:15 -0700353
Craig Tillerb56975c2015-06-15 10:11:16 -0700354void grpc_cq_mark_server_cq(grpc_completion_queue *cc) { cc->is_server_cq = 1; }
355
356int grpc_cq_is_server_cq(grpc_completion_queue *cc) { return cc->is_server_cq; }