make yoga threadsafe (#852)
Summary: Continuing https://github.com/facebook/yoga/pull/791 nokia6686 is a former member of our team, so we are trying to pick up what he left and carry out the pull request. # Solution Improved from previous solution with jpap's suggestions. 2. Passing ```gDepth``` and ```gCurrentGenerationCount``` (renamed to **_depth_** and **_generationCount_** respectively) between function calls that stem from ```YGNodeCalculateLayout```. In ```YGNodeCalculateLayout```, pass ```depth``` as value 0, to indicate the root depth. Pull Request resolved: https://github.com/facebook/yoga/pull/852 Reviewed By: SidharthGuglani Differential Revision: D15537450 Pulled By: davidaurelio fbshipit-source-id: 338f51383591ba27702ebe759f6c47c2dede3530
This commit is contained in:
committed by
Facebook Github Bot
parent
f539d68049
commit
cf005df828
115
yoga/Yoga.cpp
115
yoga/Yoga.cpp
@@ -953,7 +953,9 @@ bool YGLayoutNodeInternal(
|
|||||||
const char* reason,
|
const char* reason,
|
||||||
const YGConfigRef config,
|
const YGConfigRef config,
|
||||||
YGMarkerLayoutData& layoutMarkerData,
|
YGMarkerLayoutData& layoutMarkerData,
|
||||||
void* const layoutContext);
|
void* const layoutContext,
|
||||||
|
const uint32_t depth,
|
||||||
|
const uint32_t generationCount);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
static void YGNodePrintInternal(
|
static void YGNodePrintInternal(
|
||||||
@@ -1197,7 +1199,9 @@ static void YGNodeComputeFlexBasisForChild(
|
|||||||
const YGDirection direction,
|
const YGDirection direction,
|
||||||
const YGConfigRef config,
|
const YGConfigRef config,
|
||||||
YGMarkerLayoutData& layoutMarkerData,
|
YGMarkerLayoutData& layoutMarkerData,
|
||||||
void* const layoutContext) {
|
void* const layoutContext,
|
||||||
|
const uint32_t depth,
|
||||||
|
const uint32_t generationCount) {
|
||||||
const YGFlexDirection mainAxis =
|
const YGFlexDirection mainAxis =
|
||||||
YGResolveFlexDirection(node->getStyle().flexDirection(), direction);
|
YGResolveFlexDirection(node->getStyle().flexDirection(), direction);
|
||||||
const bool isMainAxisRow = YGFlexDirectionIsRow(mainAxis);
|
const bool isMainAxisRow = YGFlexDirectionIsRow(mainAxis);
|
||||||
@@ -1220,8 +1224,7 @@ static void YGNodeComputeFlexBasisForChild(
|
|||||||
if (child->getLayout().computedFlexBasis.isUndefined() ||
|
if (child->getLayout().computedFlexBasis.isUndefined() ||
|
||||||
(YGConfigIsExperimentalFeatureEnabled(
|
(YGConfigIsExperimentalFeatureEnabled(
|
||||||
child->getConfig(), YGExperimentalFeatureWebFlexBasis) &&
|
child->getConfig(), YGExperimentalFeatureWebFlexBasis) &&
|
||||||
child->getLayout().computedFlexBasisGeneration !=
|
child->getLayout().computedFlexBasisGeneration != generationCount)) {
|
||||||
gCurrentGenerationCount)) {
|
|
||||||
const YGFloatOptional paddingAndBorder = YGFloatOptional(
|
const YGFloatOptional paddingAndBorder = YGFloatOptional(
|
||||||
YGNodePaddingAndBorderForAxis(child, mainAxis, ownerWidth));
|
YGNodePaddingAndBorderForAxis(child, mainAxis, ownerWidth));
|
||||||
child->setLayoutComputedFlexBasis(
|
child->setLayoutComputedFlexBasis(
|
||||||
@@ -1372,13 +1375,15 @@ static void YGNodeComputeFlexBasisForChild(
|
|||||||
"measure",
|
"measure",
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
|
|
||||||
child->setLayoutComputedFlexBasis(YGFloatOptional(YGFloatMax(
|
child->setLayoutComputedFlexBasis(YGFloatOptional(YGFloatMax(
|
||||||
child->getLayout().measuredDimensions[dim[mainAxis]],
|
child->getLayout().measuredDimensions[dim[mainAxis]],
|
||||||
YGNodePaddingAndBorderForAxis(child, mainAxis, ownerWidth))));
|
YGNodePaddingAndBorderForAxis(child, mainAxis, ownerWidth))));
|
||||||
}
|
}
|
||||||
child->setLayoutComputedFlexBasisGeneration(gCurrentGenerationCount);
|
child->setLayoutComputedFlexBasisGeneration(generationCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void YGNodeAbsoluteLayoutChild(
|
static void YGNodeAbsoluteLayoutChild(
|
||||||
@@ -1390,7 +1395,9 @@ static void YGNodeAbsoluteLayoutChild(
|
|||||||
const YGDirection direction,
|
const YGDirection direction,
|
||||||
const YGConfigRef config,
|
const YGConfigRef config,
|
||||||
YGMarkerLayoutData& layoutMarkerData,
|
YGMarkerLayoutData& layoutMarkerData,
|
||||||
void* const layoutContext) {
|
void* const layoutContext,
|
||||||
|
const uint32_t depth,
|
||||||
|
const uint32_t generationCount) {
|
||||||
const YGFlexDirection mainAxis =
|
const YGFlexDirection mainAxis =
|
||||||
YGResolveFlexDirection(node->getStyle().flexDirection(), direction);
|
YGResolveFlexDirection(node->getStyle().flexDirection(), direction);
|
||||||
const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, direction);
|
const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, direction);
|
||||||
@@ -1496,7 +1503,9 @@ static void YGNodeAbsoluteLayoutChild(
|
|||||||
"abs-measure",
|
"abs-measure",
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
childWidth = child->getLayout().measuredDimensions[YGDimensionWidth] +
|
childWidth = child->getLayout().measuredDimensions[YGDimensionWidth] +
|
||||||
child->getMarginForAxis(YGFlexDirectionRow, width).unwrap();
|
child->getMarginForAxis(YGFlexDirectionRow, width).unwrap();
|
||||||
childHeight = child->getLayout().measuredDimensions[YGDimensionHeight] +
|
childHeight = child->getLayout().measuredDimensions[YGDimensionHeight] +
|
||||||
@@ -1516,7 +1525,9 @@ static void YGNodeAbsoluteLayoutChild(
|
|||||||
"abs-layout",
|
"abs-layout",
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
|
|
||||||
if (child->isTrailingPosDefined(mainAxis) &&
|
if (child->isTrailingPosDefined(mainAxis) &&
|
||||||
!child->isLeadingPositionDefined(mainAxis)) {
|
!child->isLeadingPositionDefined(mainAxis)) {
|
||||||
@@ -1844,7 +1855,9 @@ static float YGNodeComputeFlexBasisForChildren(
|
|||||||
const YGConfigRef config,
|
const YGConfigRef config,
|
||||||
bool performLayout,
|
bool performLayout,
|
||||||
YGMarkerLayoutData& layoutMarkerData,
|
YGMarkerLayoutData& layoutMarkerData,
|
||||||
void* const layoutContext) {
|
void* const layoutContext,
|
||||||
|
const uint32_t depth,
|
||||||
|
const uint32_t generationCount) {
|
||||||
float totalOuterFlexBasis = 0.0f;
|
float totalOuterFlexBasis = 0.0f;
|
||||||
YGNodeRef singleFlexChild = nullptr;
|
YGNodeRef singleFlexChild = nullptr;
|
||||||
YGVector children = node->getChildren();
|
YGVector children = node->getChildren();
|
||||||
@@ -1895,7 +1908,7 @@ static float YGNodeComputeFlexBasisForChildren(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (child == singleFlexChild) {
|
if (child == singleFlexChild) {
|
||||||
child->setLayoutComputedFlexBasisGeneration(gCurrentGenerationCount);
|
child->setLayoutComputedFlexBasisGeneration(generationCount);
|
||||||
child->setLayoutComputedFlexBasis(YGFloatOptional(0));
|
child->setLayoutComputedFlexBasis(YGFloatOptional(0));
|
||||||
} else {
|
} else {
|
||||||
YGNodeComputeFlexBasisForChild(
|
YGNodeComputeFlexBasisForChild(
|
||||||
@@ -1910,7 +1923,9 @@ static float YGNodeComputeFlexBasisForChildren(
|
|||||||
direction,
|
direction,
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
totalOuterFlexBasis +=
|
totalOuterFlexBasis +=
|
||||||
@@ -2024,7 +2039,9 @@ static float YGDistributeFreeSpaceSecondPass(
|
|||||||
const bool performLayout,
|
const bool performLayout,
|
||||||
const YGConfigRef config,
|
const YGConfigRef config,
|
||||||
YGMarkerLayoutData& layoutMarkerData,
|
YGMarkerLayoutData& layoutMarkerData,
|
||||||
void* const layoutContext) {
|
void* const layoutContext,
|
||||||
|
const uint32_t depth,
|
||||||
|
const uint32_t generationCount) {
|
||||||
float childFlexBasis = 0;
|
float childFlexBasis = 0;
|
||||||
float flexShrinkScaledFactor = 0;
|
float flexShrinkScaledFactor = 0;
|
||||||
float flexGrowFactor = 0;
|
float flexGrowFactor = 0;
|
||||||
@@ -2190,7 +2207,9 @@ static float YGDistributeFreeSpaceSecondPass(
|
|||||||
"flex",
|
"flex",
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
node->setLayoutHadOverflow(
|
node->setLayoutHadOverflow(
|
||||||
node->getLayout().hadOverflow |
|
node->getLayout().hadOverflow |
|
||||||
currentRelativeChild->getLayout().hadOverflow);
|
currentRelativeChild->getLayout().hadOverflow);
|
||||||
@@ -2321,7 +2340,9 @@ static void YGResolveFlexibleLength(
|
|||||||
const bool performLayout,
|
const bool performLayout,
|
||||||
const YGConfigRef config,
|
const YGConfigRef config,
|
||||||
YGMarkerLayoutData& layoutMarkerData,
|
YGMarkerLayoutData& layoutMarkerData,
|
||||||
void* const layoutContext) {
|
void* const layoutContext,
|
||||||
|
const uint32_t depth,
|
||||||
|
const uint32_t generationCount) {
|
||||||
const float originalFreeSpace = collectedFlexItemsValues.remainingFreeSpace;
|
const float originalFreeSpace = collectedFlexItemsValues.remainingFreeSpace;
|
||||||
// First pass: detect the flex items whose min/max constraints trigger
|
// First pass: detect the flex items whose min/max constraints trigger
|
||||||
YGDistributeFreeSpaceFirstPass(
|
YGDistributeFreeSpaceFirstPass(
|
||||||
@@ -2347,7 +2368,9 @@ static void YGResolveFlexibleLength(
|
|||||||
performLayout,
|
performLayout,
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
|
|
||||||
collectedFlexItemsValues.remainingFreeSpace =
|
collectedFlexItemsValues.remainingFreeSpace =
|
||||||
originalFreeSpace - distributedFreeSpace;
|
originalFreeSpace - distributedFreeSpace;
|
||||||
@@ -2647,7 +2670,9 @@ static void YGNodelayoutImpl(
|
|||||||
const bool performLayout,
|
const bool performLayout,
|
||||||
const YGConfigRef config,
|
const YGConfigRef config,
|
||||||
YGMarkerLayoutData& layoutMarkerData,
|
YGMarkerLayoutData& layoutMarkerData,
|
||||||
void* const layoutContext) {
|
void* const layoutContext,
|
||||||
|
const uint32_t depth,
|
||||||
|
const uint32_t generationCount) {
|
||||||
YGAssertWithNode(
|
YGAssertWithNode(
|
||||||
node,
|
node,
|
||||||
YGFloatIsUndefined(availableWidth)
|
YGFloatIsUndefined(availableWidth)
|
||||||
@@ -2828,7 +2853,9 @@ static void YGNodelayoutImpl(
|
|||||||
config,
|
config,
|
||||||
performLayout,
|
performLayout,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
|
|
||||||
const bool flexBasisOverflows = measureModeMainDim == YGMeasureModeUndefined
|
const bool flexBasisOverflows = measureModeMainDim == YGMeasureModeUndefined
|
||||||
? false
|
? false
|
||||||
@@ -2936,7 +2963,9 @@ static void YGNodelayoutImpl(
|
|||||||
performLayout,
|
performLayout,
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
node->setLayoutHadOverflow(
|
node->setLayoutHadOverflow(
|
||||||
@@ -3110,7 +3139,9 @@ static void YGNodelayoutImpl(
|
|||||||
"stretch",
|
"stretch",
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const float remainingCrossDim = containerCrossAxis -
|
const float remainingCrossDim = containerCrossAxis -
|
||||||
@@ -3318,7 +3349,9 @@ static void YGNodelayoutImpl(
|
|||||||
"multiline-stretch",
|
"multiline-stretch",
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -3458,7 +3491,9 @@ static void YGNodelayoutImpl(
|
|||||||
direction,
|
direction,
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
// STEP 11: SETTING TRAILING POSITIONS FOR CHILDREN
|
// STEP 11: SETTING TRAILING POSITIONS FOR CHILDREN
|
||||||
@@ -3486,7 +3521,6 @@ static void YGNodelayoutImpl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t gDepth = 0;
|
|
||||||
bool gPrintChanges = false;
|
bool gPrintChanges = false;
|
||||||
bool gPrintSkips = false;
|
bool gPrintSkips = false;
|
||||||
|
|
||||||
@@ -3692,13 +3726,15 @@ bool YGLayoutNodeInternal(
|
|||||||
const char* reason,
|
const char* reason,
|
||||||
const YGConfigRef config,
|
const YGConfigRef config,
|
||||||
YGMarkerLayoutData& layoutMarkerData,
|
YGMarkerLayoutData& layoutMarkerData,
|
||||||
void* const layoutContext) {
|
void* const layoutContext,
|
||||||
|
uint32_t depth,
|
||||||
|
const uint32_t generationCount) {
|
||||||
YGLayout* layout = &node->getLayout();
|
YGLayout* layout = &node->getLayout();
|
||||||
|
|
||||||
gDepth++;
|
depth++;
|
||||||
|
|
||||||
const bool needToVisitNode =
|
const bool needToVisitNode =
|
||||||
(node->isDirty() && layout->generationCount != gCurrentGenerationCount) ||
|
(node->isDirty() && layout->generationCount != generationCount) ||
|
||||||
layout->lastOwnerDirection != ownerDirection;
|
layout->lastOwnerDirection != ownerDirection;
|
||||||
|
|
||||||
if (needToVisitNode) {
|
if (needToVisitNode) {
|
||||||
@@ -3800,8 +3836,8 @@ bool YGLayoutNodeInternal(
|
|||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
nullptr,
|
nullptr,
|
||||||
"%s%d.{[skipped] ",
|
"%s%d.{[skipped] ",
|
||||||
YGSpacer(gDepth),
|
YGSpacer(depth),
|
||||||
gDepth);
|
depth);
|
||||||
node->print(layoutContext);
|
node->print(layoutContext);
|
||||||
Log::log(
|
Log::log(
|
||||||
node,
|
node,
|
||||||
@@ -3823,8 +3859,8 @@ bool YGLayoutNodeInternal(
|
|||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
nullptr,
|
nullptr,
|
||||||
"%s%d.{%s",
|
"%s%d.{%s",
|
||||||
YGSpacer(gDepth),
|
YGSpacer(depth),
|
||||||
gDepth,
|
depth,
|
||||||
needToVisitNode ? "*" : "");
|
needToVisitNode ? "*" : "");
|
||||||
node->print(layoutContext);
|
node->print(layoutContext);
|
||||||
Log::log(
|
Log::log(
|
||||||
@@ -3851,7 +3887,9 @@ bool YGLayoutNodeInternal(
|
|||||||
performLayout,
|
performLayout,
|
||||||
config,
|
config,
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext);
|
layoutContext,
|
||||||
|
depth,
|
||||||
|
generationCount);
|
||||||
|
|
||||||
if (gPrintChanges) {
|
if (gPrintChanges) {
|
||||||
Log::log(
|
Log::log(
|
||||||
@@ -3859,8 +3897,8 @@ bool YGLayoutNodeInternal(
|
|||||||
YGLogLevelVerbose,
|
YGLogLevelVerbose,
|
||||||
nullptr,
|
nullptr,
|
||||||
"%s%d.}%s",
|
"%s%d.}%s",
|
||||||
YGSpacer(gDepth),
|
YGSpacer(depth),
|
||||||
gDepth,
|
depth,
|
||||||
needToVisitNode ? "*" : "");
|
needToVisitNode ? "*" : "");
|
||||||
node->print(layoutContext);
|
node->print(layoutContext);
|
||||||
Log::log(
|
Log::log(
|
||||||
@@ -3924,8 +3962,7 @@ bool YGLayoutNodeInternal(
|
|||||||
node->setDirty(false);
|
node->setDirty(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
gDepth--;
|
layout->generationCount = generationCount;
|
||||||
layout->generationCount = gCurrentGenerationCount;
|
|
||||||
|
|
||||||
#ifdef YG_ENABLE_EVENTS
|
#ifdef YG_ENABLE_EVENTS
|
||||||
LayoutType layoutType;
|
LayoutType layoutType;
|
||||||
@@ -4110,7 +4147,9 @@ void YGNodeCalculateLayoutWithContext(
|
|||||||
"initial",
|
"initial",
|
||||||
node->getConfig(),
|
node->getConfig(),
|
||||||
marker.data,
|
marker.data,
|
||||||
layoutContext)) {
|
layoutContext,
|
||||||
|
0, // tree root
|
||||||
|
gCurrentGenerationCount)) {
|
||||||
node->setPosition(
|
node->setPosition(
|
||||||
node->getLayout().direction, ownerWidth, ownerHeight, ownerWidth);
|
node->getLayout().direction, ownerWidth, ownerHeight, ownerWidth);
|
||||||
YGRoundToPixelGrid(node, node->getConfig()->pointScaleFactor, 0.0f, 0.0f);
|
YGRoundToPixelGrid(node, node->getConfig()->pointScaleFactor, 0.0f, 0.0f);
|
||||||
@@ -4162,7 +4201,9 @@ void YGNodeCalculateLayoutWithContext(
|
|||||||
"initial",
|
"initial",
|
||||||
nodeWithoutLegacyFlag->getConfig(),
|
nodeWithoutLegacyFlag->getConfig(),
|
||||||
layoutMarkerData,
|
layoutMarkerData,
|
||||||
layoutContext)) {
|
layoutContext,
|
||||||
|
0, // tree root
|
||||||
|
gCurrentGenerationCount)) {
|
||||||
nodeWithoutLegacyFlag->setPosition(
|
nodeWithoutLegacyFlag->setPosition(
|
||||||
nodeWithoutLegacyFlag->getLayout().direction,
|
nodeWithoutLegacyFlag->getLayout().direction,
|
||||||
ownerWidth,
|
ownerWidth,
|
||||||
|
Reference in New Issue
Block a user