Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lock of ASTextNodeRendererKey #1454

Merged
Merged
Changes from 3 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
29 changes: 16 additions & 13 deletions Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,25 @@
#pragma mark - ASTextKitRenderer

@interface ASTextNodeRendererKey : NSObject
@property (nonatomic) ASTextKitAttributes attributes;
@property (nonatomic) CGSize constrainedSize;
- (instancetype)initWithTextKitAttributes:(const ASTextKitAttributes &)attributes constrainedSize:(const CGSize)constrainedSize;
@end

@implementation ASTextNodeRendererKey {
std::mutex _m;
ASTextKitAttributes _attributes;
CGSize _constrainedSize;
}

- (instancetype)initWithTextKitAttributes:(const ASTextKitAttributes &)attributes constrainedSize:(const CGSize)constrainedSize
{
if (self = [super init]) {
_attributes = attributes;
_constrainedSize = constrainedSize;
}
return self;
}

- (NSUInteger)hash
{
std::lock_guard<std::mutex> _l(_m);
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wpadded"
struct {
Expand All @@ -87,13 +95,10 @@ - (BOOL)isEqual:(ASTextNodeRendererKey *)object
if (self == object) {
return YES;
}

if (!object) {
return NO;
}
// NOTE: Skip the class check for this specialized, internal Key object.

// Lock both objects, avoiding deadlock.
std::lock(_m, object->_m);
std::lock_guard<std::mutex> lk1(_m, std::adopt_lock);
std::lock_guard<std::mutex> lk2(object->_m, std::adopt_lock);

return _attributes == object->_attributes && CGSizeEqualToSize(_constrainedSize, object->_constrainedSize);
}
Expand Down Expand Up @@ -121,9 +126,7 @@ - (BOOL)isEqual:(ASTextNodeRendererKey *)object
{
NSCache *cache = sharedRendererCache();

ASTextNodeRendererKey *key = [[ASTextNodeRendererKey alloc] init];
key.attributes = attributes;
key.constrainedSize = constrainedSize;
ASTextNodeRendererKey *key = [[ASTextNodeRendererKey alloc] initWithTextKitAttributes:attributes constrainedSize:constrainedSize];

ASTextKitRenderer *renderer = [cache objectForKey:key];
if (renderer == nil) {
Expand Down