From c75f414fdc965df365133cb0e24ccc44dece65e3 Mon Sep 17 00:00:00 2001 From: Frank <472730949@qq.com> Date: Mon, 18 Jan 2021 15:33:04 +0800 Subject: [PATCH] Fix semaphore initialization timing. (#806) --- MJExtension/MJExtensionConst.h | 7 +++++++ MJExtension/NSObject+MJClass.m | 13 ++++++------- MJExtension/NSObject+MJProperty.m | 31 ++++++++++++++----------------- MJExtensionDemo/AppDelegate.m | 1 + 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/MJExtension/MJExtensionConst.h b/MJExtension/MJExtensionConst.h index fcf895d4..5120431c 100644 --- a/MJExtension/MJExtensionConst.h +++ b/MJExtension/MJExtensionConst.h @@ -12,6 +12,13 @@ #define MJ_UNLOCK(lock) dispatch_semaphore_signal(lock); #endif +// 信号量 +#define MJExtensionSemaphoreCreate \ +extern dispatch_semaphore_t mje_signalSemaphore; \ +extern dispatch_once_t mje_onceTokenSemaphore; \ +dispatch_once(&mje_onceTokenSemaphore, ^{ \ + mje_signalSemaphore = dispatch_semaphore_create(1); \ +}); // 过期 #define MJExtensionDeprecated(instead) NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, instead) diff --git a/MJExtension/NSObject+MJClass.m b/MJExtension/NSObject+MJClass.m index 54c1dc4d..c09074c2 100644 --- a/MJExtension/NSObject+MJClass.m +++ b/MJExtension/NSObject+MJClass.m @@ -17,8 +17,6 @@ static const char MJAllowedCodingPropertyNamesKey = '\0'; static const char MJIgnoredCodingPropertyNamesKey = '\0'; -extern dispatch_semaphore_t signalSemaphore; - @implementation NSObject (MJClass) + (NSMutableDictionary *)mj_classDictForKey:(const void *)key @@ -141,15 +139,16 @@ + (void)mj_setupBlockReturnValue:(id (^)(void))block key:(const char *)key } // 清空数据 - MJ_LOCK(signalSemaphore); + MJExtensionSemaphoreCreate + MJ_LOCK(mje_signalSemaphore); [[self mj_classDictForKey:key] removeAllObjects]; - MJ_UNLOCK(signalSemaphore); + MJ_UNLOCK(mje_signalSemaphore); } + (NSMutableArray *)mj_totalObjectsWithSelector:(SEL)selector key:(const char *)key { - - MJ_LOCK(signalSemaphore); + MJExtensionSemaphoreCreate + MJ_LOCK(mje_signalSemaphore); NSMutableArray *array = [self mj_classDictForKey:key][NSStringFromClass(self)]; if (array == nil) { // 创建、存储 @@ -170,7 +169,7 @@ + (NSMutableArray *)mj_totalObjectsWithSelector:(SEL)selector key:(const char *) [array addObjectsFromArray:subArray]; }]; } - MJ_UNLOCK(signalSemaphore); + MJ_UNLOCK(mje_signalSemaphore); return array; } @end diff --git a/MJExtension/NSObject+MJProperty.m b/MJExtension/NSObject+MJProperty.m index 0255fb57..ba93a36a 100644 --- a/MJExtension/NSObject+MJProperty.m +++ b/MJExtension/NSObject+MJProperty.m @@ -25,18 +25,11 @@ static const char MJCachedPropertiesKey = '\0'; -dispatch_semaphore_t signalSemaphore; +dispatch_semaphore_t mje_signalSemaphore; +dispatch_once_t mje_onceTokenSemaphore; @implementation NSObject (Property) -+ (void)load -{ - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - signalSemaphore = dispatch_semaphore_create(1); - }); -} - + (NSMutableDictionary *)mj_propertyDictForKey:(const void *)key { static NSMutableDictionary *replacedKeyFromPropertyNameDict; @@ -133,9 +126,10 @@ + (Class)mj_propertyObjectClassInArray:(NSString *)propertyName + (void)mj_enumerateProperties:(MJPropertiesEnumeration)enumeration { // 获得成员变量 - MJ_LOCK(signalSemaphore); + MJExtensionSemaphoreCreate + MJ_LOCK(mje_signalSemaphore); NSArray *cachedProperties = [self mj_properties]; - MJ_UNLOCK(signalSemaphore); + MJ_UNLOCK(mje_signalSemaphore); // 遍历成员变量 BOOL stop = NO; for (MJProperty *property in [cachedProperties copy]) { @@ -210,9 +204,10 @@ + (void)mj_setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray { [self mj_setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey]; - MJ_LOCK(signalSemaphore); + MJExtensionSemaphoreCreate + MJ_LOCK(mje_signalSemaphore); [[self mj_propertyDictForKey:&MJCachedPropertiesKey] removeAllObjects]; - MJ_UNLOCK(signalSemaphore); + MJ_UNLOCK(mje_signalSemaphore); } #pragma mark - key配置 @@ -220,18 +215,20 @@ + (void)mj_setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)repla { [self mj_setupBlockReturnValue:replacedKeyFromPropertyName key:&MJReplacedKeyFromPropertyNameKey]; - MJ_LOCK(signalSemaphore); + MJExtensionSemaphoreCreate + MJ_LOCK(mje_signalSemaphore); [[self mj_propertyDictForKey:&MJCachedPropertiesKey] removeAllObjects]; - MJ_UNLOCK(signalSemaphore); + MJ_UNLOCK(mje_signalSemaphore); } + (void)mj_setupReplacedKeyFromPropertyName121:(MJReplacedKeyFromPropertyName121)replacedKeyFromPropertyName121 { objc_setAssociatedObject(self, &MJReplacedKeyFromPropertyName121Key, replacedKeyFromPropertyName121, OBJC_ASSOCIATION_COPY_NONATOMIC); - MJ_LOCK(signalSemaphore); + MJExtensionSemaphoreCreate + MJ_LOCK(mje_signalSemaphore); [[self mj_propertyDictForKey:&MJCachedPropertiesKey] removeAllObjects]; - MJ_UNLOCK(signalSemaphore); + MJ_UNLOCK(mje_signalSemaphore); } @end #pragma clang diagnostic pop diff --git a/MJExtensionDemo/AppDelegate.m b/MJExtensionDemo/AppDelegate.m index 5c3f931f..ba7198e2 100644 --- a/MJExtensionDemo/AppDelegate.m +++ b/MJExtensionDemo/AppDelegate.m @@ -17,6 +17,7 @@ @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. + return YES; }