From 83a2a3c9b4e5ea588a6cc3a9281ad385a388b84a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Knutzen?= <2263015+hakonk@users.noreply.github.com> Date: Sat, 20 Jul 2024 06:38:23 +0200 Subject: [PATCH] Implement synchronization with static vars on class instead of static getter/setter --- .../React/CxxBridge/RCTCxxBridge.mm | 2 +- .../ReactCommon/cxxreact/ReactMarker.cpp | 4 ++- .../ReactCommon/cxxreact/ReactMarker.h | 31 ++++++++++--------- .../jsiexecutor/jsireact/JSIExecutor.cpp | 5 ++- .../jsiexecutor/jsireact/JSINativeModules.cpp | 2 +- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/packages/react-native/React/CxxBridge/RCTCxxBridge.mm b/packages/react-native/React/CxxBridge/RCTCxxBridge.mm index 2ce5c7d41fec5d..1b6436bf34a428 100644 --- a/packages/react-native/React/CxxBridge/RCTCxxBridge.mm +++ b/packages/react-native/React/CxxBridge/RCTCxxBridge.mm @@ -167,7 +167,7 @@ static void registerPerformanceLoggerHooks(RCTPerformanceLogger *performanceLogg const ReactMarker::ReactMarkerId markerId, const char *tag) { mapReactMarkerToPerformanceLogger(markerId, weakPerformanceLogger, tag); }; - ReactMarker::setLogTaggedMarkerImpl(newMarker); + ReactMarker::LogTaggedMarkerWrapper::setLogTaggedMarkerImpl(newMarker); } @interface RCTCxxBridge () diff --git a/packages/react-native/ReactCommon/cxxreact/ReactMarker.cpp b/packages/react-native/ReactCommon/cxxreact/ReactMarker.cpp index 8a0b119c24461c..35facb977e5846 100644 --- a/packages/react-native/ReactCommon/cxxreact/ReactMarker.cpp +++ b/packages/react-native/ReactCommon/cxxreact/ReactMarker.cpp @@ -17,6 +17,8 @@ namespace ReactMarker { #endif LogTaggedMarker logTaggedMarkerBridgelessImpl = nullptr; +LogTaggedMarker LogTaggedMarkerWrapper::logTaggedMarkerImpl = nullptr; +std::shared_mutex LogTaggedMarkerWrapper::logTaggedMarkerImplMutex; #if __clang__ #pragma clang diagnostic pop @@ -27,7 +29,7 @@ void logMarker(const ReactMarkerId markerId) { } void logTaggedMarker(const ReactMarkerId markerId, const char* tag) { - logTaggedMarkerImpl(markerId, tag); + LogTaggedMarkerWrapper::getLogTaggedMarkerImpl()(markerId, tag); } void logMarkerBridgeless(const ReactMarkerId markerId) { diff --git a/packages/react-native/ReactCommon/cxxreact/ReactMarker.h b/packages/react-native/ReactCommon/cxxreact/ReactMarker.h index 3fb23fe149dee6..71dcc2ba5d076a 100644 --- a/packages/react-native/ReactCommon/cxxreact/ReactMarker.h +++ b/packages/react-native/ReactCommon/cxxreact/ReactMarker.h @@ -52,20 +52,23 @@ typedef void (*LogTaggedMarkerBridgeless)(const ReactMarkerId, const char* tag); #define RN_EXPORT __attribute__((visibility("default"))) #endif -namespace { - std::shared_mutex logTaggedMarkerImplMutex; - LogTaggedMarker logTaggedMarkerImpl = nullptr; -} - -extern RN_EXPORT inline LogTaggedMarker getLogTaggedMarkerImpl(void) { - std::shared_lock lock(logTaggedMarkerImplMutex); - return logTaggedMarkerImpl; -} - -extern RN_EXPORT inline void setLogTaggedMarkerImpl(LogTaggedMarker marker) { - std::unique_lock lock(logTaggedMarkerImplMutex); - logTaggedMarkerImpl = marker; -} +class RN_EXPORT LogTaggedMarkerWrapper { +public: + static inline LogTaggedMarker getLogTaggedMarkerImpl(void) { + std::shared_lock lock(logTaggedMarkerImplMutex); + return logTaggedMarkerImpl; + } + + static inline void setLogTaggedMarkerImpl(LogTaggedMarker marker) { + std::unique_lock lock(logTaggedMarkerImplMutex); + logTaggedMarkerImpl = marker; + } + +private: + LogTaggedMarkerWrapper() = delete; + static std::shared_mutex logTaggedMarkerImplMutex; + static LogTaggedMarker logTaggedMarkerImpl; +}; extern RN_EXPORT LogTaggedMarker logTaggedMarkerBridgelessImpl; diff --git a/packages/react-native/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp b/packages/react-native/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp index 6e7693200a24ed..2f4c17b946367f 100644 --- a/packages/react-native/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp +++ b/packages/react-native/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp @@ -139,8 +139,7 @@ void JSIExecutor::initializeRuntime() { if (runtimeInstaller_) { runtimeInstaller_(*runtime_); } - - bool hasLogger(ReactMarker::getLogTaggedMarkerImpl()); + bool hasLogger(ReactMarker::LogTaggedMarkerWrapper::getLogTaggedMarkerImpl()); if (hasLogger) { ReactMarker::logMarker(ReactMarker::CREATE_REACT_CONTEXT_STOP); } @@ -151,7 +150,7 @@ void JSIExecutor::loadBundle( std::string sourceURL) { SystraceSection s("JSIExecutor::loadBundle"); - bool hasLogger(ReactMarker::getLogTaggedMarkerImpl()); + bool hasLogger(ReactMarker::LogTaggedMarkerWrapper::getLogTaggedMarkerImpl()); std::string scriptName = simpleBasename(sourceURL); if (hasLogger) { ReactMarker::logTaggedMarker( diff --git a/packages/react-native/ReactCommon/jsiexecutor/jsireact/JSINativeModules.cpp b/packages/react-native/ReactCommon/jsiexecutor/jsireact/JSINativeModules.cpp index 62c86a9e0d8eea..ee1e42907c6000 100644 --- a/packages/react-native/ReactCommon/jsiexecutor/jsireact/JSINativeModules.cpp +++ b/packages/react-native/ReactCommon/jsiexecutor/jsireact/JSINativeModules.cpp @@ -67,7 +67,7 @@ void JSINativeModules::reset() { std::optional JSINativeModules::createModule( Runtime& rt, const std::string& name) { - bool hasLogger(ReactMarker::getLogTaggedMarkerImpl()); + bool hasLogger(ReactMarker::LogTaggedMarkerWrapper::getLogTaggedMarkerImpl()); if (hasLogger) { ReactMarker::logTaggedMarker( ReactMarker::NATIVE_MODULE_SETUP_START, name.c_str());