Skip to content

Commit 2021f7d

Browse files
authored
Merge pull request #51 from SDWebImage/feature_replace_semaphore_with_os_unfair_lock_and_spinlock
Replace the dispatch semaphore into os_unfair_lock, copied from SDWebImage Core
2 parents 049cc75 + ba51bef commit 2021f7d

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

+48-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
*/
88

99
#import "SDImageWebPCoder.h"
10+
#import "SDWebImageWebPCoderDefine.h"
11+
#import <Accelerate/Accelerate.h>
12+
#import <os/lock.h>
13+
#import <libkern/OSAtomic.h>
1014

1115
#if __has_include("webp/decode.h") && __has_include("webp/encode.h") && __has_include("webp/demux.h") && __has_include("webp/mux.h")
1216
#import "webp/decode.h"
@@ -22,8 +26,47 @@
2226
@import libwebp;
2327
#endif
2428

25-
#import <Accelerate/Accelerate.h>
26-
#import "SDWebImageWebPCoderDefine.h"
29+
#define SD_USE_OS_UNFAIR_LOCK TARGET_OS_MACCATALYST ||\
30+
(__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_10_0) ||\
31+
(__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_12) ||\
32+
(__TV_OS_VERSION_MIN_REQUIRED >= __TVOS_10_0) ||\
33+
(__WATCH_OS_VERSION_MIN_REQUIRED >= __WATCHOS_3_0)
34+
35+
#ifndef SD_LOCK_DECLARE
36+
#if SD_USE_OS_UNFAIR_LOCK
37+
#define SD_LOCK_DECLARE(lock) os_unfair_lock lock
38+
#else
39+
#define SD_LOCK_DECLARE(lock) os_unfair_lock lock API_AVAILABLE(ios(10.0), tvos(10), watchos(3), macos(10.12)); \
40+
OSSpinLock lock##_deprecated;
41+
#endif
42+
#endif
43+
44+
#ifndef SD_LOCK_INIT
45+
#if SD_USE_OS_UNFAIR_LOCK
46+
#define SD_LOCK_INIT(lock) lock = OS_UNFAIR_LOCK_INIT
47+
#else
48+
#define SD_LOCK_INIT(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) lock = OS_UNFAIR_LOCK_INIT; \
49+
else lock##_deprecated = OS_SPINLOCK_INIT;
50+
#endif
51+
#endif
52+
53+
#ifndef SD_LOCK
54+
#if SD_USE_OS_UNFAIR_LOCK
55+
#define SD_LOCK(lock) os_unfair_lock_lock(&lock)
56+
#else
57+
#define SD_LOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_lock(&lock); \
58+
else OSSpinLockLock(&lock##_deprecated);
59+
#endif
60+
#endif
61+
62+
#ifndef SD_UNLOCK
63+
#if SD_USE_OS_UNFAIR_LOCK
64+
#define SD_UNLOCK(lock) os_unfair_lock_unlock(&lock)
65+
#else
66+
#define SD_UNLOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_unlock(&lock); \
67+
else OSSpinLockUnlock(&lock##_deprecated);
68+
#endif
69+
#endif
2770

2871
/// Calculate the actual thumnail pixel size
2972
static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) {
@@ -99,7 +142,7 @@ @implementation SDImageWebPCoder {
99142
BOOL _finished;
100143
CGFloat _canvasWidth;
101144
CGFloat _canvasHeight;
102-
dispatch_semaphore_t _lock;
145+
SD_LOCK_DECLARE(_lock);
103146
NSUInteger _currentBlendIndex;
104147
BOOL _preserveAspectRatio;
105148
CGSize _thumbnailSize;
@@ -292,7 +335,7 @@ - (instancetype)initIncrementalWithOptions:(nullable SDImageCoderOptions *)optio
292335
}
293336
_preserveAspectRatio = preserveAspectRatio;
294337
_currentBlendIndex = NSNotFound;
295-
_lock = dispatch_semaphore_create(1);
338+
SD_LOCK_INIT(_lock);
296339
}
297340
return self;
298341
}
@@ -975,7 +1018,7 @@ - (instancetype)initWithAnimatedImageData:(NSData *)data options:(nullable SDIma
9751018
_demux = demuxer;
9761019
_imageData = data;
9771020
_currentBlendIndex = NSNotFound;
978-
_lock = dispatch_semaphore_create(1);
1021+
SD_LOCK_INIT(_lock);
9791022
}
9801023
return self;
9811024
}

0 commit comments

Comments
 (0)