PINOperationGroup.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // PINOperationGroup.m
  3. // PINQueue
  4. //
  5. // Created by Garrett Moon on 10/8/16.
  6. // Copyright © 2016 Pinterest. All rights reserved.
  7. //
  8. #import "PINOperationGroup.h"
  9. #import "PINOperation.h"
  10. #import <pthread.h>
  11. @interface NSNumber (PINGroupOperationQueue) <PINGroupOperationReference>
  12. @end
  13. @interface PINOperationGroup ()
  14. {
  15. pthread_mutex_t _lock;
  16. PINOperationQueue *_operationQueue;
  17. NSMutableArray <dispatch_block_t> *_operations;
  18. NSMutableArray <NSNumber *> *_operationPriorities;
  19. NSMutableArray <id <PINGroupOperationReference>> *_operationReferences;
  20. NSMapTable <id <PINGroupOperationReference>, id <PINOperationReference>> *_groupToOperationReferences;
  21. NSUInteger _operationReferenceCount;
  22. dispatch_group_t _group;
  23. dispatch_block_t _completion;
  24. BOOL _started;
  25. BOOL _canceled;
  26. }
  27. - (instancetype)initWithOperationQueue:(PINOperationQueue *)operationQueue NS_DESIGNATED_INITIALIZER;
  28. @end
  29. @implementation PINOperationGroup
  30. - (instancetype)initWithOperationQueue:(PINOperationQueue *)operationQueue
  31. {
  32. if (self = [super init]) {
  33. pthread_mutex_init(&_lock, NULL);
  34. _operationQueue = operationQueue;
  35. _operations = [[NSMutableArray alloc] init];
  36. _operationReferences = [[NSMutableArray alloc] init];
  37. _operationPriorities = [[NSMutableArray alloc] init];
  38. _groupToOperationReferences = [NSMapTable weakToStrongObjectsMapTable];
  39. _group = dispatch_group_create();
  40. }
  41. return self;
  42. }
  43. - (void)dealloc
  44. {
  45. pthread_mutex_destroy(&_lock);
  46. }
  47. + (instancetype)asyncOperationGroupWithQueue:(PINOperationQueue *)operationQueue
  48. {
  49. return [[self alloc] initWithOperationQueue:operationQueue];
  50. }
  51. - (id <PINGroupOperationReference>)locked_nextOperationReference
  52. {
  53. id <PINGroupOperationReference> reference = [NSNumber numberWithUnsignedInteger:++_operationReferenceCount];
  54. return reference;
  55. }
  56. - (void)start
  57. {
  58. [self lock];
  59. NSAssert(_canceled == NO, @"Operation group canceled.");
  60. if (_started == NO && _canceled == NO) {
  61. for (NSUInteger idx = 0; idx < _operations.count; idx++) {
  62. dispatch_group_enter(_group);
  63. dispatch_block_t originalOperation = _operations[idx];
  64. dispatch_block_t groupBlock = ^{
  65. originalOperation();
  66. dispatch_group_leave(_group);
  67. };
  68. id <PINOperationReference> operationReference = [_operationQueue addOperation:groupBlock withPriority:[_operationPriorities[idx] unsignedIntegerValue]];
  69. [_groupToOperationReferences setObject:operationReference forKey:_operationReferences[idx]];
  70. }
  71. if (_completion) {
  72. dispatch_queue_t completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  73. dispatch_group_notify(_group, completionQueue, ^{
  74. [self runCompletionIfNeeded];
  75. });
  76. }
  77. _operations = nil;
  78. _operationPriorities = nil;
  79. _operationReferences = nil;
  80. }
  81. [self unlock];
  82. }
  83. - (void)cancel
  84. {
  85. [self lock];
  86. _canceled = YES;
  87. for (id <PINOperationReference>operationReference in [_groupToOperationReferences objectEnumerator]) {
  88. if ([_operationQueue cancelOperation:operationReference]) {
  89. dispatch_group_leave(_group);
  90. }
  91. }
  92. //TODO just nil out instead? Does it make sense to support adding operations after cancelation?
  93. [_groupToOperationReferences removeAllObjects];
  94. [_operations removeAllObjects];
  95. [_operationPriorities removeAllObjects];
  96. [_operationReferences removeAllObjects];
  97. _completion = nil;
  98. [self unlock];
  99. }
  100. - (id <PINGroupOperationReference>)addOperation:(dispatch_block_t)operation
  101. {
  102. return [self addOperation:operation withPriority:PINOperationQueuePriorityDefault];
  103. }
  104. - (id <PINGroupOperationReference>)addOperation:(dispatch_block_t)operation withPriority:(PINOperationQueuePriority)priority
  105. {
  106. [self lock];
  107. id <PINGroupOperationReference> reference = nil;
  108. NSAssert(_started == NO && _canceled == NO, @"Operation group already started or canceled.");
  109. if (_started == NO && _canceled == NO) {
  110. reference = [self locked_nextOperationReference];
  111. [_operations addObject:operation];
  112. [_operationPriorities addObject:@(priority)];
  113. [_operationReferences addObject:reference];
  114. }
  115. [self unlock];
  116. return reference;
  117. }
  118. - (void)setCompletion:(dispatch_block_t)completion
  119. {
  120. [self lock];
  121. NSAssert(_started == NO && _canceled == NO, @"Operation group already started or canceled.");
  122. if (_started == NO && _canceled == NO) {
  123. _completion = completion;
  124. }
  125. [self unlock];
  126. }
  127. - (void)waitUntilComplete
  128. {
  129. [self start];
  130. dispatch_group_wait(_group, DISPATCH_TIME_FOREVER);
  131. [self runCompletionIfNeeded];
  132. }
  133. - (void)runCompletionIfNeeded
  134. {
  135. dispatch_block_t completion;
  136. [self lock];
  137. completion = _completion;
  138. _completion = nil;
  139. [self unlock];
  140. if (completion) {
  141. completion();
  142. }
  143. }
  144. - (void)lock
  145. {
  146. pthread_mutex_lock(&_lock);
  147. }
  148. - (void)unlock
  149. {
  150. pthread_mutex_unlock(&_lock);
  151. }
  152. @end