Skip to content

Instantly share code, notes, and snippets.

@steipete
Created December 2, 2013 09:00
Show Gist options
  • Save steipete/7746843 to your computer and use it in GitHub Desktop.
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…
@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
@ramikay
Copy link

ramikay commented Dec 27, 2013

Thanks for this.

In Apple's subclassing notes, they mention that initWithObjects:forKeys:count: needs to be subclassed as it's a primitive method upon which other methods are based.

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/

@ramikay
Copy link

ramikay commented Dec 27, 2013

Also, the keys should be copied shouldn't they? So that they can no longer mutate

@ramikay
Copy link

ramikay commented Dec 27, 2013

Hey... I gave this a test and it crashed because the dictionary was mutated while being enumerated... here's the code

// Define the dictionary
static int SIZE = 1000000;
PSPDFThreadSafeMutableDictionary *_dictionary = [[PSPDFThreadSafeMutableDictionary alloc] initWithCapacity:SIZE];

// Fill it up on the main thread
for (int _i = 0; _i < SIZE; _i++)
{
    [_dictionary setObject:[NSNumber numberWithInt:_i] forKey:[NSNumber numberWithInt:_i]];
}

// Remove objects asynchronously on a separate thread
dispatch_queue_t _q = dispatch_queue_create("second_thread", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(_q, ^{
    for (int _i = 0; _i < SIZE; _i++)
    {
        [_dictionary removeObjectForKey:[NSNumber numberWithInt:_i]];
    }
});

// Print the values on the main thread
NSLog(@"%@", [_dictionary allValues]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment