Skip to content

Commit

Permalink
Revert "Have node and controller share lock (#1227)" (#1347)
Browse files Browse the repository at this point in the history
This reverts commit 2baa943.
  • Loading branch information
Adlai-Holler authored Feb 22, 2019
1 parent 333380d commit 6428077
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
13 changes: 4 additions & 9 deletions Source/ASNodeController+Beta.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
#import <AsyncDisplayKit/ASDisplayNode.h>
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h> // for ASInterfaceState protocol

NS_ASSUME_NONNULL_BEGIN

/* ASNodeController is currently beta and open to change in the future */
@interface ASNodeController<__covariant DisplayNodeType : ASDisplayNode *>
: NSObject <ASInterfaceStateDelegate, ASLocking>
: NSObject <ASInterfaceStateDelegate, NSLocking>

@property (strong, readonly /* may be weak! */) DisplayNodeType node;
@property (nonatomic, strong /* may be weak! */) DisplayNodeType node;

// Until an ASNodeController can be provided in place of an ASCellNode, some apps may prefer to have
// nodes keep their controllers alive (and a weak reference from controller to node)

@property (nonatomic) BOOL shouldInvertStrongReference;

// called on an arbitrary thread by the framework. You do not call this. Return a new node instance.
- (DisplayNodeType)createNode;
- (void)loadNode;

// for descriptions see <ASInterfaceState> definition
- (void)nodeDidLoad ASDISPLAYNODE_REQUIRES_SUPER;
Expand All @@ -51,8 +48,6 @@ NS_ASSUME_NONNULL_BEGIN

@interface ASDisplayNode (ASNodeController)

@property(nullable, readonly) ASNodeController *nodeController;
@property(nonatomic, readonly) ASNodeController *nodeController;

@end

NS_ASSUME_NONNULL_END
38 changes: 16 additions & 22 deletions Source/ASNodeController+Beta.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,22 @@ @implementation ASNodeController
{
ASDisplayNode *_strongNode;
__weak ASDisplayNode *_weakNode;
ASDN::Mutex _nodeLock;
ASDN::RecursiveMutex __instanceLock__;
}

- (ASDisplayNode *)createNode
- (void)loadNode
{
return [[ASDisplayNode alloc] init];
ASLockScopeSelf();
self.node = [[ASDisplayNode alloc] init];
}

- (ASDisplayNode *)node
{
ASDN::MutexLocker l(_nodeLock);
ASDisplayNode *node = _node;
if (!node) {
node = [self createNode];
if (!node) {
ASDisplayNodeCFailAssert(@"Returned nil from -createNode.");
node = [[ASDisplayNode alloc] init];
}
[self setupReferencesWithNode:node];
ASLockScopeSelf();
if (_node == nil) {
[self loadNode];
}
return node;
return _node;
}

- (void)setupReferencesWithNode:(ASDisplayNode *)node
Expand All @@ -58,6 +53,12 @@ - (void)setupReferencesWithNode:(ASDisplayNode *)node
[node addInterfaceStateDelegate:self];
}

- (void)setNode:(ASDisplayNode *)node
{
ASLockScopeSelf();
[self setupReferencesWithNode:node];
}

- (void)setShouldInvertStrongReference:(BOOL)shouldInvertStrongReference
{
ASLockScopeSelf();
Expand Down Expand Up @@ -92,19 +93,12 @@ - (void)hierarchyDisplayDidFinish {}

- (void)lock
{
[self.node lock];
__instanceLock__.lock();
}

- (void)unlock
{
// Since the node was already locked on this thread, we don't need to call our accessor or take our lock.
ASDisplayNodeAssertNotNil(_node, @"Node deallocated while locked.");
[_node unlock];
}

- (BOOL)tryLock
{
return [self.node tryLock];
__instanceLock__.unlock();
}

@end
Expand Down

0 comments on commit 6428077

Please sign in to comment.