From ed2c575d842389ed15f55aa4d6c95b601d96d7fb Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Thu, 9 Sep 2021 13:42:50 +0200 Subject: [PATCH] Fix problem with user store --- Common/cpp/Tools/JSIStoreValueUser.cpp | 40 ++++++++++---------- Common/cpp/headers/Tools/JSIStoreValueUser.h | 11 ++++-- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Common/cpp/Tools/JSIStoreValueUser.cpp b/Common/cpp/Tools/JSIStoreValueUser.cpp index fb766f1dfdd2..ed79ecc7c672 100644 --- a/Common/cpp/Tools/JSIStoreValueUser.cpp +++ b/Common/cpp/Tools/JSIStoreValueUser.cpp @@ -1,58 +1,56 @@ #ifdef ONANDROID #include #endif -#include #include "JSIStoreValueUser.h" namespace reanimated { -std::atomic StoreUser::ctr; -std::recursive_mutex StoreUser::storeMutex; -std::unordered_map>> StoreUser::store; +std::shared_ptr StoreUser::staticStoreUserData = std::make_shared(); std::weak_ptr StoreUser::getWeakRef(jsi::Runtime &rt) { - const std::lock_guard lock(storeMutex); - if (StoreUser::store.count(identifier) == 0) { - StoreUser::store[identifier] = std::vector>(); + const std::lock_guard lock(storeUserData->storeMutex); + if (storeUserData->store.count(identifier) == 0) { + storeUserData->store[identifier] = std::vector>(); } std::shared_ptr sv = std::make_shared(rt, jsi::Value::undefined()); - StoreUser::store[identifier].push_back(sv); + storeUserData->store[identifier].push_back(sv); return sv; } StoreUser::StoreUser(std::shared_ptr s): scheduler(s) { - identifier = StoreUser::ctr++; + storeUserData = StoreUser::staticStoreUserData; + identifier = storeUserData->ctr++; } StoreUser::~StoreUser() { int id = identifier; std::shared_ptr strongScheduler = scheduler.lock(); if (strongScheduler != nullptr) { + std::shared_ptr sud = storeUserData; #ifdef ONANDROID jni::ThreadScope::WithClassLoader([&] { - strongScheduler->scheduleOnUI([id]() { - const std::lock_guard lock(storeMutex); - if (StoreUser::store.count(id) > 0) { - StoreUser::store.erase(id); + strongScheduler->scheduleOnUI([id, sud]() { + const std::lock_guard lock(sud->storeMutex); + if (sud->store.count(id) > 0) { + sud->store.erase(id); } }); }); - #else - strongScheduler->scheduleOnUI([id]() { - const std::lock_guard lock(storeMutex); - if (StoreUser::store.count(id) > 0) { - StoreUser::store.erase(id); + #else + strongScheduler->scheduleOnUI([id, sud]() { + const std::lock_guard lock(sud->storeMutex); + if (sud->store.count(id) > 0) { + sud->store.erase(id); } }); #endif } } - void StoreUser::clearStore() { - const std::lock_guard lock(storeMutex); - StoreUser::store.clear(); + const std::lock_guard lock(StoreUser::staticStoreUserData->storeMutex); + StoreUser::staticStoreUserData->store.clear(); } } diff --git a/Common/cpp/headers/Tools/JSIStoreValueUser.h b/Common/cpp/headers/Tools/JSIStoreValueUser.h index dc0d06ca81de..9fa42c2cbea1 100644 --- a/Common/cpp/headers/Tools/JSIStoreValueUser.h +++ b/Common/cpp/headers/Tools/JSIStoreValueUser.h @@ -13,11 +13,16 @@ using namespace facebook; namespace reanimated { +struct StaticStoreUser { + std::atomic ctr; + std::unordered_map>> store; + std::recursive_mutex storeMutex; +}; + class StoreUser { int identifier = 0; - static std::atomic ctr; - static std::unordered_map>> store; - static std::recursive_mutex storeMutex; + static std::shared_ptr staticStoreUserData; + std::shared_ptr storeUserData; std::weak_ptr scheduler; public: