Files
react-native/ReactCommon/react/renderer/mounting/ShadowTreeRegistry.cpp
T
Valentin Shergin 653d8540c6 Fabric: Fixing a race condition in Scheduler::stopSurface()
Summary:
One of the operations we do in `Scheduler::stopSurface()` is committing an empty tree to free up `ShadowNode` objects and "disable" `EventEmitter`s associated with them. Before this change, we had a gap in time between a moment when we commit an empty tree and remove the tree from the registry. During this time gap, JavaScript (or native, actually) can commit another tree and mount another new state on the screen. To prevent this, we remove the tree from the registry first and only then commit an empty tree to a uniquely owned tree.
Note that the deleted comment says that we actually have to have a tree in the registry for committing an empty tree, I don't think it's true now.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D23667882

fbshipit-source-id: 387052e9f3e78e7d4446f36baed50f9caa831133
2020-09-12 15:41:34 -07:00

68 lines
1.6 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ShadowTreeRegistry.h"
namespace facebook {
namespace react {
ShadowTreeRegistry::~ShadowTreeRegistry() {
assert(
registry_.empty() && "Deallocation of non-empty `ShadowTreeRegistry`.");
}
void ShadowTreeRegistry::add(std::unique_ptr<ShadowTree> &&shadowTree) const {
std::unique_lock<better::shared_mutex> lock(mutex_);
registry_.emplace(shadowTree->getSurfaceId(), std::move(shadowTree));
}
std::unique_ptr<ShadowTree> ShadowTreeRegistry::remove(
SurfaceId surfaceId) const {
std::unique_lock<better::shared_mutex> lock(mutex_);
auto iterator = registry_.find(surfaceId);
if (iterator == registry_.end()) {
return {};
}
auto shadowTree = std::unique_ptr<ShadowTree>(iterator->second.release());
registry_.erase(iterator);
return shadowTree;
}
bool ShadowTreeRegistry::visit(
SurfaceId surfaceId,
std::function<void(const ShadowTree &shadowTree)> callback) const {
std::shared_lock<better::shared_mutex> lock(mutex_);
auto iterator = registry_.find(surfaceId);
if (iterator == registry_.end()) {
return false;
}
callback(*iterator->second);
return true;
}
void ShadowTreeRegistry::enumerate(
std::function<void(const ShadowTree &shadowTree, bool &stop)> callback)
const {
std::shared_lock<better::shared_mutex> lock(mutex_);
bool stop = false;
for (auto const &pair : registry_) {
callback(*pair.second, stop);
if (stop) {
break;
}
}
}
} // namespace react
} // namespace facebook