Skip to content

Commit

Permalink
Implement synchronization with static vars on class instead of static…
Browse files Browse the repository at this point in the history
… getter/setter
  • Loading branch information
hakonk committed Jul 20, 2024
1 parent 6599883 commit 83a2a3c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/react-native/React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 () <RCTModuleDataCallInvokerProvider>
Expand Down
4 changes: 3 additions & 1 deletion packages/react-native/ReactCommon/cxxreact/ReactMarker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
31 changes: 17 additions & 14 deletions packages/react-native/ReactCommon/cxxreact/ReactMarker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void JSINativeModules::reset() {
std::optional<Object> 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());
Expand Down

0 comments on commit 83a2a3c

Please sign in to comment.