Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions YYCache/YYMemoryCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ @implementation YYMemoryCache {
pthread_mutex_t _lock;
_YYLinkedMap *_lru;
dispatch_queue_t _queue;
CFRunLoopSourceRef _noSpinSource;
}

- (void)_trimRecursively {
Expand Down Expand Up @@ -208,6 +209,8 @@ - (void)_trimToCost:(NSUInteger)costLimit {
pthread_mutex_unlock(&_lock);
if (finish) return;

CFRunLoopAddSource(CFRunLoopGetCurrent(), _noSpinSource, kCFRunLoopDefaultMode);

NSMutableArray *holder = [NSMutableArray new];
while (!finish) {
if (pthread_mutex_trylock(&_lock) == 0) {
Expand All @@ -219,7 +222,7 @@ - (void)_trimToCost:(NSUInteger)costLimit {
}
pthread_mutex_unlock(&_lock);
} else {
usleep(10 * 1000); //10 ms
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, NO); // Give RunLoop 10ms to handle input sources.
}
}
if (holder.count) {
Expand All @@ -242,6 +245,8 @@ - (void)_trimToCount:(NSUInteger)countLimit {
pthread_mutex_unlock(&_lock);
if (finish) return;

CFRunLoopAddSource(CFRunLoopGetCurrent(), _noSpinSource, kCFRunLoopDefaultMode);

NSMutableArray *holder = [NSMutableArray new];
while (!finish) {
if (pthread_mutex_trylock(&_lock) == 0) {
Expand All @@ -253,7 +258,7 @@ - (void)_trimToCount:(NSUInteger)countLimit {
}
pthread_mutex_unlock(&_lock);
} else {
usleep(10 * 1000); //10 ms
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, NO); // Give RunLoop 10ms to handle input sources.
}
}
if (holder.count) {
Expand All @@ -277,6 +282,8 @@ - (void)_trimToAge:(NSTimeInterval)ageLimit {
pthread_mutex_unlock(&_lock);
if (finish) return;

CFRunLoopAddSource(CFRunLoopGetCurrent(), _noSpinSource, kCFRunLoopDefaultMode);

NSMutableArray *holder = [NSMutableArray new];
while (!finish) {
if (pthread_mutex_trylock(&_lock) == 0) {
Expand All @@ -288,7 +295,7 @@ - (void)_trimToAge:(NSTimeInterval)ageLimit {
}
pthread_mutex_unlock(&_lock);
} else {
usleep(10 * 1000); //10 ms
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, NO); // Give RunLoop 10ms to handle input sources.
}
}
if (holder.count) {
Expand Down Expand Up @@ -331,6 +338,8 @@ - (instancetype)init {
_autoTrimInterval = 5.0;
_shouldRemoveAllObjectsOnMemoryWarning = YES;
_shouldRemoveAllObjectsWhenEnteringBackground = YES;
CFRunLoopSourceContext noSpinCtx = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
_noSpinSource = CFRunLoopSourceCreate(NULL, 0, &noSpinCtx);

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_appDidReceiveMemoryWarningNotification) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_appDidEnterBackgroundNotification) name:UIApplicationDidEnterBackgroundNotification object:nil];
Expand All @@ -344,6 +353,7 @@ - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[_lru removeAll];
pthread_mutex_destroy(&_lock);
CFRelease(_noSpinSource);
}

- (NSUInteger)totalCount {
Expand Down