Guest User

Untitled

a guest
May 24th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. //
  2. // PSPDFThreadSafeMutableDictionary.m
  3. //
  4. // Copyright (c) 2013 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23.  
  24. #import <Foundation/Foundation.h>
  25.  
  26. // Dictionary-Subclasss whose primitive operations are thread safe.
  27. @interface PSPDFThreadSafeMutableDictionary : NSMutableDictionary
  28. @end
  29.  
  30. // ----------------------------------------------------------------
  31.  
  32. //
  33. // PSPDFThreadSafeMutableDictionary.m
  34. // PSPDFKit
  35. //
  36. // Copyright (c) 2013 PSPDFKit GmbH. All rights reserved.
  37. //
  38. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY AUSTRIAN COPYRIGHT LAW
  39. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE PSPDFKIT LICENSE AGREEMENT.
  40. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  41. // This notice may not be removed from this file.
  42. //
  43.  
  44. #import "PSPDFThreadSafeMutableDictionary.h"
  45. #import <libkern/OSAtomic.h>
  46.  
  47. #define LOCKED(...) OSSpinLockLock(&_lock); \
  48. __VA_ARGS__; \
  49. OSSpinLockUnlock(&_lock);
  50.  
  51. @implementation PSPDFThreadSafeMutableDictionary {
  52. OSSpinLock _lock;
  53. NSMutableDictionary *_dictionary; // Class Cluster!
  54. }
  55.  
  56. ///////////////////////////////////////////////////////////////////////////////////////////
  57. #pragma mark - NSObject
  58.  
  59. - (id)init {
  60. return [self initWithCapacity:0];
  61. }
  62.  
  63. - (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys {
  64. if ((self = [self initWithCapacity:objects.count])) {
  65. [objects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  66. _dictionary[keys[idx]] = obj;
  67. }];
  68. }
  69. return self;
  70. }
  71.  
  72. - (id)initWithCapacity:(NSUInteger)capacity {
  73. if ((self = [super init])) {
  74. _dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity];
  75. _lock = OS_SPINLOCK_INIT;
  76. }
  77. return self;
  78. }
  79.  
  80. ///////////////////////////////////////////////////////////////////////////////////////////
  81. #pragma mark - NSMutableDictionary
  82.  
  83. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  84. LOCKED(_dictionary[aKey] = anObject)
  85. }
  86.  
  87. - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary {
  88. LOCKED([_dictionary addEntriesFromDictionary:otherDictionary]);
  89. }
  90.  
  91. - (void)setDictionary:(NSDictionary *)otherDictionary {
  92. LOCKED([_dictionary setDictionary:otherDictionary]);
  93. }
  94.  
  95. - (void)removeObjectForKey:(id)aKey {
  96. LOCKED([_dictionary removeObjectForKey:aKey])
  97. }
  98.  
  99. - (void)removeAllObjects {
  100. LOCKED([_dictionary removeAllObjects]);
  101. }
  102.  
  103. - (NSUInteger)count {
  104. LOCKED(NSUInteger count = _dictionary.count)
  105. return count;
  106. }
  107.  
  108. - (NSArray *)allKeys {
  109. LOCKED(NSArray *allKeys = _dictionary.allKeys)
  110. return allKeys;
  111. }
  112.  
  113. - (NSArray *)allValues {
  114. LOCKED(NSArray *allValues = _dictionary.allValues)
  115. return allValues;
  116. }
  117.  
  118. - (id)objectForKey:(id)aKey {
  119. LOCKED(id obj = _dictionary[aKey])
  120. return obj;
  121. }
  122.  
  123. - (NSEnumerator *)keyEnumerator {
  124. LOCKED(NSEnumerator *keyEnumerator = [_dictionary keyEnumerator])
  125. return keyEnumerator;
  126. }
  127.  
  128. - (id)copyWithZone:(NSZone *)zone {
  129. return [self mutableCopyWithZone:zone];
  130. }
  131.  
  132. - (id)mutableCopyWithZone:(NSZone *)zone {
  133. LOCKED(id copiedDictionary = [[self.class allocWithZone:zone] initWithDictionary:_dictionary])
  134. return copiedDictionary;
  135. }
  136.  
  137. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
  138. objects:(id __unsafe_unretained [])stackbuf
  139. count:(NSUInteger)len {
  140. LOCKED(NSUInteger count = [[_dictionary copy] countByEnumeratingWithState:state objects:stackbuf count:len]);
  141. return count;
  142. }
  143.  
  144. - (void)performLockedWithDictionary:(void (^)(NSDictionary *dictionary))block {
  145. if (block) LOCKED(block(_dictionary));
  146. }
  147.  
  148. - (BOOL)isEqual:(id)object {
  149. if (object == self) return YES;
  150.  
  151. if ([object isKindOfClass:PSPDFThreadSafeMutableDictionary.class]) {
  152. PSPDFThreadSafeMutableDictionary *other = object;
  153. __block BOOL isEqual = NO;
  154. [other performLockedWithDictionary:^(NSDictionary *dictionary) {
  155. [self performLockedWithDictionary:^(NSDictionary *otherDictionary) {
  156. isEqual = [dictionary isEqual:otherDictionary];
  157. }];
  158. }];
  159. return isEqual;
  160. }
  161. return NO;
  162. }
  163.  
  164. @end
Add Comment
Please, Sign In to add comment