Differences From Artifact [7d4ea0e843]:
- File
src/OFNotificationCenter.m
— part of check-in
[93851ccd0f]
at
2021-10-31 18:02:13
on branch notifications
— OFNotificationCenter: Move posting out of the lock
This prevents a deadlock when modifying observers from a handler. (user: js, size: 5217) [annotate] [blame] [check-ins using]
To Artifact [ffe3d96692]:
- File src/OFNotificationCenter.m — part of check-in [108d360116] at 2021-10-31 20:14:44 on branch notifications — OFNotificationCenter: Add support for blocks (user: js, size: 7504) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
19 20 21 22 23 24 25 26 27 28 29 | #import "OFArray.h" #import "OFDictionary.h" #ifdef OF_HAVE_THREADS # import "OFMutex.h" #endif #import "OFSet.h" #import "OFString.h" @interface OFDefaultNotificationCenter: OFNotificationCenter @end | > > | > > > > | > | | > > > > > | | > | | > > > > > > > > > > > > > > > > > > > > > > > > > | | > > > > | > | > > > > > | > > > > > | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | #import "OFArray.h" #import "OFDictionary.h" #ifdef OF_HAVE_THREADS # import "OFMutex.h" #endif #import "OFSet.h" #import "OFString.h" #import "OFInvalidArgumentException.h" @interface OFDefaultNotificationCenter: OFNotificationCenter @end @interface OFNotificationCenterHandle: OFObject { @public OFNotificationName _name; id _observer; SEL _selector; unsigned long _selectorHash; #ifdef OF_HAVE_BLOCKS OFNotificationCenterBlock _block; #endif id _object; } - (instancetype)initWithName: (OFNotificationName)name observer: (id)observer selector: (SEL)selector object: (id)object; #ifdef OF_HAVE_BLOCKS - (instancetype)initWithName: (OFNotificationName)name object: (id)object block: (OFNotificationCenterBlock)block; #endif @end static OFNotificationCenter *defaultCenter; @implementation OFNotificationCenterHandle - (instancetype)initWithName: (OFNotificationName)name observer: (id)observer selector: (SEL)selector object: (id)object { self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); _name = [name copy]; _observer = [observer retain]; _selector = selector; _object = [object retain]; _selectorHash = [[OFString stringWithUTF8String: sel_getName(_selector)] hash]; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } return self; } #ifdef OF_HAVE_BLOCKS - (instancetype)initWithName: (OFNotificationName)name object: (id)object block: (OFNotificationCenterBlock)block { self = [super init]; @try { _name = [name copy]; _object = [object retain]; _block = [block copy]; } @catch (id e) { [self release]; @throw e; } return self; } #endif - (void)dealloc { [_name release]; [_observer release]; #ifdef OF_HAVE_BLOCKS [_block release]; #endif [_object release]; [super dealloc]; } - (bool)isEqual: (OFNotificationCenterHandle *)handle { if (![handle isKindOfClass: [OFNotificationCenterHandle class]]) return false; if (![handle->_name isEqual: _name]) return false; if (handle->_observer != _observer && ![handle->_observer isEqual: _observer]) return false; if (handle->_selector != _selector && !sel_isEqual(handle->_selector, _selector)) return false; #ifdef OF_HAVE_BLOCKS if (handle->_block != _block) return false; #endif if (handle->_object != _object && ![handle->_object isEqual: _object]) return false; return true; } - (unsigned long)hash { unsigned long hash; OFHashInit(&hash); OFHashAddHash(&hash, _name.hash); OFHashAddHash(&hash, [_observer hash]); OFHashAddHash(&hash, _selectorHash); #ifdef OF_HAVE_BLOCKS if (_block != NULL) OFHashAddHash(&hash, (unsigned long)(uintptr_t)_block); #endif OFHashAddHash(&hash, [_object hash]); OFHashFinalize(&hash); return hash; } @end |
︙ | ︙ | |||
128 129 130 131 132 133 134 | { self = [super init]; @try { #ifdef OF_HAVE_THREADS _mutex = [[OFMutex alloc] init]; #endif | | | > > > > > > > > > > > > > > > > > > > > > > > | > > | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | < < < | | | < < | > > | | | | < < < < < < < < < < | | | | | | | > > > > > | | | > > | > > > | 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | { self = [super init]; @try { #ifdef OF_HAVE_THREADS _mutex = [[OFMutex alloc] init]; #endif _handles = [[OFMutableDictionary alloc] init]; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { #ifdef OF_HAVE_THREADS [_mutex release]; #endif [_handles release]; [super dealloc]; } - (void)of_addObserver: (OFNotificationCenterHandle *)handle { #ifdef OF_HAVE_THREADS [_mutex lock]; @try { #endif OFMutableSet *handlesForName = [_handles objectForKey: handle->_name]; if (handlesForName == nil) { handlesForName = [OFMutableSet set]; [_handles setObject: handlesForName forKey: handle->_name]; } [handlesForName addObject: handle]; #ifdef OF_HAVE_THREADS } @finally { [_mutex unlock]; } #endif } - (void)addObserver: (id)observer selector: (SEL)selector name: (OFNotificationName)name object: (id)object { void *pool = objc_autoreleasePoolPush(); [self of_addObserver: [[[OFNotificationCenterHandle alloc] initWithName: name observer: observer selector: selector object: object] autorelease]]; objc_autoreleasePoolPop(pool); } #ifdef OF_HAVE_BLOCKS - (OFNotificationCenterHandle *) addObserverForName: (OFNotificationName)name object: (id)object usingBlock: (OFNotificationCenterBlock)block { void *pool = objc_autoreleasePoolPush(); OFNotificationCenterHandle *handle = [[[OFNotificationCenterHandle alloc] initWithName: name object: object block: block] autorelease]; [self of_addObserver: handle]; [handle retain]; objc_autoreleasePoolPop(pool); return [handle autorelease]; } #endif - (void)removeObserver: (OFNotificationCenterHandle *)handle { if (![handle isKindOfClass: [OFNotificationCenterHandle class]]) @throw [OFInvalidArgumentException exception]; #ifdef OF_HAVE_THREADS [_mutex lock]; @try { #endif OFNotificationName name = [[handle->_name copy] autorelease]; OFMutableSet *handlesForName = [_handles objectForKey: name]; [handlesForName removeObject: handle]; if (handlesForName.count == 0) [_handles removeObjectForKey: name]; #ifdef OF_HAVE_THREADS } @finally { [_mutex unlock]; } #endif } - (void)removeObserver: (id)observer selector: (SEL)selector name: (OFNotificationName)name object: (id)object { void *pool = objc_autoreleasePoolPush(); [self removeObserver: [[[OFNotificationCenterHandle alloc] initWithName: name observer: observer selector: selector object: object] autorelease]]; objc_autoreleasePoolPop(pool); } - (void)postNotification: (OFNotification *)notification { void *pool = objc_autoreleasePoolPush(); OFMutableArray *matchedHandles = [OFMutableArray array]; #ifdef OF_HAVE_THREADS [_mutex lock]; @try { #endif for (OFNotificationCenterHandle *handle in [_handles objectForKey: notification.name]) if (handle->_object == nil || handle->_object == notification.object) [matchedHandles addObject: handle]; #ifdef OF_HAVE_THREADS } @finally { [_mutex unlock]; } #endif for (OFNotificationCenterHandle *handle in matchedHandles) { #ifdef OF_HAVE_BLOCKS if (handle->_block != NULL) handle->_block(notification); else { #endif void (*callback)(id, SEL, OFNotification *) = (void (*)(id, SEL, OFNotification *)) [handle->_observer methodForSelector: handle->_selector]; callback(handle->_observer, handle->_selector, notification); #ifdef OF_HAVE_BLOCKS } #endif } objc_autoreleasePoolPop(pool); } @end @implementation OFDefaultNotificationCenter |
︙ | ︙ |