From 09020fac87ac1274c99f7002991ca854bec1e62e Mon Sep 17 00:00:00 2001 From: Lizhen Hu Date: Mon, 28 May 2018 23:12:38 +0800 Subject: [PATCH] Replace @synchronized lock with dispatch_semaphore lock for SDWebImageCombinedOperation --- SDWebImage/SDWebImageManager.m | 36 +++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 839be3a2..8bd276ea 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -347,20 +347,34 @@ @end +@interface SDWebImageCombinedOperation () + +@property (strong, nonatomic, nonnull) dispatch_semaphore_t cancelLock; // a lock to make the `cancel` method thread-safe + +@end + @implementation SDWebImageCombinedOperation -- (void)cancel { - @synchronized(self) { - self.cancelled = YES; - if (self.cacheOperation) { - [self.cacheOperation cancel]; - self.cacheOperation = nil; - } - if (self.downloadToken) { - [self.manager.imageDownloader cancel:self.downloadToken]; - } - [self.manager safelyRemoveOperationFromRunning:self]; +- (instancetype)init { + self = [super init]; + if (self) { + _cancelLock = dispatch_semaphore_create(1); } + return self; +} + +- (void)cancel { + LOCK(self.cancelLock); + self.cancelled = YES; + if (self.cacheOperation) { + [self.cacheOperation cancel]; + self.cacheOperation = nil; + } + if (self.downloadToken) { + [self.manager.imageDownloader cancel:self.downloadToken]; + } + [self.manager safelyRemoveOperationFromRunning:self]; + UNLOCK(self.cancelLock); } @end