Skip to content

Commit

Permalink
Make ASBatchContext lock-free (TextureGroup#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adlai-Holler authored Mar 26, 2018
1 parent 27fac9f commit 063194c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
8 changes: 4 additions & 4 deletions AsyncDisplayKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
B35062131B010EFD0018CF92 /* ASBasicImageDownloader.h in Headers */ = {isa = PBXBuildFile; fileRef = 054963471A1EA066000F8E56 /* ASBasicImageDownloader.h */; settings = {ATTRIBUTES = (Public, ); }; };
B35062141B010EFD0018CF92 /* ASBasicImageDownloader.mm in Sources */ = {isa = PBXBuildFile; fileRef = 054963481A1EA066000F8E56 /* ASBasicImageDownloader.mm */; };
B35062151B010EFD0018CF92 /* ASBatchContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 299DA1A71A828D2900162D41 /* ASBatchContext.h */; settings = {ATTRIBUTES = (Public, ); }; };
B35062161B010EFD0018CF92 /* ASBatchContext.mm in Sources */ = {isa = PBXBuildFile; fileRef = 299DA1A81A828D2900162D41 /* ASBatchContext.mm */; };
B35062161B010EFD0018CF92 /* ASBatchContext.m in Sources */ = {isa = PBXBuildFile; fileRef = 299DA1A81A828D2900162D41 /* ASBatchContext.m */; };
B35062171B010EFD0018CF92 /* ASDataController.h in Headers */ = {isa = PBXBuildFile; fileRef = 464052191A3F83C40061C0BA /* ASDataController.h */; settings = {ATTRIBUTES = (Public, ); }; };
B35062181B010EFD0018CF92 /* ASDataController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4640521A1A3F83C40061C0BA /* ASDataController.mm */; };
B350621B1B010EFD0018CF92 /* ASTableLayoutController.h in Headers */ = {isa = PBXBuildFile; fileRef = 4640521B1A3F83C40061C0BA /* ASTableLayoutController.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -643,7 +643,7 @@
296A0A311A951715005ACEAA /* ASScrollDirection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASScrollDirection.h; path = Source/Details/ASScrollDirection.h; sourceTree = SOURCE_ROOT; };
296A0A341A951ABF005ACEAA /* ASBatchFetchingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = ASBatchFetchingTests.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
299DA1A71A828D2900162D41 /* ASBatchContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASBatchContext.h; sourceTree = "<group>"; };
299DA1A81A828D2900162D41 /* ASBatchContext.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASBatchContext.mm; sourceTree = "<group>"; };
299DA1A81A828D2900162D41 /* ASBatchContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASBatchContext.m; sourceTree = "<group>"; };
29CDC2E11AAE70D000833CA4 /* ASBasicImageDownloaderContextTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = ASBasicImageDownloaderContextTests.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
3917EBD21E9C2FC400D04A01 /* _ASCollectionReusableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _ASCollectionReusableView.h; sourceTree = "<group>"; };
3917EBD31E9C2FC400D04A01 /* _ASCollectionReusableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _ASCollectionReusableView.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1333,7 +1333,7 @@
054963471A1EA066000F8E56 /* ASBasicImageDownloader.h */,
054963481A1EA066000F8E56 /* ASBasicImageDownloader.mm */,
299DA1A71A828D2900162D41 /* ASBatchContext.h */,
299DA1A81A828D2900162D41 /* ASBatchContext.mm */,
299DA1A81A828D2900162D41 /* ASBatchContext.m */,
E5C347B01ECB3D9200EC4BE4 /* ASBatchFetchingDelegate.h */,
205F0E1B1B373A2C007741D0 /* ASCollectionViewLayoutController.h */,
205F0E1C1B373A2C007741D0 /* ASCollectionViewLayoutController.m */,
Expand Down Expand Up @@ -2339,7 +2339,7 @@
34EFC7621B701CA400AD841F /* ASBackgroundLayoutSpec.mm in Sources */,
DE8BEAC41C2DF3FC00D57C12 /* ASDelegateProxy.m in Sources */,
B35062141B010EFD0018CF92 /* ASBasicImageDownloader.mm in Sources */,
B35062161B010EFD0018CF92 /* ASBatchContext.mm in Sources */,
B35062161B010EFD0018CF92 /* ASBatchContext.m in Sources */,
AC47D9421B3B891B00AAEE9D /* ASCellNode.mm in Sources */,
E58E9E451E941D74004CFC59 /* ASCollectionLayoutContext.m in Sources */,
34EFC7641B701CC600AD841F /* ASCenterLayoutSpec.mm in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ASBatchContext.mm
// ASBatchContext.m
// Texture
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
Expand All @@ -18,62 +18,52 @@
#import <AsyncDisplayKit/ASBatchContext.h>

#import <AsyncDisplayKit/ASLog.h>
#import <AsyncDisplayKit/ASThread.h>
#import <stdatomic.h>

typedef NS_ENUM(NSInteger, ASBatchContextState) {
ASBatchContextStateFetching,
ASBatchContextStateCancelled,
ASBatchContextStateCompleted
};

@interface ASBatchContext ()
{
ASBatchContextState _state;
ASDN::RecursiveMutex __instanceLock__;
@implementation ASBatchContext {
atomic_int _state;
}
@end

@implementation ASBatchContext

- (instancetype)init
{
if (self = [super init]) {
_state = ASBatchContextStateCompleted;
_state = ATOMIC_VAR_INIT(ASBatchContextStateCompleted);
}
return self;
}

- (BOOL)isFetching
{
ASDN::MutexLocker l(__instanceLock__);
return _state == ASBatchContextStateFetching;
return atomic_load(&_state) == ASBatchContextStateFetching;
}

- (BOOL)batchFetchingWasCancelled
{
ASDN::MutexLocker l(__instanceLock__);
return _state == ASBatchContextStateCancelled;
return atomic_load(&_state) == ASBatchContextStateCancelled;
}

- (void)beginBatchFetching
{
ASDN::MutexLocker l(__instanceLock__);
_state = ASBatchContextStateFetching;
atomic_store(&_state, ASBatchContextStateFetching);
}

- (void)completeBatchFetching:(BOOL)didComplete
{
if (didComplete) {
as_log_debug(ASCollectionLog(), "Completed batch fetch with context %@", self);
ASDN::MutexLocker l(__instanceLock__);
_state = ASBatchContextStateCompleted;
atomic_store(&_state, ASBatchContextStateCompleted);
}
}

- (void)cancelBatchFetching
{
ASDN::MutexLocker l(__instanceLock__);
_state = ASBatchContextStateCancelled;
atomic_store(&_state, ASBatchContextStateCancelled);
}

@end

0 comments on commit 063194c

Please sign in to comment.