SQueue updates

This commit is contained in:
Peter 2015-02-05 18:24:35 +03:00
parent 8a90fe2db6
commit b14f28c089
3 changed files with 65 additions and 1 deletions

View File

@ -671,6 +671,7 @@
"DEBUG=1",
"$(inherited)",
);
ONLY_ACTIVE_ARCH = NO;
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;

View File

@ -2,6 +2,10 @@
@interface SQueue : NSObject
+ (SQueue *)mainQueue;
+ (SQueue *)concurrentDefaultQueue;
+ (SQueue *)concurrentBackgroundQueue;
- (void)dispatch:(dispatch_block_t)block;
- (dispatch_queue_t)_dispatch_queue;

View File

@ -1,20 +1,69 @@
#import "SQueue.h"
static const void *SQueueSpecificKey = &SQueueSpecificKey;
@interface SQueue ()
{
dispatch_queue_t _queue;
void *_queueSpecific;
bool _specialIsMainQueue;
}
@end
@implementation SQueue
+ (SQueue *)mainQueue
{
static SQueue *queue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
queue = [[SQueue alloc] initWithNativeQueue:dispatch_get_main_queue() queueSpecific:NULL];
queue->_specialIsMainQueue = true;
});
return queue;
}
+ (SQueue *)concurrentDefaultQueue
{
static SQueue *queue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
queue = [[SQueue alloc] initWithNativeQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) queueSpecific:NULL];
});
return queue;
}
+ (SQueue *)concurrentBackgroundQueue
{
static SQueue *queue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
queue = [[SQueue alloc] initWithNativeQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) queueSpecific:NULL];
});
return queue;
}
- (instancetype)init
{
dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
dispatch_queue_set_specific(queue, SQueueSpecificKey, (__bridge void *)self, NULL);
return [self initWithNativeQueue:queue queueSpecific:(__bridge void *)self];
}
- (instancetype)initWithNativeQueue:(dispatch_queue_t)queue queueSpecific:(void *)queueSpecific
{
self = [super init];
if (self != nil)
{
_queue = dispatch_queue_create(NULL, NULL);
_queue = queue;
_queueSpecific = queueSpecific;
}
return self;
}
@ -29,4 +78,14 @@
dispatch_async(_queue, block);
}
- (void)dispatchSync:(dispatch_block_t)block
{
if (_queueSpecific != NULL && dispatch_get_specific(SQueueSpecificKey) == _queueSpecific)
block();
else if (_specialIsMainQueue && [NSThread isMainThread])
block();
else
dispatch_sync(_queue, block);
}
@end