From 5ffc526cbc784c2c9c252a34b4580a46a8908a54 Mon Sep 17 00:00:00 2001 From: Richard Frith-Macdonald Date: Sun, 8 Feb 2004 09:42:38 +0000 Subject: [PATCH] More simplification by using lazy locking git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@18559 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 9 ++++ Source/GSAttributedString.m | 30 +++----------- Source/GSTcpPort.m | 82 ++----------------------------------- Source/NSCharacterSet.m | 26 +----------- Source/NSCountedSet.m | 32 ++------------- Source/NSDebug.m | 22 +--------- Source/NSMessagePort.m | 74 ++------------------------------- Source/NSSocketPort.m | 82 ++----------------------------------- Source/NSTimeZone.m | 30 +------------- 9 files changed, 37 insertions(+), 350 deletions(-) diff --git a/ChangeLog b/ChangeLog index d1b3b2af8..2aae16df2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,15 @@ * Source/NSConection.m: Simplify code by using GSLazyLock and GSRecursiveLazylock. Removed unused include of float.h + * Source/GSAttributedString.m: + * Source/GSTcpPort.m: + * Source/NSCharacterSet.m: + * Source/NSCountedSet.m: + * Source/NSDebug.m: + * Source/NSMessagePort.m: + * Source/NSSocketPort.m: + * Source/NSTimeZone.m: + Simplify by using lazy locking. 2004-02-07 Adam Fedor diff --git a/Source/GSAttributedString.m b/Source/GSAttributedString.m index 2fae0aa5b..c32a9492c 100644 --- a/Source/GSAttributedString.m +++ b/Source/GSAttributedString.m @@ -45,6 +45,7 @@ #include "config.h" #include "GNUstepBase/preface.h" +#include "GNUstepBase/GSLock.h" #include "Foundation/NSAttributedString.h" #include "Foundation/NSException.h" #include "Foundation/NSRange.h" @@ -427,34 +428,15 @@ _attributesAtIndexEffectiveRange( return nil; } -/* - * If we are multi-threaded, we must guard access to the uniquing set. - */ -+ (void) _becomeThreaded: (id)notification -{ - attrLock = [NSLock new]; - lockSel = @selector(lock); - unlockSel = @selector(unlock); - lockImp = [attrLock methodForSelector: lockSel]; - unlockImp = [attrLock methodForSelector: unlockSel]; -} - + (void) initialize { _setup(); - if ([NSThread isMultiThreaded]) - { - [self _becomeThreaded: nil]; - } - else - { - [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(_becomeThreaded:) - name: NSWillBecomeMultiThreadedNotification - object: nil]; - } + attrLock = [GSLazyLock new]; + lockSel = @selector(lock); + unlockSel = @selector(unlock); + lockImp = [attrLock methodForSelector: lockSel]; + unlockImp = [attrLock methodForSelector: unlockSel]; } - (id) initWithString: (NSString*)aString diff --git a/Source/GSTcpPort.m b/Source/GSTcpPort.m index f976238a1..b7c563a23 100644 --- a/Source/GSTcpPort.m +++ b/Source/GSTcpPort.m @@ -23,6 +23,7 @@ #include "config.h" #include "GNUstepBase/preface.h" +#include "GNUstepBase/GSLock.h" #include "Foundation/NSArray.h" #include "Foundation/NSNotification.h" #include "Foundation/NSException.h" @@ -97,8 +98,6 @@ #endif /* !__MINGW__ */ -static BOOL multi_threaded = NO; - /* * Largest chunk of data possible in DO */ @@ -431,10 +430,7 @@ static Class runLoopClass; handle = (GSTcpHandle*)NSAllocateObject(self, 0, NSDefaultMallocZone()); handle->desc = d; handle->wMsgs = [NSMutableArray new]; - if (multi_threaded == YES) - { - handle->myLock = [NSRecursiveLock new]; - } + handle->myLock = [GSLazyRecursiveLock new]; handle->valid = YES; return AUTORELEASE(handle); } @@ -1277,62 +1273,6 @@ static NSRecursiveLock *tcpPortLock = nil; static NSMapTable *tcpPortMap = 0; static Class tcpPortClass; -/* - * When the system becomes multithreaded, we set a flag to say so and - * make sure that port and handle locking is enabled. - */ -+ (void) _becomeThreaded: (NSNotification*)notification -{ - if (multi_threaded == NO) - { - NSMapEnumerator pEnum; - NSMapTable *m; - void *dummy; - - multi_threaded = YES; - if (tcpPortLock == nil) - { - tcpPortLock = [NSRecursiveLock new]; - } - pEnum = NSEnumerateMapTable(tcpPortMap); - while (NSNextMapEnumeratorPair(&pEnum, &dummy, (void**)&m)) - { - NSMapEnumerator mEnum; - GSTcpPort *p; - - mEnum = NSEnumerateMapTable(m); - while (NSNextMapEnumeratorPair(&mEnum, &dummy, (void**)&p)) - { - if ([p isValid] == YES) - { - NSMapEnumerator hEnum; - GSTcpHandle *h; - - if (p->myLock == nil) - { - p->myLock = [NSRecursiveLock new]; - } - hEnum = NSEnumerateMapTable(p->handles); - while (NSNextMapEnumeratorPair(&hEnum, &dummy, (void**)&h)) - { - if ([h isValid] == YES && h->myLock == nil) - { - h->myLock = [NSRecursiveLock new]; - } - } - NSEndMapTableEnumeration(&hEnum); - } - } - NSEndMapTableEnumeration(&mEnum); - } - NSEndMapTableEnumeration(&pEnum); - } - [[NSNotificationCenter defaultCenter] - removeObserver: self - name: NSWillBecomeMultiThreadedNotification - object: nil]; -} - #if NEED_WORD_ALIGNMENT static unsigned wordAlign; #endif @@ -1348,18 +1288,7 @@ static unsigned wordAlign; tcpPortMap = NSCreateMapTable(NSIntMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, 0); - if ([NSThread isMultiThreaded]) - { - [self _becomeThreaded: nil]; - } - else - { - [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(_becomeThreaded:) - name: NSWillBecomeMultiThreadedNotification - object: nil]; - } + tcpPortLock = [GSLazyRecursiveLock new]; } } @@ -1455,10 +1384,7 @@ static unsigned wordAlign; port->address = [addr copy]; port->handles = NSCreateMapTable(NSIntMapKeyCallBacks, NSObjectMapValueCallBacks, 0); - if (multi_threaded == YES) - { - port->myLock = [NSRecursiveLock new]; - } + port->myLock = [GSLazyRecursiveLock new]; port->_is_valid = YES; if (shouldListen == YES && [thisHost isEqual: aHost]) diff --git a/Source/NSCharacterSet.m b/Source/NSCharacterSet.m index 03cd66bb9..781a677c1 100644 --- a/Source/NSCharacterSet.m +++ b/Source/NSCharacterSet.m @@ -25,6 +25,7 @@ */ #include "config.h" +#include "GNUstepBase/GSLock.h" #include "Foundation/NSArray.h" #include "Foundation/NSBitmapCharSet.h" #include "Foundation/NSException.h" @@ -46,18 +47,6 @@ static Class abstractClass = nil; @implementation NSCharacterSet -+ (void) _becomeThreaded: (NSNotification*)notification -{ - if (cache_lock == nil) - { - cache_lock = [NSLock new]; - [[NSNotificationCenter defaultCenter] - removeObserver: self - name: NSWillBecomeMultiThreadedNotification - object: nil]; - } -} - + (void) initialize { static BOOL one_time = NO; @@ -67,18 +56,7 @@ static Class abstractClass = nil; abstractClass = [NSCharacterSet class]; one_time = YES; } - if ([NSThread isMultiThreaded]) - { - [self _becomeThreaded: nil]; - } - else - { - [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(_becomeThreaded:) - name: NSWillBecomeMultiThreadedNotification - object: nil]; - } + cache_lock = [GSLazyLock new]; } /* Provide a default object for allocation */ diff --git a/Source/NSCountedSet.m b/Source/NSCountedSet.m index a6a02224a..5b7bcf960 100644 --- a/Source/NSCountedSet.m +++ b/Source/NSCountedSet.m @@ -25,6 +25,7 @@ */ #include "config.h" +#include "GNUstepBase/GSLock.h" #include "Foundation/NSSet.h" #include "Foundation/NSCoder.h" #include "Foundation/NSArray.h" @@ -47,10 +48,6 @@ static IMP lockImp = 0; static IMP unlockImp = 0; static BOOL uniquing = NO; -@interface NSCountedSet (GSThreading) -+ (void) _becomeThreaded: (id)notification; -@end - /** *

