Avoid acquiring multiple locks in async node creation (#54359)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/54359

## Changelog:

[Internal] [Changed] [react-native][cxx animated] Avoid acquiring multiple locks in async node creation

Reviewed By: christophpurrer

Differential Revision: D85969336

fbshipit-source-id: bfc05d5b8b36318aec21e1ad966e21108978afd0
This commit is contained in:
Zeya Peng authored and meta-codesync[bot] committed 2025-11-01 14:10:55 -07:00
1 parent 693e9628e8
commit d8e6a985a3
1 file changed
+13 -6
@@ -1044,13 +1044,20 @@ void NativeAnimatedNodesManager::onRender() {
{
// Flush async created animated nodes
std::lock_guard<std::mutex> lock(animatedNodesCreatedAsyncMutex_);
std::lock_guard<std::mutex> lockCreateAsync(connectedAnimatedNodesMutex_);
for (auto& [tag, node] : animatedNodesCreatedAsync_) {
animatedNodes_.insert({tag, std::move(node)});
updatedNodeTags_.insert(tag);
std::unordered_map<Tag, std::unique_ptr<AnimatedNode>>
animatedNodesCreatedAsync;
{
std::lock_guard<std::mutex> lock(animatedNodesCreatedAsyncMutex_);
std::swap(animatedNodesCreatedAsync, animatedNodesCreatedAsync_);
}
if (!animatedNodesCreatedAsync.empty()) {
std::lock_guard<std::mutex> lock(connectedAnimatedNodesMutex_);
for (auto& [tag, node] : animatedNodesCreatedAsync) {
animatedNodes_.insert({tag, std::move(node)});
updatedNodeTags_.insert(tag);
}
}
animatedNodesCreatedAsync_.clear();
}
// Run operations scheduled from AnimatedModule