From 8308a4ce49c21b9a5c7fc5168c67ac828cebfc10 Mon Sep 17 00:00:00 2001 From: Scott Goodson Date: Sat, 19 Mar 2016 19:56:41 -0700 Subject: [PATCH] [AsyncDisplayKit] Adopt #pragma once, remove last remaining dispatch_sync (UIScreen scale). --- ...SCollectionViewLayoutFacilitatorProtocol.h | 5 +---- AsyncDisplayKit/ASDisplayNode.mm | 6 +++++ AsyncDisplayKit/ASMultiplexImageNode.mm | 4 ++-- .../ASCollectionViewFlowLayoutInspector.h | 9 ++------ .../Details/ASDataController+Subclasses.h | 5 +---- AsyncDisplayKit/Details/ASDataController.h | 5 +---- AsyncDisplayKit/Details/ASMainSerialQueue.mm | 9 ++------ AsyncDisplayKit/Private/ASInternalHelpers.mm | 22 +++++-------------- AsyncDisplayKit/TextKit/ASTextKitAttributes.h | 7 ++---- AsyncDisplayKitTests/ASTableViewTests.m | 12 ---------- Base/ASAssert.h | 9 ++++---- 11 files changed, 27 insertions(+), 66 deletions(-) diff --git a/AsyncDisplayKit/ASCollectionViewLayoutFacilitatorProtocol.h b/AsyncDisplayKit/ASCollectionViewLayoutFacilitatorProtocol.h index b681ceaad4..38f43b71a8 100644 --- a/AsyncDisplayKit/ASCollectionViewLayoutFacilitatorProtocol.h +++ b/AsyncDisplayKit/ASCollectionViewLayoutFacilitatorProtocol.h @@ -6,8 +6,7 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -#ifndef ASCollectionViewLayoutFacilitatorProtocol_h -#define ASCollectionViewLayoutFacilitatorProtocol_h +#pragma once /** * This facilitator protocol is intended to help Layout to better @@ -41,5 +40,3 @@ - (void)collectionViewWillPerformBatchUpdates; @end - -#endif /* ASCollectionViewLayoutFacilitatorProtocol_h */ diff --git a/AsyncDisplayKit/ASDisplayNode.mm b/AsyncDisplayKit/ASDisplayNode.mm index 89fb8d2bad..6b42181aec 100644 --- a/AsyncDisplayKit/ASDisplayNode.mm +++ b/AsyncDisplayKit/ASDisplayNode.mm @@ -198,6 +198,12 @@ static ASDisplayNodeMethodOverrides GetASDisplayNodeMethodOverrides(Class c) class_replaceMethod(self, @selector(_staticInitialize), staticInitialize, "v:@"); } ++ (void)load +{ + // Ensure this value is cached on the main thread before needed in the background. + ASScreenScale(); +} + + (BOOL)layerBackedNodesEnabled { return YES; diff --git a/AsyncDisplayKit/ASMultiplexImageNode.mm b/AsyncDisplayKit/ASMultiplexImageNode.mm index 765deec583..cd7c448e0b 100644 --- a/AsyncDisplayKit/ASMultiplexImageNode.mm +++ b/AsyncDisplayKit/ASMultiplexImageNode.mm @@ -250,7 +250,7 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent // Delegateify if (_delegateFlags.displayFinish) { - if ([NSThread isMainThread]) + if (ASDisplayNodeThreadIsMain()) [_delegate multiplexImageNodeDidFinishDisplay:self]; else { __weak __typeof__(self) weakSelf = self; @@ -405,7 +405,7 @@ typedef void(^ASMultiplexImageLoadCompletionBlock)(UIImage *image, id imageIdent // Delegateify. // Note that we're using the params here instead of self.image and _displayedImageIdentifier because those can change before the async block below executes. if (_delegateFlags.updatedImageDisplayFinish) { - if ([NSThread isMainThread]) + if (ASDisplayNodeThreadIsMain()) [_delegate multiplexImageNode:self didDisplayUpdatedImage:image withIdentifier:displayedImageIdentifier]; else { __weak __typeof__(self) weakSelf = self; diff --git a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h index e8d758cd2b..cd640c3e2a 100644 --- a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h +++ b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h @@ -6,13 +6,11 @@ * of patent rights can be found in the PATENTS file in the same directory. */ +#pragma once + #import - #import -#ifndef ASCollectionViewLayoutInspecting_h -#define ASCollectionViewLayoutInspecting_h - @class ASCollectionView; @protocol ASCollectionDelegate; @@ -56,6 +54,3 @@ - (instancetype)initWithCollectionView:(ASCollectionView *)collectionView flowLayout:(UICollectionViewFlowLayout *)flowLayout; @end - -#endif /* ASCollectionViewLayoutInspecting_h */ - diff --git a/AsyncDisplayKit/Details/ASDataController+Subclasses.h b/AsyncDisplayKit/Details/ASDataController+Subclasses.h index ee9a51d254..cb67a203ef 100644 --- a/AsyncDisplayKit/Details/ASDataController+Subclasses.h +++ b/AsyncDisplayKit/Details/ASDataController+Subclasses.h @@ -6,8 +6,7 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -#ifndef ASDataControllerSubclasses_Included -#define ASDataControllerSubclasses_Included +#pragma once @class ASIndexedNodeContext; @@ -162,5 +161,3 @@ typedef void (^ASDataControllerCompletionBlock)(NSArray *nodes, NS - (void)willMoveSection:(NSInteger)section toSection:(NSInteger)newSection; @end - -#endif diff --git a/AsyncDisplayKit/Details/ASDataController.h b/AsyncDisplayKit/Details/ASDataController.h index 587adf2ad7..190119ed62 100644 --- a/AsyncDisplayKit/Details/ASDataController.h +++ b/AsyncDisplayKit/Details/ASDataController.h @@ -6,8 +6,7 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -#ifndef ASDataController_Included -#define ASDataController_Included +#pragma once #import #import @@ -195,5 +194,3 @@ FOUNDATION_EXPORT NSString * const ASDataControllerRowNodeKind; @end NS_ASSUME_NONNULL_END - -#endif diff --git a/AsyncDisplayKit/Details/ASMainSerialQueue.mm b/AsyncDisplayKit/Details/ASMainSerialQueue.mm index d46a02a1ad..3112757af0 100644 --- a/AsyncDisplayKit/Details/ASMainSerialQueue.mm +++ b/AsyncDisplayKit/Details/ASMainSerialQueue.mm @@ -9,6 +9,7 @@ #import "ASMainSerialQueue.h" #import "ASThread.h" +#import "ASInternalHelpers.h" @interface ASMainSerialQueue () { @@ -55,13 +56,7 @@ } while (true); }; - if ([NSThread isMainThread]) { - mainThread(); - } else { - dispatch_async(dispatch_get_main_queue(), ^{ - mainThread(); - }); - } + ASPerformBlockOnMainThread(mainThread); } - (NSString *)description diff --git a/AsyncDisplayKit/Private/ASInternalHelpers.mm b/AsyncDisplayKit/Private/ASInternalHelpers.mm index 0f6aa6f08d..40f6445434 100644 --- a/AsyncDisplayKit/Private/ASInternalHelpers.mm +++ b/AsyncDisplayKit/Private/ASInternalHelpers.mm @@ -34,19 +34,6 @@ BOOL ASSubclassOverridesClassSelector(Class superclass, Class subclass, SEL sele return (superclassIMP != subclassIMP); } -static void ASDispatchOnceOnMainThread(dispatch_once_t *predicate, dispatch_block_t block) -{ - if (ASDisplayNodeThreadIsMain()) { - dispatch_once(predicate, block); - } else { - if (DISPATCH_EXPECT(*predicate == 0L, NO)) { - dispatch_sync(dispatch_get_main_queue(), ^{ - dispatch_once(predicate, block); - }); - } - } -} - void ASPerformBlockOnMainThread(void (^block)()) { if (ASDisplayNodeThreadIsMain()) { @@ -67,12 +54,13 @@ void ASPerformBlockOnBackgroundThread(void (^block)()) CGFloat ASScreenScale() { - static CGFloat _scale; + static CGFloat __scale = 0.0; static dispatch_once_t onceToken; - ASDispatchOnceOnMainThread(&onceToken, ^{ - _scale = [UIScreen mainScreen].scale; + dispatch_once(&onceToken, ^{ + ASDisplayNodeCAssertMainThread(); + __scale = [[UIScreen mainScreen] scale]; }); - return _scale; + return __scale; } CGFloat ASFloorPixelValue(CGFloat f) diff --git a/AsyncDisplayKit/TextKit/ASTextKitAttributes.h b/AsyncDisplayKit/TextKit/ASTextKitAttributes.h index 7fcb8ff144..49dc9b92f3 100755 --- a/AsyncDisplayKit/TextKit/ASTextKitAttributes.h +++ b/AsyncDisplayKit/TextKit/ASTextKitAttributes.h @@ -8,12 +8,11 @@ * */ +#pragma once + #import #import "ASEqualityHelpers.h" -#ifndef ComponentKit_ASTextKitAttributes_h -#define ComponentKit_ASTextKitAttributes_h - @protocol ASTextKitTruncating; extern NSString *const ASTextKitTruncationAttributeName; @@ -141,5 +140,3 @@ struct ASTextKitAttributes { size_t hash() const; }; - -#endif diff --git a/AsyncDisplayKitTests/ASTableViewTests.m b/AsyncDisplayKitTests/ASTableViewTests.m index 550f3fec9d..2949a16e2f 100644 --- a/AsyncDisplayKitTests/ASTableViewTests.m +++ b/AsyncDisplayKitTests/ASTableViewTests.m @@ -144,18 +144,6 @@ @implementation ASTableViewTests -- (void)setUp -{ - /// Load a display node before the first test. - /// Without this, running this suite specifically - /// (as opposed to all tests) will cause a deadlock - /// because of the dispatch_sync in `ASScreenScale()`. - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - [ASDisplayNode new]; - }); -} - // TODO: Convert this to ARC. - (void)DISABLED_testTableViewDoesNotRetainItselfAndDelegate { diff --git a/Base/ASAssert.h b/Base/ASAssert.h index b0b53cc855..446f64b4b7 100644 --- a/Base/ASAssert.h +++ b/Base/ASAssert.h @@ -9,6 +9,7 @@ #pragma once #import +#import #define ASDisplayNodeAssertWithSignalAndLogFunction(condition, description, logFunction, ...) NSAssert(condition, description, ##__VA_ARGS__); #define ASDisplayNodeCAssertWithSignalAndLogFunction(condition, description, logFunction, ...) NSCAssert(condition, description, ##__VA_ARGS__); @@ -30,11 +31,11 @@ #define ASDisplayNodeAssertNotInstantiable() ASDisplayNodeAssertWithSignal(NO, nil, @"This class is not instantiable."); #define ASDisplayNodeAssertNotSupported() ASDisplayNodeAssertWithSignal(NO, nil, @"This method is not supported by class %@", [self class]); -#define ASDisplayNodeAssertMainThread() ASDisplayNodeAssertWithSignal([NSThread isMainThread], nil, @"This method must be called on the main thread") -#define ASDisplayNodeCAssertMainThread() ASDisplayNodeCAssertWithSignal([NSThread isMainThread], nil, @"This function must be called on the main thread") +#define ASDisplayNodeAssertMainThread() ASDisplayNodeAssertWithSignal(0 != pthread_main_np(), nil, @"This method must be called on the main thread") +#define ASDisplayNodeCAssertMainThread() ASDisplayNodeCAssertWithSignal(0 != pthread_main_np(), nil, @"This function must be called on the main thread") -#define ASDisplayNodeAssertNotMainThread() ASDisplayNodeAssertWithSignal(![NSThread isMainThread], nil, @"This method must be called off the main thread") -#define ASDisplayNodeCAssertNotMainThread() ASDisplayNodeCAssertWithSignal(![NSThread isMainThread], nil, @"This function must be called off the main thread") +#define ASDisplayNodeAssertNotMainThread() ASDisplayNodeAssertWithSignal(0 == pthread_main_np(), nil, @"This method must be called off the main thread") +#define ASDisplayNodeCAssertNotMainThread() ASDisplayNodeCAssertWithSignal(0 == pthread_main_np(), nil, @"This function must be called off the main thread") #define ASDisplayNodeAssertFlag(X) ASDisplayNodeAssertWithSignal((1 == __builtin_popcount(X)), nil, nil) #define ASDisplayNodeCAssertFlag(X) ASDisplayNodeCAssertWithSignal((1 == __builtin_popcount(X)), nil, nil)