Skip to content

Loading…

Added a lock around newMatchingFont #63

Merged
merged 4 commits into from

2 participants

@dhoerl

Prevents multiple threads from trying to instantiate the same new font

@odrobnik

Why do you need to force the font creation onto main thread? Isn't that causing more of a bottleneck than necessary?

@dhoerl
@odrobnik

That does not explain the performing on main thread. The mutex should be sufficient, shouldn't it?

@dhoerl
@dhoerl
@odrobnik odrobnik merged commit 078ef84 into Cocoanetics:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 17, 2011
  1. @dhoerl

    Lock for font actions

    dhoerl committed
Commits on Aug 22, 2011
  1. @dhoerl
  2. @dhoerl

    fixed some issues

    dhoerl committed
Commits on Aug 23, 2011
  1. @dhoerl
Showing with 45 additions and 9 deletions.
  1. +3 −6 Classes/DTCoreTextFontDescriptor.h
  2. +42 −3 Classes/DTCoreTextFontDescriptor.m
View
9 Classes/DTCoreTextFontDescriptor.h
@@ -29,15 +29,12 @@
CTFontStylisticClass stylisticClass;
- BOOL smallCapsFeature;
+ BOOL smallCapsFeature;
- BOOL _hashSet;
- NSUInteger _hash;
+ BOOL _hashSet;
+ NSUInteger _hash;
}
-// generated fonts are cached
-+ (DTCache *)fontCache;
-
// sets the font face name to use for a specific font family
+ (void)setSmallCapsFontName:(NSString *)fontName forFontFamily:(NSString *)fontFamily bold:(BOOL)bold italic:(BOOL)italic;
+ (NSString *)smallCapsFontNameforFontFamily:(NSString *)fontFamily bold:(BOOL)bold italic:(BOOL)italic;
View
45 Classes/DTCoreTextFontDescriptor.m
@@ -11,10 +11,34 @@
static DTCache *_fontCache = nil;
static NSMutableDictionary *_fontOverrides = nil;
+#ifndef __IPHONE_4_3
+ #define __IPHONE_4_3 40300
+#endif
+
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+static dispatch_semaphore_t fontLock;
+#else
+static pthread_mutex_t fontLock;
+#endif
+
+@interface DTCoreTextFontDescriptor ()
+
+// generated fonts are cached
++ (DTCache *)fontCache;
+
+@end
@implementation DTCoreTextFontDescriptor
++ (void)initialize
+{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+ fontLock = dispatch_semaphore_create(1);
+#else
+ pthread_mutex_init(&fontLock, NULL);
+#endif
+}
+ (DTCache *)fontCache
{
@@ -22,7 +46,7 @@ + (DTCache *)fontCache
{
_fontCache = [[DTCache alloc] init];
}
-
+
return _fontCache;
}
@@ -395,6 +419,12 @@ - (BOOL)supportsNativeSmallCaps
- (CTFontRef)newMatchingFont
{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+ dispatch_semaphore_wait(fontLock, DISPATCH_TIME_FOREVER);
+#else
+ pthread_mutex_lock(&fontLock);
+#endif
+
NSDictionary *attributes = [self fontAttributes];
DTCache *fontCache = [DTCoreTextFontDescriptor fontCache];
@@ -405,6 +435,11 @@ - (CTFontRef)newMatchingFont
if (cachedFont)
{
CFRetain(cachedFont);
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+ dispatch_semaphore_signal(fontLock);
+#else
+ pthread_mutex_unlock(&fontLock);
+#endif
return cachedFont;
}
@@ -414,7 +449,6 @@ - (CTFontRef)newMatchingFont
NSString *usedName = fontName;
-
// override fontName if a small caps or regular override is registered
if (fontFamily)
{
@@ -486,7 +520,12 @@ - (CTFontRef)newMatchingFont
// cache it
[fontCache setObject:(id)matchingFont forKey:cacheKey];
}
-
+
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
+ dispatch_semaphore_signal(fontLock);
+#else
+ pthread_mutex_unlock(&fontLock);
+#endif
return matchingFont;
}
Something went wrong with that request. Please try again.