From f8fe88633495f9a95560ed9160d0634f04c071ae Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Thu, 12 Jul 2018 21:10:28 +0300 Subject: [PATCH] Instead of defining `LOCK` and `UNLOCK` macros in each file, moved them to the `SDWebImageCompat.h`. Also, split into `LOCK` and `LOCKBLOCK` --- SDWebImage/SDAnimatedImage.m | 10 +++------- SDWebImage/SDAnimatedImageView.m | 18 +++++++----------- SDWebImage/SDImageCodersManager.m | 3 --- SDWebImage/SDImageLoadersManager.m | 3 --- SDWebImage/SDMemoryCache.m | 3 --- SDWebImage/SDWebImageCompat.h | 14 ++++++++++++++ SDWebImage/SDWebImageDownloader.m | 3 --- SDWebImage/SDWebImageDownloaderOperation.m | 3 --- SDWebImage/SDWebImageManager.m | 3 --- SDWebImage/WebP/SDImageWebPCoder.m | 6 +----- 10 files changed, 25 insertions(+), 41 deletions(-) diff --git a/SDWebImage/SDAnimatedImage.m b/SDWebImage/SDAnimatedImage.m index d05a430c..fa20c898 100644 --- a/SDWebImage/SDAnimatedImage.m +++ b/SDWebImage/SDAnimatedImage.m @@ -12,10 +12,6 @@ #import "SDImageCodersManager.h" #import "SDImageFrame.h" -#define LOCK(...) dispatch_semaphore_wait(self->_lock, DISPATCH_TIME_FOREVER); \ -__VA_ARGS__; \ -dispatch_semaphore_signal(self->_lock); - static CGFloat SDImageScaleFromPath(NSString *string) { if (string.length == 0 || [string hasSuffix:@"/"]) return 1; NSString *name = string.stringByDeletingPathExtension; @@ -108,7 +104,7 @@ static NSArray *SDBundlePreferredScales() { } - (void)didReceiveMemoryWarning:(NSNotification *)notification { - LOCK({ + LOCKBLOCK({ [self.imageTable removeAllObjects]; }); } @@ -178,7 +174,7 @@ static NSArray *SDBundlePreferredScales() { - (UIImage *)imageForName:(NSString *)name { NSParameterAssert(name); UIImage *image; - LOCK({ + LOCKBLOCK({ image = [self.imageTable objectForKey:name]; }); return image; @@ -187,7 +183,7 @@ static NSArray *SDBundlePreferredScales() { - (void)storeImage:(UIImage *)image forName:(NSString *)name { NSParameterAssert(image); NSParameterAssert(name); - LOCK({ + LOCKBLOCK({ [self.imageTable setObject:image forKey:name]; }); } diff --git a/SDWebImage/SDAnimatedImageView.m b/SDWebImage/SDAnimatedImageView.m index a848cade..ebbffd78 100644 --- a/SDWebImage/SDAnimatedImageView.m +++ b/SDWebImage/SDAnimatedImageView.m @@ -38,10 +38,6 @@ static NSUInteger SDDeviceFreeMemory() { return vm_stat.free_count * page_size; } -#define LOCK(...) dispatch_semaphore_wait(self->_lock, DISPATCH_TIME_FOREVER); \ -__VA_ARGS__; \ -dispatch_semaphore_signal(self->_lock); - @interface SDWeakProxy : NSProxy @property (nonatomic, weak, readonly) id target; @@ -242,7 +238,7 @@ dispatch_semaphore_signal(self->_lock); self.animatedImageScale = 1; [_fetchQueue cancelAllOperations]; _fetchQueue = nil; - LOCK({ + LOCKBLOCK({ [_frameBuffer removeAllObjects]; _frameBuffer = nil; }); @@ -303,7 +299,7 @@ dispatch_semaphore_signal(self->_lock); self.animatedImageScale = image.scale; if (!self.isProgressive) { self.currentFrame = image; - LOCK({ + LOCKBLOCK({ self.frameBuffer[@(self.currentFrameIndex)] = self.currentFrame; }); } @@ -424,7 +420,7 @@ dispatch_semaphore_signal(self->_lock); [_fetchQueue cancelAllOperations]; [_fetchQueue addOperationWithBlock:^{ NSNumber *currentFrameIndex = @(self.currentFrameIndex); - LOCK({ + LOCKBLOCK({ NSArray *keys = self.frameBuffer.allKeys; // only keep the next frame for later rendering for (NSNumber * key in keys) { @@ -681,12 +677,12 @@ dispatch_semaphore_signal(self->_lock); // Update the current frame UIImage *currentFrame; - LOCK({ + LOCKBLOCK({ currentFrame = self.frameBuffer[@(currentFrameIndex)]; }); BOOL bufferFull = NO; if (currentFrame) { - LOCK({ + LOCKBLOCK({ // Remove the frame buffer if need if (self.frameBuffer.count > self.maxBufferCount) { self.frameBuffer[@(currentFrameIndex)] = nil; @@ -710,7 +706,7 @@ dispatch_semaphore_signal(self->_lock); if (self.isProgressive) { // Recovery the current frame index and removed frame buffer (See above) self.currentFrameIndex = currentFrameIndex; - LOCK({ + LOCKBLOCK({ self.frameBuffer[@(currentFrameIndex)] = self.currentFrame; }); [self stopAnimating]; @@ -740,7 +736,7 @@ dispatch_semaphore_signal(self->_lock); UIImage *animatedImage = self.animatedImage; NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ UIImage *fetchFrame = [animatedImage animatedImageFrameAtIndex:fetchFrameIndex]; - LOCK({ + LOCKBLOCK({ self.frameBuffer[@(fetchFrameIndex)] = fetchFrame; }); }]; diff --git a/SDWebImage/SDImageCodersManager.m b/SDWebImage/SDImageCodersManager.m index 83da2455..131925d1 100644 --- a/SDWebImage/SDImageCodersManager.m +++ b/SDWebImage/SDImageCodersManager.m @@ -14,9 +14,6 @@ #import "SDImageWebPCoder.h" #endif -#define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); -#define UNLOCK(lock) dispatch_semaphore_signal(lock); - @interface SDImageCodersManager () @property (nonatomic, strong, nonnull) dispatch_semaphore_t codersLock; diff --git a/SDWebImage/SDImageLoadersManager.m b/SDWebImage/SDImageLoadersManager.m index c53e4247..0e0a0c5d 100644 --- a/SDWebImage/SDImageLoadersManager.m +++ b/SDWebImage/SDImageLoadersManager.m @@ -9,9 +9,6 @@ #import "SDImageLoadersManager.h" #import "SDWebImageDownloader.h" -#define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); -#define UNLOCK(lock) dispatch_semaphore_signal(lock); - @interface SDImageLoadersManager () @property (nonatomic, strong, nonnull) dispatch_semaphore_t loadersLock; diff --git a/SDWebImage/SDMemoryCache.m b/SDWebImage/SDMemoryCache.m index 6562185b..e434f4ec 100644 --- a/SDWebImage/SDMemoryCache.m +++ b/SDWebImage/SDMemoryCache.m @@ -9,9 +9,6 @@ #import "SDMemoryCache.h" #import "SDImageCacheConfig.h" -#define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); -#define UNLOCK(lock) dispatch_semaphore_signal(lock); - NSUInteger SDMemoryCacheCostForImage(UIImage * _Nullable image) { #if SD_MAC return image.size.height * image.size.width; diff --git a/SDWebImage/SDWebImageCompat.h b/SDWebImage/SDWebImageCompat.h index f47a248a..c58cbdab 100644 --- a/SDWebImage/SDWebImageCompat.h +++ b/SDWebImage/SDWebImageCompat.h @@ -93,3 +93,17 @@ dispatch_async(dispatch_get_main_queue(), block);\ } #endif + +#ifndef LOCK +#define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); +#endif + +#ifndef UNLOCK +#define UNLOCK(lock) dispatch_semaphore_signal(lock); +#endif + +#ifndef LOCKBLOCK +#define LOCKBLOCK(...) dispatch_semaphore_wait(self->_lock, DISPATCH_TIME_FOREVER); \ +__VA_ARGS__; \ +dispatch_semaphore_signal(self->_lock); +#endif diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 8e0f89bb..7dd3c7aa 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -13,9 +13,6 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext; -#define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); -#define UNLOCK(lock) dispatch_semaphore_signal(lock); - @interface SDWebImageDownloadToken () @property (nonatomic, strong, nullable, readwrite) NSURL *url; diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index a687367d..5232400b 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -9,9 +9,6 @@ #import "SDWebImageDownloaderOperation.h" #import "SDWebImageError.h" -#define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); -#define UNLOCK(lock) dispatch_semaphore_signal(lock); - // iOS 8 Foundation.framework extern these symbol but the define is in CFNetwork.framework. We just fix this without import CFNetwork.framework #if (__IPHONE_OS_VERSION_MIN_REQUIRED && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0) const float NSURLSessionTaskPriorityHigh = 0.75; diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 85f57d36..9e0adb05 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -12,9 +12,6 @@ #import "UIImage+Metadata.h" #import "SDWebImageError.h" -#define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); -#define UNLOCK(lock) dispatch_semaphore_signal(lock); - static id _defaultImageCache; static id _defaultImageLoader; diff --git a/SDWebImage/WebP/SDImageWebPCoder.m b/SDWebImage/WebP/SDImageWebPCoder.m index f2171a7c..c6366162 100644 --- a/SDWebImage/WebP/SDImageWebPCoder.m +++ b/SDWebImage/WebP/SDImageWebPCoder.m @@ -26,10 +26,6 @@ #endif #import -#define LOCK(...) dispatch_semaphore_wait(self->_lock, DISPATCH_TIME_FOREVER); \ -__VA_ARGS__; \ -dispatch_semaphore_signal(self->_lock); - @interface SDWebPCoderFrame : NSObject @property (nonatomic, assign) NSUInteger index; // Frame index (zero based) @@ -741,7 +737,7 @@ static void FreeImageData(void *info, const void *data, size_t size) { if (index >= _frameCount) { return nil; } - LOCK({ + LOCKBLOCK({ image = [self safeAnimatedImageFrameAtIndex:index]; }); return image;