* The NSCountedSet class is used to maintain a set of objects where @@ -73,18 +70,9 @@ static Class NSCountedSet_concrete_class; { NSCountedSet_abstract_class = self; NSCountedSet_concrete_class = [GSCountedSet class]; - if ([NSThread isMultiThreaded]) - { - [self _becomeThreaded: nil]; - } - else - { - [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(_becomeThreaded:) - name: NSWillBecomeMultiThreadedNotification - object: nil]; - } + uniqueLock = [GSLazyLock new]; + lockImp = [uniqueLock methodForSelector: @selector(lock)]; + unlockImp = [uniqueLock methodForSelector: @selector(unlock)]; } } @@ -299,18 +287,6 @@ static Class NSCountedSet_concrete_class; } @end -@implementation NSCountedSet (GSThreading) -/* - * If we are multi-threaded, we must guard access to the uniquing set. - */ -+ (void) _becomeThreaded: (id)notification -{ - uniqueLock = [NSLock new]; - lockImp = [uniqueLock methodForSelector: @selector(lock)]; - unlockImp = [uniqueLock methodForSelector: @selector(unlock)]; -} -@end - /** * This function purges the global NSCountedSet object used for * uniquing. It handles locking as necessary. It can be used to diff --git a/Source/NSDebug.m b/Source/NSDebug.m index 39ce1b844..861439bad 100644 --- a/Source/NSDebug.m +++ b/Source/NSDebug.m @@ -28,6 +28,7 @@ #include "config.h" #include +#include "GNUstepBase/GSLock.h" #include "Foundation/NSData.h" #include "Foundation/NSDebug.h" #include "Foundation/NSString.h" @@ -70,32 +71,13 @@ void (*_GSDebugAllocationRemoveFunc)(Class c, id o) = _GSDebugAllocationRemove; @interface GSDebugAlloc : NSObject + (void) initialize; -+ (void) _becomeThreaded: (NSNotification*)notification; @end @implementation GSDebugAlloc - + (void) initialize { - if ([NSThread isMultiThreaded]) - { - [self _becomeThreaded: nil]; - } - else - { - [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(_becomeThreaded:) - name: NSWillBecomeMultiThreadedNotification - object: nil]; - } + uniqueLock = [GSLazyRecursiveLock new]; } - -+ (void) _becomeThreaded: (NSNotification*)notification -{ - uniqueLock = [NSRecursiveLock new]; -} - @end /** diff --git a/Source/NSMessagePort.m b/Source/NSMessagePort.m index 6b623b47e..b6cc389fa 100644 --- a/Source/NSMessagePort.m +++ b/Source/NSMessagePort.m @@ -23,6 +23,7 @@ #include "config.h" #include "GNUstepBase/preface.h" +#include "GNUstepBase/GSLock.h" #include "Foundation/NSArray.h" #include "Foundation/NSNotification.h" #include "Foundation/NSException.h" @@ -104,8 +105,6 @@ (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path)) #endif -static BOOL multi_threaded = NO; - /* * Largest chunk of data possible in DO */ @@ -343,10 +342,7 @@ static Class runLoopClass; handle = (GSMessageHandle*)NSAllocateObject(self, 0, NSDefaultMallocZone()); handle->desc = d; handle->wMsgs = [NSMutableArray new]; - if (multi_threaded == YES) - { - handle->myLock = [NSRecursiveLock new]; - } + handle->myLock = [GSLazyRecursiveLock new]; handle->valid = YES; return AUTORELEASE(handle); } @@ -1167,54 +1163,6 @@ static void clean_up_sockets(void) } -/* - * When the system becomes multithreaded, we set a flag to say so and - * make sure that port and handle locking is enabled. - */ -+ (void) _becomeThreaded: (NSNotification*)notification -{ - if (multi_threaded == NO) - { - NSMapEnumerator mEnum; - NSMessagePort *p; - void *dummy; - - multi_threaded = YES; - if (messagePortLock == nil) - { - messagePortLock = [NSRecursiveLock new]; - } - mEnum = NSEnumerateMapTable(messagePortMap); - while (NSNextMapEnumeratorPair(&mEnum, &dummy, (void**)&p)) - { - if ([p isValid] == YES) - { - NSMapEnumerator hEnum; - GSMessageHandle *h; - - if (p->myLock == nil) - { - p->myLock = [NSRecursiveLock new]; - } - hEnum = NSEnumerateMapTable(p->handles); - while (NSNextMapEnumeratorPair(&hEnum, &dummy, (void**)&h)) - { - if ([h isValid] == YES && h->myLock == nil) - { - h->myLock = [NSRecursiveLock new]; - } - } - NSEndMapTableEnumeration(&hEnum); - } - } - NSEndMapTableEnumeration(&mEnum); - } - [[NSNotificationCenter defaultCenter] - removeObserver: self - name: NSWillBecomeMultiThreadedNotification - object: nil]; -} - #if NEED_WORD_ALIGNMENT static unsigned wordAlign; #endif @@ -1230,18 +1178,7 @@ static unsigned wordAlign; messagePortMap = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, 0); - if ([NSThread isMultiThreaded]) - { - [self _becomeThreaded: nil]; - } - else - { - [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(_becomeThreaded:) - name: NSWillBecomeMultiThreadedNotification - object: nil]; - } + messagePortLock = [GSLazyRecursiveLock new]; atexit(clean_up_sockets); } } @@ -1294,10 +1231,7 @@ static int unique_index = 0; port->listener = -1; port->handles = NSCreateMapTable(NSIntMapKeyCallBacks, NSObjectMapValueCallBacks, 0); - if (multi_threaded == YES) - { - port->myLock = [NSRecursiveLock new]; - } + port->myLock = [GSLazyRecursiveLock new]; port->_is_valid = YES; if (shouldListen == YES) diff --git a/Source/NSSocketPort.m b/Source/NSSocketPort.m index fe313c493..6690dfd68 100644 --- a/Source/NSSocketPort.m +++ b/Source/NSSocketPort.m @@ -23,6 +23,7 @@ #include "config.h" #include "GNUstepBase/preface.h" +#include "GNUstepBase/GSLock.h" #include "Foundation/NSArray.h" #include "Foundation/NSNotification.h" #include "Foundation/NSException.h" @@ -97,8 +98,6 @@ #endif /* !__MINGW__ */ -static BOOL multi_threaded = NO; - /* * Largest chunk of data possible in DO */ @@ -395,10 +394,7 @@ static Class runLoopClass; handle = (GSTcpHandle*)NSAllocateObject(self, 0, NSDefaultMallocZone()); handle->desc = d; handle->wMsgs = [NSMutableArray new]; - if (multi_threaded == YES) - { - handle->myLock = [NSRecursiveLock new]; - } + handle->myLock = [GSLazyRecursiveLock new]; handle->valid = YES; return AUTORELEASE(handle); } @@ -1256,62 +1252,6 @@ static NSRecursiveLock *tcpPortLock = nil; static NSMapTable *tcpPortMap = 0; static Class tcpPortClass; -/* - * When the system becomes multithreaded, we set a flag to say so and - * make sure that port and handle locking is enabled. - */ -+ (void) _becomeThreaded: (NSNotification*)notification -{ - if (multi_threaded == NO) - { - NSMapEnumerator pEnum; - NSMapTable *m; - void *dummy; - - multi_threaded = YES; - if (tcpPortLock == nil) - { - tcpPortLock = [NSRecursiveLock new]; - } - pEnum = NSEnumerateMapTable(tcpPortMap); - while (NSNextMapEnumeratorPair(&pEnum, &dummy, (void**)&m)) - { - NSMapEnumerator mEnum; - NSSocketPort *p; - - mEnum = NSEnumerateMapTable(m); - while (NSNextMapEnumeratorPair(&mEnum, &dummy, (void**)&p)) - { - if ([p isValid] == YES) - { - NSMapEnumerator hEnum; - GSTcpHandle *h; - - if (p->myLock == nil) - { - p->myLock = [NSRecursiveLock new]; - } - hEnum = NSEnumerateMapTable(p->handles); - while (NSNextMapEnumeratorPair(&hEnum, &dummy, (void**)&h)) - { - if ([h isValid] == YES && h->myLock == nil) - { - h->myLock = [NSRecursiveLock new]; - } - } - NSEndMapTableEnumeration(&hEnum); - } - } - NSEndMapTableEnumeration(&mEnum); - } - NSEndMapTableEnumeration(&pEnum); - } - [[NSNotificationCenter defaultCenter] - removeObserver: self - name: NSWillBecomeMultiThreadedNotification - object: nil]; -} - #if NEED_WORD_ALIGNMENT static unsigned wordAlign; #endif @@ -1327,18 +1267,7 @@ static unsigned wordAlign; tcpPortMap = NSCreateMapTable(NSIntMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, 0); - if ([NSThread isMultiThreaded]) - { - [self _becomeThreaded: nil]; - } - else - { - [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(_becomeThreaded:) - name: NSWillBecomeMultiThreadedNotification - object: nil]; - } + tcpPortLock = [GSLazyRecursiveLock new]; } } @@ -1434,10 +1363,7 @@ static unsigned wordAlign; port->address = [addr copy]; port->handles = NSCreateMapTable(NSIntMapKeyCallBacks, NSObjectMapValueCallBacks, 0); - if (multi_threaded == YES) - { - port->myLock = [NSRecursiveLock new]; - } + port->myLock = [GSLazyRecursiveLock new]; port->_is_valid = YES; if (shouldListen == YES && [thisHost isEqual: aHost]) diff --git a/Source/NSTimeZone.m b/Source/NSTimeZone.m index d0f5dc2ec..622a48301 100644 --- a/Source/NSTimeZone.m +++ b/Source/NSTimeZone.m @@ -50,6 +50,7 @@ #include "config.h" #include "GNUstepBase/preface.h" +#include "GNUstepBase/GSLock.h" #include #include #include @@ -265,7 +266,6 @@ static NSString *_time_zone_path(NSString *subpath) /* Private methods for obtaining resource file names. */ @interface NSTimeZone (Private) -+ (void) _becomeThreaded: (NSNotification*)notification; + (NSString*) getAbbreviationFile; + (NSString*) getRegionsFile; + (NSString*) getTimeZoneFile: (NSString*)name; @@ -1015,18 +1015,7 @@ static NSMapTable *absolutes = 0; localTimeZone = [[NSLocalTimeZone alloc] init]; fake_abbrev_dict = [[NSInternalAbbrevDict alloc] init]; - if ([NSThread isMultiThreaded]) - { - [self _becomeThreaded: nil]; - } - else - { - [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(_becomeThreaded:) - name: NSWillBecomeMultiThreadedNotification - object: nil]; - } + zone_mutex = [GSLazyRecursiveLock new]; } } @@ -1488,21 +1477,6 @@ static NSMapTable *absolutes = 0; @implementation NSTimeZone (Private) -/* - * When the system becomes multithreaded, we set a flag to say so - */ -+ (void) _becomeThreaded: (NSNotification*)notification -{ - if (zone_mutex == nil) - { - zone_mutex = [NSRecursiveLock new]; - } - [[NSNotificationCenter defaultCenter] - removeObserver: self - name: NSWillBecomeMultiThreadedNotification - object: nil]; -} - + (NSString*) getAbbreviationFile { return _time_zone_path (ABBREV_DICT);