Make Yoga thread safe #786

Closed
nokia6686 wants to merge 3 commits from yoga_thread_safety_master into master
nokia6686 commented 2018-06-19 01:51:18 -07:00 (Migrated from github.com)

Issue ref: https://github.com/facebook/yoga/issues/769

Problem

  • gNodeInstanceCount, gConfigInstanceCount, gCurrentGenerationCount, gDepth are global variables in Yoga.cpp. They are not safe when using multithreading.

My idea

  • Uses atomic for gNodeInstanceCount and gConfigInstanceCount because of counting instances only.
  • Store gCurrentGenerationCount and gDepth in root node for multithreading.

My solution

  • Uses for gNodeInstanceCount and gConfigInstanceCount
std::atomic<int32_t> gNodeInstanceCount(0);
std::atomic<int32_t> gConfigInstanceCount(0);
  • Every Yoga Node has its root reference (root-ref). New node's root point to itself. Root-ref will be updated when inserting children.
  • gCurrentGenerationCount and gDepth are stored in YGNode.
  • When updating children, we must update root-ref for each new child.
struct YGNode {
...
  // We must setChildRoot() for new child
  void replaceChild(YGNodeRef oldChild, YGNodeRef newChild);
  void replaceChild(YGNodeRef child, uint32_t index);
  void insertChild(YGNodeRef child, uint32_t index);
...
  private:
    YGNodeRef pRoot = nullptr;

  public:
    uint32_t  gCurrentGenerationCount = 0;
    uint32_t  gDepth = 0;
    YGNodeRef getRoot();
    void setChildRoot(YGNodeRef node);
}
...
void YGNode::setChildRoot(YGNodeRef node) {
  node->pRoot = getRoot();
  for (auto child: node->children_) {
    setChildRoot(child);
  }
}
  • Global variables are accessed via root-ref
//gCurrentGenerationCount++;
node->getRoot()->gCurrentGenerationCount++;

//gDepth++;
node->getRoot()->gDepth++;
Issue ref: https://github.com/facebook/yoga/issues/769 ### Problem - `gNodeInstanceCount`, `gConfigInstanceCount`, `gCurrentGenerationCount`, `gDepth` are global variables in `Yoga.cpp`. They are not safe when using multithreading. ### My idea - Uses `atomic` for `gNodeInstanceCount` and `gConfigInstanceCount` because of counting instances only. - Store `gCurrentGenerationCount` and `gDepth` in root node for multithreading. ### My solution - Uses <atomic> for `gNodeInstanceCount` and `gConfigInstanceCount` ```C++ std::atomic<int32_t> gNodeInstanceCount(0); std::atomic<int32_t> gConfigInstanceCount(0); ``` - Every Yoga Node has its root reference (root-ref). New node's root point to itself. Root-ref will be updated when inserting children. - `gCurrentGenerationCount` and `gDepth` are stored in `YGNode`. - When updating children, we must update root-ref for each new child. ```c++ struct YGNode { ... // We must setChildRoot() for new child void replaceChild(YGNodeRef oldChild, YGNodeRef newChild); void replaceChild(YGNodeRef child, uint32_t index); void insertChild(YGNodeRef child, uint32_t index); ... private: YGNodeRef pRoot = nullptr; public: uint32_t gCurrentGenerationCount = 0; uint32_t gDepth = 0; YGNodeRef getRoot(); void setChildRoot(YGNodeRef node); } ... void YGNode::setChildRoot(YGNodeRef node) { node->pRoot = getRoot(); for (auto child: node->children_) { setChildRoot(child); } } ``` - Global variables are accessed via root-ref ```C++ //gCurrentGenerationCount++; node->getRoot()->gCurrentGenerationCount++; //gDepth++; node->getRoot()->gDepth++; ```
facebook-github-bot commented 2018-06-19 03:42:44 -07:00 (Migrated from github.com)

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. **If you are contributing on behalf of someone else (eg your employer)**, the individual CLA may not be sufficient and your employer may need the corporate CLA signed. If you have received this in error or have any questions, please contact us at [cla@fb.com](mailto:cla@fb.com?subject=CLA%20for%20facebook%2Fyoga%20%23786). Thanks!
facebook-github-bot commented 2018-06-21 03:53:34 -07:00 (Migrated from github.com)

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

Pull request closed

Sign in to join this conversation.
No description provided.