aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/surface/completion_queue.c
blob: a7d611579f38e46c2948fc63d00888ed9c29fa76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 *
 * Copyright 2014, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "src/core/surface/completion_queue.h"

#include <stdio.h>
#include <string.h>

#include "src/core/eventmanager/em.h"
#include "src/core/surface/call.h"
#include "src/core/surface/event_string.h"
#include "src/core/surface/surface_em.h"
#include "src/core/surface/surface_trace.h"
#include <grpc/support/alloc.h>
#include <grpc/support/atm.h>
#include <grpc/support/log.h>
#include <grpc/support/string.h>

#define NUM_TAG_BUCKETS 31

/* A single event: extends grpc_event to form a linked list with a destruction
   function (on_finish) that is hidden from outside this module */
typedef struct event {
  grpc_event base;
  grpc_event_finish_func on_finish;
  void *on_finish_user_data;
  struct event *queue_next;
  struct event *queue_prev;
  struct event *bucket_next;
  struct event *bucket_prev;
} event;

/* Completion queue structure */
struct grpc_completion_queue {
  grpc_em *em;
  int allow_polling;

  /* When refs drops to zero, we are in shutdown mode, and will be destroyable
     once all queued events are drained */
  gpr_refcount refs;
  /* 0 initially, 1 once we've begun shutting down */
  int shutdown;
  /* Head of a linked list of queued events (prev points to the last element) */
  event *queue;
  /* Fixed size chained hash table of events for pluck() */
  event *buckets[NUM_TAG_BUCKETS];

#ifndef NDEBUG
  /* Debug support: track which operations are in flight at any given time */
  gpr_atm pending_op_count[GRPC_COMPLETION_DO_NOT_USE];
#endif
};

/* Default do-nothing on_finish function */
static void null_on_finish(void *user_data, grpc_op_error error) {}

grpc_completion_queue *grpc_completion_queue_create() {
  grpc_completion_queue *cc = gpr_malloc(sizeof(grpc_completion_queue));
  memset(cc, 0, sizeof(*cc));
  /* Initial ref is dropped by grpc_completion_queue_shutdown */
  gpr_ref_init(&cc->refs, 1);
  cc->em = grpc_surface_em();
  cc->allow_polling = 1;
  return cc;
}

void grpc_completion_queue_dont_poll_test_only(grpc_completion_queue *cc) {
  cc->allow_polling = 0;
}

/* Create and append an event to the queue. Returns the event so that its data
   members can be filled in.
   Requires cc->em->mu locked. */
static event *add_locked(grpc_completion_queue *cc, grpc_completion_type type,
                         void *tag, grpc_call *call,
                         grpc_event_finish_func on_finish, void *user_data) {
  event *ev = gpr_malloc(sizeof(event));
  gpr_intptr bucket = ((gpr_intptr)tag) % NUM_TAG_BUCKETS;
  GPR_ASSERT(!cc->shutdown);
  ev->base.type = type;
  ev->base.tag = tag;
  ev->base.call = call;
  ev->on_finish = on_finish ? on_finish : null_on_finish;
  ev->on_finish_user_data = user_data;
  if (cc->queue == NULL) {
    cc->queue = ev->queue_next = ev->queue_prev = ev;
  } else {
    ev->queue_next = cc->queue;
    ev->queue_prev = cc->queue->queue_prev;
    ev->queue_next->queue_prev = ev->queue_prev->queue_next = ev;
  }
  if (cc->buckets[bucket] == NULL) {
    cc->buckets[bucket] = ev->bucket_next = ev->bucket_prev = ev;
  } else {
    ev->bucket_next = cc->buckets[bucket];
    ev->bucket_prev = cc->buckets[bucket]->bucket_prev;
    ev->bucket_next->bucket_prev = ev->bucket_prev->bucket_next = ev;
  }
  gpr_cv_broadcast(&cc->em->cv);
  return ev;
}

void grpc_cq_begin_op(grpc_completion_queue *cc, grpc_call *call,
                      grpc_completion_type type) {
  gpr_ref(&cc->refs);
  if (call) grpc_call_internal_ref(call);
#ifndef NDEBUG
  gpr_atm_no_barrier_fetch_add(&cc->pending_op_count[type], 1);
#endif
}

/* Signal the end of an operation - if this is the last waiting-to-be-queued
   event, then enter shutdown mode */
static void end_op_locked(grpc_completion_queue *cc,
                          grpc_completion_type type) {
#ifndef NDEBUG
  GPR_ASSERT(gpr_atm_full_fetch_add(&cc->pending_op_count[type], -1) > 0);
#endif
  if (gpr_unref(&cc->refs)) {
    GPR_ASSERT(!cc->shutdown);
    cc->shutdown = 1;
    gpr_cv_broadcast(&cc->em->cv);
  }
}

