From 62564278d73e2d879bb21b3c951acadf03ab7392 Mon Sep 17 00:00:00 2001 From: rfm Date: Sat, 7 Jun 2008 05:44:58 +0000 Subject: [PATCH] Fix deadlock and revert previous change git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@26603 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 8 +------- Source/GSPrivate.h | 6 ++++++ Source/NSCalendarDate.m | 4 ++-- Source/NSDate.m | 2 +- Source/NSDecimalNumber.m | 13 +++++-------- Source/NSString.m | 10 +++------- Source/NSUserDefaults.m | 25 +++++++++++++++++++++++++ 7 files changed, 43 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8f101274f..81af26bec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,13 +1,7 @@ 2008-06-07 Richard Frith-Macdonald - * Source/NSCalendarDate.m: * Source/NSUserDefaults.m: - * Source/GSPrivate.h: - * Source/NSString.m: - * Source/NSDecimalNumber.m: - * Source/NSDate.m: - Remove GSPrivateDefaultLocale(), as the performance optimisation - doesn't seem worth the additional complexity. + Fix possible deadlock reported by Larry Campbell 2008-06-06 Richard Frith-Macdonald diff --git a/Source/GSPrivate.h b/Source/GSPrivate.h index c92b3aff2..376a6d5ee 100644 --- a/Source/GSPrivate.h +++ b/Source/GSPrivate.h @@ -318,6 +318,12 @@ GSPrivateCheckTasks(void) GS_ATTRIB_PRIVATE; NSStringEncoding GSPrivateDefaultCStringEncoding() GS_ATTRIB_PRIVATE; +/* Get default locale quickly (usually from cache). + * External apps would cache the locale themselves. + */ +NSDictionary * +GSPrivateDefaultLocale() GS_ATTRIB_PRIVATE; + /* Get one of several standard values. */ BOOL diff --git a/Source/NSCalendarDate.m b/Source/NSCalendarDate.m index f99eb812a..88907e02b 100644 --- a/Source/NSCalendarDate.m +++ b/Source/NSCalendarDate.m @@ -673,7 +673,7 @@ static inline int getDigits(const char *from, char *to, int limit, BOOL *error) sourceLen = strlen(source); if (locale == nil) { - locale = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + locale = GSPrivateDefaultLocale(); } if (fmt == nil) { @@ -2267,7 +2267,7 @@ static void Grow(DescriptionInfo *info, unsigned size) DescriptionInfo info; if (locale == nil) - locale = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + locale = GSPrivateDefaultLocale(); if (format == nil) format = [locale objectForKey: NSTimeDateFormatString]; diff --git a/Source/NSDate.m b/Source/NSDate.m index 71693d69c..2692f30c9 100644 --- a/Source/NSDate.m +++ b/Source/NSDate.m @@ -264,7 +264,7 @@ otherTime(NSDate* other) if (locale == nil) { - locale = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + locale = GSPrivateDefaultLocale(); } ws = [NSCharacterSet whitespaceAndNewlineCharacterSet]; digits = [NSCharacterSet decimalDigitCharacterSet]; diff --git a/Source/NSDecimalNumber.m b/Source/NSDecimalNumber.m index 5d6b3b76b..8cb100a00 100644 --- a/Source/NSDecimalNumber.m +++ b/Source/NSDecimalNumber.m @@ -35,7 +35,6 @@ #include "Foundation/NSDecimalNumber.h" #include "Foundation/NSException.h" #include "Foundation/NSPortCoder.h" -#include "Foundation/NSUserDefaults.h" #include "GSPrivate.h" @@ -357,9 +356,8 @@ static NSDecimalNumber *one; float v = *(float *)value; if (GSIsNAN(v)) return notANumber; if (GSIsInf(v)) return (v < 0.0) ? minNumber : maxNumber; - s = [[NSString alloc] initWithFormat: @"%g" locale: - [[NSUserDefaults standardUserDefaults] dictionaryRepresentation], - (double)v]; + s = [[NSString alloc] initWithFormat: @"%g" + locale: GSPrivateDefaultLocale(), (double)v]; self = [self initWithString: s]; RELEASE(s); return self; @@ -372,9 +370,8 @@ static NSDecimalNumber *one; double v = *(double *)value; if (GSIsNAN(v)) return notANumber; if (GSIsInf(v)) return (v < 0.0) ? minNumber : maxNumber; - s = [[NSString alloc] initWithFormat: @"%g" locale: - [[NSUserDefaults standardUserDefaults] dictionaryRepresentation], - v]; + s = [[NSString alloc] initWithFormat: @"%g" + locale: GSPrivateDefaultLocale(), v]; self = [self initWithString: s]; RELEASE(s); return self; @@ -416,7 +413,7 @@ static NSDecimalNumber *one; - (id) initWithString: (NSString*)numberValue { return [self initWithString: numberValue - locale: [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]; + locale: GSPrivateDefaultLocale()]; } - (id) initWithString: (NSString*)numberValue diff --git a/Source/NSString.m b/Source/NSString.m index 35dbfcee1..2833b49c4 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -4384,9 +4384,7 @@ static NSFileManager *fm = nil; else { ret = AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] - initWithFormat: format locale: - [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] - arguments: ap]); + initWithFormat: format locale: GSPrivateDefaultLocale() arguments: ap]); } va_end(ap); return ret; @@ -4439,8 +4437,7 @@ static NSFileManager *fm = nil; return [self compare: string options: 0 range: ((NSRange){0, [self length]}) - locale: - [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]; + locale: GSPrivateDefaultLocale()]; } /** @@ -4452,8 +4449,7 @@ static NSFileManager *fm = nil; return [self compare: string options: NSCaseInsensitiveSearch range: ((NSRange){0, [self length]}) - locale: - [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]; + locale: GSPrivateDefaultLocale()]; } /** diff --git a/Source/NSUserDefaults.m b/Source/NSUserDefaults.m index 6422623e9..d293efeb1 100644 --- a/Source/NSUserDefaults.m +++ b/Source/NSUserDefaults.m @@ -1872,3 +1872,28 @@ GSPrivateDefaultsFlag(GSUserDefaultFlagType type) return flags[type]; } +/* FIXME ... Slightly faster than + * [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] + * but is it really worthwile? + */ +NSDictionary *GSPrivateDefaultLocale() +{ + NSDictionary *locale; + NSUserDefaults *defs; + + if (classLock == nil) + { + [NSUserDefaults standardUserDefaults]; + } + [classLock lock]; + if (sharedDefaults == nil) + { + [NSUserDefaults standardUserDefaults]; + } + defs = [sharedDefaults retain]; + [classLock unlock]; + locale = [defs dictionaryRepresentation]; + [defs release]; + return locale; +} +