completion_queue.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2015, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. */
  32. #include <grpc++/completion_queue.h>
  33. #include <memory>
  34. #include <grpc/grpc.h>
  35. #include <grpc/support/log.h>
  36. #include <grpc++/time.h>
  37. namespace grpc {
  38. CompletionQueue::CompletionQueue() { cq_ = grpc_completion_queue_create(); }
  39. CompletionQueue::CompletionQueue(grpc_completion_queue* take) : cq_(take) {}
  40. CompletionQueue::~CompletionQueue() { grpc_completion_queue_destroy(cq_); }
  41. void CompletionQueue::Shutdown() { grpc_completion_queue_shutdown(cq_); }
  42. CompletionQueue::NextStatus CompletionQueue::AsyncNextInternal(
  43. void** tag, bool* ok, gpr_timespec deadline) {
  44. for (;;) {
  45. auto ev = grpc_completion_queue_next(cq_, deadline);
  46. switch (ev.type) {
  47. case GRPC_QUEUE_TIMEOUT:
  48. return TIMEOUT;
  49. case GRPC_QUEUE_SHUTDOWN:
  50. return SHUTDOWN;
  51. case GRPC_OP_COMPLETE:
  52. auto cq_tag = static_cast<CompletionQueueTag*>(ev.tag);
  53. *ok = ev.success != 0;
  54. *tag = cq_tag;
  55. if (cq_tag->FinalizeResult(tag, ok)) {
  56. return GOT_EVENT;
  57. }
  58. break;
  59. }
  60. }
  61. }
  62. bool CompletionQueue::Pluck(CompletionQueueTag* tag) {
  63. auto ev =
  64. grpc_completion_queue_pluck(cq_, tag, gpr_inf_future(GPR_CLOCK_REALTIME));
  65. bool ok = ev.success != 0;
  66. void* ignored = tag;
  67. GPR_ASSERT(tag->FinalizeResult(&ignored, &ok));
  68. GPR_ASSERT(ignored == tag);
  69. // Ignore mutations by FinalizeResult: Pluck returns the C API status
  70. return ev.success != 0;
  71. }
  72. void CompletionQueue::TryPluck(CompletionQueueTag* tag) {
  73. auto ev =
  74. grpc_completion_queue_pluck(cq_, tag, gpr_time_0(GPR_CLOCK_REALTIME));
  75. if (ev.type == GRPC_QUEUE_TIMEOUT) return;
  76. bool ok = ev.success != 0;
  77. void* ignored = tag;
  78. // the tag must be swallowed if using TryPluck
  79. GPR_ASSERT(!tag->FinalizeResult(&ignored, &ok));
  80. }
  81. } // namespace grpc