Skip to content

Commit 789930b

Browse files
authored
Merge pull request #3360 from jckarter/no-static-locals
StdlibStubs: Favor swift::Lazy over static local initialization.
2 parents 875bad5 + ba11a1c commit 789930b

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

include/swift/Basic/Lazy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_BASIC_LAZY_H
1414
#define SWIFT_BASIC_LAZY_H
1515

16+
#include <memory>
1617
#ifdef __APPLE__
1718
#include <dispatch/dispatch.h>
1819
#else

stdlib/public/stubs/Availability.mm

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#include "swift/Basic/Lazy.h"
1718
#include "swift/Runtime/Debug.h"
1819
#import <Foundation/Foundation.h>
1920
#include <TargetConditionals.h>
@@ -73,20 +74,22 @@ static NSOperatingSystemVersion operatingSystemVersionFromPlist() {
7374
return versionStruct;
7475
}
7576

77+
static NSOperatingSystemVersion getOSVersion() {
78+
// Use -[NSProcessInfo.operatingSystemVersion] when present
79+
// (on iOS 8 and OS X 10.10 and above).
80+
if ([NSProcessInfo
81+
instancesRespondToSelector:@selector(operatingSystemVersion)]) {
82+
return [[NSProcessInfo processInfo] operatingSystemVersion];
83+
} else {
84+
// Otherwise load and parse from SystemVersion dictionary.
85+
return operatingSystemVersionFromPlist();
86+
}
87+
}
88+
7689
/// Return the version of the operating system currently running for use in
7790
/// API availability queries.
7891
_SwiftNSOperatingSystemVersion swift::_swift_stdlib_operatingSystemVersion() {
79-
static NSOperatingSystemVersion version = ([]{
80-
// Use -[NSProcessInfo.operatingSystemVersion] when present
81-
// (on iOS 8 and OS X 10.10 and above).
82-
if ([NSProcessInfo
83-
instancesRespondToSelector:@selector(operatingSystemVersion)]) {
84-
return [[NSProcessInfo processInfo] operatingSystemVersion];
85-
} else {
86-
// Otherwise load and parse from SystemVersion dictionary.
87-
return operatingSystemVersionFromPlist();
88-
}
89-
})();
92+
NSOperatingSystemVersion version = SWIFT_LAZY_CONSTANT(getOSVersion());
9093

9194
return { version.majorVersion, version.minorVersion, version.patchVersion };
9295
}

stdlib/public/stubs/LibcShims.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdlib.h>
2121
#include <stdio.h>
2222
#include <string.h>
23+
#include "swift/Basic/Lazy.h"
2324
#include "../SwiftShims/LibcShims.h"
2425
#include "llvm/Support/DataTypes.h"
2526

@@ -105,9 +106,10 @@ size_t swift::_swift_stdlib_malloc_size(const void *ptr) {
105106
#error No malloc_size analog known for this platform/libc.
106107
#endif
107108

109+
static Lazy<std::mt19937> theGlobalMT19937;
110+
108111
static std::mt19937 &getGlobalMT19937() {
109-
static std::mt19937 MersenneRandom;
110-
return MersenneRandom;
112+
return theGlobalMT19937.get();
111113
}
112114

113115
__swift_uint32_t swift::_swift_stdlib_cxx11_mt19937() {

stdlib/public/stubs/UnicodeNormalization.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#include "swift/Basic/Lazy.h"
1718
#include "swift/Runtime/Config.h"
1819
#include "swift/Runtime/Debug.h"
1920

@@ -63,21 +64,19 @@ static const UCollator *MakeRootCollator() {
6364
// const here to make sure we don't misuse it.
6465
// http://sourceforge.net/p/icu/mailman/message/27427062/
6566
static const UCollator *GetRootCollator() {
66-
static const UCollator *RootCollator = MakeRootCollator();
67-
return RootCollator;
67+
return SWIFT_LAZY_CONSTANT(MakeRootCollator());
6868
}
6969

7070
/// This class caches the collation element results for the ASCII subset of
7171
/// unicode.
7272
class ASCIICollation {
7373
int32_t CollationTable[128];
7474
public:
75+
friend class swift::Lazy<ASCIICollation>;
7576

77+
static swift::Lazy<ASCIICollation> theTable;
7678
static const ASCIICollation *getTable() {
77-
// We are reallying on C++11's guaranteed of thread safe static variable
78-
// initialization.
79-
static ASCIICollation collation;
80-
return &collation;
79+
return &theTable.get();
8180
}
8281

8382
/// Maps an ASCII character to a collation element priority as would be
@@ -343,3 +342,5 @@ swift::_swift_stdlib_unicode_strToLower(uint16_t *Destination,
343342
}
344343
return OutputLength;
345344
}
345+
346+
swift::Lazy<ASCIICollation> ASCIICollation::theTable;

0 commit comments

Comments
 (0)