Skip to content

Commit 3065fd3

Browse files
committed
Avoid some more potential data race conditions.
Use `dispatch_once` instead of a `static BOOL` to guard the work. While at it, remove one use of `@synchronized` to instead standardize on just `dispatch_once`.
1 parent 8c349c7 commit 3065fd3

3 files changed

Lines changed: 16 additions & 22 deletions

File tree

Sources/Core/GTLRBase64.m

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,20 @@ static void CreateDecodingTable(const char *encodingTable,
119119

120120
NSData *GTLRDecodeBase64(NSString *base64Str) {
121121
static char decodingTable[128];
122-
static BOOL hasInited = NO;
123-
124-
if (!hasInited) {
122+
static dispatch_once_t onceToken;
123+
dispatch_once(&onceToken, ^{
125124
CreateDecodingTable(gStandardEncodingTable, sizeof(gStandardEncodingTable),
126125
decodingTable);
127-
hasInited = YES;
128-
}
126+
});
129127
return DecodeBase64StringCommon(base64Str, decodingTable, YES /* requirePadding */ );
130128
}
131129

132130
NSData *GTLRDecodeWebSafeBase64(NSString *base64Str) {
133131
static char decodingTable[128];
134-
static BOOL hasInited = NO;
135-
136-
if (!hasInited) {
132+
static dispatch_once_t onceToken;
133+
dispatch_once(&onceToken, ^{
137134
CreateDecodingTable(gWebSafeEncodingTable, sizeof(gWebSafeEncodingTable),
138135
decodingTable);
139-
hasInited = YES;
140-
}
136+
});
141137
return DecodeBase64StringCommon(base64Str, decodingTable, NO /* requirePadding */);
142138
}

Sources/Core/GTLRRuntimeCommon.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,19 +484,18 @@ typedef NS_ENUM(NSUInteger, GTLRPropertyType) {
484484
},
485485
};
486486

487-
static BOOL hasLookedUpClasses = NO;
488-
if (!hasLookedUpClasses) {
487+
static dispatch_once_t onceToken;
488+
dispatch_once(&onceToken, ^{
489489
// Unfortunately, you can't put [NSString class] into the static structure,
490490
// so this lookup has to be done at runtime.
491-
hasLookedUpClasses = YES;
492491
for (uint32_t idx = 0; idx < sizeof(kImplInfo)/sizeof(kImplInfo[0]); ++idx) {
493492
if (kImplInfo[idx].returnClassName) {
494493
kImplInfo[idx].returnClass = objc_getClass(kImplInfo[idx].returnClassName);
495494
NSCAssert1(kImplInfo[idx].returnClass != nil,
496495
@"GTLRRuntimeCommon: class lookup failed: %s", kImplInfo[idx].returnClassName);
497496
}
498497
}
499-
}
498+
});
500499

501500
const char *attr = property_getAttributes(prop);
502501

Sources/Core/GTLRUtilities.m

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,13 @@ BOOL GTLR_AreBoolsEqual(BOOL b1, BOOL b2) {
8585
if ([str rangeOfString:@"."].location != NSNotFound) {
8686
// This is a floating-point number.
8787
// Force the parser to use '.' as the decimal separator.
88-
static NSLocale *usLocale = nil;
89-
@synchronized([GTLRUtilities class]) {
90-
if (usLocale == nil) {
91-
usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
92-
}
93-
newNum = [NSDecimalNumber decimalNumberWithString:(NSString*)num
94-
locale:(id)usLocale];
95-
}
88+
static NSLocale *usLocale;
89+
static dispatch_once_t onceToken;
90+
dispatch_once(&onceToken, ^{
91+
usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
92+
});
93+
newNum = [NSDecimalNumber decimalNumberWithString:(NSString*)num
94+
locale:(id)usLocale];
9695
} else {
9796
// NSDecimalNumber +decimalNumberWithString:locale:
9897
// does not correctly create an NSNumber for large values like

0 commit comments

Comments
 (0)