Merge pull request #2669 from zhongwuzw/fix_cachesoperation_thread_safe

Fix caches manager operation thread safe issue
This commit is contained in:
DreamPiggy 2019-04-02 17:22:35 +08:00 committed by GitHub
commit 5e427f6984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 0 deletions

View File

@ -9,10 +9,22 @@
#import "SDImageCachesManagerOperation.h" #import "SDImageCachesManagerOperation.h"
@implementation SDImageCachesManagerOperation @implementation SDImageCachesManagerOperation
{
dispatch_semaphore_t _pendingCountLock;
}
@synthesize executing = _executing; @synthesize executing = _executing;
@synthesize finished = _finished; @synthesize finished = _finished;
@synthesize cancelled = _cancelled; @synthesize cancelled = _cancelled;
@synthesize pendingCount = _pendingCount;
- (instancetype)init {
if (self = [super init]) {
_pendingCountLock = dispatch_semaphore_create(1);
_pendingCount = 0;
}
return self;
}
- (void)beginWithTotalCount:(NSUInteger)totalCount { - (void)beginWithTotalCount:(NSUInteger)totalCount {
self.executing = YES; self.executing = YES;
@ -20,8 +32,17 @@
_pendingCount = totalCount; _pendingCount = totalCount;
} }
- (NSUInteger)pendingCount {
SD_LOCK(_pendingCountLock);
NSUInteger pendingCount = _pendingCount;
SD_UNLOCK(_pendingCountLock);
return pendingCount;
}
- (void)completeOne { - (void)completeOne {
SD_LOCK(_pendingCountLock);
_pendingCount = _pendingCount > 0 ? _pendingCount - 1 : 0; _pendingCount = _pendingCount > 0 ? _pendingCount - 1 : 0;
SD_UNLOCK(_pendingCountLock);
} }
- (void)cancel { - (void)cancel {
@ -36,7 +57,9 @@
} }
- (void)reset { - (void)reset {
SD_LOCK(_pendingCountLock);
_pendingCount = 0; _pendingCount = 0;
SD_UNLOCK(_pendingCountLock);
} }
- (void)setFinished:(BOOL)finished { - (void)setFinished:(BOOL)finished {