Fix image prefetcher completion logical handler

This commit is contained in:
zhongwuzw 2018-07-08 14:26:38 +08:00
parent bb55b6eb72
commit ab86065f38
1 changed files with 5 additions and 4 deletions

View File

@ -15,8 +15,9 @@
// These value are just used as incrementing counter, keep thread-safe using memory_order_relaxed for performance. // These value are just used as incrementing counter, keep thread-safe using memory_order_relaxed for performance.
atomic_ulong _skippedCount; atomic_ulong _skippedCount;
atomic_ulong _finishedCount; atomic_ulong _finishedCount;
atomic_ulong _totalCount;
atomic_flag _isAllFinished; atomic_flag _isAllFinished;
unsigned long _totalCount;
} }
@property (nonatomic, copy, readwrite) NSArray<NSURL *> *urls; @property (nonatomic, copy, readwrite) NSArray<NSURL *> *urls;
@ -105,7 +106,7 @@
// Current operation finished // Current operation finished
[sself callProgressBlockForToken:token imageURL:imageURL]; [sself callProgressBlockForToken:token imageURL:imageURL];
if (atomic_load_explicit(&(token->_finishedCount), memory_order_relaxed) + atomic_load_explicit(&(token->_skippedCount), memory_order_relaxed) >= atomic_load_explicit(&(token->_totalCount), memory_order_relaxed)) { if (atomic_load_explicit(&(token->_finishedCount), memory_order_relaxed) == token->_totalCount) {
// All finished // All finished
if (!atomic_flag_test_and_set_explicit(&(token->_isAllFinished), memory_order_relaxed)) { if (!atomic_flag_test_and_set_explicit(&(token->_isAllFinished), memory_order_relaxed)) {
[sself callCompletionBlockForToken:token]; [sself callCompletionBlockForToken:token];
@ -138,7 +139,7 @@
NSUInteger tokenFinishedCount = [self tokenFinishedCount]; NSUInteger tokenFinishedCount = [self tokenFinishedCount];
NSUInteger tokenTotalCount = [self tokenTotalCount]; NSUInteger tokenTotalCount = [self tokenTotalCount];
NSUInteger finishedCount = atomic_load_explicit(&(token->_finishedCount), memory_order_relaxed); NSUInteger finishedCount = atomic_load_explicit(&(token->_finishedCount), memory_order_relaxed);
NSUInteger totalCount = atomic_load_explicit(&(token->_totalCount), memory_order_relaxed); NSUInteger totalCount = token->_totalCount;
dispatch_async(self.delegateQueue, ^{ dispatch_async(self.delegateQueue, ^{
if (shouldCallDelegate) { if (shouldCallDelegate) {
[self.delegate imagePrefetcher:self didPrefetchURL:url finishedCount:tokenFinishedCount totalCount:tokenTotalCount]; [self.delegate imagePrefetcher:self didPrefetchURL:url finishedCount:tokenFinishedCount totalCount:tokenTotalCount];
@ -173,7 +174,7 @@
NSUInteger tokenTotalCount = 0; NSUInteger tokenTotalCount = 0;
@synchronized (self.runningTokens) { @synchronized (self.runningTokens) {
for (SDWebImagePrefetchToken *token in self.runningTokens) { for (SDWebImagePrefetchToken *token in self.runningTokens) {
tokenTotalCount += atomic_load_explicit(&(token->_totalCount), memory_order_relaxed); tokenTotalCount += token->_totalCount;
} }
} }
return tokenTotalCount; return tokenTotalCount;