blob: cb839b069d57c4fdbe8339577b2c352a2d45c07f [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,
Craig Tillerd9ccbbf2015-09-22 09:30:00 -070074 grpc_closure_list *closure_list);
Craig Tillerdfff1b82015-09-21 14:39:57 -070075
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,
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700103 grpc_closure_list *closure_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,
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700137 grpc_closure_list *closure_list),
Craig Tillerdfff1b82015-09-21 14:39:57 -0700138 void *done_arg, grpc_cq_completion *storage,
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700139 grpc_closure_list *closure_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 Tillerd9ccbbf2015-09-22 09:30:00 -0700173 grpc_pollset_shutdown(&cc->pollset, &cc->pollset_destroy_done,
174 closure_list);
Craig Tiller6f2d6472015-05-22 21:34:15 -0700175 }
Craig Tillercce17ac2015-01-20 09:29:28 -0800176}
177
Craig Tiller64be9f72015-05-04 14:53:51 -0700178grpc_event grpc_completion_queue_next(grpc_completion_queue *cc,
Craig Tiller8f7bff72015-08-17 13:23:14 -0700179 gpr_timespec deadline, void *reserved) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700180 grpc_event ret;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700181 grpc_pollset_worker worker;
Craig Tiller69f90e62015-08-06 08:32:35 -0700182 int first_loop = 1;
183 gpr_timespec now;
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700184 grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185
Nicolas "Pixel" Noble45992882015-07-29 23:52:14 +0200186 GPR_ASSERT(!reserved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187
Craig Tiller6a7626c2015-07-19 22:21:41 -0700188 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
189
Craig Tiller463f2372015-05-28 16:16:15 -0700190 GRPC_CQ_INTERNAL_REF(cc, "next");
ctiller58393c22015-01-07 14:03:30 -0800191 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192 for (;;) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700193 if (cc->completed_tail != &cc->completed_head) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700194 grpc_cq_completion *c = (grpc_cq_completion *)cc->completed_head.next;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700195 cc->completed_head.next = c->next & ~(gpr_uintptr)1;
196 if (c == cc->completed_tail) {
197 cc->completed_tail = &cc->completed_head;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 }
Craig Tiller97fc6a32015-07-08 15:31:35 -0700199 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
200 ret.type = GRPC_OP_COMPLETE;
201 ret.success = c->next & 1u;
202 ret.tag = c->tag;
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700203 c->done(c->done_arg, c, &closure_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204 break;
205 }
206 if (cc->shutdown) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700207 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
208 memset(&ret, 0, sizeof(ret));
209 ret.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210 break;
211 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700212 now = gpr_now(GPR_CLOCK_MONOTONIC);
213 if (!first_loop && gpr_time_cmp(now, deadline) >= 0) {
ctiller58393c22015-01-07 14:03:30 -0800214 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700215 memset(&ret, 0, sizeof(ret));
216 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700217 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800218 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700219 first_loop = 0;
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700220 grpc_pollset_work(&cc->pollset, &worker, now, deadline, &closure_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800221 }
Craig Tiller64be9f72015-05-04 14:53:51 -0700222 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700223 GRPC_CQ_INTERNAL_UNREF(cc, "next");
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700224 grpc_closure_list_run(&closure_list);
Craig Tiller64be9f72015-05-04 14:53:51 -0700225 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800226}
227
Craig Tiller791e78a2015-08-01 16:20:17 -0700228static int add_plucker(grpc_completion_queue *cc, void *tag,
229 grpc_pollset_worker *worker) {
230 if (cc->num_pluckers == GRPC_MAX_COMPLETION_QUEUE_PLUCKERS) {
231 return 0;
232 }
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700233 cc->pluckers[cc->num_pluckers].tag = tag;
234 cc->pluckers[cc->num_pluckers].worker = worker;
235 cc->num_pluckers++;
Craig Tiller791e78a2015-08-01 16:20:17 -0700236 return 1;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700237}
238
239static void del_plucker(grpc_completion_queue *cc, void *tag,
240 grpc_pollset_worker *worker) {
241 int i;
242 for (i = 0; i < cc->num_pluckers; i++) {
243 if (cc->pluckers[i].tag == tag && cc->pluckers[i].worker == worker) {
244 cc->num_pluckers--;
245 GPR_SWAP(plucker, cc->pluckers[i], cc->pluckers[cc->num_pluckers]);
246 return;
247 }
248 }
249 gpr_log(GPR_ERROR, "should never reach here");
250 abort();
251}
252
Craig Tiller64be9f72015-05-04 14:53:51 -0700253grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag,
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200254 gpr_timespec deadline, void *reserved) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700255 grpc_event ret;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700256 grpc_cq_completion *c;
257 grpc_cq_completion *prev;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700258 grpc_pollset_worker worker;
Craig Tiller4c06b822015-08-06 08:41:31 -0700259 gpr_timespec now;
Craig Tiller69f90e62015-08-06 08:32:35 -0700260 int first_loop = 1;
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700261 grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800262
Nicolas "Pixel" Noble45992882015-07-29 23:52:14 +0200263 GPR_ASSERT(!reserved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800264
Craig Tiller6a7626c2015-07-19 22:21:41 -0700265 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
266
Craig Tiller463f2372015-05-28 16:16:15 -0700267 GRPC_CQ_INTERNAL_REF(cc, "pluck");
ctiller58393c22015-01-07 14:03:30 -0800268 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800269 for (;;) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700270 prev = &cc->completed_head;
Craig Tiller12cf5372015-07-09 13:48:11 -0700271 while ((c = (grpc_cq_completion *)(prev->next & ~(gpr_uintptr)1)) !=
272 &cc->completed_head) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700273 if (c->tag == tag) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700274 prev->next =
275 (prev->next & (gpr_uintptr)1) | (c->next & ~(gpr_uintptr)1);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700276 if (c == cc->completed_tail) {
277 cc->completed_tail = prev;
278 }
279 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
280 ret.type = GRPC_OP_COMPLETE;
281 ret.success = c->next & 1u;
282 ret.tag = c->tag;
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700283 c->done(c->done_arg, c, &closure_list);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700284 goto done;
285 }
286 prev = c;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800287 }
288 if (cc->shutdown) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700289 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
290 memset(&ret, 0, sizeof(ret));
291 ret.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800292 break;
293 }
Craig Tiller791e78a2015-08-01 16:20:17 -0700294 if (!add_plucker(cc, tag, &worker)) {
Craig Tiller8f7bff72015-08-17 13:23:14 -0700295 gpr_log(GPR_DEBUG,
296 "Too many outstanding grpc_completion_queue_pluck calls: maximum "
297 "is %d",
Craig Tiller791e78a2015-08-01 16:20:17 -0700298 GRPC_MAX_COMPLETION_QUEUE_PLUCKERS);
299 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
300 memset(&ret, 0, sizeof(ret));
301 /* TODO(ctiller): should we use a different result here */
302 ret.type = GRPC_QUEUE_TIMEOUT;
303 break;
304 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700305 now = gpr_now(GPR_CLOCK_MONOTONIC);
306 if (!first_loop && gpr_time_cmp(now, deadline) >= 0) {
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700307 del_plucker(cc, tag, &worker);
ctiller58393c22015-01-07 14:03:30 -0800308 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700309 memset(&ret, 0, sizeof(ret));
310 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700311 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800312 }
Craig Tiller69f90e62015-08-06 08:32:35 -0700313 first_loop = 0;
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700314 grpc_pollset_work(&cc->pollset, &worker, now, deadline, &closure_list);
Craig Tillerd5689302015-08-06 13:27:22 -0700315 del_plucker(cc, tag, &worker);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800316 }
Craig Tiller97fc6a32015-07-08 15:31:35 -0700317done:
Craig Tiller4751fb12015-05-15 14:42:54 -0700318 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700319 GRPC_CQ_INTERNAL_UNREF(cc, "pluck");
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700320 grpc_closure_list_run(&closure_list);
Craig Tiller64be9f72015-05-04 14:53:51 -0700321 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800322}
323
324/* Shutdown simply drops a ref that we reserved at creation time; if we drop
325 to zero here, then enter shutdown mode and wake up any waiters */
326void grpc_completion_queue_shutdown(grpc_completion_queue *cc) {
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700327 grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT;
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000328 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller91c069c2015-05-29 11:32:10 -0700329 if (cc->shutdown_called) {
330 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
331 return;
332 }
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000333 cc->shutdown_called = 1;
334 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
335
Craig Tiller97fc6a32015-07-08 15:31:35 -0700336 if (gpr_unref(&cc->pending_events)) {
ctiller58393c22015-01-07 14:03:30 -0800337 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800338 GPR_ASSERT(!cc->shutdown);
339 cc->shutdown = 1;
ctiller58393c22015-01-07 14:03:30 -0800340 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700341 grpc_pollset_shutdown(&cc->pollset, &cc->pollset_destroy_done,
342 &closure_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343 }
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700344 grpc_closure_list_run(&closure_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800345}
346
David Klempnerb5056612015-02-24 14:22:50 -0800347void grpc_completion_queue_destroy(grpc_completion_queue *cc) {
Craig Tiller91c069c2015-05-29 11:32:10 -0700348 grpc_completion_queue_shutdown(cc);
Craig Tiller463f2372015-05-28 16:16:15 -0700349 GRPC_CQ_INTERNAL_UNREF(cc, "destroy");
David Klempnerb5056612015-02-24 14:22:50 -0800350}
351
ctillerd79b4862014-12-17 16:36:59 -0800352grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) {
353 return &cc->pollset;
Craig Tiller190d3602015-02-18 09:23:38 -0800354}
Craig Tilleraec96aa2015-04-07 14:32:15 -0700355
Craig Tillerb56975c2015-06-15 10:11:16 -0700356void grpc_cq_mark_server_cq(grpc_completion_queue *cc) { cc->is_server_cq = 1; }
357
358int grpc_cq_is_server_cq(grpc_completion_queue *cc) { return cc->is_server_cq; }