void grpc_cq_end_read(grpc_completion_queue *cc, void *tag, grpc_call *call,
                      grpc_event_finish_func on_finish, void *user_data,
                      grpc_byte_buffer *read) {
  event *ev;
  gpr_mu_lock(&cc->em->mu);
  ev = add_locked(cc, GRPC_READ, tag, call, on_finish, user_data);
  ev->base.data.read = read;
  end_op_locked(cc, GRPC_READ);
  gpr_mu_unlock(&cc->em->mu);
}

void grpc_cq_end_invoke_accepted(grpc_completion_queue *cc, void *tag,
                                 grpc_call *call,
                                 grpc_event_finish_func on_finish,
                                 void *user_data, grpc_op_error error) {
  event *ev;
  gpr_mu_lock(&cc->em->mu);
  ev = add_locked(cc, GRPC_INVOKE_ACCEPTED, tag, call, on_finish, user_data);
  ev->base.data.invoke_accepted = error;
  end_op_locked(cc, GRPC_INVOKE_ACCEPTED);
  gpr_mu_unlock(&cc->em->mu);
}

void grpc_cq_end_write_accepted(grpc_completion_queue *cc, void *tag,
                                grpc_call *call,
                                grpc_event_finish_func on_finish,
                                void *user_data, grpc_op_error error) {
  event *ev;
  gpr_mu_lock(&cc->em->mu);
  ev = add_locked(cc, GRPC_WRITE_ACCEPTED, tag, call, on_finish, user_data);
  ev->base.data.write_accepted = error;
  end_op_locked(cc, GRPC_WRITE_ACCEPTED);
  gpr_mu_unlock(&cc->em->mu);
}

void grpc_cq_end_finish_accepted(grpc_completion_queue *cc, void *tag,
                                 grpc_call *call,
                                 grpc_event_finish_func on_finish,
                                 void *user_data, grpc_op_error error) {
  event *ev;
  gpr_mu_lock(&cc->em->mu);
  ev = add_locked(cc, GRPC_FINISH_ACCEPTED, tag, call, on_finish, user_data);
  ev->base.data.finish_accepted = error;
  end_op_locked(cc, GRPC_FINISH_ACCEPTED);
  gpr_mu_unlock(&cc->em->mu);
}

void grpc_cq_end_client_metadata_read(grpc_completion_queue *cc, void *tag,
                                      grpc_call *call,
                                      grpc_event_finish_func on_finish,
                                      void *user_data, size_t count,
                                      grpc_metadata *elements) {
  event *ev;
  gpr_mu_lock(&cc->em->mu);
  ev = add_locked(cc, GRPC_CLIENT_METADATA_READ, tag, call, on_finish,
                  user_data);
  ev->base.data.client_metadata_read.count = count;
  ev->base.data.client_metadata_read.elements = elements;
  end_op_locked(cc, GRPC_CLIENT_METADATA_READ);
  gpr_mu_unlock(&cc->em->mu);
}

void grpc_cq_end_finished(grpc_completion_queue *cc, void *tag, grpc_call *call,
                          grpc_event_finish_func on_finish, void *user_data,
                          grpc_status status) {
  event *ev;
  gpr_mu_lock(&cc->em->mu);
  ev = add_locked(cc, GRPC_FINISHED, tag, call, on_finish, user_data);
  ev->base.data.finished = status;
  end_op_locked(cc, GRPC_FINISHED);
  gpr_mu_unlock(&cc->em->mu);
}

void grpc_cq_end_new_rpc(grpc_completion_queue *cc, void *tag, grpc_call *call,
                         grpc_event_finish_func on_finish, void *user_data,
                         const char *method, const char *host,
                         gpr_timespec deadline, size_t metadata_count,
                         grpc_metadata *metadata_elements) {
  event *ev;
  gpr_mu_lock(&cc->em->mu);
  ev = add_locked(cc, GRPC_SERVER_RPC_NEW, tag, call, on_finish, user_data);
  ev->base.data.server_rpc_new.method = method;
  ev->base.data.server_rpc_new.host = host;
  ev->base.data.server_rpc_new.deadline = deadline;
  ev->base.data.server_rpc_new.metadata_count = metadata_count;
  ev->base.data.server_rpc_new.metadata_elements = metadata_elements;
  end_op_locked(cc, GRPC_SERVER_RPC_NEW);
  gpr_mu_unlock(&cc->em->mu);
}

/* Create a GRPC_QUEUE_SHUTDOWN event without queuing it anywhere */
static event *create_shutdown_event() {
  event *ev = gpr_malloc(sizeof(event));
  ev->base.type = GRPC_QUEUE_SHUTDOWN;
  ev->base.call = NULL;
  ev->base.tag = NULL;
  ev->on_finish = null_on_finish;
  return ev;
}

