FSTDispatchQueue.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. FSTTimerIDAll, // Sentinel value to be used with runDelayedCallbacksUntil: to run all blocks.
  24. FSTTimerIDListenStreamIdle,
  25. FSTTimerIDListenStreamConnection,
  26. FSTTimerIDWriteStreamIdle,
  27. FSTTimerIDWriteStreamConnection
  28. };
  29. /**
  30. * Handle to a callback scheduled via [FSTDispatchQueue dispatchAfterDelay:]. Supports cancellation
  31. * via the cancel method.
  32. */
  33. @interface FSTDelayedCallback : NSObject
  34. /**
  35. * Cancels the callback if it hasn't already been executed or canceled.
  36. *
  37. * As long as the callback has not yet been run, calling cancel() (from a callback already running
  38. * on the dispatch queue) provides a guarantee that the operation will not be run.
  39. */
  40. - (void)cancel;
  41. @end
  42. @interface FSTDispatchQueue : NSObject
  43. /** Creates and returns an FSTDispatchQueue wrapping the specified dispatch_queue_t. */
  44. + (instancetype)queueWith:(dispatch_queue_t)dispatchQueue;
  45. - (instancetype)initWithQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
  46. - (instancetype)init __attribute__((unavailable("Use static constructor method.")));
  47. /**
  48. * Asserts that we are already running on this queue (actually, we can only verify that the
  49. * queue's label is the same, but hopefully that's good enough.)
  50. */
  51. - (void)verifyIsCurrentQueue;
  52. /**
  53. * Same as dispatch_async() except it asserts that we're not already on the queue, since this
  54. * generally indicates a bug (and can lead to re-ordering of operations, etc).
  55. *
  56. * @param block The block to run.
  57. */
  58. - (void)dispatchAsync:(void (^)(void))block;
  59. /**
  60. * Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
  61. * the current one (thus it is equivalent to a raw dispatch_async()).
  62. *
  63. * This is useful, e.g. for dispatching to the user's queue directly from user API call (in which
  64. * case we don't know if we're already on the user's queue or not).
  65. *
  66. * @param block The block to run.
  67. */
  68. - (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block;
  69. /**
  70. * Schedules a callback after the specified delay.
  71. *
  72. * Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
  73. * the current one.
  74. *
  75. * The returned FSTDelayedCallback handle can be used to cancel the callback prior to its running.
  76. *
  77. * @param block The block to run.
  78. * @param delay The delay (in seconds) after which to run the block.
  79. * @param timerID An FSTTimerID that can be used from tests to check for the presence of this
  80. * callback or to schedule it to run early.
  81. * @return A FSTDelayedCallback instance that can be used for cancellation.
  82. */
  83. - (FSTDelayedCallback *)dispatchAfterDelay:(NSTimeInterval)delay
  84. timerID:(FSTTimerID)timerID
  85. block:(void (^)(void))block;
  86. /**
  87. * For Tests: Determine if a delayed callback with a particular FSTTimerID exists.
  88. */
  89. - (BOOL)containsDelayedCallbackWithTimerID:(FSTTimerID)timerID;
  90. /**
  91. * For Tests: Runs delayed callbacks early, blocking until completion.
  92. *
  93. * @param lastTimerID Only delayed callbacks up to and including one that was scheduled using this
  94. * FSTTimerID will be run. Method throws if no matching callback exists.
  95. */
  96. - (void)runDelayedCallbacksUntil:(FSTTimerID)lastTimerID;
  97. /** The underlying wrapped dispatch_queue_t */
  98. @property(nonatomic, strong, readonly) dispatch_queue_t queue;
  99. @end
  100. NS_ASSUME_NONNULL_END