Skip to content

Commit

Permalink
Avoid using global Mutex variables (#1252)
Browse files Browse the repository at this point in the history
After 5c9815f, some Mutexes are used as global C++ variables which are loaded before main(). Since the Mutex constructor checks for unfair lock experiment, it triggers an experiment configuration load, and our app isn't ready to respond that early in the process.
  • Loading branch information
nguyenhuy committed Nov 27, 2018
1 parent f2bc63f commit a255953
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
8 changes: 6 additions & 2 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,15 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(NS_NOESC
}

static ASWeakMap<ASImageNodeContentsKey *, UIImage *> *cache = nil;
// Allocate cacheLock on the heap to prevent destruction at app exit (https://github.com/TextureGroup/Texture/issues/136)
static auto *cacheLock = new ASDN::Mutex;

+ (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key drawParameters:(id)drawParameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
{
static dispatch_once_t onceToken;
static ASDN::Mutex *cacheLock = nil;
dispatch_once(&onceToken, ^{
cacheLock = new ASDN::Mutex();
});

{
ASDN::MutexLocker l(*cacheLock);
if (!cache) {
Expand Down
6 changes: 3 additions & 3 deletions Source/ASTextNode2.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ @implementation ASTextCacheValue
* NOTE: Be careful to copy `text` if needed.
*/
static NS_RETURNS_RETAINED ASTextLayout *ASTextNodeCompatibleLayoutWithContainerAndText(ASTextContainer *container, NSAttributedString *text) {
// Allocate layoutCacheLock on the heap to prevent destruction at app exit (https://github.com/TextureGroup/Texture/issues/136)
static auto *layoutCacheLock = new ASDN::Mutex;
static NSCache<NSAttributedString *, ASTextCacheValue *> *textLayoutCache;
static dispatch_once_t onceToken;
static ASDN::Mutex *layoutCacheLock;
static NSCache<NSAttributedString *, ASTextCacheValue *> *textLayoutCache;
dispatch_once(&onceToken, ^{
layoutCacheLock = new ASDN::Mutex();
textLayoutCache = [[NSCache alloc] init];
});

Expand Down
16 changes: 12 additions & 4 deletions Source/Details/ASBasicImageDownloader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ @interface ASBasicImageDownloaderContext ()
@implementation ASBasicImageDownloaderContext

static NSMutableDictionary *currentRequests = nil;
// Allocate currentRequestsLock on the heap to prevent destruction at app exit (https://github.com/TextureGroup/Texture/issues/136)
static auto *currentRequestsLock = new ASDN::Mutex;

+ (ASDN::Mutex *)currentRequestLock
{
static dispatch_once_t onceToken;
static ASDN::Mutex *currentRequestsLock;
dispatch_once(&onceToken, ^{
currentRequestsLock = new ASDN::Mutex();
});
return currentRequestsLock;
}

+ (ASBasicImageDownloaderContext *)contextForURL:(NSURL *)URL
{
ASDN::MutexLocker l(*currentRequestsLock);
ASDN::MutexLocker l(*self.currentRequestLock);
if (!currentRequests) {
currentRequests = [[NSMutableDictionary alloc] init];
}
Expand All @@ -57,7 +65,7 @@ + (ASBasicImageDownloaderContext *)contextForURL:(NSURL *)URL

+ (void)cancelContextWithURL:(NSURL *)URL
{
ASDN::MutexLocker l(*currentRequestsLock);
ASDN::MutexLocker l(*self.currentRequestLock);
if (currentRequests) {
[currentRequests removeObjectForKey:URL];
}
Expand Down
8 changes: 6 additions & 2 deletions Source/TextKit/ASTextKitContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ - (instancetype)initWithAttributedString:(NSAttributedString *)attributedString

{
if (self = [super init]) {
static dispatch_once_t onceToken;
static ASDN::Mutex *mutex;
dispatch_once(&onceToken, ^{
mutex = new ASDN::Mutex();
});

// Concurrently initialising TextKit components crashes (rdar://18448377) so we use a global lock.
// Allocate mutex on the heap to prevent destruction at app exit (https://github.com/TextureGroup/Texture/issues/136)
static auto *mutex = new ASDN::Mutex;
ASDN::MutexLocker l(*mutex);

__instanceLock__ = std::make_shared<ASDN::Mutex>();
Expand Down

0 comments on commit a255953

Please sign in to comment.