Fix a race condition.
This commit is contained in:
1 parent
9224a6e0db
commit
b21eab22e4
1 file changed
+53
-20
+53
-20
@@ -29,7 +29,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
|
|||||||
@property (nonatomic) IOReturn io_success;
|
@property (nonatomic) IOReturn io_success;
|
||||||
|
|
||||||
@property (nonatomic) dispatch_queue_t workQueue;
|
@property (nonatomic) dispatch_queue_t workQueue;
|
||||||
@property (nonatomic) dispatch_queue_t saveQueue;
|
@property (nonatomic) dispatch_queue_t arrayQueue;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
|
|||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
if (self) {
|
||||||
_workQueue = dispatch_queue_create("org.subler.WorkQueue", NULL);
|
_workQueue = dispatch_queue_create("org.subler.WorkQueue", NULL);
|
||||||
_saveQueue = dispatch_queue_create("org.subler.SaveQueue", NULL);
|
_arrayQueue = dispatch_queue_create("org.subler.SaveQueue", NULL);
|
||||||
_URL = [queueURL copy];
|
_URL = [queueURL copy];
|
||||||
|
|
||||||
if ([[NSFileManager defaultManager] fileExistsAtPath:queueURL.path]) {
|
if ([[NSFileManager defaultManager] fileExistsAtPath:queueURL.path]) {
|
||||||
@@ -51,10 +51,11 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
|
|||||||
_items = nil;
|
_items = nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (SBQueueItem *item in _items)
|
for (SBQueueItem *item in _items) {
|
||||||
if (item.status == SBQueueItemStatusWorking)
|
if (item.status == SBQueueItemStatusWorking) {
|
||||||
item.status = SBQueueItemStatusFailed;
|
item.status = SBQueueItemStatusFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_items) {
|
if (!_items) {
|
||||||
@@ -67,7 +68,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
|
|||||||
|
|
||||||
- (BOOL)saveQueueToDisk {
|
- (BOOL)saveQueueToDisk {
|
||||||
__block BOOL result;
|
__block BOOL result;
|
||||||
dispatch_sync(self.saveQueue, ^{
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
result = [NSKeyedArchiver archiveRootObject:self.items toFile:self.URL.path];
|
result = [NSKeyedArchiver archiveRootObject:self.items toFile:self.URL.path];
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
@@ -75,7 +76,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
|
|||||||
|
|
||||||
- (SBQueueItem *)firstItemInQueue {
|
- (SBQueueItem *)firstItemInQueue {
|
||||||
__block SBQueueItem *firstItem = nil;
|
__block SBQueueItem *firstItem = nil;
|
||||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
for (SBQueueItem *item in self.items) {
|
for (SBQueueItem *item in self.items) {
|
||||||
if ((item.status != SBQueueItemStatusCompleted) && (item.status != SBQueueItemStatusFailed)) {
|
if ((item.status != SBQueueItemStatusCompleted) && (item.status != SBQueueItemStatusFailed)) {
|
||||||
firstItem = item;
|
firstItem = item;
|
||||||
@@ -89,7 +90,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
|
|||||||
|
|
||||||
- (NSUInteger)indexOfCurrentItem {
|
- (NSUInteger)indexOfCurrentItem {
|
||||||
__block NSUInteger index;
|
__block NSUInteger index;
|
||||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
index = [self.items indexOfObject:self.currentItem];
|
index = [self.items indexOfObject:self.currentItem];
|
||||||
});
|
});
|
||||||
return index;
|
return index;
|
||||||
@@ -98,63 +99,95 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
|
|||||||
#pragma mark - item management
|
#pragma mark - item management
|
||||||
|
|
||||||
- (void)addItem:(SBQueueItem *)item {
|
- (void)addItem:(SBQueueItem *)item {
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
[self.items addObject:item];
|
[self.items addObject:item];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSUInteger)count {
|
- (NSUInteger)count {
|
||||||
return self.items.count;
|
__block NSUInteger count;
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
|
count = self.items.count;
|
||||||
|
});
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSUInteger)readyCount {
|
- (NSUInteger)readyCount {
|
||||||
NSUInteger count = 0;
|
__block NSUInteger count = 0;
|
||||||
for (SBQueueItem *item in self.items)
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
if (item.status == SBQueueItemStatusReady)
|
for (SBQueueItem *item in self.items) {
|
||||||
|
if (item.status == SBQueueItemStatusReady) {
|
||||||
count++;
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (SBQueueItem *)itemAtIndex:(NSUInteger)index {
|
- (SBQueueItem *)itemAtIndex:(NSUInteger)index {
|
||||||
return (self.items)[index];
|
__block SBQueueItem *item;
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
|
item = self.items[index];
|
||||||
|
});
|
||||||
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSArray *)itemsAtIndexes:(NSIndexSet *)indexes {
|
- (NSArray<SBQueueItem *> *)itemsAtIndexes:(NSIndexSet *)indexes {
|
||||||
return [self.items objectsAtIndexes:indexes];
|
__block NSArray<SBQueueItem *> * items;
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
|
items = [self.items objectsAtIndexes:indexes];
|
||||||
|
});
|
||||||
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSUInteger)indexOfItem:(SBQueueItem *)item {
|
- (NSUInteger)indexOfItem:(SBQueueItem *)item {
|
||||||
return [self.items indexOfObject:item];
|
__block NSUInteger index = 0;
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
|
index = [self.items indexOfObject:item];
|
||||||
|
});
|
||||||
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSIndexSet *)indexesOfItemsWithStatus:(SBQueueItemStatus)status {
|
- (NSIndexSet *)indexesOfItemsWithStatus:(SBQueueItemStatus)status {
|
||||||
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
|
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
[self.items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
[self.items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
||||||
if (((SBQueueItem *)obj).status == status) {
|
if (((SBQueueItem *)obj).status == status) {
|
||||||
[indexes addIndex:idx];
|
[indexes addIndex:idx];
|
||||||
}
|
}
|
||||||
}];
|
}];
|
||||||
|
});
|
||||||
return indexes;
|
return indexes;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)insertItem:(SBQueueItem *)anItem atIndex:(NSUInteger)index {
|
- (void)insertItem:(SBQueueItem *)anItem atIndex:(NSUInteger)index {
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
[self.items insertObject:anItem atIndex:index];
|
[self.items insertObject:anItem atIndex:index];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)removeItemsAtIndexes:(NSIndexSet *)indexes {
|
- (void)removeItemsAtIndexes:(NSIndexSet *)indexes {
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
[self.items removeObjectsAtIndexes:indexes];
|
[self.items removeObjectsAtIndexes:indexes];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)removeItem:(SBQueueItem *)item {
|
- (void)removeItem:(SBQueueItem *)item {
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
[self.items removeObject:item];
|
[self.items removeObject:item];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSIndexSet *)removeCompletedItems {
|
- (NSIndexSet *)removeCompletedItems {
|
||||||
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
|
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
|
||||||
|
|
||||||
|
dispatch_sync(self.arrayQueue, ^{
|
||||||
for (SBQueueItem *item in self.items)
|
for (SBQueueItem *item in self.items)
|
||||||
if (item.status == SBQueueItemStatusCompleted)
|
if (item.status == SBQueueItemStatusCompleted)
|
||||||
[indexes addIndex:[self.items indexOfObject:item]];
|
[indexes addIndex:[self.items indexOfObject:item]];
|
||||||
|
|
||||||
[self.items removeObjectsAtIndexes:indexes];
|
[self.items removeObjectsAtIndexes:indexes];
|
||||||
|
});
|
||||||
|
|
||||||
return indexes;
|
return indexes;
|
||||||
}
|
}
|
||||||
@@ -271,11 +304,11 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
|
|||||||
* sends SBQueueWorkingNotification.
|
* sends SBQueueWorkingNotification.
|
||||||
*/
|
*/
|
||||||
- (void)handleSBStatusWorking:(CGFloat)progress index:(NSInteger)index {
|
- (void)handleSBStatusWorking:(CGFloat)progress index:(NSInteger)index {
|
||||||
NSString *description = self.currentItem.localizedWorkingDescription;
|
NSString *itemDescription = self.currentItem.localizedWorkingDescription;
|
||||||
if (!description) {
|
if (!itemDescription) {
|
||||||
description = NSLocalizedString(@"Working", @"Queue Working.");
|
itemDescription = NSLocalizedString(@"Working", @"Queue Working.");
|
||||||
}
|
}
|
||||||
NSString *info = [NSString stringWithFormat:@"%@, item %ld of %lu.", description, (long)self.currentIndex + 1, (unsigned long)self.items.count];
|
NSString *info = [NSString stringWithFormat:@"%@, item %ld.", itemDescription, (long)self.currentIndex + 1];
|
||||||
|
|
||||||
[[NSNotificationCenter defaultCenter] postNotificationName:SBQueueWorkingNotification object:self userInfo:@{@"ProgressString": info,
|
[[NSNotificationCenter defaultCenter] postNotificationName:SBQueueWorkingNotification object:self userInfo:@{@"ProgressString": info,
|
||||||
@"Progress": @(progress),
|
@"Progress": @(progress),
|
||||||
|
|||||||
Reference in new issue
Block a user