Remove ThreadStorage class in favour of thread_local

Summary:
#changelog: [internal]

When I built ThreadStorage I didn't know about existence of `thread_local` keyword. Because it achieves the same goal, using built in c++ features is preferred over building our own.

Reviewed By: JoshuaGross, shergin

Differential Revision: D24380680

fbshipit-source-id: e961fc34c6d3f085fc9b918b20bb4827de0d5624
This commit is contained in:
Samuel Susla
2020-10-19 01:24:05 -07:00
committed by Facebook GitHub Bot
parent 500bdf74d2
commit 1ca5ccc2ab
2 changed files with 4 additions and 62 deletions
@@ -12,7 +12,6 @@
#include <react/renderer/core/LayoutContext.h>
#include <react/renderer/debug/DebugStringConvertibleItem.h>
#include <react/renderer/debug/SystraceSection.h>
#include <react/utils/ThreadStorage.h>
#include <yoga/Yoga.h>
#include <algorithm>
#include <limits>
@@ -21,6 +20,8 @@
namespace facebook {
namespace react {
thread_local LayoutContext threadLocalLayoutContext;
static void applyLayoutConstraints(
YGStyle &yogaStyle,
LayoutConstraints const &layoutConstraints) {
@@ -332,7 +333,7 @@ void YogaLayoutableShadowNode::layoutTree(
applyLayoutConstraints(yogaNode_.getStyle(), layoutConstraints);
ThreadStorage<LayoutContext>::getInstance().set(layoutContext);
threadLocalLayoutContext = layoutContext;
if (layoutContext.swapLeftAndRightInRTL) {
swapLeftAndRightInTree(*this);
@@ -492,10 +493,8 @@ YGSize YogaLayoutableShadowNode::yogaNodeMeasureCallbackConnector(
break;
}
auto layoutContext = ThreadStorage<LayoutContext>::getInstance().get();
auto size = shadowNodeRawPtr->measureContent(
layoutContext.value_or(LayoutContext{}), {minimumSize, maximumSize});
threadLocalLayoutContext, {minimumSize, maximumSize});
return YGSize{yogaFloatFromFloat(size.width),
yogaFloatFromFloat(size.height)};
-57
View File
@@ -1,57 +0,0 @@
/*
* 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.
*/
#pragma once
#include <better/map.h>
#include <better/optional.h>
#include <mutex>
#include <thread>
namespace facebook {
namespace react {
/*
* ThreadStorage is a class designed to store data for specific thread.
* When data is inserted from thread 1, it can only be retrieved from thread 1.
*/
template <typename DataT>
class ThreadStorage {
/*
* Private default constructor. This class has to be used as a singleton.
*/
ThreadStorage() = default;
public:
static ThreadStorage<DataT> &getInstance() {
static ThreadStorage<DataT> threadStorage;
return threadStorage;
}
better::optional<DataT> get() const {
std::lock_guard<std::mutex> lock(mutex_);
auto iterator = storage_.find(std::this_thread::get_id());
if (iterator != storage_.end()) {
return iterator->second;
} else {
return {};
}
}
void set(DataT data) {
std::lock_guard<std::mutex> lock(mutex_);
storage_[std::this_thread::get_id()] = data;
}
private:
mutable std::mutex mutex_;
better::map<std::thread::id, DataT> storage_;
};
} // namespace react
} // namespace facebook