From 7a0cd9ad595a52029cfaffe9f1bb036b8cf8e5f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Wed, 18 Nov 2026 12:24:47 +0100 Subject: [PATCH] Make Node::owner_ a relaxed atomic pointer A Yoga node shared between two trees can have its owner read by a layout pass on one thread while a clone of its previous owner on another thread rewrites it. owner_ was a plain pointer, so that is a data race. Loads and stores of the owner now use std::atomic with relaxed ordering, through a small copyable wrapper so Node keeps its defaulted copy operations. --- yoga/node/Node.cpp | 14 ++++++++------ yoga/node/Node.h | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/yoga/node/Node.cpp b/yoga/node/Node.cpp index 2cbe841625..a5eef355cd 100644 --- a/yoga/node/Node.cpp +++ b/yoga/node/Node.cpp @@ -318,7 +318,7 @@ void Node::setPosition( /* Root nodes should always be laid out as LTR, so we don't return negative * values. */ const Direction directionRespectingRoot = - owner_ != nullptr ? direction : Direction::LTR; + getOwner() != nullptr ? direction : Direction::LTR; const FlexDirection mainAxis = yoga::resolveDirection(style_.flexDirection(), directionRespectingRoot); const FlexDirection crossAxis = @@ -458,15 +458,15 @@ void Node::markDirtyAndPropagate() { if (!isDirty_) { setDirty(true); setLayoutComputedFlexBasis(FloatOptional()); - if (owner_ != nullptr) { - owner_->markDirtyAndPropagate(); + if (Node* owner = getOwner()) { + owner->markDirtyAndPropagate(); } } } float Node::resolveFlexGrow() const { // Root nodes flexGrow should always be 0 - if (owner_ == nullptr) { + if (getOwner() == nullptr) { return 0.0; } if (style_.flexGrow().isDefined()) { @@ -479,7 +479,7 @@ float Node::resolveFlexGrow() const { } float Node::resolveFlexShrink() const { - if (owner_ == nullptr) { + if (getOwner() == nullptr) { return 0.0; } if (style_.flexShrink().isDefined()) { @@ -505,7 +505,9 @@ void Node::reset() { children_.empty(), "Cannot reset a node which still has children attached"); yoga::assertFatalWithNode( - this, owner_ == nullptr, "Cannot reset a node still attached to a owner"); + this, + getOwner() == nullptr, + "Cannot reset a node still attached to a owner"); *this = Node{getConfig()}; } diff --git a/yoga/node/Node.h b/yoga/node/Node.h index 72b3d496db..9bc7140887 100644 --- a/yoga/node/Node.h +++ b/yoga/node/Node.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -30,6 +31,35 @@ struct YGNode {}; namespace facebook::yoga { +/** + * A pointer with relaxed atomic loads and stores. A node shared between trees + * can have its owner read by a layout pass on one thread while a clone of its + * previous owner, on another thread, writes it. Copies read the pointer once, + * so Node keeps its defaulted copy operations. + */ +template +class RelaxedAtomicPointer { + public: + RelaxedAtomicPointer() = default; + RelaxedAtomicPointer(const RelaxedAtomicPointer& other) noexcept + : value_(other.load()) {} + RelaxedAtomicPointer& operator=(const RelaxedAtomicPointer& other) noexcept { + store(other.load()); + return *this; + } + + T* load() const noexcept { + return value_.load(std::memory_order_relaxed); + } + + void store(T* value) noexcept { + value_.store(value, std::memory_order_relaxed); + } + + private: + std::atomic value_{nullptr}; +}; + class YG_EXPORT Node : public ::YGNode { public: using LayoutableChildren = yoga::LayoutableChildren; @@ -153,7 +183,7 @@ class YG_EXPORT Node : public ::YGNode { // of the Node when a Node only belongs to one YogaTree or nullptr when // the Node is shared between two or more YogaTrees. Node* getOwner() const { - return owner_; + return owner_.load(); } const std::vector& getChildren() const { @@ -275,7 +305,7 @@ class YG_EXPORT Node : public ::YGNode { } void setOwner(Node* owner) { - owner_ = owner; + owner_.store(owner); } // TODO: rvalue override for setChildren @@ -355,7 +385,7 @@ class YG_EXPORT Node : public ::YGNode { LayoutResults layout_; size_t lineIndex_ = 0; size_t contentsChildrenCount_ = 0; - Node* owner_ = nullptr; + RelaxedAtomicPointer owner_; std::vector children_; const Config* config_; std::array processedDimensions_{