Skip to content

Commit 46704b2

Browse files
committed
Use a condition lock instead. Thanks @appleguy!
1 parent c673afc commit 46704b2

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

Source/PINDiskCache.m

+24-26
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ @interface PINDiskCache () {
5959
@property (strong, nonatomic) NSURL *cacheURL;
6060
@property (strong, nonatomic) PINOperationQueue *operationQueue;
6161
@property (strong, nonatomic) NSMutableDictionary <NSString *, PINDiskCacheMetadata *> *metadata;
62+
@property (assign, nonatomic) pthread_cond_t diskWritableCondition;
6263
@property (assign, nonatomic) BOOL diskWritable;
64+
@property (assign, nonatomic) pthread_cond_t diskStateKnownCondition;
6365
@property (assign, nonatomic) BOOL diskStateKnown;
6466
@end
6567

@@ -203,6 +205,9 @@ - (instancetype)initWithName:(NSString *)name
203205
} else {
204206
_keyDecoder = self.defaultKeyDecoder;
205207
}
208+
209+
pthread_cond_init(&_diskWritableCondition, NULL);
210+
pthread_cond_init(&_diskStateKnownCondition, NULL);
206211

207212
//we don't want to do anything without setting up the disk cache, but we also don't want to block init, it can take a while to initialize. This must *not* be done on _operationQueue because other operations added may hold the lock and fill up the queue.
208213
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@@ -422,22 +427,24 @@ + (void)emptyTrash
422427

423428
- (BOOL)_locked_createCacheDirectory
424429
{
425-
if ([[NSFileManager defaultManager] fileExistsAtPath:[_cacheURL path]]) {
426-
_diskWritable = YES;
427-
return NO;
430+
BOOL created = NO;
431+
if ([[NSFileManager defaultManager] fileExistsAtPath:[_cacheURL path]] == NO) {
432+
NSError *error = nil;
433+
BOOL success = [[NSFileManager defaultManager] createDirectoryAtURL:_cacheURL
434+
withIntermediateDirectories:YES
435+
attributes:nil
436+
error:&error];
437+
PINDiskCacheError(error);
438+
created = success;
428439
}
429440

430-
NSError *error = nil;
431-
BOOL success = [[NSFileManager defaultManager] createDirectoryAtURL:_cacheURL
432-
withIntermediateDirectories:YES
433-
attributes:nil
434-
error:&error];
435-
PINDiskCacheError(error);
441+
436442

437443
// while this may not be true if success is false, it's better than deadlocking later.
438444
_diskWritable = YES;
445+
pthread_cond_broadcast(&_diskWritableCondition);
439446

440-
return success;
447+
return created;
441448
}
442449

443450
- (void)initializeDiskProperties
@@ -490,6 +497,7 @@ - (void)initializeDiskProperties
490497
[self trimToSizeByDateAsync:self->_byteLimit completion:nil];
491498

492499
_diskStateKnown = YES;
500+
pthread_cond_broadcast(&_diskStateKnownCondition);
493501
[self unlock];
494502
}
495503

@@ -1436,31 +1444,21 @@ - (void)setWritingProtectionOption:(NSDataWritingOptions)writingProtectionOption
14361444

14371445
- (void)lockUntilWritable
14381446
{
1439-
__unused int result = pthread_mutex_lock(&_mutex);
1440-
NSAssert(result == 0, @"Failed to lock PINDiskCache %@. Code: %d", self, result);
1447+
[self lock];
14411448

14421449
// spinlock if the disk isn't writable
1443-
while (_diskWritable == NO) {
1444-
[self unlock];
1445-
usleep(100);
1446-
1447-
__unused int result = pthread_mutex_lock(&_mutex);
1448-
NSAssert(result == 0, @"Failed to lock PINDiskCache %@. Code: %d", self, result);
1450+
if (_diskWritable == NO) {
1451+
pthread_cond_wait(&_diskWritableCondition, &_mutex);
14491452
}
14501453
}
14511454

14521455
- (void)lockUntilDiskStateKnown
14531456
{
1454-
__unused int result = pthread_mutex_lock(&_mutex);
1455-
NSAssert(result == 0, @"Failed to lock PINDiskCache %@. Code: %d", self, result);
1457+
[self lock];
14561458

14571459
// spinlock if the disk state isn't known
1458-
while (_diskStateKnown == NO) {
1459-
[self unlock];
1460-
usleep(100);
1461-
1462-
__unused int result = pthread_mutex_lock(&_mutex);
1463-
NSAssert(result == 0, @"Failed to lock PINDiskCache %@. Code: %d", self, result);
1460+
if (_diskStateKnown == NO) {
1461+
pthread_cond_wait(&_diskStateKnownCondition, &_mutex);
14641462
}
14651463
}
14661464

0 commit comments

Comments
 (0)