Skip to content

Commit a8bdbf4

Browse files
javachemeta-codesync[bot]
authored andcommitted
Require constant initialization for native static state (#59489)
Summary: Pull Request resolved: #59489 Enforce constant initialization for eligible native globals, static defaults, synchronization primitives, and thread-local state without changing their mutability or lifetime. Changelog: [Internal] Reviewed By: christophpurrer, cipolleschi Differential Revision: D127576609 fbshipit-source-id: dca8d92a1187f92d204b252764b370239358c028
1 parent 7692d60 commit a8bdbf4

29 files changed

Lines changed: 39 additions & 36 deletions

File tree

‎packages/react-native/Libraries/Image/RCTImageLoader.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static NSInteger RCTImageBytesForImage(UIImage *image)
3232
return image.images ? image.images.count * singleImageBytes : singleImageBytes;
3333
}
3434

35-
static auto currentRequestCount = std::atomic<uint64_t>(0);
35+
static constinit auto currentRequestCount = std::atomic<uint64_t>(0);
3636

3737
static NSError *addResponseHeadersToError(NSError *originalError, NSHTTPURLResponse *response)
3838
{

‎packages/react-native/Libraries/Network/RCTNetworkTask.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ @implementation RCTNetworkTask {
2121
RCTNetworkTask *_selfReference;
2222
}
2323

24-
static auto currentRequestId = std::atomic<NSUInteger>(0);
24+
static constinit auto currentRequestId = std::atomic<NSUInteger>(0);
2525

2626
- (instancetype)initWithRequest:(NSURLRequest *)request
2727
handler:(id<RCTURLRequestHandler>)handler

‎packages/react-native/React/Base/RCTUtils.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ UIDeviceOrientation RCTDeviceOrientation(void)
386386

387387
CGSize RCTScreenSize(void)
388388
{
389-
static std::atomic<CGSize> cachedSize;
389+
static constinit std::atomic<CGSize> cachedSize;
390390
__block CGSize size = cachedSize.load(std::memory_order_relaxed);
391391

392392
if (CGSizeEqualToSize(size, CGSizeZero)) {

‎packages/react-native/React/CoreModules/RCTDevSettings.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ - (void)_reloadWithDefaults:(NSDictionary *)defaultValues
127127
#if RCT_DEV_SETTINGS_ENABLE_PACKAGER_CONNECTION
128128
static RCTHandlerToken reloadToken;
129129
static RCTHandlerToken devMenuToken;
130-
static std::atomic<int> numInitializedModules{0};
130+
static constinit std::atomic<int> numInitializedModules{0};
131131
#endif
132132

133133
@interface RCTDevSettings () <RCTBridgeModule, RCTInvalidating, NativeDevSettingsSpec, RCTDevSettingsInspectable> {

‎packages/react-native/ReactAndroid/src/main/jni/first-party/fbgloginit/glog_init.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ lastResort(const char* tag, const char* msg, const char* arg = nullptr) {
112112
namespace facebook::gloginit {
113113

114114
void initialize(const char* tag) {
115-
static std::once_flag flag{};
115+
static constinit std::once_flag flag{};
116116
static auto failed = false;
117117

118118
std::call_once(flag, [tag] {

‎packages/react-native/ReactAndroid/src/main/jni/react/hermes/reactexecutor/OnLoad.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static void hermesFatalHandler(const std::string& reason) {
2525
__android_log_assert(nullptr, "Hermes", "%s", reason.c_str());
2626
}
2727

28-
static std::once_flag flag;
28+
static constinit std::once_flag flag;
2929

3030
static ::hermes::vm::RuntimeConfig makeRuntimeConfig(jlong heapSizeMB) {
3131
namespace vm = ::hermes::vm;

‎packages/react-native/ReactAndroid/src/main/jni/react/jni/JReactMarker.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const int kInstanceKey = 1;
1818
}
1919

2020
void JReactMarker::setLogPerfMarkerIfNeeded() {
21-
static std::once_flag flag{};
21+
static constinit std::once_flag flag{};
2222
std::call_once(flag, []() {
2323
ReactMarker::logTaggedMarkerImpl = JReactMarker::logPerfMarker;
2424
});

‎packages/react-native/ReactAndroid/src/main/jni/react/jni/SafeReleaseJniRef.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void ensureThreadDurationJNIEnvAttached() {
2020
// > exits, and call DetachCurrentThread() from there.
2121
// https://github.com/facebookincubator/fbjni/blob/7b7efda0d49b956acf1d3307510e3c73fc55b404/cxx/fbjni/detail/Environment.h#L101
2222
// https://developer.android.com/training/articles/perf-jni#threads
23-
static thread_local std::optional<jni::ThreadScope> threadScope;
23+
static constinit thread_local std::optional<jni::ThreadScope> threadScope;
2424

2525
if (jni::detail::currentOrNull() == nullptr) {
2626
threadScope.emplace();

‎packages/react-native/ReactCommon/cxxreact/TraceSection.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ inline os_log_t instrumentsLogHandle = nullptr;
168168

169169
static inline os_log_t getOrCreateInstrumentsLogHandle()
170170
{
171-
static std::once_flag flag{};
171+
static constinit std::once_flag flag{};
172172
std::call_once(flag, []() {
173173
if (!instrumentsLogHandle) {
174174
instrumentsLogHandle = os_log_create("dev.reactnative.instruments", OS_LOG_CATEGORY_DYNAMIC_TRACING);

‎packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<e54d2f7c5761d2bdb0edff1065896eb2>>
7+
* @generated SignedSource<<56e127471f63e47798ec619e6fa56738>>
88
*/
99

1010
/**
@@ -23,7 +23,7 @@ namespace facebook::react {
2323

2424
#pragma GCC diagnostic push
2525
#pragma GCC diagnostic ignored "-Wglobal-constructors"
26-
std::unique_ptr<ReactNativeFeatureFlagsAccessor> accessor_;
26+
constinit std::unique_ptr<ReactNativeFeatureFlagsAccessor> accessor_;
2727
#pragma GCC diagnostic pop
2828

2929
bool ReactNativeFeatureFlags::commonTestFlag() {

0 commit comments

Comments
 (0)