| 7 |
| dispatch_queue_t _queue; |
| 8 | void *_queueSpecific; |
| 9 | bool _specialIsMainQueue; |
| 10 | } |
| 11 |
| 12 | @end |
| 13 |
| 14 | @implementation SQueue |
| 15 |
| 16 | + (SQueue *)mainQueue |
| 17 | { |
| 18 | static SQueue *queue = nil; |
| 19 | static dispatch_once_t onceToken; |
| 20 | dispatch_once(&onceToken, ^ |
| 21 | { |
| 22 | queue = [[SQueue alloc] initWithNativeQueue:dispatch_get_main_queue() queueSpecific:NULL]; |
| 23 | queue->_specialIsMainQueue = true; |
| 24 | }); |
| 25 |
| 26 | return queue; |
| 27 | } |
| 28 |
| 29 | + (SQueue *)concurrentDefaultQueue |
| 30 | { |
| 31 | static SQueue *queue = nil; |
| 32 | static dispatch_once_t onceToken; |
| 33 | dispatch_once(&onceToken, ^ |
| 34 | { |
| 35 | queue = [[SQueue alloc] initWithNativeQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) queueSpecific:NULL]; |
| 36 | }); |
| 37 |
| 38 | return queue; |
| 39 | } |
| 40 |
| 41 | + (SQueue *)concurrentBackgroundQueue |
| 42 | { |
| 43 | static SQueue *queue = nil; |
| 44 | static dispatch_once_t onceToken; |
| 45 | dispatch_once(&onceToken, ^ |
| 46 | { |
| 47 | queue = [[SQueue alloc] initWithNativeQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) queueSpecific:NULL]; |
| 48 | }); |
| 49 |
| 50 | return queue; |
| 51 | } |
| 52 |
| 53 | + (SQueue *)wrapConcurrentNativeQueue:(dispatch_queue_t)nativeQueue |
| 54 | { |
| 55 | return [[SQueue alloc] initWithNativeQueue:nativeQueue queueSpecific:NULL]; |
| 56 | } |
| 57 |
| 58 | - (instancetype)init |
| 59 | { |
| 60 | dispatch_queue_t queue = dispatch_queue_create(NULL, NULL); |
| 61 | dispatch_queue_set_specific(queue, SQueueSpecificKey, (__bridge void *)self, NULL); |
| 62 | return [self initWithNativeQueue:queue queueSpecific:(__bridge void *)self]; |
| 63 | } |
| 64 |
| 65 | - (instancetype)initWithNativeQueue:(dispatch_queue_t)queue queueSpecific:(void *)queueSpecific |
| 66 | { |
| 67 | self = [super init]; |
| 68 | if (self != nil) |
| 69 | { |
| 70 | _queue = queue; |
| 71 | _queueSpecific = queueSpecific; |
| 72 | } |
| 73 | return self; |
| 74 | } |
| 75 |
| 76 | - (dispatch_queue_t)_dispatch_queue |
| 77 | { |
| 78 | return _queue; |
| 79 | } |
| 80 |
| 81 | - (void)dispatch:(dispatch_block_t)block |
| 82 | { |
| 83 | if (_queueSpecific != NULL && dispatch_get_specific(SQueueSpecificKey) == _queueSpecific) |
| 84 | block(); |
| 85 | else if (_specialIsMainQueue && [NSThread isMainThread]) |
| 86 | block(); |
| 87 | else |
| 88 | dispatch_async(_queue, block); |
| 89 | } |
| 90 |
| 91 | - (void)dispatchSync:(dispatch_block_t)block |
| 92 | { |
| 93 | if (_queueSpecific != NULL && dispatch_get_specific(SQueueSpecificKey) == _queueSpecific) |
| 94 | block(); |
| 95 | else if (_specialIsMainQueue && [NSThread isMainThread]) |
| 96 | block(); |
| 97 | else |
| 98 | dispatch_sync(_queue, block); |
| 99 | } |
| 100 |
| 101 | - (void)dispatch:(dispatch_block_t)block synchronous:(bool)synchronous { |
| 102 | if (_queueSpecific != NULL && dispatch_get_specific(SQueueSpecificKey) == _queueSpecific) |
| 103 | block(); |
| 104 | else if (_specialIsMainQueue && [NSThread isMainThread]) |
| 105 | block(); |
| 106 | else { |
| 107 | if (synchronous) { |
| 108 | dispatch_sync(_queue, block); |
| 109 | } else { |
| 110 | dispatch_async(_queue, block); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 |
| 115 | - (bool)isCurrentQueue |
| 116 | { |
| 117 | if (_queueSpecific != NULL && dispatch_get_specific(SQueueSpecificKey) == _queueSpecific) |
| 118 | return true; |
| 119 | else if (_specialIsMainQueue && [NSThread isMainThread]) |
| 120 | return true; |
| 121 | return false; |
| 122 | } |
| 123 |
| 124 | @end |
| 125 |