SDImageCachesManagerOperation.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDImageCachesManagerOperation.h"
  9. #import "SDInternalMacros.h"
  10. @implementation SDImageCachesManagerOperation
  11. {
  12. dispatch_semaphore_t _pendingCountLock;
  13. }
  14. @synthesize executing = _executing;
  15. @synthesize finished = _finished;
  16. @synthesize cancelled = _cancelled;
  17. @synthesize pendingCount = _pendingCount;
  18. - (instancetype)init {
  19. if (self = [super init]) {
  20. _pendingCountLock = dispatch_semaphore_create(1);
  21. _pendingCount = 0;
  22. }
  23. return self;
  24. }
  25. - (void)beginWithTotalCount:(NSUInteger)totalCount {
  26. self.executing = YES;
  27. self.finished = NO;
  28. _pendingCount = totalCount;
  29. }
  30. - (NSUInteger)pendingCount {
  31. SD_LOCK(_pendingCountLock);
  32. NSUInteger pendingCount = _pendingCount;
  33. SD_UNLOCK(_pendingCountLock);
  34. return pendingCount;
  35. }
  36. - (void)completeOne {
  37. SD_LOCK(_pendingCountLock);
  38. _pendingCount = _pendingCount > 0 ? _pendingCount - 1 : 0;
  39. SD_UNLOCK(_pendingCountLock);
  40. }
  41. - (void)cancel {
  42. self.cancelled = YES;
  43. [self reset];
  44. }
  45. - (void)done {
  46. self.finished = YES;
  47. self.executing = NO;
  48. [self reset];
  49. }
  50. - (void)reset {
  51. SD_LOCK(_pendingCountLock);
  52. _pendingCount = 0;
  53. SD_UNLOCK(_pendingCountLock);
  54. }
  55. - (void)setFinished:(BOOL)finished {
  56. [self willChangeValueForKey:@"isFinished"];
  57. _finished = finished;
  58. [self didChangeValueForKey:@"isFinished"];
  59. }
  60. - (void)setExecuting:(BOOL)executing {
  61. [self willChangeValueForKey:@"isExecuting"];
  62. _executing = executing;
  63. [self didChangeValueForKey:@"isExecuting"];
  64. }
  65. - (void)setCancelled:(BOOL)cancelled {
  66. [self willChangeValueForKey:@"isCancelled"];
  67. _cancelled = cancelled;
  68. [self didChangeValueForKey:@"isCancelled"];
  69. }
  70. @end