blob: 2f1d81ee84776b4035686834fbee360bddb57750 [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 {
ctiller58393c22015-01-07 14:03:30 -080062 /* TODO(ctiller): see if this can be removed */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063 int allow_polling;
64
65 /* When refs drops to zero, we are in shutdown mode, and will be destroyable
66 once all queued events are drained */
67 gpr_refcount refs;
Craig Tiller5717a982015-04-27 12:01:49 -070068 /* Once owning_refs drops to zero, we will destroy the cq */
69 gpr_refcount owning_refs;
ctillerd79b4862014-12-17 16:36:59 -080070 /* the set of low level i/o things that concern this cq */
71 grpc_pollset pollset;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072 /* 0 initially, 1 once we've begun shutting down */
73 int shutdown;
Craig Tillerf5fd4ba2015-03-02 18:01:21 +000074 int shutdown_called;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 /* Head of a linked list of queued events (prev points to the last element) */
76 event *queue;
77 /* Fixed size chained hash table of events for pluck() */
78 event *buckets[NUM_TAG_BUCKETS];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079};
80
Craig Tiller32946d32015-01-15 11:37:30 -080081grpc_completion_queue *grpc_completion_queue_create(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080082 grpc_completion_queue *cc = gpr_malloc(sizeof(grpc_completion_queue));
83 memset(cc, 0, sizeof(*cc));
84 /* Initial ref is dropped by grpc_completion_queue_shutdown */
85 gpr_ref_init(&cc->refs, 1);
Craig Tiller5717a982015-04-27 12:01:49 -070086 gpr_ref_init(&cc->owning_refs, 1);
ctillerd79b4862014-12-17 16:36:59 -080087 grpc_pollset_init(&cc->pollset);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088 cc->allow_polling = 1;
89 return cc;
90}
91
Craig Tiller5717a982015-04-27 12:01:49 -070092void grpc_cq_internal_ref(grpc_completion_queue *cc) {
93 gpr_ref(&cc->owning_refs);
94}
95
96static void on_pollset_destroy_done(void *arg) {
97 grpc_completion_queue *cc = arg;
98 grpc_pollset_destroy(&cc->pollset);
99 gpr_free(cc);
100}
101
102void grpc_cq_internal_unref(grpc_completion_queue *cc) {
103 if (gpr_unref(&cc->owning_refs)) {
104 GPR_ASSERT(cc->queue == NULL);
105 grpc_pollset_shutdown(&cc->pollset, on_pollset_destroy_done, cc);
106 }
107}
108
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109void grpc_completion_queue_dont_poll_test_only(grpc_completion_queue *cc) {
110 cc->allow_polling = 0;
111}
112
113/* Create and append an event to the queue. Returns the event so that its data
114 members can be filled in.
ctiller58393c22015-01-07 14:03:30 -0800115 Requires GRPC_POLLSET_MU(&cc->pollset) locked. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116static event *add_locked(grpc_completion_queue *cc, grpc_completion_type type,
Craig Tiller64be9f72015-05-04 14:53:51 -0700117 void *tag, grpc_call *call) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800118 event *ev = gpr_malloc(sizeof(event));
nnoble0c475f02014-12-05 15:37:39 -0800119 gpr_uintptr bucket = ((gpr_uintptr)tag) % NUM_TAG_BUCKETS;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120 ev->base.type = type;
121 ev->base.tag = tag;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800122 if (cc->queue == NULL) {
123 cc->queue = ev->queue_next = ev->queue_prev = ev;
124 } else {
125 ev->queue_next = cc->queue;
126 ev->queue_prev = cc->queue->queue_prev;
127 ev->queue_next->queue_prev = ev->queue_prev->queue_next = ev;
128 }
129 if (cc->buckets[bucket] == NULL) {
130 cc->buckets[bucket] = ev->bucket_next = ev->bucket_prev = ev;
131 } else {
132 ev->bucket_next = cc->buckets[bucket];
133 ev->bucket_prev = cc->buckets[bucket]->bucket_prev;
134 ev->bucket_next->bucket_prev = ev->bucket_prev->bucket_next = ev;
135 }
ctiller58393c22015-01-07 14:03:30 -0800136 gpr_cv_broadcast(GRPC_POLLSET_CV(&cc->pollset));
137 grpc_pollset_kick(&cc->pollset);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138 return ev;
139}
140
Craig Tiller64be9f72015-05-04 14:53:51 -0700141void grpc_cq_begin_op(grpc_completion_queue *cc, grpc_call *call) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142 gpr_ref(&cc->refs);
Craig Tiller4df412b2015-04-28 07:57:54 -0700143 if (call) GRPC_CALL_INTERNAL_REF(call, "cq");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144}
145
146/* Signal the end of an operation - if this is the last waiting-to-be-queued
147 event, then enter shutdown mode */
148static void end_op_locked(grpc_completion_queue *cc,
149 grpc_completion_type type) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800150 if (gpr_unref(&cc->refs)) {
151 GPR_ASSERT(!cc->shutdown);
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000152 GPR_ASSERT(cc->shutdown_called);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153 cc->shutdown = 1;
ctiller58393c22015-01-07 14:03:30 -0800154 gpr_cv_broadcast(GRPC_POLLSET_CV(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800155 }
156}
157
Craig Tillerfb189f82015-02-03 12:07:07 -0800158void grpc_cq_end_op(grpc_completion_queue *cc, void *tag, grpc_call *call,
Craig Tiller64be9f72015-05-04 14:53:51 -0700159 int success) {
Craig Tillercce17ac2015-01-20 09:29:28 -0800160 event *ev;
161 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700162 ev = add_locked(cc, GRPC_OP_COMPLETE, tag, call);
163 ev->base.success = success;
Craig Tillerfb189f82015-02-03 12:07:07 -0800164 end_op_locked(cc, GRPC_OP_COMPLETE);
Craig Tillercce17ac2015-01-20 09:29:28 -0800165 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
166}
167
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800168/* Create a GRPC_QUEUE_SHUTDOWN event without queuing it anywhere */
Craig Tiller32946d32015-01-15 11:37:30 -0800169static event *create_shutdown_event(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170 event *ev = gpr_malloc(sizeof(event));
171 ev->base.type = GRPC_QUEUE_SHUTDOWN;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800172 ev->base.tag = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800173 return ev;
174}
175
Craig Tiller64be9f72015-05-04 14:53:51 -0700176grpc_event grpc_completion_queue_next(grpc_completion_queue *cc,
177 gpr_timespec deadline) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178 event *ev = NULL;
Craig Tiller64be9f72015-05-04 14:53:51 -0700179 grpc_event ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180
ctiller58393c22015-01-07 14:03:30 -0800181 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182 for (;;) {
183 if (cc->queue != NULL) {
nnoble0c475f02014-12-05 15:37:39 -0800184 gpr_uintptr bucket;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185 ev = cc->queue;
nnoble0c475f02014-12-05 15:37:39 -0800186 bucket = ((gpr_uintptr)ev->base.tag) % NUM_TAG_BUCKETS;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187 cc->queue = ev->queue_next;
188 ev->queue_next->queue_prev = ev->queue_prev;
189 ev->queue_prev->queue_next = ev->queue_next;
190 ev->bucket_next->bucket_prev = ev->bucket_prev;
191 ev->bucket_prev->bucket_next = ev->bucket_next;
192 if (ev == cc->buckets[bucket]) {
193 cc->buckets[bucket] = ev->bucket_next;
194 if (ev == cc->buckets[bucket]) {
195 cc->buckets[bucket] = NULL;
196 }
197 }
198 if (cc->queue == ev) {
199 cc->queue = NULL;
200 }
201 break;
202 }
203 if (cc->shutdown) {
204 ev = create_shutdown_event();
205 break;
206 }
ctiller58393c22015-01-07 14:03:30 -0800207 if (cc->allow_polling && grpc_pollset_work(&cc->pollset, deadline)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800208 continue;
209 }
ctiller58393c22015-01-07 14:03:30 -0800210 if (gpr_cv_wait(GRPC_POLLSET_CV(&cc->pollset),
211 GRPC_POLLSET_MU(&cc->pollset), deadline)) {
212 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700213 memset(&ret, 0, sizeof(ret));
214 ret.type = GRPC_QUEUE_TIMEOUT;
215 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800216 }
217 }
ctiller58393c22015-01-07 14:03:30 -0800218 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700219 ret = ev->base;
220 gpr_free(ev);
221 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
222 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223}
224
225static event *pluck_event(grpc_completion_queue *cc, void *tag) {
nnoble0c475f02014-12-05 15:37:39 -0800226 gpr_uintptr bucket = ((gpr_uintptr)tag) % NUM_TAG_BUCKETS;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800227 event *ev = cc->buckets[bucket];
228 if (ev == NULL) return NULL;
229 do {
230 if (ev->base.tag == tag) {
231 ev->queue_next->queue_prev = ev->queue_prev;
232 ev->queue_prev->queue_next = ev->queue_next;
233 ev->bucket_next->bucket_prev = ev->bucket_prev;
234 ev->bucket_prev->bucket_next = ev->bucket_next;
235 if (ev == cc->buckets[bucket]) {
236 cc->buckets[bucket] = ev->bucket_next;
237 if (ev == cc->buckets[bucket]) {
238 cc->buckets[bucket] = NULL;
239 }
240 }
241 if (cc->queue == ev) {
242 cc->queue = ev->queue_next;
243 if (cc->queue == ev) {
244 cc->queue = NULL;
245 }
246 }
247 return ev;
248 }
249 ev = ev->bucket_next;
250 } while (ev != cc->buckets[bucket]);
251 return NULL;
252}
253
Craig Tiller64be9f72015-05-04 14:53:51 -0700254grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag,
255 gpr_timespec deadline) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800256 event *ev = NULL;
Craig Tiller64be9f72015-05-04 14:53:51 -0700257 grpc_event ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258
ctiller58393c22015-01-07 14:03:30 -0800259 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800260 for (;;) {
261 if ((ev = pluck_event(cc, tag))) {
262 break;
263 }
264 if (cc->shutdown) {
265 ev = create_shutdown_event();
266 break;
267 }
ctiller58393c22015-01-07 14:03:30 -0800268 if (cc->allow_polling && grpc_pollset_work(&cc->pollset, deadline)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800269 continue;
270 }
ctiller58393c22015-01-07 14:03:30 -0800271 if (gpr_cv_wait(GRPC_POLLSET_CV(&cc->pollset),
272 GRPC_POLLSET_MU(&cc->pollset), deadline)) {
273 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700274 memset(&ret, 0, sizeof(ret));
275 ret.type = GRPC_QUEUE_TIMEOUT;
276 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800277 }
278 }
ctiller58393c22015-01-07 14:03:30 -0800279 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Craig Tiller64be9f72015-05-04 14:53:51 -0700280 ret = ev->base;
281 gpr_free(ev);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800282 GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ev->base);
Craig Tiller64be9f72015-05-04 14:53:51 -0700283 return ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284}
285
286/* Shutdown simply drops a ref that we reserved at creation time; if we drop
287 to zero here, then enter shutdown mode and wake up any waiters */
288void grpc_completion_queue_shutdown(grpc_completion_queue *cc) {
Craig Tillerf5fd4ba2015-03-02 18:01:21 +0000289 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
290 cc->shutdown_called = 1;
291 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
292
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800293 if (gpr_unref(&cc->refs)) {
ctiller58393c22015-01-07 14:03:30 -0800294 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800295 GPR_ASSERT(!cc->shutdown);
296 cc->shutdown = 1;
ctiller58393c22015-01-07 14:03:30 -0800297 gpr_cv_broadcast(GRPC_POLLSET_CV(&cc->pollset));
298 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800299 }
300}
301
David Klempnerb5056612015-02-24 14:22:50 -0800302void grpc_completion_queue_destroy(grpc_completion_queue *cc) {
Craig Tiller5717a982015-04-27 12:01:49 -0700303 grpc_cq_internal_unref(cc);
David Klempnerb5056612015-02-24 14:22:50 -0800304}
305
ctillerd79b4862014-12-17 16:36:59 -0800306grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) {
307 return &cc->pollset;
Craig Tiller190d3602015-02-18 09:23:38 -0800308}
Craig Tilleraec96aa2015-04-07 14:32:15 -0700309
310void grpc_cq_hack_spin_pollset(grpc_completion_queue *cc) {
311 gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
312 grpc_pollset_kick(&cc->pollset);
Craig Tillerc02c1d82015-04-07 16:21:55 -0700313 grpc_pollset_work(&cc->pollset,
314 gpr_time_add(gpr_now(), gpr_time_from_millis(100)));
Craig Tilleraec96aa2015-04-07 14:32:15 -0700315 gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
316}