Skip to content
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

Avoid using global Mutex variables #1252

Merged
merged 3 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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