Skip to content

Stricter locking assertions#1024

Merged
nguyenhuy merged 9 commits into
TextureGroup:masterfrom
nguyenhuy:HN-Stricter-Locking-Safety-Checks
Jul 13, 2018
Merged

Stricter locking assertions#1024
nguyenhuy merged 9 commits into
TextureGroup:masterfrom
nguyenhuy:HN-Stricter-Locking-Safety-Checks

Conversation

@nguyenhuy

@nguyenhuy nguyenhuy commented Jul 12, 2018

Copy link
Copy Markdown
Member

@nguyenhuy

Copy link
Copy Markdown
Member Author

Note to reviewers: please review this after #1022 and #1023.

Comment thread Source/ASDisplayNode+Layout.mm Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply the same fix in #1022 here just to be safe, i.e if _u_setNeedsLayoutFromAbove causes an assertion, it'll be correctly shown.

Comment thread Source/ASDisplayNode+Layout.mm Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While having the lock, call the _locked_ method.

Comment thread Source/ASDisplayNode+Layout.mm Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache these properties to improve readability of the following lines

#notAPerfOptimization

Comment thread Source/ASDisplayNode+Layout.mm Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easier to just call the "unlocked" variant.

Comment thread Source/ASDisplayNode.mm Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't call the _locked_ method here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Comment thread Source/ASVideoNode.mm Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentions in the description, play shouldn't be called with the lock.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding a comment in code?

@ghost

ghost commented Jul 12, 2018

Copy link
Copy Markdown

🚫 CI failed with log

@Adlai-Holler Adlai-Holler left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're renaming the macro, how about ASAssertLocked and ASAssertUnlocked? Over time I'd like to move away from ASDisplayNode as a prefix, since the framework now encompasses so much more.

@nguyenhuy

Copy link
Copy Markdown
Member Author

Uhh, I like that. Will update!

@TextureGroup TextureGroup deleted a comment Jul 12, 2018
@TextureGroup TextureGroup deleted a comment Jul 12, 2018
@TextureGroup TextureGroup deleted a comment Jul 12, 2018
@nguyenhuy nguyenhuy force-pushed the HN-Stricter-Locking-Safety-Checks branch from 4a0ebd2 to 465be36 Compare July 12, 2018 20:43
- Rename `ASDisplayNodeAssertLockUnownedByCurrentThread` to `ASDisplayNodeAssertLockNotHeld`, and `ASDisplayNodeAssertLockOwnedByCurrentThread` to `ASDisplayNodeAssertLockHeld` -> shorter and hopefully easier to distinguish between the two.
- Add assertions to `_locked_` and `_u_` (i.e "unlocked") methods.
- Turn `CHECK_LOCKING_SAFETY` flag on by default. After TextureGroup#1022 and TextureGroup#1023, we're in a good shape to actually enforce locked/unlocked requirements of internal methods. Our test suite passed, and we'll test more at Pinterest after the sync this week.
- Fix ASVideoNode to avoid calling `play` while holding the lock. That method inserts a subnode and must be called lock free.
- Other minor changes.
@nguyenhuy nguyenhuy force-pushed the HN-Stricter-Locking-Safety-Checks branch from 465be36 to c037799 Compare July 12, 2018 20:49
Comment thread Source/ASDisplayNode+Layout.mm Outdated
ASAssertLocked(__instanceLock__);

CGSize boundsSizeForLayout = ASCeilSizeValues(self.threadSafeBounds.size);
std::shared_ptr<ASDisplayNodeLayout> pendingLayout = _pendingDisplayNodeLayout;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding a comment explaining the shared_ptr's here?

Comment thread Source/ASDisplayNode.mm Outdated

- (BOOL)_isNodeLoaded
{
return (_view != nil || (_layer != nil && _flags.layerBacked));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be updated with a lock?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is called unlocked, and the client is responsible for its safety. Renamed to -_unsafe_unlocked_isNodeLoaded to reflect that.

Comment thread Source/ASDisplayNode.mm Outdated
}

/// Called without the lock. Client is responsible for locking safety.
- (BOOL)_unsafe_unlocked_isNodeLoaded

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify all of this by just using _layer != nil inline when we want a fast loaded check?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great point!

@TextureGroup TextureGroup deleted a comment Jul 12, 2018
Comment thread Source/ASDisplayNode.mm Outdated
ASDISPLAYNODE_INLINE AS_WARN_UNUSED_RESULT BOOL _ASIsNodeLoaded(ASDisplayNode *node)
{
return (_view != nil || (_layer != nil && _flags.layerBacked));
return (node->_view != nil || (node->_layer != nil && node->_flags.layerBacked));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we get rid of this function and just put _layer != nil inside the places where we need it?

@nguyenhuy nguyenhuy merged commit 0dc97fb into TextureGroup:master Jul 13, 2018
@nguyenhuy nguyenhuy deleted the HN-Stricter-Locking-Safety-Checks branch July 13, 2018 21:58
mikezucc pushed a commit to mikezucc/Texture that referenced this pull request Oct 2, 2018
- Rename `ASDisplayNodeAssertLockUnownedByCurrentThread` to `ASAssertUnlocked`, and `ASDisplayNodeAssertLockOwnedByCurrentThread` to `ASAssertLocked` -> shorter and hopefully easier to distinguish between the two.
- Add assertions to `_locked_` and `_u_` (i.e "unlocked") methods.
- Turn `CHECK_LOCKING_SAFETY` flag on by default. After TextureGroup#1022 and TextureGroup#1023, we're in a good shape to actually enforce locked/unlocked requirements of internal methods. Our test suite passed, and we'll test more at Pinterest after the sync this week.
- Fix ASVideoNode to avoid calling `play` while holding the lock. That method inserts a subnode and must be called lock free.
- Simplify `_loaded(node)` to only nil-check `_layer` because regardless of whether the node is view or layer backed, the layer should always be set if loaded. Use it throughout.
- Other minor changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants