Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions yoga/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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()};
}
Expand Down
36 changes: 33 additions & 3 deletions yoga/node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <atomic>
#include <cstdint>
#include <cstdio>
#include <vector>
Expand All @@ -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 <typename T>
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<T*> value_{nullptr};
};

class YG_EXPORT Node : public ::YGNode {
public:
using LayoutableChildren = yoga::LayoutableChildren<Node>;
Expand Down Expand Up @@ -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<Node*>& getChildren() const {
Expand Down Expand Up @@ -275,7 +305,7 @@ class YG_EXPORT Node : public ::YGNode {
}

void setOwner(Node* owner) {
owner_ = owner;
owner_.store(owner);
}

// TODO: rvalue override for setChildren
Expand Down Expand Up @@ -355,7 +385,7 @@ class YG_EXPORT Node : public ::YGNode {
LayoutResults layout_;
size_t lineIndex_ = 0;
size_t contentsChildrenCount_ = 0;
Node* owner_ = nullptr;
RelaxedAtomicPointer<Node> owner_;
std::vector<Node*> children_;
const Config* config_;
std::array<Style::SizeLength, 2> processedDimensions_{
Expand Down
Loading