Skip to content

Commit

Permalink
Use dispatch_once instead of static flags
Browse files Browse the repository at this point in the history
  • Loading branch information
defagos committed Sep 30, 2014
1 parent c6eab20 commit 75b4d6a
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ - (instancetype)initWithName:(NSString *)name type:(DeviceType)type
- (instancetype)init
{
HLSForbiddenInheritedMethod();
return nil;
return [self initWithName:nil type:0];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ - (void)textField:(UITextField *)textField didFailValidationWithError:(NSError *
- (void)reloadData
{
static NSNumberFormatter *s_numberFormatter = nil;
if (! s_numberFormatter) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
s_numberFormatter = [[NSNumberFormatter alloc] init];
[s_numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[s_numberFormatter setAllowsFloats:NO];
}
});

[self.firstNameTextField bindToManagedObject:self.personInformation
fieldName:@"firstName"
Expand Down
8 changes: 3 additions & 5 deletions CoconutKit/Sources/Animation/HLSLayerAnimationStep.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ - (void)playAnimationWithStartTime:(NSTimeInterval)startTime animated:(BOOL)anim

#if TARGET_IPHONE_SIMULATOR
static float (*s_UIAnimationDragCoefficient)(void) = NULL;
static BOOL s_firstLoad = YES;
if (s_firstLoad) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
void *UIKitDylib = dlopen([[[NSBundle bundleForClass:[UIApplication class]] executablePath] fileSystemRepresentation], RTLD_LAZY);
s_UIAnimationDragCoefficient = (float (*)(void))dlsym(UIKitDylib, "UIAnimationDragCoefficient");
if (! s_UIAnimationDragCoefficient) {
HLSLoggerInfo(@"UIAnimationDragCoefficient not found. Slow animations won't be available for animations based on Core Animation");
}

s_firstLoad = NO;
}
});

if (s_UIAnimationDragCoefficient) {
duration *= s_UIAnimationDragCoefficient();
Expand Down
12 changes: 6 additions & 6 deletions CoconutKit/Sources/Core/HLSNotifications.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ @implementation HLSNotificationManager {
+ (instancetype)sharedNotificationManager
{
static HLSNotificationManager *s_instance = nil;

if (! s_instance) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
s_instance = [[[self class] alloc] init];
}
});
return s_instance;
}

Expand Down Expand Up @@ -130,10 +130,10 @@ @implementation HLSNotificationConverter
+ (instancetype)sharedNotificationConverter
{
static HLSNotificationConverter *s_instance = nil;

if (! s_instance) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
s_instance = [[[self class] alloc] init];
}
});
return s_instance;
}

Expand Down
6 changes: 3 additions & 3 deletions CoconutKit/Sources/Core/HLSUserInterfaceLock.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ @implementation HLSUserInterfaceLock {
+ (instancetype)sharedUserInterfaceLock
{
static HLSUserInterfaceLock *s_instance = nil;

if (! s_instance) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
s_instance = [[[self class] alloc] init];
}
});
return s_instance;
}

Expand Down
5 changes: 3 additions & 2 deletions CoconutKit/Sources/Core/NSBundle+HLSExtensions.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ + (NSBundle *)bundleWithName:(NSString *)name
}

static NSMutableDictionary *s_nameToBundleMap = nil;
if (! s_nameToBundleMap) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
s_nameToBundleMap = [[NSMutableDictionary alloc] init];
}
});

NSBundle *bundle = [s_nameToBundleMap objectForKey:name];
if (bundle) {
Expand Down
5 changes: 3 additions & 2 deletions CoconutKit/Sources/Core/NSDate+HLSExtensions.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ - (BOOL)isLaterThanOrEqualToDate:(NSDate *)date
static NSString *swizzled_NSDate__descriptionWithLocale_Imp(NSDate *self, SEL _cmd, id locale)
{
static NSDateFormatter *s_dateFormatter = nil;
if (! s_dateFormatter) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
// Create time formatter for system timezone (which is the default one if not set)
s_dateFormatter = [[NSDateFormatter alloc] init];
[s_dateFormatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'ZZZ"];
}
});

NSString *originalString = (*s_NSDate__descriptionWithLocale_Imp)(self, _cmd, locale);
return [NSString stringWithFormat:@"%@ (system time zone: %@)", originalString, [s_dateFormatter stringFromDate:self]];
Expand Down
10 changes: 6 additions & 4 deletions CoconutKit/Sources/Core/NSDateFormatter+HLSExtensions.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@ @implementation NSDateFormatter (HLSExtensions)
+ (NSArray *)orderedWeekdaySymbols
{
static NSArray *s_orderedWeekdays = nil;
if (! s_orderedWeekdays) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSArray *weekDays = [dateFormatter weekdaySymbols];
// firstWeekday returns indices starting at 1
NSUInteger offset = [[NSCalendar currentCalendar] firstWeekday] - 1;
s_orderedWeekdays = [weekDays arrayByLeftRotatingNumberOfObjects:offset];
}
});
return s_orderedWeekdays;
}

+ (NSArray *)orderedShortWeekdaySymbols
{
static NSArray *s_orderedShortWeekdays = nil;
if (! s_orderedShortWeekdays) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSArray *shortWeekDays = [dateFormatter shortWeekdaySymbols];
// firstWeekday returns indices starting at 1
NSUInteger offset = [[NSCalendar currentCalendar] firstWeekday] - 1;
s_orderedShortWeekdays = [shortWeekDays arrayByLeftRotatingNumberOfObjects:offset];
}
});
return s_orderedShortWeekdays;
}

