From 100869dd70782c71901b8803e5dec76e3f62a376 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 2 Apr 2019 16:59:57 +0800 Subject: [PATCH] Fix caches manager operation thread safe issue --- .../Private/SDImageCachesManagerOperation.m | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/SDWebImage/Private/SDImageCachesManagerOperation.m b/SDWebImage/Private/SDImageCachesManagerOperation.m index 67f41450..131a18fe 100644 --- a/SDWebImage/Private/SDImageCachesManagerOperation.m +++ b/SDWebImage/Private/SDImageCachesManagerOperation.m @@ -9,10 +9,22 @@ #import "SDImageCachesManagerOperation.h" @implementation SDImageCachesManagerOperation +{ + dispatch_semaphore_t _pendingCountLock; +} @synthesize executing = _executing; @synthesize finished = _finished; @synthesize cancelled = _cancelled; +@synthesize pendingCount = _pendingCount; + +- (instancetype)init { + if (self = [super init]) { + _pendingCountLock = dispatch_semaphore_create(1); + _pendingCount = 0; + } + return self; +} - (void)beginWithTotalCount:(NSUInteger)totalCount { self.executing = YES; @@ -20,8 +32,17 @@ _pendingCount = totalCount; } +- (NSUInteger)pendingCount { + SD_LOCK(_pendingCountLock); + NSUInteger pendingCount = _pendingCount; + SD_UNLOCK(_pendingCountLock); + return pendingCount; +} + - (void)completeOne { + SD_LOCK(_pendingCountLock); _pendingCount = _pendingCount > 0 ? _pendingCount - 1 : 0; + SD_UNLOCK(_pendingCountLock); } - (void)cancel { @@ -36,7 +57,9 @@ } - (void)reset { + SD_LOCK(_pendingCountLock); _pendingCount = 0; + SD_UNLOCK(_pendingCountLock); } - (void)setFinished:(BOOL)finished {