Skip to content

Commit 58635e4

Browse files
authored
Remove locking on callback (pinterest#97)
* Restructuring blocks to avoid holding lock in callback. * Remove locking on all callbacks except for enumeration and fileURL requests. Add warnings about deadlock. * Updating docs * indicate which functions should be called with lock held / more removing of locks on callback
1 parent 569df93 commit 58635e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1167
-10570
lines changed

PINCache/PINCache.m

+4-6
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,14 @@ - (void)objectForKey:(NSString *)key block:(PINCacheObjectBlock)block
110110
return;
111111

112112
if (memoryCacheObject) {
113-
[strongSelf->_diskCache fileURLForKey:memoryCacheKey block:^(PINDiskCache *diskCache, NSString *diskCacheKey, id <NSCoding> diskCacheObject, NSURL *fileURL) {
114-
// update the access time on disk
115-
}];
113+
[strongSelf->_diskCache fileURLForKey:memoryCacheKey block:NULL];
116114
dispatch_async(strongSelf->_concurrentQueue, ^{
117115
PINCache *strongSelf = weakSelf;
118116
if (strongSelf)
119117
block(strongSelf, memoryCacheKey, memoryCacheObject);
120118
});
121119
} else {
122-
[strongSelf->_diskCache objectForKey:memoryCacheKey block:^(PINDiskCache *diskCache, NSString *diskCacheKey, id <NSCoding> diskCacheObject, NSURL *fileURL) {
120+
[strongSelf->_diskCache objectForKey:memoryCacheKey block:^(PINDiskCache *diskCache, NSString *diskCacheKey, id <NSCoding> diskCacheObject) {
123121
PINCache *strongSelf = weakSelf;
124122
if (!strongSelf)
125123
return;
@@ -158,7 +156,7 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key block:(PINCacheOb
158156
dispatch_group_leave(group);
159157
};
160158

161-
diskBlock = ^(PINDiskCache *diskCache, NSString *diskCacheKey, id <NSCoding> memoryCacheObject, NSURL *memoryCacheFileURL) {
159+
diskBlock = ^(PINDiskCache *diskCache, NSString *diskCacheKey, id <NSCoding> memoryCacheObject) {
162160
dispatch_group_leave(group);
163161
};
164162
}
@@ -198,7 +196,7 @@ - (void)removeObjectForKey:(NSString *)key block:(PINCacheObjectBlock)block
198196
dispatch_group_leave(group);
199197
};
200198

201-
diskBlock = ^(PINDiskCache *diskCache, NSString *diskCacheKey, id <NSCoding> memoryCacheObject, NSURL *memoryCacheFileURL) {
199+
diskBlock = ^(PINDiskCache *diskCache, NSString *diskCacheKey, id <NSCoding> memoryCacheObject) {
202200
dispatch_group_leave(group);
203201
};
204202
}

PINCache/PINDiskCache.h

+27-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ typedef void (^PINDiskCacheBlock)(PINDiskCache *cache);
1919
/**
2020
A callback block which provides the cache, key and object as arguments
2121
*/
22-
typedef void (^PINDiskCacheObjectBlock)(PINDiskCache *cache, NSString *key, id <NSCoding> __nullable object, NSURL * __nullable fileURL);
22+
typedef void (^PINDiskCacheObjectBlock)(PINDiskCache *cache, NSString *key, id <NSCoding> __nullable object);
23+
24+
/**
25+
A callback block which provides the key and fileURL of the object
26+
*/
27+
typedef void (^PINDiskCacheFileURLBlock)(NSString *key, NSURL * __nullable fileURL);
2328

2429
/**
2530
A callback block which provides a BOOL value as argument
@@ -227,8 +232,6 @@ typedef void (^PINDiskCacheContainsBlock)(BOOL containsObject);
227232
Retrieves the object for the specified key. This method returns immediately and executes the passed
228233
block as soon as the object is available.
229234
230-
@warning The fileURL is only valid for the duration of this block, do not use it after the block ends.
231-
232235
@param key The key associated with the requested object.
233236
@param block A block to be executed serially when the object is available.
234237
*/
@@ -241,10 +244,15 @@ typedef void (^PINDiskCacheContainsBlock)(BOOL containsObject);
241244
@warning Access is protected for the duration of the block, but to maintain safe disk access do not
242245
access this fileURL after the block has ended.
243246
247+
@warning The PINDiskCache lock is held while block is executed. Any synchronous calls to the diskcache
248+
or a cache which owns the instance of the disk cache are likely to cause a deadlock. This is why the block is
249+
*not* passed the instance of the disk cache. You should also avoid doing extensive work while this
250+
lock is held.
251+
244252
@param key The key associated with the requested object.
245253
@param block A block to be executed serially when the file URL is available.
246254
*/
247-
- (void)fileURLForKey:(nullable NSString *)key block:(nullable PINDiskCacheObjectBlock)block;
255+
- (void)fileURLForKey:(NSString *)key block:(nullable PINDiskCacheFileURLBlock)block;
248256

249257
/**
250258
Stores an object in the cache for the specified key. This method returns immediately and executes the
@@ -308,8 +316,14 @@ typedef void (^PINDiskCacheContainsBlock)(BOOL containsObject);
308316
309317
@param block A block to be executed for every object in the cache.
310318
@param completionBlock An optional block to be executed after the enumeration is complete.
319+
320+
@warning The PINDiskCache lock is held while block is executed. Any synchronous calls to the diskcache
321+
or a cache which owns the instance of the disk cache are likely to cause a deadlock. This is why the block is
322+
*not* passed the instance of the disk cache. You should also avoid doing extensive work while this
323+
lock is held.
324+
311325
*/
312-
- (void)enumerateObjectsWithBlock:(PINDiskCacheObjectBlock)block completionBlock:(nullable PINDiskCacheBlock)completionBlock;
326+
- (void)enumerateObjectsWithBlock:(PINDiskCacheFileURLBlock)block completionBlock:(nullable PINDiskCacheBlock)completionBlock;
313327

314328
#pragma mark -
315329
/// @name Synchronous Methods
@@ -404,10 +418,17 @@ typedef void (^PINDiskCacheContainsBlock)(BOOL containsObject);
404418
read from disk, the `object` parameter of the block will be `nil` but the `fileURL` will be available.
405419
This method blocks the calling thread until all objects have been enumerated.
406420
@param block A block to be executed for every object in the cache.
421+
407422
@warning Do not call this method within the event blocks (<didRemoveObjectBlock>, etc.)
408423
Instead use the asynchronous version, <enumerateObjectsWithBlock:completionBlock:>.
424+
425+
@warning The PINDiskCache lock is held while block is executed. Any synchronous calls to the diskcache
426+
or a cache which owns the instance of the disk cache are likely to cause a deadlock. This is why the block is
427+
*not* passed the instance of the disk cache. You should also avoid doing extensive work while this
428+
lock is held.
429+
409430
*/
410-
- (void)enumerateObjectsWithBlock:(nullable PINDiskCacheObjectBlock)block;
431+
- (void)enumerateObjectsWithBlock:(nullable PINDiskCacheFileURLBlock)block;
411432

412433
@end
413434

0 commit comments

Comments
 (0)