Skip to content
This repository
tree: e37055c070
Fetching contributors…

Cannot retrieve contributors at this time

file 154 lines (129 sloc) 3.901 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/*
// CWSerialBlockQueue.m
// Zangetsu
//
// Created by Colin Wheeler on 2/23/12.
// Copyright (c) 2012. All rights reserved.
//
Copyright (c) 2012 Colin Wheeler
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#import "CWSerialBlockQueue.h"

@interface CWSerialBlockOperation()
@property(copy) dispatch_block_t operationBlock;
@end

@implementation CWSerialBlockOperation

- (id)init
{
    self = [super init];
    if (self) {
        _operationBlock = nil;
    }
    return self;
}

+(CWSerialBlockOperation *)blockOperationWithBlock:(dispatch_block_t)block
{
CWSerialBlockOperation *operation = [[self alloc] init];
operation.operationBlock = block;
return operation;
}

@end

@interface CWSerialBlockQueue()
@property(assign) dispatch_queue_t queue;
@end

@implementation CWSerialBlockQueue

- (id)init
{
    self = [super init];
    if (self) {
_queue = dispatch_queue_create(CWUUIDCStringPrependedWithString(@"com.Zangetsu.CWSerialBlockQueue-"), DISPATCH_QUEUE_SERIAL);
    }
    return self;
}

-(id)initWithLabel:(NSString *)qLabel
{
self = [super init];
if (self) {
if (qLabel) {
_queue = dispatch_queue_create([qLabel UTF8String], DISPATCH_QUEUE_SERIAL);
} else {
_queue = dispatch_queue_create(CWUUIDCStringPrependedWithString(@"com.Zangetsu.CWSerialBlockQueue-"), DISPATCH_QUEUE_SERIAL);
}
}
return self;
}

-(id)initWithLabel:(NSString *)qLabel andBlockOperationObjects:(NSArray *)blockOperations
{
self = [super init];
if (self) {
if (qLabel) {
_queue = dispatch_queue_create([qLabel UTF8String], DISPATCH_QUEUE_SERIAL);
} else {
_queue = dispatch_queue_create(CWUUIDCStringPrependedWithString(@"com.Zangetsu.CWSerialBlockQueue-"), DISPATCH_QUEUE_SERIAL);
}
for (CWSerialBlockOperation *op in blockOperations) {
dispatch_async(_queue, ^{ op.operationBlock(); });
}
}
return self;
}

-(NSString *)label
{
NSString *queueLabel = [NSString stringWithCString:dispatch_queue_get_label(self.queue)
encoding:NSUTF8StringEncoding];
return queueLabel;
}

-(void)resume
{
dispatch_resume(self.queue);
}

-(void)suspend
{
dispatch_suspend(self.queue);
}

-(void)addBlockOperation:(CWSerialBlockOperation *)operation
{
if(!operation) { return; }
dispatch_async(self.queue, ^{ operation.operationBlock(); });
}

-(void)addBlockOperationObjects:(NSArray *)operationObjects
{
if (operationObjects && ([operationObjects count] > 0)) {
for (id op in operationObjects) {
if ([op isMemberOfClass:[CWSerialBlockOperation class]]) {
[self addBlockOperationObjects:op];
}
}
}
}

-(void)addOperationwithBlock:(dispatch_block_t)block
{
dispatch_async(self.queue, block);
}

-(void)waitUntilAllBlocksHaveProcessed
{
dispatch_barrier_sync(self.queue, ^{ });
}

-(void)executeWhenAllBlocksHaveFinished:(dispatch_block_t)block
{
dispatch_barrier_async(self.queue, block);
}

-(void)dealloc
{
dispatch_release(_queue);
}

@end
Something went wrong with that request. Please try again.