From ba51befd7c8ab9cde085bfe1a02b222b46eb002a Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 14 Dec 2020 11:53:45 +0800 Subject: [PATCH] Replace the dispatch semaphore into os_unfair_lock, copied from SDWebImage Core --- .../Classes/SDImageWebPCoder.m | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m index 612ed91..207aa1c 100644 --- a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m +++ b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m @@ -7,6 +7,10 @@ */ #import "SDImageWebPCoder.h" +#import "SDWebImageWebPCoderDefine.h" +#import +#import +#import #if __has_include("webp/decode.h") && __has_include("webp/encode.h") && __has_include("webp/demux.h") && __has_include("webp/mux.h") #import "webp/decode.h" @@ -22,8 +26,47 @@ @import libwebp; #endif -#import -#import "SDWebImageWebPCoderDefine.h" +#define SD_USE_OS_UNFAIR_LOCK TARGET_OS_MACCATALYST ||\ + (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_10_0) ||\ + (__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_12) ||\ + (__TV_OS_VERSION_MIN_REQUIRED >= __TVOS_10_0) ||\ + (__WATCH_OS_VERSION_MIN_REQUIRED >= __WATCHOS_3_0) + +#ifndef SD_LOCK_DECLARE +#if SD_USE_OS_UNFAIR_LOCK +#define SD_LOCK_DECLARE(lock) os_unfair_lock lock +#else +#define SD_LOCK_DECLARE(lock) os_unfair_lock lock API_AVAILABLE(ios(10.0), tvos(10), watchos(3), macos(10.12)); \ +OSSpinLock lock##_deprecated; +#endif +#endif + +#ifndef SD_LOCK_INIT +#if SD_USE_OS_UNFAIR_LOCK +#define SD_LOCK_INIT(lock) lock = OS_UNFAIR_LOCK_INIT +#else +#define SD_LOCK_INIT(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) lock = OS_UNFAIR_LOCK_INIT; \ +else lock##_deprecated = OS_SPINLOCK_INIT; +#endif +#endif + +#ifndef SD_LOCK +#if SD_USE_OS_UNFAIR_LOCK +#define SD_LOCK(lock) os_unfair_lock_lock(&lock) +#else +#define SD_LOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_lock(&lock); \ +else OSSpinLockLock(&lock##_deprecated); +#endif +#endif + +#ifndef SD_UNLOCK +#if SD_USE_OS_UNFAIR_LOCK +#define SD_UNLOCK(lock) os_unfair_lock_unlock(&lock) +#else +#define SD_UNLOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_unlock(&lock); \ +else OSSpinLockUnlock(&lock##_deprecated); +#endif +#endif /// Calculate the actual thumnail pixel size static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) { @@ -99,7 +142,7 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio BOOL _finished; CGFloat _canvasWidth; CGFloat _canvasHeight; - dispatch_semaphore_t _lock; + SD_LOCK_DECLARE(_lock); NSUInteger _currentBlendIndex; BOOL _preserveAspectRatio; CGSize _thumbnailSize; @@ -292,7 +335,7 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio } _preserveAspectRatio = preserveAspectRatio; _currentBlendIndex = NSNotFound; - _lock = dispatch_semaphore_create(1); + SD_LOCK_INIT(_lock); } return self; } @@ -975,7 +1018,7 @@ static void FreeImageData(void *info, const void *data, size_t size) { _demux = demuxer; _imageData = data; _currentBlendIndex = NSNotFound; - _lock = dispatch_semaphore_create(1); + SD_LOCK_INIT(_lock); } return self; }