Index: src/OFDate.m ================================================================== --- src/OFDate.m +++ src/OFDate.m @@ -299,11 +299,10 @@ # pragma clang diagnostic pop #endif OF_SINGLETON_METHODS @end - @implementation OFDate + (void)initialize { #ifdef OF_WINDOWS Index: src/OFIRIHandler.m ================================================================== --- src/OFIRIHandler.m +++ src/OFIRIHandler.m @@ -22,14 +22,10 @@ #import "OFIRIHandler.h" #import "OFDictionary.h" #import "OFIRI.h" #import "OFNumber.h" -#ifdef OF_HAVE_THREADS -# import "OFMutex.h" -#endif - #import "OFArchiveIRIHandler.h" #import "OFEmbeddedIRIHandler.h" #ifdef OF_HAVE_FILES # import "OFFileIRIHandler.h" #endif @@ -38,19 +34,10 @@ #endif #import "OFUnsupportedProtocolException.h" static OFMutableDictionary OF_GENERIC(OFString *, OFIRIHandler *) *handlers; -#ifdef OF_HAVE_THREADS -static OFMutex *mutex; - -static void -releaseMutex(void) -{ - [mutex release]; -} -#endif @implementation OFIRIHandler @synthesize scheme = _scheme; + (void)initialize @@ -57,14 +44,10 @@ { if (self != [OFIRIHandler class]) return; handlers = [[OFMutableDictionary alloc] init]; -#ifdef OF_HAVE_THREADS - mutex = [[OFMutex alloc] init]; - atexit(releaseMutex); -#endif [self registerClass: [OFEmbeddedIRIHandler class] forScheme: @"embedded"]; #ifdef OF_HAVE_FILES [self registerClass: [OFFileIRIHandler class] forScheme: @"file"]; @@ -80,14 +63,11 @@ [self registerClass: [OFArchiveIRIHandler class] forScheme: @"zoo"]; } + (bool)registerClass: (Class)class forScheme: (OFString *)scheme { -#ifdef OF_HAVE_THREADS - [mutex lock]; - @try { -#endif + @synchronized (handlers) { OFIRIHandler *handler; if ([handlers objectForKey: scheme] != nil) return false; @@ -95,33 +75,22 @@ @try { [handlers setObject: handler forKey: scheme]; } @finally { [handler release]; } -#ifdef OF_HAVE_THREADS - } @finally { - [mutex unlock]; } -#endif return true; } + (OFIRIHandler *)handlerForIRI: (OFIRI *)IRI { OF_KINDOF(OFIRIHandler *) handler; -#ifdef OF_HAVE_THREADS - [mutex lock]; - @try { -#endif + @synchronized (handlers) { handler = [handlers objectForKey: IRI.scheme]; -#ifdef OF_HAVE_THREADS - } @finally { - [mutex unlock]; } -#endif if (handler == nil) @throw [OFUnsupportedProtocolException exceptionWithIRI: IRI]; return handler; Index: src/OFKernelEventObserver.h ================================================================== --- src/OFKernelEventObserver.h +++ src/OFKernelEventObserver.h @@ -29,13 +29,10 @@ OF_ASSUME_NONNULL_BEGIN @class OFMutableArray OF_GENERIC(ObjectType); @class OFDate; -#ifdef OF_HAVE_THREADS -@class OFMutex; -#endif @class OFMutableData; /** * @protocol OFKernelEventObserverDelegate * OFKernelEventObserver.h ObjFW/OFKernelEventObserver.h Index: src/OFNotificationCenter.h ================================================================== --- src/OFNotificationCenter.h +++ src/OFNotificationCenter.h @@ -21,13 +21,10 @@ #import "OFNotification.h" OF_ASSUME_NONNULL_BEGIN @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType); -#ifdef OF_HAVE_THREADS -@class OFMutex; -#endif #ifdef OF_HAVE_BLOCKS /** * @brief A block which is called when a notification has been posted. * @@ -45,13 +42,10 @@ #ifndef OF_NOTIFICATION_CENTER_M OF_SUBCLASSING_RESTRICTED #endif @interface OFNotificationCenter: OFObject { -#ifdef OF_HAVE_THREADS - OFMutex *_mutex; -#endif OFMutableDictionary *_handles; } #ifdef OF_HAVE_CLASS_PROPERTIES @property (class, readonly, nonatomic) OFNotificationCenter *defaultCenter; Index: src/OFNotificationCenter.m ================================================================== --- src/OFNotificationCenter.m +++ src/OFNotificationCenter.m @@ -22,13 +22,10 @@ #include "config.h" #import "OFNotificationCenter.h" #import "OFArray.h" #import "OFDictionary.h" -#ifdef OF_HAVE_THREADS -# import "OFMutex.h" -#endif #import "OFSet.h" #import "OFString.h" #import "OFInvalidArgumentException.h" @@ -185,13 +182,10 @@ - (instancetype)init { self = [super init]; @try { -#ifdef OF_HAVE_THREADS - _mutex = [[OFMutex alloc] init]; -#endif _handles = [[OFMutableDictionary alloc] init]; } @catch (id e) { [self release]; @throw e; } @@ -199,24 +193,18 @@ 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 + @synchronized (_handles) { OFMutableSet *handlesForName = [_handles objectForKey: handle->_name]; if (handlesForName == nil) { handlesForName = [OFMutableSet set]; @@ -223,15 +211,11 @@ [_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 @@ -280,31 +264,22 @@ @throw [OFInvalidArgumentException exception]; handle = handle_; pool = objc_autoreleasePoolPush(); - /* {} required to avoid -Wmisleading-indentation false positive. */ - if (![handle isKindOfClass: [OFNotificationCenterHandle class]]) { + if (![handle isKindOfClass: [OFNotificationCenterHandle class]]) @throw [OFInvalidArgumentException exception]; - } -#ifdef OF_HAVE_THREADS - [_mutex lock]; - @try { -#endif + @synchronized (_handles) { 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 objc_autoreleasePoolPop(pool); } - (void)removeObserver: (id)observer @@ -327,24 +302,17 @@ - (void)postNotification: (OFNotification *)notification { void *pool = objc_autoreleasePoolPush(); OFMutableArray *matchedHandles = [OFMutableArray array]; -#ifdef OF_HAVE_THREADS - [_mutex lock]; - @try { -#endif + @synchronized (_handles) { 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);