blob: 9d6f78db5595cbad32519e46027dfc788969733b [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];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070};
71
Craig Tiller32946d32015-01-15 11:37:30 -080072grpc_completion_queue *grpc_completion_queue_create(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073 grpc_completion_queue *cc = gpr_malloc(sizeof(grpc_completion_queue));
74 memset(cc, 0, sizeof(*cc));
75 /* Initial ref is dropped by grpc_completion_queue_shutdown */
Craig Tiller97fc6a32015-07-08 15:31:35 -070076 gpr_ref_init(&cc->pending_events, 1);
Craig Tiller70730b42015-05-22 14:42:38 -070077 /* One for destroy(), one for pollset_shutdown */
78 gpr_ref_init(&cc->owning_refs, 2);
ctillerd79b4862014-12-17 16:36:59 -080079 grpc_pollset_init(&cc->pollset);
Craig Tiller97fc6a32015-07-08 15:31:35 -070080 cc->completed_tail = &cc->completed_head;
Craig Tiller12cf5372015-07-09 13:48:11 -070081 cc->completed_head.next = (gpr_uintptr)cc->completed_tail;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080082 return cc;
83}
84
Craig Tiller463f2372015-05-28 16:16:15 -070085#ifdef GRPC_CQ_REF_COUNT_DEBUG
Nicolas "Pixel" Nobleaaf39ab2015-06-24 23:22:53 +020086void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason,
87 const char *file, int line) {
Craig Tiller12cf5372015-07-09 13:48:11 -070088 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p ref %d -> %d %s", cc,
89 (int)cc->owning_refs.count, (int)cc->owning_refs.count + 1, reason);
Craig Tiller463f2372015-05-28 16:16:15 -070090#else
Craig Tiller5717a982015-04-27 12:01:49 -070091void grpc_cq_internal_ref(grpc_completion_queue *cc) {
Craig Tiller463f2372015-05-28 16:16:15 -070092#endif
Craig Tiller5717a982015-04-27 12:01:49 -070093 gpr_ref(&cc->owning_refs);
94}
95
96static void on_pollset_destroy_done(void *arg) {
97 grpc_completion_queue *cc = arg;
Craig Tiller463f2372015-05-28 16:16:15 -070098 GRPC_CQ_INTERNAL_UNREF(cc, "pollset_destroy");
Craig Tiller5717a982015-04-27 12:01:49 -070099}
100
Craig Tiller463f2372015-05-28 16:16:15 -0700101#ifdef GRPC_CQ_REF_COUNT_DEBUG
Nicolas "Pixel" Nobleaaf39ab2015-06-24 23:22:53 +0200102void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason,
103 const char *file, int line) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700104 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p unref %d -> %d %s", cc,
105 (int)cc->owning_refs.count, (int)cc->owning_refs.count - 1, reason);
Craig Tiller463f2372015-05-28 16:16:15 -0700106#else
Craig Tiller5717a982015-04-27 12:01:49 -0700107void grpc_cq_internal_unref(grpc_completion_queue *cc) {
Craig Tiller463f2372015-05-28 16:16:15 -0700108#endif
Craig Tiller5717a982015-04-27 12:01:49 -0700109 if (gpr_unref(&cc->owning_refs)) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700110 GPR_ASSERT(cc->completed_head.next == (gpr_uintptr)&cc->completed_head);
Craig Tiller70730b42015-05-22 14:42:38 -0700111 grpc_pollset_destroy(&cc->pollset);
112 gpr_free(cc);
Craig Tiller5717a982015-04-27 12:01:49 -0700113 }
114}
115
Craig Tiller97fc6a32015-07-08 15:31:35 -0700116void grpc_cq_begin_op(grpc_completion_queue *cc) {
117 gpr_ref(&cc->pending_events);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800118}
119
120/* Signal the end of an operation - if this is the last waiting-to-be-queued
121 event, then enter shutdown mode */
Craig Tiller97fc6a32015-07-08 15:31:35 -0700122/* Queue a GRPC_OP_COMPLETED operation */
Craig Tiller12cf5372015-07-09 13:48:11 -0700123void grpc_cq_end_op(grpc_completion_queue *cc, void *tag, int success,
124 void (*done)(void *done_arg, grpc_cq_completion *storage),
125 void *done_arg, grpc_cq_completion *storage) {
Craig Tiller304048c2015-07-17 11:19:58 -0700126 int shutdown;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700127 int i;
128 grpc_pollset_worker *pluck_worker;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700129
130 storage->tag = tag;
131 storage->done = done;
132 storage->done_arg = done_arg;
Craig Tiller12cf5372015-07-09 13:48:11 -0700133 storage->next =
134 ((gpr_uintptr)&cc->completed_head) | ((gpr_uintptr)(success != 0));
Craig Tiller97fc6a32015-07-08 15:31:35 -0700135
Craig Tiller304048c2015-07-17 11:19:58 -0700136 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
137 shutdown = gpr_unref(&cc->pending_events);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700138 if (!shutdown) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700139 cc->completed_tail->next =
140 ((gpr_uintptr)storage) | (1u & (gpr_uintptr)cc->completed_tail->next);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700141 cc->completed_tail = storage;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700142 pluck_worker = NULL;
143 for (i = 0; i < cc->num_pluckers; i++) {
144 if (cc->pluckers[i].tag == tag) {
145 pluck_worker = cc->pluckers[i].worker;
146 break;
147 }
148 }
149 grpc_pollset_kick(&cc->pollset, pluck_worker);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700150 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
151 } else {
Craig Tiller12cf5372015-07-09 13:48:11 -0700152 cc->completed_tail->next =
153 ((gpr_uintptr)storage) | (1u & (gpr_uintptr)cc->completed_tail->next);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700154 cc->completed_tail = storage;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800155 GPR_ASSERT(!cc->shutdown);
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000156 GPR_ASSERT(cc->shutdown_called);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157 cc->shutdown = 1;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700158 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller70730b42015-05-22 14:42:38 -0700159 grpc_pollset_shutdown(&cc->pollset, on_pollset_destroy_done, cc);
Craig Tiller6f2d6472015-05-22 21:34:15 -0700160 }
Craig Tillercce17ac2015-01-20 09:29:28 -0800161}
162
Craig Tiller64be9f72015-05-04 14:53:51 -0700163grpc_event grpc_completion_queue_next(grpc_completion_queue *cc,
164 gpr_timespec deadline) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700165 grpc_event ret;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700166 grpc_pollset_worker worker;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800167
Craig Tiller6a7626c2015-07-19 22:21:41 -0700168 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
169
Craig Tiller463f2372015-05-28 16:16:15 -0700170 GRPC_CQ_INTERNAL_REF(cc, "next");
ctiller58393c22015-01-07 14:03:30 -0800171 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800172 for (;;) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700173 if (cc->completed_tail != &cc->completed_head) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700174 grpc_cq_completion *c = (grpc_cq_completion *)cc->completed_head.next;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700175 cc->completed_head.next = c->next & ~(gpr_uintptr)1;
176 if (c == cc->completed_tail) {
177 cc->completed_tail = &cc->completed_head;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178 }
Craig Tiller97fc6a32015-07-08 15:31:35 -0700179 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
180 ret.type = GRPC_OP_COMPLETE;
181 ret.success = c->next & 1u;
182 ret.tag = c->tag;
183 c->done(c->done_arg, c);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800184 break;
185 }
186 if (cc->shutdown) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700187 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
188 memset(&ret, 0, sizeof(ret));
189 ret.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800190 break;
191 }
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700192 if (!grpc_pollset_work(&cc->pollset, &worker, deadline)) {
ctiller58393c22015-01-07 14:03:30 -0800193 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700194 memset(&ret, 0, sizeof(ret));
195 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700196 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197 }
198 }
Craig Tiller64be9f72015-05-04 14:53:51 -0700199 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700200 GRPC_CQ_INTERNAL_UNREF(cc, "next");
Craig Tiller64be9f72015-05-04 14:53:51 -0700201 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800202}
203
Craig Tiller791e78a2015-08-01 16:20:17 -0700204static int add_plucker(grpc_completion_queue *cc, void *tag,
205 grpc_pollset_worker *worker) {
206 if (cc->num_pluckers == GRPC_MAX_COMPLETION_QUEUE_PLUCKERS) {
207 return 0;
208 }
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700209 cc->pluckers[cc->num_pluckers].tag = tag;
210 cc->pluckers[cc->num_pluckers].worker = worker;
211 cc->num_pluckers++;
Craig Tiller791e78a2015-08-01 16:20:17 -0700212 return 1;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700213}
214
215static void del_plucker(grpc_completion_queue *cc, void *tag,
216 grpc_pollset_worker *worker) {
217 int i;
218 for (i = 0; i < cc->num_pluckers; i++) {
219 if (cc->pluckers[i].tag == tag && cc->pluckers[i].worker == worker) {
220 cc->num_pluckers--;
221 GPR_SWAP(plucker, cc->pluckers[i], cc->pluckers[cc->num_pluckers]);
222 return;
223 }
224 }
225 gpr_log(GPR_ERROR, "should never reach here");
226 abort();
227}
228
Craig Tiller64be9f72015-05-04 14:53:51 -0700229grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag,
230 gpr_timespec deadline) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700231 grpc_event ret;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700232 grpc_cq_completion *c;
233 grpc_cq_completion *prev;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700234 grpc_pollset_worker worker;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800235
Craig Tiller6a7626c2015-07-19 22:21:41 -0700236 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
237
Craig Tiller463f2372015-05-28 16:16:15 -0700238 GRPC_CQ_INTERNAL_REF(cc, "pluck");
ctiller58393c22015-01-07 14:03:30 -0800239 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800240 for (;;) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700241 prev = &cc->completed_head;
Craig Tiller12cf5372015-07-09 13:48:11 -0700242 while ((c = (grpc_cq_completion *)(prev->next & ~(gpr_uintptr)1)) !=
243 &cc->completed_head) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700244 if (c->tag == tag) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700245 prev->next =
246 (prev->next & (gpr_uintptr)1) | (c->next & ~(gpr_uintptr)1);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700247 if (c == cc->completed_tail) {
248 cc->completed_tail = prev;
249 }
250 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
251 ret.type = GRPC_OP_COMPLETE;
252 ret.success = c->next & 1u;
253 ret.tag = c->tag;
254 c->done(c->done_arg, c);
255 goto done;
256 }
257 prev = c;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258 }
259 if (cc->shutdown) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700260 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
261 memset(&ret, 0, sizeof(ret));
262 ret.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800263 break;
264 }
Craig Tiller791e78a2015-08-01 16:20:17 -0700265 if (!add_plucker(cc, tag, &worker)) {
266 gpr_log(GPR_DEBUG,
267 "Too many outstanding grpc_completion_queue_pluck calls: maximum is %d".
268 GRPC_MAX_COMPLETION_QUEUE_PLUCKERS);
269 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
270 memset(&ret, 0, sizeof(ret));
271 /* TODO(ctiller): should we use a different result here */
272 ret.type = GRPC_QUEUE_TIMEOUT;
273 break;
274 }
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700275 if (!grpc_pollset_work(&cc->pollset, &worker, deadline)) {
276 del_plucker(cc, tag, &worker);
ctiller58393c22015-01-07 14:03:30 -0800277 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700278 memset(&ret, 0, sizeof(ret));
279 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700280 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800281 }
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700282 del_plucker(cc, tag, &worker);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800283 }
Craig Tiller97fc6a32015-07-08 15:31:35 -0700284done:
Craig Tiller4751fb12015-05-15 14:42:54 -0700285 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700286 GRPC_CQ_INTERNAL_UNREF(cc, "pluck");
Craig Tiller64be9f72015-05-04 14:53:51 -0700287 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800288}
289
290/* Shutdown simply drops a ref that we reserved at creation time; if we drop
291 to zero here, then enter shutdown mode and wake up any waiters */
292void grpc_completion_queue_shutdown(grpc_completion_queue *cc) {
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000293 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller91c069c2015-05-29 11:32:10 -0700294 if (cc->shutdown_called) {
295 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
296 return;
297 }
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000298 cc->shutdown_called = 1;
299 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
300
Craig Tiller97fc6a32015-07-08 15:31:35 -0700301 if (gpr_unref(&cc->pending_events)) {
ctiller58393c22015-01-07 14:03:30 -0800302 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303 GPR_ASSERT(!cc->shutdown);
304 cc->shutdown = 1;
ctiller58393c22015-01-07 14:03:30 -0800305 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller70730b42015-05-22 14:42:38 -0700306 grpc_pollset_shutdown(&cc->pollset, on_pollset_destroy_done, cc);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800307 }
308}
309
David Klempnerb5056612015-02-24 14:22:50 -0800310void grpc_completion_queue_destroy(grpc_completion_queue *cc) {
Craig Tiller91c069c2015-05-29 11:32:10 -0700311 grpc_completion_queue_shutdown(cc);
Craig Tiller463f2372015-05-28 16:16:15 -0700312 GRPC_CQ_INTERNAL_UNREF(cc, "destroy");
David Klempnerb5056612015-02-24 14:22:50 -0800313}
314
ctillerd79b4862014-12-17 16:36:59 -0800315grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) {
316 return &cc->pollset;
Craig Tiller190d3602015-02-18 09:23:38 -0800317}
Craig Tilleraec96aa2015-04-07 14:32:15 -0700318
Craig Tillerb56975c2015-06-15 10:11:16 -0700319void grpc_cq_mark_server_cq(grpc_completion_queue *cc) { cc->is_server_cq = 1; }
320
321int grpc_cq_is_server_cq(grpc_completion_queue *cc) { return cc->is_server_cq; }