completion_queue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * 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. #include "src/core/surface/completion_queue.h"
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include "src/core/iomgr/pollset.h"
  37. #include "src/core/support/string.h"
  38. #include "src/core/surface/call.h"
  39. #include "src/core/surface/event_string.h"
  40. #include "src/core/surface/surface_trace.h"
  41. #include <grpc/support/alloc.h>
  42. #include <grpc/support/atm.h>
  43. #include <grpc/support/log.h>
  44. #define NUM_TAG_BUCKETS 31
  45. /* A single event: extends grpc_event to form a linked list with a destruction
  46. function (on_finish) that is hidden from outside this module */
  47. typedef struct event {
  48. grpc_event base;
  49. grpc_event_finish_func on_finish;
  50. void *on_finish_user_data;
  51. struct event *queue_next;
  52. struct event *queue_prev;
  53. struct event *bucket_next;
  54. struct event *bucket_prev;
  55. } event;
  56. /* Completion queue structure */
  57. struct grpc_completion_queue {
  58. /* TODO(ctiller): see if this can be removed */
  59. int allow_polling;
  60. /* When refs drops to zero, we are in shutdown mode, and will be destroyable
  61. once all queued events are drained */
  62. gpr_refcount refs;
  63. /* the set of low level i/o things that concern this cq */
  64. grpc_pollset pollset;
  65. /* 0 initially, 1 once we've begun shutting down */
  66. int shutdown;
  67. /* Head of a linked list of queued events (prev points to the last element) */
  68. event *queue;
  69. /* Fixed size chained hash table of events for pluck() */
  70. event *buckets[NUM_TAG_BUCKETS];
  71. #ifndef NDEBUG
  72. /* Debug support: track which operations are in flight at any given time */
  73. gpr_atm pending_op_count[GRPC_COMPLETION_DO_NOT_USE];
  74. #endif
  75. };
  76. /* Default do-nothing on_finish function */
  77. static void null_on_finish(void *user_data, grpc_op_error error) {}
  78. grpc_completion_queue *grpc_completion_queue_create(void) {
  79. 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);
  83. grpc_pollset_init(&cc->pollset);
  84. cc->allow_polling = 1;
  85. return cc;
  86. }
  87. void grpc_completion_queue_dont_poll_test_only(grpc_completion_queue *cc) {
  88. cc->allow_polling = 0;
  89. }
  90. /* Create and append an event to the queue. Returns the event so that its data
  91. members can be filled in.
  92. Requires GRPC_POLLSET_MU(&cc->pollset) locked. */
  93. static event *add_locked(grpc_completion_queue *cc, grpc_completion_type type,
  94. void *tag, grpc_call *call,
  95. grpc_event_finish_func on_finish, void *user_data) {
  96. event *ev = gpr_malloc(sizeof(event));
  97. gpr_uintptr bucket = ((gpr_uintptr)tag) % NUM_TAG_BUCKETS;
  98. GPR_ASSERT(!cc->shutdown);
  99. ev->base.type = type;
  100. ev->base.tag = tag;
  101. ev->base.call = call;
  102. ev->on_finish = on_finish ? on_finish : null_on_finish;
  103. ev->on_finish_user_data = user_data;
  104. if (cc->queue == NULL) {
  105. cc->queue = ev->queue_next = ev->queue_prev = ev;
  106. } else {
  107. ev->queue_next = cc->queue;
  108. ev->queue_prev = cc->queue->queue_prev;
  109. ev->queue_next->queue_prev = ev->queue_prev->queue_next = ev;
  110. }
  111. if (cc->buckets[bucket] == NULL) {
  112. cc->buckets[bucket] = ev->bucket_next = ev->bucket_prev = ev;
  113. } else {
  114. ev->bucket_next = cc->buckets[bucket];
  115. ev->bucket_prev = cc->buckets[bucket]->bucket_prev;
  116. ev->bucket_next->bucket_prev = ev->bucket_prev->bucket_next = ev;
  117. }
  118. gpr_cv_broadcast(GRPC_POLLSET_CV(&cc->pollset));
  119. grpc_pollset_kick(&cc->pollset);
  120. return ev;
  121. }
  122. void grpc_cq_begin_op(grpc_completion_queue *cc, grpc_call *call,
  123. grpc_completion_type type) {
  124. gpr_ref(&cc->refs);
  125. if (call) grpc_call_internal_ref(call);
  126. #ifndef NDEBUG
  127. gpr_atm_no_barrier_fetch_add(&cc->pending_op_count[type], 1);
  128. #endif
  129. }
  130. /* Signal the end of an operation - if this is the last waiting-to-be-queued
  131. event, then enter shutdown mode */
  132. static void end_op_locked(grpc_completion_queue *cc,
  133. grpc_completion_type type) {
  134. #ifndef NDEBUG
  135. GPR_ASSERT(gpr_atm_full_fetch_add(&cc->pending_op_count[type], -1) > 0);
  136. #endif
  137. if (gpr_unref(&cc->refs)) {
  138. GPR_ASSERT(!cc->shutdown);
  139. cc->shutdown = 1;
  140. gpr_cv_broadcast(GRPC_POLLSET_CV(&cc->pollset));
  141. }
  142. }
  143. void grpc_cq_end_server_shutdown(grpc_completion_queue *cc, void *tag) {
  144. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  145. add_locked(cc, GRPC_SERVER_SHUTDOWN, tag, NULL, NULL, NULL);
  146. end_op_locked(cc, GRPC_SERVER_SHUTDOWN);
  147. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  148. }
  149. void grpc_cq_end_read(grpc_completion_queue *cc, void *tag, grpc_call *call,
  150. grpc_event_finish_func on_finish, void *user_data,
  151. grpc_byte_buffer *read) {
  152. event *ev;
  153. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  154. ev = add_locked(cc, GRPC_READ, tag, call, on_finish, user_data);
  155. ev->base.data.read = read;
  156. end_op_locked(cc, GRPC_READ);
  157. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  158. }
  159. void grpc_cq_end_write_accepted(grpc_completion_queue *cc, void *tag,
  160. grpc_call *call,
  161. grpc_event_finish_func on_finish,
  162. void *user_data, grpc_op_error error) {
  163. event *ev;
  164. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  165. ev = add_locked(cc, GRPC_WRITE_ACCEPTED, tag, call, on_finish, user_data);
  166. ev->base.data.write_accepted = error;
  167. end_op_locked(cc, GRPC_WRITE_ACCEPTED);
  168. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  169. }
  170. void grpc_cq_end_op_complete(grpc_completion_queue *cc, void *tag,
  171. grpc_call *call, grpc_event_finish_func on_finish,
  172. void *user_data, grpc_op_error error) {
  173. event *ev;
  174. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  175. ev = add_locked(cc, GRPC_OP_COMPLETE, tag, call, on_finish, user_data);
  176. ev->base.data.write_accepted = error;
  177. end_op_locked(cc, GRPC_OP_COMPLETE);
  178. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  179. }
  180. void grpc_cq_end_op(grpc_completion_queue *cc, void *tag, grpc_call *call,
  181. grpc_event_finish_func on_finish, void *user_data,
  182. grpc_op_error error) {
  183. event *ev;
  184. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  185. ev = add_locked(cc, GRPC_OP_COMPLETE, tag, call, on_finish, user_data);
  186. ev->base.data.write_accepted = error;
  187. end_op_locked(cc, GRPC_OP_COMPLETE);
  188. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  189. }
  190. void grpc_cq_end_finish_accepted(grpc_completion_queue *cc, void *tag,
  191. grpc_call *call,
  192. grpc_event_finish_func on_finish,
  193. void *user_data, grpc_op_error error) {
  194. event *ev;
  195. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  196. ev = add_locked(cc, GRPC_FINISH_ACCEPTED, tag, call, on_finish, user_data);
  197. ev->base.data.finish_accepted = error;
  198. end_op_locked(cc, GRPC_FINISH_ACCEPTED);
  199. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  200. }
  201. void grpc_cq_end_client_metadata_read(grpc_completion_queue *cc, void *tag,
  202. grpc_call *call,
  203. grpc_event_finish_func on_finish,
  204. void *user_data, size_t count,
  205. grpc_metadata *elements) {
  206. event *ev;
  207. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  208. ev = add_locked(cc, GRPC_CLIENT_METADATA_READ, tag, call, on_finish,
  209. user_data);
  210. ev->base.data.client_metadata_read.count = count;
  211. ev->base.data.client_metadata_read.elements = elements;
  212. end_op_locked(cc, GRPC_CLIENT_METADATA_READ);
  213. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  214. }
  215. void grpc_cq_end_finished(grpc_completion_queue *cc, void *tag, grpc_call *call,
  216. grpc_event_finish_func on_finish, void *user_data,
  217. grpc_status_code status, const char *details,
  218. grpc_metadata *metadata_elements,
  219. size_t metadata_count) {
  220. event *ev;
  221. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  222. ev = add_locked(cc, GRPC_FINISHED, tag, call, on_finish, user_data);
  223. ev->base.data.finished.status = status;
  224. ev->base.data.finished.details = details;
  225. ev->base.data.finished.metadata_count = metadata_count;
  226. ev->base.data.finished.metadata_elements = metadata_elements;
  227. end_op_locked(cc, GRPC_FINISHED);
  228. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  229. }
  230. void grpc_cq_end_new_rpc(grpc_completion_queue *cc, void *tag, grpc_call *call,
  231. grpc_event_finish_func on_finish, void *user_data,
  232. const char *method, const char *host,
  233. gpr_timespec deadline, size_t metadata_count,
  234. grpc_metadata *metadata_elements) {
  235. event *ev;
  236. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  237. ev = add_locked(cc, GRPC_SERVER_RPC_NEW, tag, call, on_finish, user_data);
  238. ev->base.data.server_rpc_new.method = method;
  239. ev->base.data.server_rpc_new.host = host;
  240. ev->base.data.server_rpc_new.deadline = deadline;
  241. ev->base.data.server_rpc_new.metadata_count = metadata_count;
  242. ev->base.data.server_rpc_new.metadata_elements = metadata_elements;
  243. end_op_locked(cc, GRPC_SERVER_RPC_NEW);
  244. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  245. }
  246. /* Create a GRPC_QUEUE_SHUTDOWN event without queuing it anywhere */
  247. static event *create_shutdown_event(void) {
  248. event *ev = gpr_malloc(sizeof(event));
  249. ev->base.type = GRPC_QUEUE_SHUTDOWN;
  250. ev->base.call = NULL;
  251. ev->base.tag = NULL;
  252. ev->on_finish = null_on_finish;
  253. return ev;
  254. }
  255. grpc_event *grpc_completion_queue_next(grpc_completion_queue *cc,
  256. gpr_timespec deadline) {
  257. event *ev = NULL;
  258. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  259. for (;;) {
  260. if (cc->queue != NULL) {
  261. gpr_uintptr bucket;
  262. ev = cc->queue;
  263. bucket = ((gpr_uintptr)ev->base.tag) % NUM_TAG_BUCKETS;
  264. cc->queue = ev->queue_next;
  265. ev->queue_next->queue_prev = ev->queue_prev;
  266. ev->queue_prev->queue_next = ev->queue_next;
  267. ev->bucket_next->bucket_prev = ev->bucket_prev;
  268. ev->bucket_prev->bucket_next = ev->bucket_next;
  269. if (ev == cc->buckets[bucket]) {
  270. cc->buckets[bucket] = ev->bucket_next;
  271. if (ev == cc->buckets[bucket]) {
  272. cc->buckets[bucket] = NULL;
  273. }
  274. }
  275. if (cc->queue == ev) {
  276. cc->queue = NULL;
  277. }
  278. break;
  279. }
  280. if (cc->shutdown) {
  281. ev = create_shutdown_event();
  282. break;
  283. }
  284. if (cc->allow_polling && grpc_pollset_work(&cc->pollset, deadline)) {
  285. continue;
  286. }
  287. if (gpr_cv_wait(GRPC_POLLSET_CV(&cc->pollset),
  288. GRPC_POLLSET_MU(&cc->pollset), deadline)) {
  289. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  290. return NULL;
  291. }
  292. }
  293. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  294. GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ev->base);
  295. return &ev->base;
  296. }
  297. static event *pluck_event(grpc_completion_queue *cc, void *tag) {
  298. gpr_uintptr bucket = ((gpr_uintptr)tag) % NUM_TAG_BUCKETS;
  299. event *ev = cc->buckets[bucket];
  300. if (ev == NULL) return NULL;
  301. do {
  302. if (ev->base.tag == tag) {
  303. ev->queue_next->queue_prev = ev->queue_prev;
  304. ev->queue_prev->queue_next = ev->queue_next;
  305. ev->bucket_next->bucket_prev = ev->bucket_prev;
  306. ev->bucket_prev->bucket_next = ev->bucket_next;
  307. if (ev == cc->buckets[bucket]) {
  308. cc->buckets[bucket] = ev->bucket_next;
  309. if (ev == cc->buckets[bucket]) {
  310. cc->buckets[bucket] = NULL;
  311. }
  312. }
  313. if (cc->queue == ev) {
  314. cc->queue = ev->queue_next;
  315. if (cc->queue == ev) {
  316. cc->queue = NULL;
  317. }
  318. }
  319. return ev;
  320. }
  321. ev = ev->bucket_next;
  322. } while (ev != cc->buckets[bucket]);
  323. return NULL;
  324. }
  325. grpc_event *grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag,
  326. gpr_timespec deadline) {
  327. event *ev = NULL;
  328. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  329. for (;;) {
  330. if ((ev = pluck_event(cc, tag))) {
  331. break;
  332. }
  333. if (cc->shutdown) {
  334. ev = create_shutdown_event();
  335. break;
  336. }
  337. if (cc->allow_polling && grpc_pollset_work(&cc->pollset, deadline)) {
  338. continue;
  339. }
  340. if (gpr_cv_wait(GRPC_POLLSET_CV(&cc->pollset),
  341. GRPC_POLLSET_MU(&cc->pollset), deadline)) {
  342. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  343. return NULL;
  344. }
  345. }
  346. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  347. GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ev->base);
  348. return &ev->base;
  349. }
  350. /* Shutdown simply drops a ref that we reserved at creation time; if we drop
  351. to zero here, then enter shutdown mode and wake up any waiters */
  352. void grpc_completion_queue_shutdown(grpc_completion_queue *cc) {
  353. if (gpr_unref(&cc->refs)) {
  354. gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
  355. GPR_ASSERT(!cc->shutdown);
  356. cc->shutdown = 1;
  357. gpr_cv_broadcast(GRPC_POLLSET_CV(&cc->pollset));
  358. gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
  359. }
  360. }
  361. void grpc_completion_queue_destroy(grpc_completion_queue *cc) {
  362. GPR_ASSERT(cc->queue == NULL);
  363. grpc_pollset_destroy(&cc->pollset);
  364. gpr_free(cc);
  365. }
  366. void grpc_event_finish(grpc_event *base) {
  367. event *ev = (event *)base;
  368. ev->on_finish(ev->on_finish_user_data, GRPC_OP_OK);
  369. if (ev->base.call) {
  370. grpc_call_internal_unref(ev->base.call, 1);
  371. }
  372. gpr_free(ev);
  373. }
  374. void grpc_cq_dump_pending_ops(grpc_completion_queue *cc) {
  375. #ifndef NDEBUG
  376. char tmp[GRPC_COMPLETION_DO_NOT_USE * (1 + GPR_LTOA_MIN_BUFSIZE)];
  377. char *p = tmp;
  378. int i;
  379. for (i = 0; i < GRPC_COMPLETION_DO_NOT_USE; i++) {
  380. *p++ = ' ';
  381. p += gpr_ltoa(cc->pending_op_count[i], p);
  382. }
  383. gpr_log(GPR_INFO, "pending ops:%s", tmp);
  384. #endif
  385. }
  386. grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) {
  387. return &cc->pollset;
  388. }