From 41b63719409f373a4b4f0cfc1de7d3ff95c74fd4 Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Tue, 12 Mar 2024 13:18:25 -0700 Subject: [PATCH] Synchronize access to LongLivedObjectCollection instances (#43450) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43450 Changelog: [Internal] This is just to make sure that all instances of `LongLivedObjectCollection` map can be safely accessed by multiple threads. Reviewed By: RSNara Differential Revision: D54801015 fbshipit-source-id: e0b15bfbeac9ce3a1051f83a59c5513a90ba2a4b --- .../ReactCommon/react/bridging/LongLivedObject.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp b/packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp index 64a93c1b7ec508..9c6d7c38d86fb2 100644 --- a/packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp +++ b/packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp @@ -16,6 +16,9 @@ LongLivedObjectCollection& LongLivedObjectCollection::get( jsi::Runtime& runtime) { static std::unordered_map> instances; + static std::mutex instancesMutex; + + std::scoped_lock lock(instancesMutex); void* key = static_cast(&runtime); auto entry = instances.find(key); if (entry == instances.end()) {