FSTDispatchQueue.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. NS_ASSUME_NONNULL_BEGIN
  18. /**
  19. * Well-known "timer" IDs used when scheduling delayed callbacks on the FSTDispatchQueue. These IDs
  20. * can then be used from tests to check for the presence of callbacks or to run them early.
  21. */
  22. typedef NS_ENUM(NSInteger, FSTTimerID) {
  23. /** All can be used with runDelayedCallbacksUntil: to run all timers. */
  24. FSTTimerIDAll,
  25. /**
  26. * The following 4 timers are used in FSTStream for the listen and write streams. The "Idle" timer
  27. * is used to close the stream due to inactivity. The "ConnectionBackoff" timer is used to
  28. * restart a stream once the appropriate backoff delay has elapsed.
  29. */
  30. FSTTimerIDListenStreamIdle,
  31. FSTTimerIDListenStreamConnectionBackoff,
  32. FSTTimerIDWriteStreamIdle,
  33. FSTTimerIDWriteStreamConnectionBackoff,
  34. /**
  35. * A timer used in FSTOnlineStateTracker to transition from FSTOnlineState Unknown to Offline
  36. * after a set timeout, rather than waiting indefinitely for success or failure.
  37. */
  38. FSTTimerIDOnlineStateTimeout
  39. };
  40. /**
  41. * Handle to a callback scheduled via [FSTDispatchQueue dispatchAfterDelay:]. Supports cancellation
  42. * via the cancel method.
  43. */
  44. @interface FSTDelayedCallback : NSObject
  45. /**
  46. * Cancels the callback if it hasn't already been executed or canceled.
  47. *
  48. * As long as the callback has not yet been run, calling cancel() (from a callback already running
  49. * on the dispatch queue) provides a guarantee that the operation will not be run.
  50. */
  51. - (void)cancel;
  52. @end
  53. @interface FSTDispatchQueue : NSObject
  54. /** Creates and returns an FSTDispatchQueue wrapping the specified dispatch_queue_t. */
  55. + (instancetype)queueWith:(dispatch_queue_t)dispatchQueue;
  56. - (instancetype)initWithQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
  57. - (instancetype)init __attribute__((unavailable("Use static constructor method.")));
  58. /**
  59. * Asserts that we are already running on this queue (actually, we can only verify that the
  60. * queue's label is the same, but hopefully that's good enough.)
  61. */
  62. - (void)verifyIsCurrentQueue;
  63. /**
  64. * Declares that we are already executing on the correct dispatch_queue_t and would like to
  65. * officially execute code on behalf of this FSTDispatchQueue. To be used only when called back
  66. * by some other API directly onto our queue. This allows us to safely dispatch directly onto the
  67. * worker queue without destroying the invariants this class helps us maintain.
  68. */
  69. - (void)enterCheckedOperation:(void (^)(void))block;
  70. /**
  71. * Same as dispatch_async() except it asserts that we're not already on the queue, since this
  72. * generally indicates a bug (and can lead to re-ordering of operations, etc).
  73. *
  74. * @param block The block to run.
  75. */
  76. - (void)dispatchAsync:(void (^)(void))block;
  77. /**
  78. * Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
  79. * the current one (thus it is equivalent to a raw dispatch_async()).
  80. *
  81. * This is useful, e.g. for dispatching to the user's queue directly from user API call (in which
  82. * case we don't know if we're already on the user's queue or not).
  83. *
  84. * @param block The block to run.
  85. */
  86. - (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block;
  87. /**
  88. * Wrapper for dispatch_sync(). Mostly meant for use in tests.
  89. *
  90. * @param block The block to run.
  91. */
  92. - (void)dispatchSync:(void (^)(void))block;
  93. /**
  94. * Schedules a callback after the specified delay.
  95. *
  96. * Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
  97. * the current one.
  98. *
  99. * The returned FSTDelayedCallback handle can be used to cancel the callback prior to its running.
  100. *
  101. * @param block The block to run.
  102. * @param delay The delay (in seconds) after which to run the block.
  103. * @param timerID An FSTTimerID that can be used from tests to check for the presence of this
  104. * callback or to schedule it to run early.
  105. * @return A FSTDelayedCallback instance that can be used for cancellation.
  106. */
  107. - (FSTDelayedCallback *)dispatchAfterDelay:(NSTimeInterval)delay
  108. timerID:(FSTTimerID)timerID
  109. block:(void (^)(void))block;
  110. /**
  111. * For Tests: Determine if a delayed callback with a particular FSTTimerID exists.
  112. */
  113. - (BOOL)containsDelayedCallbackWithTimerID:(FSTTimerID)timerID;
  114. /**
  115. * For Tests: Runs delayed callbacks early, blocking until completion.
  116. *
  117. * @param lastTimerID Only delayed callbacks up to and including one that was scheduled using this
  118. * FSTTimerID will be run. Method throws if no matching callback exists.
  119. */
  120. - (void)runDelayedCallbacksUntil:(FSTTimerID)lastTimerID;
  121. /** The underlying wrapped dispatch_queue_t */
  122. @property(nonatomic, strong, readonly) dispatch_queue_t queue;
  123. @end
  124. NS_ASSUME_NONNULL_END