blob: 732813fed0ec850e135b3023f8304c740df84504 [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 -070048#define MAX_PLUCKERS 4
49
50typedef struct {
51 grpc_pollset_worker *worker;
52 void *tag;
53} plucker;
54
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055/* Completion queue structure */
56struct grpc_completion_queue {
Craig Tiller97fc6a32015-07-08 15:31:35 -070057 /** completed events */
58 grpc_cq_completion completed_head;
59 grpc_cq_completion *completed_tail;
60 /** Number of pending events (+1 if we're not shutdown) */
61 gpr_refcount pending_events;
62 /** Once owning_refs drops to zero, we will destroy the cq */
Craig Tiller5717a982015-04-27 12:01:49 -070063 gpr_refcount owning_refs;
Craig Tiller97fc6a32015-07-08 15:31:35 -070064 /** the set of low level i/o things that concern this cq */
ctillerd79b4862014-12-17 16:36:59 -080065 grpc_pollset pollset;
Craig Tiller97fc6a32015-07-08 15:31:35 -070066 /** 0 initially, 1 once we've begun shutting down */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 int shutdown;
Craig Tillerf5fd4ba2015-03-02 18:01:21 +000068 int shutdown_called;
Craig Tillerb56975c2015-06-15 10:11:16 -070069 int is_server_cq;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070070 int num_pluckers;
71 plucker pluckers[MAX_PLUCKERS];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072};
73
Craig Tiller32946d32015-01-15 11:37:30 -080074grpc_completion_queue *grpc_completion_queue_create(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 grpc_completion_queue *cc = gpr_malloc(sizeof(grpc_completion_queue));
76 memset(cc, 0, sizeof(*cc));
77 /* Initial ref is dropped by grpc_completion_queue_shutdown */
Craig Tiller97fc6a32015-07-08 15:31:35 -070078 gpr_ref_init(&cc->pending_events, 1);
Craig Tiller70730b42015-05-22 14:42:38 -070079 /* One for destroy(), one for pollset_shutdown */
80 gpr_ref_init(&cc->owning_refs, 2);
ctillerd79b4862014-12-17 16:36:59 -080081 grpc_pollset_init(&cc->pollset);
Craig Tiller97fc6a32015-07-08 15:31:35 -070082 cc->completed_tail = &cc->completed_head;
Craig Tiller12cf5372015-07-09 13:48:11 -070083 cc->completed_head.next = (gpr_uintptr)cc->completed_tail;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084 return cc;
85}
86
Craig Tiller463f2372015-05-28 16:16:15 -070087#ifdef GRPC_CQ_REF_COUNT_DEBUG
Nicolas "Pixel" Nobleaaf39ab2015-06-24 23:22:53 +020088void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason,
89 const char *file, int line) {
Craig Tiller12cf5372015-07-09 13:48:11 -070090 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p ref %d -> %d %s", cc,
91 (int)cc->owning_refs.count, (int)cc->owning_refs.count + 1, reason);
Craig Tiller463f2372015-05-28 16:16:15 -070092#else
Craig Tiller5717a982015-04-27 12:01:49 -070093void grpc_cq_internal_ref(grpc_completion_queue *cc) {
Craig Tiller463f2372015-05-28 16:16:15 -070094#endif
Craig Tiller5717a982015-04-27 12:01:49 -070095 gpr_ref(&cc->owning_refs);
96}
97
98static void on_pollset_destroy_done(void *arg) {
99 grpc_completion_queue *cc = arg;
Craig Tiller463f2372015-05-28 16:16:15 -0700100 GRPC_CQ_INTERNAL_UNREF(cc, "pollset_destroy");
Craig Tiller5717a982015-04-27 12:01:49 -0700101}
102
Craig Tiller463f2372015-05-28 16:16:15 -0700103#ifdef GRPC_CQ_REF_COUNT_DEBUG
Nicolas "Pixel" Nobleaaf39ab2015-06-24 23:22:53 +0200104void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason,
105 const char *file, int line) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700106 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p unref %d -> %d %s", cc,
107 (int)cc->owning_refs.count, (int)cc->owning_refs.count - 1, reason);
Craig Tiller463f2372015-05-28 16:16:15 -0700108#else
Craig Tiller5717a982015-04-27 12:01:49 -0700109void grpc_cq_internal_unref(grpc_completion_queue *cc) {
Craig Tiller463f2372015-05-28 16:16:15 -0700110#endif
Craig Tiller5717a982015-04-27 12:01:49 -0700111 if (gpr_unref(&cc->owning_refs)) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700112 GPR_ASSERT(cc->completed_head.next == (gpr_uintptr)&cc->completed_head);
Craig Tiller70730b42015-05-22 14:42:38 -0700113 grpc_pollset_destroy(&cc->pollset);
114 gpr_free(cc);
Craig Tiller5717a982015-04-27 12:01:49 -0700115 }
116}
117
Craig Tiller97fc6a32015-07-08 15:31:35 -0700118void grpc_cq_begin_op(grpc_completion_queue *cc) {
119 gpr_ref(&cc->pending_events);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120}
121
122/* Signal the end of an operation - if this is the last waiting-to-be-queued
123 event, then enter shutdown mode */
Craig Tiller97fc6a32015-07-08 15:31:35 -0700124/* Queue a GRPC_OP_COMPLETED operation */
Craig Tiller12cf5372015-07-09 13:48:11 -0700125void grpc_cq_end_op(grpc_completion_queue *cc, void *tag, int success,
126 void (*done)(void *done_arg, grpc_cq_completion *storage),
127 void *done_arg, grpc_cq_completion *storage) {
Craig Tiller304048c2015-07-17 11:19:58 -0700128 int shutdown;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700129 int i;
130 grpc_pollset_worker *pluck_worker;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700131
132 storage->tag = tag;
133 storage->done = done;
134 storage->done_arg = done_arg;
Craig Tiller12cf5372015-07-09 13:48:11 -0700135 storage->next =
136 ((gpr_uintptr)&cc->completed_head) | ((gpr_uintptr)(success != 0));
Craig Tiller97fc6a32015-07-08 15:31:35 -0700137
Craig Tiller304048c2015-07-17 11:19:58 -0700138 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
139 shutdown = gpr_unref(&cc->pending_events);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700140 if (!shutdown) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700141 cc->completed_tail->next =
142 ((gpr_uintptr)storage) | (1u & (gpr_uintptr)cc->completed_tail->next);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700143 cc->completed_tail = storage;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700144 pluck_worker = NULL;
145 for (i = 0; i < cc->num_pluckers; i++) {
146 if (cc->pluckers[i].tag == tag) {
147 pluck_worker = cc->pluckers[i].worker;
148 break;
149 }
150 }
151 grpc_pollset_kick(&cc->pollset, pluck_worker);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700152 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
153 } else {
Craig Tiller12cf5372015-07-09 13:48:11 -0700154 cc->completed_tail->next =
155 ((gpr_uintptr)storage) | (1u & (gpr_uintptr)cc->completed_tail->next);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700156 cc->completed_tail = storage;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157 GPR_ASSERT(!cc->shutdown);
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000158 GPR_ASSERT(cc->shutdown_called);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800159 cc->shutdown = 1;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700160 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller70730b42015-05-22 14:42:38 -0700161 grpc_pollset_shutdown(&cc->pollset, on_pollset_destroy_done, cc);
Craig Tiller6f2d6472015-05-22 21:34:15 -0700162 }
Craig Tillercce17ac2015-01-20 09:29:28 -0800163}
164
Craig Tiller64be9f72015-05-04 14:53:51 -0700165grpc_event grpc_completion_queue_next(grpc_completion_queue *cc,
166 gpr_timespec deadline) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700167 grpc_event ret;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700168 grpc_pollset_worker worker;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169
Craig Tiller6a7626c2015-07-19 22:21:41 -0700170 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
171
Craig Tiller463f2372015-05-28 16:16:15 -0700172 GRPC_CQ_INTERNAL_REF(cc, "next");
ctiller58393c22015-01-07 14:03:30 -0800173 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174 for (;;) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700175 if (cc->completed_tail != &cc->completed_head) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700176 grpc_cq_completion *c = (grpc_cq_completion *)cc->completed_head.next;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700177 cc->completed_head.next = c->next & ~(gpr_uintptr)1;
178 if (c == cc->completed_tail) {
179 cc->completed_tail = &cc->completed_head;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180 }
Craig Tiller97fc6a32015-07-08 15:31:35 -0700181 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
182 ret.type = GRPC_OP_COMPLETE;
183 ret.success = c->next & 1u;
184 ret.tag = c->tag;
185 c->done(c->done_arg, c);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800186 break;
187 }
188 if (cc->shutdown) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700189 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
190 memset(&ret, 0, sizeof(ret));
191 ret.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192 break;
193 }
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700194 if (!grpc_pollset_work(&cc->pollset, &worker, deadline)) {
ctiller58393c22015-01-07 14:03:30 -0800195 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700196 memset(&ret, 0, sizeof(ret));
197 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700198 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199 }
200 }
Craig Tiller64be9f72015-05-04 14:53:51 -0700201 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700202 GRPC_CQ_INTERNAL_UNREF(cc, "next");
Craig Tiller64be9f72015-05-04 14:53:51 -0700203 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204}
205
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700206static void add_plucker(grpc_completion_queue *cc, void *tag,
207 grpc_pollset_worker *worker) {
208 GPR_ASSERT(cc->num_pluckers != MAX_PLUCKERS);
209 cc->pluckers[cc->num_pluckers].tag = tag;
210 cc->pluckers[cc->num_pluckers].worker = worker;
211 cc->num_pluckers++;
212}
213
214static void del_plucker(grpc_completion_queue *cc, void *tag,
215 grpc_pollset_worker *worker) {
216 int i;
217 for (i = 0; i < cc->num_pluckers; i++) {
218 if (cc->pluckers[i].tag == tag && cc->pluckers[i].worker == worker) {
219 cc->num_pluckers--;
220 GPR_SWAP(plucker, cc->pluckers[i], cc->pluckers[cc->num_pluckers]);
221 return;
222 }
223 }
224 gpr_log(GPR_ERROR, "should never reach here");
225 abort();
226}
227
Craig Tiller64be9f72015-05-04 14:53:51 -0700228grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag,
229 gpr_timespec deadline) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700230 grpc_event ret;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700231 grpc_cq_completion *c;
232 grpc_cq_completion *prev;
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700233 grpc_pollset_worker worker;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800234
Craig Tiller6a7626c2015-07-19 22:21:41 -0700235 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
236
Craig Tiller463f2372015-05-28 16:16:15 -0700237 GRPC_CQ_INTERNAL_REF(cc, "pluck");
ctiller58393c22015-01-07 14:03:30 -0800238 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239 for (;;) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700240 prev = &cc->completed_head;
Craig Tiller12cf5372015-07-09 13:48:11 -0700241 while ((c = (grpc_cq_completion *)(prev->next & ~(gpr_uintptr)1)) !=
242 &cc->completed_head) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700243 if (c->tag == tag) {
Craig Tiller12cf5372015-07-09 13:48:11 -0700244 prev->next =
245 (prev->next & (gpr_uintptr)1) | (c->next & ~(gpr_uintptr)1);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700246 if (c == cc->completed_tail) {
247 cc->completed_tail = prev;
248 }
249 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
250 ret.type = GRPC_OP_COMPLETE;
251 ret.success = c->next & 1u;
252 ret.tag = c->tag;
253 c->done(c->done_arg, c);
254 goto done;
255 }
256 prev = c;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800257 }
258 if (cc->shutdown) {
Craig Tiller97fc6a32015-07-08 15:31:35 -0700259 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
260 memset(&ret, 0, sizeof(ret));
261 ret.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800262 break;
263 }
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700264 add_plucker(cc, tag, &worker);
265 if (!grpc_pollset_work(&cc->pollset, &worker, deadline)) {
266 del_plucker(cc, tag, &worker);
ctiller58393c22015-01-07 14:03:30 -0800267 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700268 memset(&ret, 0, sizeof(ret));
269 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700270 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800271 }
Craig Tiller5ddbb9d2015-07-29 15:58:11 -0700272 del_plucker(cc, tag, &worker);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800273 }
Craig Tiller97fc6a32015-07-08 15:31:35 -0700274done:
Craig Tiller4751fb12015-05-15 14:42:54 -0700275 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700276 GRPC_CQ_INTERNAL_UNREF(cc, "pluck");
Craig Tiller64be9f72015-05-04 14:53:51 -0700277 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800278}
279
280/* Shutdown simply drops a ref that we reserved at creation time; if we drop
281 to zero here, then enter shutdown mode and wake up any waiters */
282void grpc_completion_queue_shutdown(grpc_completion_queue *cc) {
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000283 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller91c069c2015-05-29 11:32:10 -0700284 if (cc->shutdown_called) {
285 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
286 return;
287 }
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000288 cc->shutdown_called = 1;
289 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
290
Craig Tiller97fc6a32015-07-08 15:31:35 -0700291 if (gpr_unref(&cc->pending_events)) {
ctiller58393c22015-01-07 14:03:30 -0800292 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800293 GPR_ASSERT(!cc->shutdown);
294 cc->shutdown = 1;
ctiller58393c22015-01-07 14:03:30 -0800295 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller70730b42015-05-22 14:42:38 -0700296 grpc_pollset_shutdown(&cc->pollset, on_pollset_destroy_done, cc);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800297 }
298}
299
David Klempnerb5056612015-02-24 14:22:50 -0800300void grpc_completion_queue_destroy(grpc_completion_queue *cc) {
Craig Tiller91c069c2015-05-29 11:32:10 -0700301 grpc_completion_queue_shutdown(cc);
Craig Tiller463f2372015-05-28 16:16:15 -0700302 GRPC_CQ_INTERNAL_UNREF(cc, "destroy");
David Klempnerb5056612015-02-24 14:22:50 -0800303}
304
ctillerd79b4862014-12-17 16:36:59 -0800305grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) {
306 return &cc->pollset;
Craig Tiller190d3602015-02-18 09:23:38 -0800307}
Craig Tilleraec96aa2015-04-07 14:32:15 -0700308
Craig Tillerb56975c2015-06-15 10:11:16 -0700309void grpc_cq_mark_server_cq(grpc_completion_queue *cc) { cc->is_server_cq = 1; }
310
311int grpc_cq_is_server_cq(grpc_completion_queue *cc) { return cc->is_server_cq; }