grpc_completion.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "Firestore/core/src/remote/grpc_completion.h"
  17. #include <memory>
  18. #include <utility>
  19. namespace firebase {
  20. namespace firestore {
  21. namespace remote {
  22. using util::AsyncQueue;
  23. std::shared_ptr<GrpcCompletion> GrpcCompletion::Create(
  24. Type type,
  25. const std::shared_ptr<util::AsyncQueue>& worker_queue,
  26. Callback&& callback) {
  27. // Construct in two steps to use the private constructor.
  28. GrpcCompletion partial(type, worker_queue, std::move(callback));
  29. auto completion = std::make_shared<GrpcCompletion>(std::move(partial));
  30. // Prepare the `GrpcCompletion` for submission to gRPC.
  31. //
  32. // Note: this is done in a separate step because `shared_from_this` cannot be
  33. // called in a constructor.
  34. completion->grpc_ownership_ = completion;
  35. return completion;
  36. }
  37. GrpcCompletion::GrpcCompletion(
  38. Type type,
  39. const std::shared_ptr<util::AsyncQueue>& worker_queue,
  40. Callback&& callback)
  41. : worker_queue_{worker_queue}, callback_{std::move(callback)}, type_{type} {
  42. }
  43. void GrpcCompletion::Cancel() {
  44. worker_queue_->VerifyIsCurrentQueue();
  45. callback_ = {};
  46. // Does not release grpc_ownership_. If gRPC still holds this completion it
  47. // must remain valid to avoid a use-after-free once Complete is actually
  48. // called.
  49. }
  50. void GrpcCompletion::WaitUntilOffQueue() {
  51. worker_queue_->VerifyIsCurrentQueue();
  52. EnsureValidFuture();
  53. off_queue_future_.wait();
  54. }
  55. std::future_status GrpcCompletion::WaitUntilOffQueue(
  56. std::chrono::milliseconds timeout) {
  57. worker_queue_->VerifyIsCurrentQueue();
  58. EnsureValidFuture();
  59. return off_queue_future_.wait_for(timeout);
  60. }
  61. void GrpcCompletion::EnsureValidFuture() {
  62. if (!off_queue_future_.valid()) {
  63. off_queue_future_ = off_queue_.get_future();
  64. }
  65. }
  66. void GrpcCompletion::Complete(bool ok) {
  67. // This mechanism allows `GrpcStream` to know when the completion is off the
  68. // gRPC completion queue (and thus no longer requires the underlying gRPC
  69. // objects to be valid).
  70. off_queue_.set_value();
  71. // The queued operation needs to also retain this completion. It's possible
  72. // for Complete to fire, shutdown to start, and then have this queued
  73. // operation run. If this weren't a retain that ordering would have the
  74. // callback use after free.
  75. auto shared_this = grpc_ownership_;
  76. worker_queue_->Enqueue([shared_this, ok] {
  77. if (shared_this->callback_) {
  78. shared_this->callback_(ok, shared_this);
  79. }
  80. });
  81. // Having called Complete, gRPC has released its ownership interest in this
  82. // object. Once the queued operation completes the `GrpcCompletion` will be
  83. // deleted.
  84. grpc_ownership_.reset();
  85. }
  86. } // namespace remote
  87. } // namespace firestore
  88. } // namespace firebase