Use synchronized instead of semaphore in SDWebImageDownloader to make it more easy to understand :)
Showing
1 changed file
with
7 additions
and
13 deletions
@@ -9,10 +9,6 @@ | @@ -9,10 +9,6 @@ | ||
9 | #import "SDWebImageDownloader.h" | 9 | #import "SDWebImageDownloader.h" |
10 | #import "SDWebImageDownloaderOperation.h" | 10 | #import "SDWebImageDownloaderOperation.h" |
11 | 11 | ||
12 | -#define LOCK(...) dispatch_semaphore_wait(self->_lock, DISPATCH_TIME_FOREVER); \ | ||
13 | -__VA_ARGS__; \ | ||
14 | -dispatch_semaphore_signal(self->_lock); | ||
15 | - | ||
16 | @interface SDWebImageDownloadToken () | 12 | @interface SDWebImageDownloadToken () |
17 | 13 | ||
18 | @property (nonatomic, weak, nullable) NSOperation<SDWebImageDownloaderOperationInterface> *downloadOperation; | 14 | @property (nonatomic, weak, nullable) NSOperation<SDWebImageDownloaderOperationInterface> *downloadOperation; |
@@ -40,7 +36,6 @@ dispatch_semaphore_signal(self->_lock); | @@ -40,7 +36,6 @@ dispatch_semaphore_signal(self->_lock); | ||
40 | @property (assign, nonatomic, nullable) Class operationClass; | 36 | @property (assign, nonatomic, nullable) Class operationClass; |
41 | @property (strong, nonatomic, nonnull) NSMutableDictionary<NSURL *, SDWebImageDownloaderOperation *> *URLOperations; | 37 | @property (strong, nonatomic, nonnull) NSMutableDictionary<NSURL *, SDWebImageDownloaderOperation *> *URLOperations; |
42 | @property (strong, nonatomic, nullable) SDHTTPHeadersMutableDictionary *HTTPHeaders; | 38 | @property (strong, nonatomic, nullable) SDHTTPHeadersMutableDictionary *HTTPHeaders; |
43 | -@property (strong, nonatomic, nonnull) dispatch_semaphore_t lock; // a lock to keep the access to `URLOperations` thread-safe | ||
44 | 39 | ||
45 | // The session in which data tasks will run | 40 | // The session in which data tasks will run |
46 | @property (strong, nonatomic) NSURLSession *session; | 41 | @property (strong, nonatomic) NSURLSession *session; |
@@ -99,7 +94,6 @@ dispatch_semaphore_signal(self->_lock); | @@ -99,7 +94,6 @@ dispatch_semaphore_signal(self->_lock); | ||
99 | #else | 94 | #else |
100 | _HTTPHeaders = [@{@"Accept": @"image/*;q=0.8"} mutableCopy]; | 95 | _HTTPHeaders = [@{@"Accept": @"image/*;q=0.8"} mutableCopy]; |
101 | #endif | 96 | #endif |
102 | - _lock = dispatch_semaphore_create(1); | ||
103 | _downloadTimeout = 15.0; | 97 | _downloadTimeout = 15.0; |
104 | 98 | ||
105 | [self createNewSessionWithConfiguration:sessionConfiguration]; | 99 | [self createNewSessionWithConfiguration:sessionConfiguration]; |
@@ -293,9 +287,9 @@ dispatch_semaphore_signal(self->_lock); | @@ -293,9 +287,9 @@ dispatch_semaphore_signal(self->_lock); | ||
293 | return nil; | 287 | return nil; |
294 | } | 288 | } |
295 | SDWebImageDownloaderOperation *operation; | 289 | SDWebImageDownloaderOperation *operation; |
296 | - LOCK({ | 290 | + @synchronized (self.URLOperations) { |
297 | operation = [self.URLOperations objectForKey:url]; | 291 | operation = [self.URLOperations objectForKey:url]; |
298 | - }); | 292 | + } |
299 | return operation; | 293 | return operation; |
300 | } | 294 | } |
301 | 295 | ||
@@ -303,18 +297,18 @@ dispatch_semaphore_signal(self->_lock); | @@ -303,18 +297,18 @@ dispatch_semaphore_signal(self->_lock); | ||
303 | if (!operation || !url) { | 297 | if (!operation || !url) { |
304 | return; | 298 | return; |
305 | } | 299 | } |
306 | - LOCK({ | ||
307 | - [self.URLOperations setObject:operation forKey:url]; | ||
308 | - }); | 300 | + @synchronized (self.URLOperations) { |
301 | + [self.URLOperations setObject:operation forKey:url]; | ||
302 | + } | ||
309 | } | 303 | } |
310 | 304 | ||
311 | - (void)removeOperationForURL:(NSURL *)url { | 305 | - (void)removeOperationForURL:(NSURL *)url { |
312 | if (!url) { | 306 | if (!url) { |
313 | return; | 307 | return; |
314 | } | 308 | } |
315 | - LOCK({ | 309 | + @synchronized (self.URLOperations) { |
316 | [self.URLOperations removeObjectForKey:url]; | 310 | [self.URLOperations removeObjectForKey:url]; |
317 | - }); | 311 | + } |
318 | } | 312 | } |
319 | 313 | ||
320 | - (SDWebImageDownloaderOperation *)operationWithTask:(NSURLSessionTask *)task { | 314 | - (SDWebImageDownloaderOperation *)operationWithTask:(NSURLSessionTask *)task { |
-
Please register or login to post a comment