Created
December 2, 2013 09:00
-
-
Save steipete/7746843 to your computer and use it in GitHub Desktop.
Dictionary subclass whose primitive operations are thread safe. Most of the time you don't want locking at the collection level, but there are valid use cases for this, and it's interesting how to properly subclass the NSDictionary class cluster. We're using SpinLocks here, since it's highly unlikely that any of the operation locks for more than…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface PSPDFThreadSafeMutableDictionary : NSMutableDictionary | |
@end | |
#import "PSPDFThreadSafeMutableDictionary.h" | |
#import <libkern/OSAtomic.h> | |
@implementation PSPDFThreadSafeMutableDictionary { | |
OSSpinLock _lock; | |
NSMutableDictionary *_dictionary; // Class Cluster! | |
} | |
- (id)init { | |
return [self initWithCapacity:0]; | |
} | |
- (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys { | |
if ((self = [self initWithCapacity:objects.count])) { | |
[objects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | |
_dictionary[keys[idx]] = obj; | |
}]; | |
} | |
return self; | |
} | |
- (id)initWithCapacity:(NSUInteger)capacity { | |
if ((self = [super init])) { | |
_dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity]; | |
_lock = OS_SPINLOCK_INIT; | |
} | |
return self; | |
} | |
- (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey { | |
OSSpinLockLock(&_lock); | |
_dictionary[aKey] = anObject; | |
OSSpinLockUnlock(&_lock); | |
} | |
- (void)removeObjectForKey:(id)aKey { | |
OSSpinLockLock(&_lock); | |
[_dictionary removeObjectForKey:aKey]; | |
OSSpinLockUnlock(&_lock); | |
} | |
- (NSUInteger)count { | |
OSSpinLockLock(&_lock); | |
NSUInteger count = _dictionary.count; | |
OSSpinLockUnlock(&_lock); | |
return count; | |
} | |
- (id)objectForKey:(id)aKey { | |
OSSpinLockLock(&_lock); | |
id obj = _dictionary[aKey]; | |
OSSpinLockUnlock(&_lock); | |
return obj; | |
} | |
- (NSEnumerator *)keyEnumerator { | |
OSSpinLockLock(&_lock); | |
NSEnumerator *keyEnumerator = [_dictionary keyEnumerator]; | |
OSSpinLockUnlock(&_lock); | |
return keyEnumerator; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey... I gave this a test and it crashed because the dictionary was mutated while being enumerated... here's the code