Skip to content
Browse files

used a dispatch semaphore to protect newMatchingFont

  • Loading branch information...
1 parent dce97b9 commit 753e6cec8a01b457e7958754015a963c063b54aa @dhoerl dhoerl committed Aug 22, 2011
Showing with 14 additions and 20 deletions.
  1. +1 −1 Classes/DTCoreTextFontDescriptor.h
  2. +13 −19 Classes/DTCoreTextFontDescriptor.m
View
2 Classes/DTCoreTextFontDescriptor.h
@@ -36,7 +36,7 @@
}
// generated fonts are cached
-+ (DTCache *)fontCache;
+//+ (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;
View
32 Classes/DTCoreTextFontDescriptor.m
@@ -11,21 +11,20 @@
static DTCache *_fontCache = nil;
static NSMutableDictionary *_fontOverrides = nil;
+static dispatch_semaphore_t fontLock;
+
+
@interface DTCoreTextFontDescriptor ()
-{
- CTFontRef fontRef;
-}
-- (void)_newMatchingFont;
-@end
++ (DTCache *)fontCache;
-static pthread_mutex_t mutex;
+@end
@implementation DTCoreTextFontDescriptor
+ (void)initialize
{
- pthread_mutex_init(&mutex, NULL);
+ fontLock = dispatch_semaphore_create(1);
}
+ (DTCache *)fontCache
@@ -34,7 +33,7 @@ + (DTCache *)fontCache
{
_fontCache = [[DTCache alloc] init];
}
-
+
return _fontCache;
}
@@ -407,14 +406,8 @@ - (BOOL)supportsNativeSmallCaps
- (CTFontRef)newMatchingFont
{
- pthread_mutex_lock(&mutex);
- [self performSelectorOnMainThread:@selector(_newMatchingFont) withObject:nil waitUntilDone:YES];
- pthread_mutex_unlock(&mutex);
- return fontRef;
-}
+ dispatch_semaphore_wait(fontLock, DISPATCH_TIME_FOREVER);
-- (void)_newMatchingFont
-{
NSDictionary *attributes = [self fontAttributes];
DTCache *fontCache = [DTCoreTextFontDescriptor fontCache];
@@ -425,8 +418,7 @@ - (void)_newMatchingFont
if (cachedFont)
{
CFRetain(cachedFont);
- fontRef = cachedFont;
- return;
+ return cachedFont;
}
CTFontDescriptorRef fontDesc = NULL;
@@ -506,8 +498,10 @@ - (void)_newMatchingFont
// cache it
[fontCache setObject:(id)matchingFont forKey:cacheKey];
}
-
- fontRef = matchingFont;
+
+ dispatch_semaphore_signal(fontLock);
+
+ return matchingFont;
}
- (void)normalizeSlow

0 comments on commit 753e6ce

Please sign in to comment.
Something went wrong with that request. Please try again.