Skip to content

Commit

Permalink
Fix semaphore initialization timing. (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfcon committed Jan 18, 2021
1 parent 7b3bc5b commit c75f414
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
7 changes: 7 additions & 0 deletions MJExtension/MJExtensionConst.h
Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions MJExtension/NSObject+MJClass.m
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
// 创建、存储
Expand All @@ -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
31 changes: 14 additions & 17 deletions MJExtension/NSObject+MJProperty.m
Expand Up @@ -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;
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -210,28 +204,31 @@ + (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配置
+ (void)mj_setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName
{
[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
1 change: 1 addition & 0 deletions MJExtensionDemo/AppDelegate.m
Expand Up @@ -17,6 +17,7 @@ @implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

return YES;
}

Expand Down

0 comments on commit c75f414

Please sign in to comment.