FSTDispatchQueue.mm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2017 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. #import <Foundation/Foundation.h>
  17. #include <memory>
  18. #include <utility>
  19. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  20. #include "Firestore/core/src/firebase/firestore/util/async_queue.h"
  21. #include "Firestore/core/src/firebase/firestore/util/executor_libdispatch.h"
  22. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  23. #include "absl/memory/memory.h"
  24. using firebase::firestore::util::AsyncQueue;
  25. using firebase::firestore::util::DelayedOperation;
  26. using firebase::firestore::util::TimerId;
  27. using firebase::firestore::util::internal::Executor;
  28. using firebase::firestore::util::internal::ExecutorLibdispatch;
  29. NS_ASSUME_NONNULL_BEGIN
  30. #pragma mark - FSTDelayedCallback
  31. @interface FSTDelayedCallback () {
  32. DelayedOperation _impl;
  33. }
  34. @end
  35. @implementation FSTDelayedCallback
  36. - (instancetype)initWithImpl:(DelayedOperation &&)impl {
  37. if (self = [super init]) {
  38. _impl = std::move(impl);
  39. }
  40. return self;
  41. }
  42. - (void)cancel {
  43. _impl.Cancel();
  44. }
  45. @end
  46. #pragma mark - FSTDispatchQueue
  47. @implementation FSTDispatchQueue {
  48. std::unique_ptr<AsyncQueue> _impl;
  49. }
  50. + (TimerId)convertTimerId:(FSTTimerID)objcTimerID {
  51. const TimerId converted = static_cast<TimerId>(objcTimerID);
  52. switch (converted) {
  53. case TimerId::All:
  54. case TimerId::ListenStreamIdle:
  55. case TimerId::ListenStreamConnectionBackoff:
  56. case TimerId::WriteStreamIdle:
  57. case TimerId::WriteStreamConnectionBackoff:
  58. case TimerId::OnlineStateTimeout:
  59. return converted;
  60. default:
  61. HARD_FAIL("Unknown value of enum FSTTimerID.");
  62. }
  63. }
  64. + (instancetype)queueWith:(dispatch_queue_t)dispatchQueue {
  65. return [[FSTDispatchQueue alloc] initWithQueue:dispatchQueue];
  66. }
  67. - (instancetype)initWithQueue:(dispatch_queue_t)queue {
  68. if (self = [super init]) {
  69. _queue = queue;
  70. auto executor = absl::make_unique<ExecutorLibdispatch>(queue);
  71. _impl = absl::make_unique<AsyncQueue>(std::move(executor));
  72. }
  73. return self;
  74. }
  75. - (void)verifyIsCurrentQueue {
  76. _impl->VerifyIsCurrentQueue();
  77. }
  78. - (void)enterCheckedOperation:(void (^)(void))block {
  79. _impl->ExecuteBlocking([block] { block(); });
  80. }
  81. - (void)dispatchAsync:(void (^)(void))block {
  82. _impl->Enqueue([block] { block(); });
  83. }
  84. - (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block {
  85. _impl->EnqueueRelaxed([block] { block(); });
  86. }
  87. - (void)dispatchSync:(void (^)(void))block {
  88. _impl->EnqueueBlocking([block] { block(); });
  89. }
  90. - (FSTDelayedCallback *)dispatchAfterDelay:(NSTimeInterval)delay
  91. timerID:(FSTTimerID)timerID
  92. block:(void (^)(void))block {
  93. const AsyncQueue::Milliseconds delayMs =
  94. std::chrono::milliseconds(static_cast<long long>(delay * 1000));
  95. const TimerId convertedTimerId = [FSTDispatchQueue convertTimerId:timerID];
  96. DelayedOperation delayed_operation =
  97. _impl->EnqueueAfterDelay(delayMs, convertedTimerId, [block] { block(); });
  98. return [[FSTDelayedCallback alloc] initWithImpl:std::move(delayed_operation)];
  99. }
  100. - (BOOL)containsDelayedCallbackWithTimerID:(FSTTimerID)timerID {
  101. return _impl->IsScheduled([FSTDispatchQueue convertTimerId:timerID]);
  102. }
  103. - (void)runDelayedCallbacksUntil:(FSTTimerID)lastTimerID {
  104. _impl->RunScheduledOperationsUntil([FSTDispatchQueue convertTimerId:lastTimerID]);
  105. }
  106. @end
  107. NS_ASSUME_NONNULL_END