blob: 3ab2dfcb05ef146d2f0dd9b1447908960345054c [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
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 */
52typedef struct event {
53 grpc_event base;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054 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 */
61struct grpc_completion_queue {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062 /* When refs drops to zero, we are in shutdown mode, and will be destroyable
63 once all queued events are drained */
64 gpr_refcount refs;
Craig Tiller5717a982015-04-27 12:01:49 -070065 /* Once owning_refs drops to zero, we will destroy the cq */
66 gpr_refcount owning_refs;
ctillerd79b4862014-12-17 16:36:59 -080067 /* the set of low level i/o things that concern this cq */
68 grpc_pollset pollset;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069 /* 0 initially, 1 once we've begun shutting down */
70 int shutdown;
Craig Tillerf5fd4ba2015-03-02 18:01:21 +000071 int shutdown_called;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072 /* Head of a linked list of queued events (prev points to the last element) */
73 event *queue;
74 /* Fixed size chained hash table of events for pluck() */
75 event *buckets[NUM_TAG_BUCKETS];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076};
77
Craig Tiller32946d32015-01-15 11:37:30 -080078grpc_completion_queue *grpc_completion_queue_create(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079 grpc_completion_queue *cc = gpr_malloc(sizeof(grpc_completion_queue));
80 memset(cc, 0, sizeof(*cc));
81 /* Initial ref is dropped by grpc_completion_queue_shutdown */
82 gpr_ref_init(&cc->refs, 1);
Craig Tiller70730b42015-05-22 14:42:38 -070083 /* One for destroy(), one for pollset_shutdown */
84 gpr_ref_init(&cc->owning_refs, 2);
ctillerd79b4862014-12-17 16:36:59 -080085 grpc_pollset_init(&cc->pollset);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086 return cc;
87}
88
Craig Tiller463f2372015-05-28 16:16:15 -070089
90
91
92
93
94
95
96
97
98
99
100
101#ifdef GRPC_CQ_REF_COUNT_DEBUG
102void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason) {
103 gpr_log(GPR_DEBUG, "CQ:%p ref %d -> %d %s", cc, (int)cc->owning_refs.count, (int)cc->owning_refs.count + 1, reason);
104#else
Craig Tiller5717a982015-04-27 12:01:49 -0700105void grpc_cq_internal_ref(grpc_completion_queue *cc) {
Craig Tiller463f2372015-05-28 16:16:15 -0700106#endif
Craig Tiller5717a982015-04-27 12:01:49 -0700107 gpr_ref(&cc->owning_refs);
108}
109
110static void on_pollset_destroy_done(void *arg) {
111 grpc_completion_queue *cc = arg;
Craig Tiller463f2372015-05-28 16:16:15 -0700112 GRPC_CQ_INTERNAL_UNREF(cc, "pollset_destroy");
Craig Tiller5717a982015-04-27 12:01:49 -0700113}
114
Craig Tiller463f2372015-05-28 16:16:15 -0700115#ifdef GRPC_CQ_REF_COUNT_DEBUG
116void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason) {
117 gpr_log(GPR_DEBUG, "CQ:%p unref %d -> %d %s", cc, (int)cc->owning_refs.count, (int)cc->owning_refs.count - 1, reason);
118#else
Craig Tiller5717a982015-04-27 12:01:49 -0700119void grpc_cq_internal_unref(grpc_completion_queue *cc) {
Craig Tiller463f2372015-05-28 16:16:15 -0700120#endif
Craig Tiller5717a982015-04-27 12:01:49 -0700121 if (gpr_unref(&cc->owning_refs)) {
122 GPR_ASSERT(cc->queue == NULL);
Craig Tiller70730b42015-05-22 14:42:38 -0700123 grpc_pollset_destroy(&cc->pollset);
124 gpr_free(cc);
Craig Tiller5717a982015-04-27 12:01:49 -0700125 }
126}
127
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128/* Create and append an event to the queue. Returns the event so that its data
129 members can be filled in.
ctiller58393c22015-01-07 14:03:30 -0800130 Requires GRPC_POLLSET_MU(&cc->pollset) locked. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131static event *add_locked(grpc_completion_queue *cc, grpc_completion_type type,
Craig Tiller64be9f72015-05-04 14:53:51 -0700132 void *tag, grpc_call *call) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133 event *ev = gpr_malloc(sizeof(event));
nnoble0c475f02014-12-05 15:37:39 -0800134 gpr_uintptr bucket = ((gpr_uintptr)tag) % NUM_TAG_BUCKETS;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135 ev->base.type = type;
136 ev->base.tag = tag;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800137 if (cc->queue == NULL) {
138 cc->queue = ev->queue_next = ev->queue_prev = ev;
139 } else {
140 ev->queue_next = cc->queue;
141 ev->queue_prev = cc->queue->queue_prev;
142 ev->queue_next->queue_prev = ev->queue_prev->queue_next = ev;
143 }
144 if (cc->buckets[bucket] == NULL) {
145 cc->buckets[bucket] = ev->bucket_next = ev->bucket_prev = ev;
146 } else {
147 ev->bucket_next = cc->buckets[bucket];
148 ev->bucket_prev = cc->buckets[bucket]->bucket_prev;
149 ev->bucket_next->bucket_prev = ev->bucket_prev->bucket_next = ev;
150 }
ctiller58393c22015-01-07 14:03:30 -0800151 grpc_pollset_kick(&cc->pollset);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152 return ev;
153}
154
Craig Tiller64be9f72015-05-04 14:53:51 -0700155void grpc_cq_begin_op(grpc_completion_queue *cc, grpc_call *call) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800156 gpr_ref(&cc->refs);
Craig Tiller4df412b2015-04-28 07:57:54 -0700157 if (call) GRPC_CALL_INTERNAL_REF(call, "cq");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158}
159
160/* Signal the end of an operation - if this is the last waiting-to-be-queued
161 event, then enter shutdown mode */
Craig Tiller70730b42015-05-22 14:42:38 -0700162void grpc_cq_end_op(grpc_completion_queue *cc, void *tag, grpc_call *call,
163 int success) {
164 event *ev;
165 int shutdown = 0;
166 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
167 ev = add_locked(cc, GRPC_OP_COMPLETE, tag, call);
168 ev->base.success = success;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169 if (gpr_unref(&cc->refs)) {
170 GPR_ASSERT(!cc->shutdown);
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000171 GPR_ASSERT(cc->shutdown_called);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800172 cc->shutdown = 1;
Craig Tiller70730b42015-05-22 14:42:38 -0700173 shutdown = 1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174 }
Craig Tillercce17ac2015-01-20 09:29:28 -0800175 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller745d63a2015-05-04 15:27:51 -0700176 if (call) GRPC_CALL_INTERNAL_UNREF(call, "cq", 0);
Craig Tiller6f2d6472015-05-22 21:34:15 -0700177 if (shutdown) {
Craig Tiller70730b42015-05-22 14:42:38 -0700178 grpc_pollset_shutdown(&cc->pollset, on_pollset_destroy_done, cc);
Craig Tiller6f2d6472015-05-22 21:34:15 -0700179 }
Craig Tillercce17ac2015-01-20 09:29:28 -0800180}
181
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182/* Create a GRPC_QUEUE_SHUTDOWN event without queuing it anywhere */
Craig Tiller32946d32015-01-15 11:37:30 -0800183static event *create_shutdown_event(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800184 event *ev = gpr_malloc(sizeof(event));
185 ev->base.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800186 ev->base.tag = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187 return ev;
188}
189
Craig Tiller64be9f72015-05-04 14:53:51 -0700190grpc_event grpc_completion_queue_next(grpc_completion_queue *cc,
191 gpr_timespec deadline) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192 event *ev = NULL;
Craig Tiller64be9f72015-05-04 14:53:51 -0700193 grpc_event ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194
Craig Tiller463f2372015-05-28 16:16:15 -0700195 GRPC_CQ_INTERNAL_REF(cc, "next");
ctiller58393c22015-01-07 14:03:30 -0800196 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197 for (;;) {
198 if (cc->queue != NULL) {
nnoble0c475f02014-12-05 15:37:39 -0800199 gpr_uintptr bucket;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200 ev = cc->queue;
nnoble0c475f02014-12-05 15:37:39 -0800201 bucket = ((gpr_uintptr)ev->base.tag) % NUM_TAG_BUCKETS;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800202 cc->queue = ev->queue_next;
203 ev->queue_next->queue_prev = ev->queue_prev;
204 ev->queue_prev->queue_next = ev->queue_next;
205 ev->bucket_next->bucket_prev = ev->bucket_prev;
206 ev->bucket_prev->bucket_next = ev->bucket_next;
207 if (ev == cc->buckets[bucket]) {
208 cc->buckets[bucket] = ev->bucket_next;
209 if (ev == cc->buckets[bucket]) {
210 cc->buckets[bucket] = NULL;
211 }
212 }
213 if (cc->queue == ev) {
214 cc->queue = NULL;
215 }
216 break;
217 }
218 if (cc->shutdown) {
219 ev = create_shutdown_event();
220 break;
221 }
Craig Tiller44883302015-05-28 12:44:27 -0700222 if (!grpc_pollset_work(&cc->pollset, deadline)) {
ctiller58393c22015-01-07 14:03:30 -0800223 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700224 memset(&ret, 0, sizeof(ret));
225 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller1b6b2122015-05-12 09:44:28 -0700226 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700227 GRPC_CQ_INTERNAL_UNREF(cc, "next");
Craig Tiller64be9f72015-05-04 14:53:51 -0700228 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800229 }
230 }
ctiller58393c22015-01-07 14:03:30 -0800231 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700232 ret = ev->base;
233 gpr_free(ev);
234 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700235 GRPC_CQ_INTERNAL_UNREF(cc, "next");
Craig Tiller64be9f72015-05-04 14:53:51 -0700236 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800237}
238
239static event *pluck_event(grpc_completion_queue *cc, void *tag) {
nnoble0c475f02014-12-05 15:37:39 -0800240 gpr_uintptr bucket = ((gpr_uintptr)tag) % NUM_TAG_BUCKETS;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800241 event *ev = cc->buckets[bucket];
242 if (ev == NULL) return NULL;
243 do {
244 if (ev->base.tag == tag) {
245 ev->queue_next->queue_prev = ev->queue_prev;
246 ev->queue_prev->queue_next = ev->queue_next;
247 ev->bucket_next->bucket_prev = ev->bucket_prev;
248 ev->bucket_prev->bucket_next = ev->bucket_next;
249 if (ev == cc->buckets[bucket]) {
250 cc->buckets[bucket] = ev->bucket_next;
251 if (ev == cc->buckets[bucket]) {
252 cc->buckets[bucket] = NULL;
253 }
254 }
255 if (cc->queue == ev) {
256 cc->queue = ev->queue_next;
257 if (cc->queue == ev) {
258 cc->queue = NULL;
259 }
260 }
261 return ev;
262 }
263 ev = ev->bucket_next;
264 } while (ev != cc->buckets[bucket]);
265 return NULL;
266}
267
Craig Tiller64be9f72015-05-04 14:53:51 -0700268grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag,
269 gpr_timespec deadline) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800270 event *ev = NULL;
Craig Tiller64be9f72015-05-04 14:53:51 -0700271 grpc_event ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800272
Craig Tiller463f2372015-05-28 16:16:15 -0700273 GRPC_CQ_INTERNAL_REF(cc, "pluck");
ctiller58393c22015-01-07 14:03:30 -0800274 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800275 for (;;) {
276 if ((ev = pluck_event(cc, tag))) {
277 break;
278 }
279 if (cc->shutdown) {
280 ev = create_shutdown_event();
281 break;
282 }
Craig Tiller44883302015-05-28 12:44:27 -0700283 if (!grpc_pollset_work(&cc->pollset, deadline)) {
ctiller58393c22015-01-07 14:03:30 -0800284 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700285 memset(&ret, 0, sizeof(ret));
286 ret.type = GRPC_QUEUE_TIMEOUT;
Craig Tiller4751fb12015-05-15 14:42:54 -0700287 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700288 GRPC_CQ_INTERNAL_UNREF(cc, "pluck");
Craig Tiller64be9f72015-05-04 14:53:51 -0700289 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800290 }
291 }
ctiller58393c22015-01-07 14:03:30 -0800292 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700293 ret = ev->base;
294 gpr_free(ev);
Craig Tiller4751fb12015-05-15 14:42:54 -0700295 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
Craig Tiller463f2372015-05-28 16:16:15 -0700296 GRPC_CQ_INTERNAL_UNREF(cc, "pluck");
Craig Tiller64be9f72015-05-04 14:53:51 -0700297 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800298}
299
300/* Shutdown simply drops a ref that we reserved at creation time; if we drop
301 to zero here, then enter shutdown mode and wake up any waiters */
302void grpc_completion_queue_shutdown(grpc_completion_queue *cc) {
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000303 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
304 cc->shutdown_called = 1;
305 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
306
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800307 if (gpr_unref(&cc->refs)) {
ctiller58393c22015-01-07 14:03:30 -0800308 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800309 GPR_ASSERT(!cc->shutdown);
310 cc->shutdown = 1;
ctiller58393c22015-01-07 14:03:30 -0800311 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller70730b42015-05-22 14:42:38 -0700312 grpc_pollset_shutdown(&cc->pollset, on_pollset_destroy_done, cc);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800313 }
314}
315
David Klempnerb5056612015-02-24 14:22:50 -0800316void grpc_completion_queue_destroy(grpc_completion_queue *cc) {
Craig Tiller463f2372015-05-28 16:16:15 -0700317 GRPC_CQ_INTERNAL_UNREF(cc, "destroy");
David Klempnerb5056612015-02-24 14:22:50 -0800318}
319
ctillerd79b4862014-12-17 16:36:59 -0800320grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) {
321 return &cc->pollset;
Craig Tiller190d3602015-02-18 09:23:38 -0800322}
Craig Tilleraec96aa2015-04-07 14:32:15 -0700323
324void grpc_cq_hack_spin_pollset(grpc_completion_queue *cc) {
325 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
326 grpc_pollset_kick(&cc->pollset);
Craig Tillerc02c1d82015-04-07 16:21:55 -0700327 grpc_pollset_work(&cc->pollset,
328 gpr_time_add(gpr_now(), gpr_time_from_millis(100)));
Craig Tilleraec96aa2015-04-07 14:32:15 -0700329 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
330}