grpc_event *grpc_completion_queue_next(grpc_completion_queue *cc,
                                       gpr_timespec deadline) {
  event *ev = NULL;

  gpr_mu_lock(&cc->em->mu);
  for (;;) {
    if (cc->queue != NULL) {
      gpr_intptr bucket;
      ev = cc->queue;
      bucket = ((gpr_intptr)ev->base.tag) % NUM_TAG_BUCKETS;
      cc->queue = ev->queue_next;
      ev->queue_next->queue_prev = ev->queue_prev;
      ev->queue_prev->queue_next = ev->queue_next;
      ev->bucket_next->bucket_prev = ev->bucket_prev;
      ev->bucket_prev->bucket_next = ev->bucket_next;
      if (ev == cc->buckets[bucket]) {
        cc->buckets[bucket] = ev->bucket_next;
        if (ev == cc->buckets[bucket]) {
          cc->buckets[bucket] = NULL;
        }
      }
      if (cc->queue == ev) {
        cc->queue = NULL;
      }
      break;
    }
    if (cc->shutdown) {
      ev = create_shutdown_event();
      break;
    }
    if (cc->allow_polling && grpc_em_work(cc->em, deadline)) {
      continue;
    }
    if (gpr_cv_wait(&cc->em->cv, &cc->em->mu, deadline)) {
      gpr_mu_unlock(&cc->em->mu);
      return NULL;
    }
  }
  gpr_mu_unlock(&cc->em->mu);
  GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ev->base);
  return &ev->base;
}

static event *pluck_event(grpc_completion_queue *cc, void *tag) {
  gpr_intptr bucket = ((gpr_intptr)tag) % NUM_TAG_BUCKETS;
  event *ev = cc->buckets[bucket];
  if (ev == NULL) return NULL;
  do {
    if (ev->base.tag == tag) {
      ev->queue_next->queue_prev = ev->queue_prev;
      ev->queue_prev->queue_next = ev->queue_next;
      ev->bucket_next->bucket_prev = ev->bucket_prev;
      ev->bucket_prev->bucket_next = ev->bucket_next;
      if (ev == cc->buckets[bucket]) {
        cc->buckets[bucket] = ev->bucket_next;
        if (ev == cc->buckets[bucket]) {
          cc->buckets[bucket] = NULL;
        }
      }
      if (cc->queue == ev) {
        cc->queue = ev->queue_next;
        if (cc->queue == ev) {
          cc->queue = NULL;
        }
      }
      return ev;
    }
    ev = ev->bucket_next;
  } while (ev != cc->buckets[bucket]);
  return NULL;
}

grpc_event *grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag,
                                        gpr_timespec deadline) {
  event *ev = NULL;

  gpr_mu_lock(&cc->em->mu);
  for (;;) {
    if ((ev = pluck_event(cc, tag))) {
      break;
    }
    if (cc->shutdown) {
      ev = create_shutdown_event();
      break;
    }
    if (cc->allow_polling && grpc_em_work(cc->em, deadline)) {
      continue;
    }
    if (gpr_cv_wait(&cc->em->cv, &cc->em->mu, deadline)) {
      gpr_mu_unlock(&cc->em->mu);
      return NULL;
    }
  }
  gpr_mu_unlock(&cc->em->mu);
  GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ev->base);
  return &ev->base;
}

/* Shutdown simply drops a ref that we reserved at creation time; if we drop
   to zero here, then enter shutdown mode and wake up any waiters */
void grpc_completion_queue_shutdown(grpc_completion_queue *cc) {
  if (gpr_unref(&cc->refs)) {
    gpr_mu_lock(&cc->em->mu);
    GPR_ASSERT(!cc->shutdown);
    cc->shutdown = 1;
    gpr_cv_broadcast(&cc->em->cv);
    gpr_mu_unlock(&cc->em->mu);
  }
}

void grpc_completion_queue_destroy(grpc_completion_queue *cc) {
  GPR_ASSERT(cc->queue == NULL);
  gpr_free(cc);
}

void grpc_event_finish(grpc_event *base) {
  event *ev = (event *)base;
  ev->on_finish(ev->on_finish_user_data, GRPC_OP_OK);
  if (ev->base.call) {
    grpc_call_internal_unref(ev->base.call);
  }
  gpr_free(ev);
}

void grpc_cq_dump_pending_ops(grpc_completion_queue *cc) {
#ifndef NDEBUG
  char tmp[256];
  char *p = tmp;
  int i;

  for (i = 0; i < GRPC_COMPLETION_DO_NOT_USE; i++) {
    p += sprintf(p, " %d", (int)cc->pending_op_count[i]);
  }

  gpr_log(GPR_INFO, "pending ops:%s", tmp);
#endif
}