Expand Down
33 changes: 15 additions & 18 deletions CoconutKit/Sources/Logging/HLSLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@ @implementation HLSLogger
+ (instancetype)sharedLogger
{
static HLSLogger *s_instance = nil;

// Double-checked locking pattern
if (! s_instance) {
@synchronized(self) {
if (! s_instance) {
NSString *logDirectoryPath = [HLSApplicationLibraryDirectoryPath() stringByAppendingPathComponent:@"HLSLogger"];
s_instance = [[[self class] alloc] initWithLogDirectoryPath:logDirectoryPath];
}
}
}
static dispatch_once_t s_onceToken;

dispatch_once(&s_onceToken, ^{
NSString *logDirectoryPath = [HLSApplicationLibraryDirectoryPath() stringByAppendingPathComponent:@"HLSLogger"];
s_instance = [[[self class] alloc] initWithLogDirectoryPath:logDirectoryPath];
});
return s_instance;
}

Expand Down Expand Up @@ -133,13 +129,12 @@ - (void)logMessage:(NSString *)message forMode:(HLSLoggerMode)mode
return;
}

static BOOL s_configurationLoaded = NO;
static BOOL s_xcodeColorsEnabled = NO;
if (! s_configurationLoaded) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
NSString *xcodeColorsValue = [[[NSProcessInfo processInfo] environment] objectForKey:@"XcodeColors"];
s_xcodeColorsEnabled = [xcodeColorsValue isEqualToString:@"YES"];
s_configurationLoaded = YES;
}
});

// NSLog is thread-safe and adds date, thread and process information in front of each line
NSString *logEntry = [NSString stringWithFormat:@"[%@] %@", mode.name, message];
Expand Down Expand Up @@ -172,10 +167,11 @@ - (void)logToFileWithEntry:(NSString *)logEntry

// File name: BundleName_version_date.log
static NSDateFormatter *s_dateFormatter = nil;
if (! s_dateFormatter) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
s_dateFormatter = [[NSDateFormatter alloc] init];
[s_dateFormatter setDateFormat:@"yyyyMMdd_HHmmss"];
}
});

NSString *dateString = [s_dateFormatter stringFromDate:[NSDate date]];
NSString *bundleName = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"] stringByReplacingOccurrencesOfString:@"." withString:@"-"];
Expand All @@ -194,10 +190,11 @@ - (void)logToFileWithEntry:(NSString *)logEntry

// Add date, thread and process information, as NSLog does
static NSDateFormatter *s_dateFormatter = nil;
if (! s_dateFormatter) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
s_dateFormatter = [[NSDateFormatter alloc] init];
[s_dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
}
});

mach_port_t threadId = pthread_mach_thread_np(pthread_self());
NSString *logFileEntry = [NSString stringWithFormat:@"%@ [%x] %@\n",
Expand Down
5 changes: 3 additions & 2 deletions CoconutKit/Sources/Task/HLSTaskManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ @implementation HLSTaskManager
+ (instancetype)defaultManager
{
static HLSTaskManager *s_instance = nil;
if (! s_instance) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
s_instance = [[[self class] alloc] init];
}
});
return s_instance;
}

Expand Down
5 changes: 3 additions & 2 deletions CoconutKit/Sources/ViewControllers/HLSPlaceholderInsetSegue.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ - (void)perform
NSString *indexString = [self.identifier stringByReplacingOccurrencesOfString:HLSPlaceholderPreloadSegueIdentifierPrefix
withString:@""];
static NSNumberFormatter *s_numberFormatter = nil;
if (! s_numberFormatter) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
s_numberFormatter = [[NSNumberFormatter alloc] init];
}
});
NSNumber *indexNumber = [s_numberFormatter numberFromString:indexString];
if (! indexNumber) {
HLSLoggerError(@"Cannot parse inset index from segue identifier '%@'", self.identifier);
Expand Down
12 changes: 7 additions & 5 deletions CoconutKit/Sources/ViewControllers/HLSTransition.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ @implementation HLSTransition
+ (NSArray *)availableTransitionNames
{
static NSArray *s_availableTransitionNames = nil;
if (! s_availableTransitionNames) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
NSMutableArray *availableTransitionNames = [NSMutableArray array];

// Find all HLSTransition subclasses (except HLSTransition itself)
Expand Down Expand Up @@ -60,7 +61,7 @@ + (NSArray *)availableTransitionNames
free(classes);

s_availableTransitionNames = [availableTransitionNames sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}
});
return s_availableTransitionNames;
}

Expand Down Expand Up @@ -139,10 +140,11 @@ + (NSTimeInterval)defaultDuration
{
// Durations are constants for each transition animation class. Can cache them
static NSMutableDictionary *s_animationClassNameToDurationMap = nil;
if (! s_animationClassNameToDurationMap) {
static dispatch_once_t s_onceToken;
dispatch_once(&s_onceToken, ^{
s_animationClassNameToDurationMap = [NSMutableDictionary dictionary];
}

});
NSNumber *duration = [s_animationClassNameToDurationMap objectForKey:[self className]];
if (! duration) {
// Calculate for a dummy animation
Expand Down

0 comments on commit 75b4d6a

Please sign in to comment.