From ad592765cbbd3b4dcc12fa014062625630c83104 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 19 Sep 2022 17:28:38 +0800 Subject: [PATCH] Revert "Synchronise getter and setter of the cancelled property of the SDWebImageCombinedOperation in order to eliminate data race." This reverts commit 5a18c84529dd5cd9cd45c8689d55b62d3b73b54a. --- SDWebImage/Core/SDWebImageManager.m | 49 +++++++++-------------------- 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/SDWebImage/Core/SDWebImageManager.m b/SDWebImage/Core/SDWebImageManager.m index 6bddcfc2..3b29632d 100644 --- a/SDWebImage/Core/SDWebImageManager.m +++ b/SDWebImage/Core/SDWebImageManager.m @@ -17,9 +17,7 @@ static id _defaultImageCache; static id _defaultImageLoader; -@interface SDWebImageCombinedOperation () { - SD_LOCK_DECLARE(_cancelledLock); // a lock to keep the access to `cancelled` thread-safe -} +@interface SDWebImageCombinedOperation () @property (assign, nonatomic, getter = isCancelled) BOOL cancelled; @property (strong, nonatomic, readwrite, nullable) id loaderOperation; @@ -805,39 +803,22 @@ static id _defaultImageLoader; @implementation SDWebImageCombinedOperation -- (instancetype)init { - if (self = [super init]) { - SD_LOCK_INIT(_cancelledLock); - } - - return self; -} - -- (BOOL)isCancelled { - BOOL isCancelled = NO; - SD_LOCK(_cancelledLock); - isCancelled = _cancelled; - SD_UNLOCK(_cancelledLock); - return isCancelled; -} - - (void)cancel { - SD_LOCK(_cancelledLock); - if (_cancelled) { - SD_UNLOCK(_cancelledLock); - return; + @synchronized(self) { + if (self.isCancelled) { + return; + } + self.cancelled = YES; + if (self.cacheOperation) { + [self.cacheOperation cancel]; + self.cacheOperation = nil; + } + if (self.loaderOperation) { + [self.loaderOperation cancel]; + self.loaderOperation = nil; + } + [self.manager safelyRemoveOperationFromRunning:self]; } - _cancelled = YES; - if (self.cacheOperation) { - [self.cacheOperation cancel]; - self.cacheOperation = nil; - } - if (self.loaderOperation) { - [self.loaderOperation cancel]; - self.loaderOperation = nil; - } - [self.manager safelyRemoveOperationFromRunning:self]; - SD_UNLOCK(_cancelledLock); } @end