-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Need Design Review] Set up an experiment to share one lock across each entire embedding #1528
Open
Adlai-Holler
wants to merge
1
commit into
master
Choose a base branch
from
AHNodeContext
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// ASNodeContext.h | ||
// Texture | ||
// | ||
// Copyright (c) Pinterest, Inc. All rights reserved. | ||
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import <AsyncDisplayKit/AsyncDisplayKit.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@class ASNodeContext; | ||
|
||
/** | ||
* Push the given context, which will apply to any nodes initialized until the corresponding `pop`. | ||
* | ||
* Generally each cell in a collection or table, and the root of an ASViewController, will be a context. | ||
*/ | ||
AS_EXTERN void ASNodeContextPush(unowned ASNodeContext *context); | ||
|
||
/** | ||
* Get the current top context, if there is one. | ||
*/ | ||
AS_EXTERN ASNodeContext *_Nullable ASNodeContextGet(void); | ||
|
||
/** | ||
* Pop the current context, matching a previous call to ASNodeContextPush. | ||
*/ | ||
AS_EXTERN void ASNodeContextPop(void); | ||
|
||
/** | ||
* A convenience to perform the given block with the provided context active. | ||
*/ | ||
AS_EXTERN id ASNodeContextPerform(unowned ASNodeContext *ctx, id (^NS_NOESCAPE body)(void)); | ||
|
||
/** | ||
* Node contexts can have extensions attached to them. For instance, if your application wants to do custom logging | ||
* on a per-context basis, you can define an extension identifier (pick an arbitrary uint32_t that won't collide) | ||
* and the logger can be stored & retrieved from the node context. | ||
*/ | ||
typedef uint32_t ASNodeContextExtensionIdentifier NS_TYPED_EXTENSIBLE_ENUM; | ||
|
||
/** | ||
* A node context is an object that is shared by, and uniquely identifies, an "embedding" of nodes. For example, | ||
* each cell in a collection view has its own context. Each ASViewController's node has its own context. You can | ||
* also explicitly establish a context for a node tree in another context. | ||
* | ||
* Node contexts store the mutex that is shared by all member nodes for synchronization. Operations such as addSubnode: | ||
* will lock the context's mutex for the duration of the work. | ||
* | ||
* Nodes may not be moved from one context to another. For instance, you may not detach a subnode of a cell node, | ||
* and reattach it to a subtree of another cell node in the same or another collection view. | ||
*/ | ||
AS_SUBCLASSING_RESTRICTED | ||
@interface ASNodeContext : NSObject <ASLocking> | ||
|
||
#pragma mark - Extension | ||
|
||
- (nullable id)extensionWithIdentifier:(ASNodeContextExtensionIdentifier)extensionIdentifier; | ||
|
||
- (void)setExtension:(nullable id)extension forIdentifier:(ASNodeContextExtensionIdentifier)extensionIdentifier; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// | ||
// ASNodeContext.mm | ||
// Texture | ||
// | ||
// Copyright (c) Pinterest, Inc. All rights reserved. | ||
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
#import <AsyncDisplayKit/ASNodeContext+Private.h> | ||
|
||
#import <AsyncDisplayKit/ASAssert.h> | ||
|
||
#import <stack> | ||
#import <unordered_map> | ||
|
||
#if AS_TLS_AVAILABLE | ||
|
||
static thread_local std::stack<ASNodeContext *> gContexts; | ||
|
||
void ASNodeContextPush(unowned ASNodeContext *context) { | ||
gContexts.push(context); | ||
} | ||
|
||
ASNodeContext *ASNodeContextGet() { | ||
Adlai-Holler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return gContexts.empty() ? nil : gContexts.top(); | ||
} | ||
|
||
void ASNodeContextPop() { | ||
if (DISPATCH_EXPECT(gContexts.empty(), false)) { | ||
ASDisplayNodeCFailAssert(@"Attempt to pop empty context stack."); | ||
return; | ||
} | ||
gContexts.pop(); | ||
} | ||
|
||
#else // !AS_TLS_AVAILABLE | ||
|
||
// Only on 32-bit simulator. Performance expendable. | ||
|
||
// Points to a NSMutableArray<ASNodeContext *>. | ||
static constexpr NSString *ASNodeContextStackKey = @"org.TextureGroup.Texture.nodeContexts"; | ||
|
||
void ASNodeContextPush(unowned ASNodeContext *context) { | ||
unowned NSMutableDictionary *td = NSThread.currentThread.threadDictionary; | ||
unowned NSMutableArray<ASNodeContext *> *stack = td[ASNodeContextStackKey]; | ||
if (!stack) { | ||
td[ASNodeContextStackKey] = [[NSMutableArray alloc] initWithObjects:context, nil]; | ||
} else { | ||
[stack addObject:context]; | ||
} | ||
} | ||
|
||
ASNodeContext *ASNodeContextGet() { | ||
return [NSThread.currentThread.threadDictionary[ASNodeContextStackKey] lastObject]; | ||
} | ||
|
||
void ASNodeContextPop() { | ||
if (ASActivateExperimentalFeature(ASExperimentalNodeContext)) { | ||
[NSThread.currentThread.threadDictionary[ASNodeContextStackKey] removeLastObject]; | ||
} | ||
} | ||
|
||
#endif // !AS_TLS_AVAILABLE | ||
|
||
id ASNodeContextPerform(unowned ASNodeContext *ctx, id(^ NS_NOESCAPE body)(void)) | ||
{ | ||
ASNodeContextPush(ctx); | ||
id result = body(); | ||
ASNodeContextPop(); | ||
return result; | ||
} | ||
|
||
@implementation ASNodeContext { | ||
std::unordered_map<uint32_t, id> _extensions; | ||
} | ||
|
||
- (id)createObject:(id(^ NS_NOESCAPE)())body | ||
{ | ||
ASNodeContextPush(self); | ||
id result = body(); | ||
ASNodeContextPop(); | ||
return result; | ||
} | ||
|
||
- (instancetype)init | ||
{ | ||
if (self = [super init]) { | ||
_mutex.SetDebugNameWithObject(self); | ||
} | ||
return self; | ||
} | ||
|
||
- (id)extensionWithIdentifier:(ASNodeContextExtensionIdentifier)extensionIdentifier | ||
{ | ||
AS::MutexLocker l(_mutex); | ||
auto it = _extensions.find(extensionIdentifier); | ||
return it != _extensions.end() ? it->second : nil; | ||
} | ||
|
||
- (void)setExtension:(id)extension forIdentifier:(ASNodeContextExtensionIdentifier)extensionIdentifier | ||
{ | ||
AS::MutexLocker l(_mutex); | ||
if (extension) { | ||
_extensions.emplace(extensionIdentifier, extension); | ||
} else { | ||
_extensions.erase(extensionIdentifier); | ||
} | ||
} | ||
|
||
#pragma mark ASLocking | ||
|
||
ASSynthesizeLockingMethodsWithMutex(_mutex); | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to expose the
nodeContext
publicly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To your question above, if users want to implement custom containers, and they want to add new nodes to a given context outside of our automatic places (like layout) they will need to push the context for an existing node. At least that's my thinking although I haven't written out an example case yet.