2010-02-14 10:48:10 +00:00
|
|
|
/* Implementation of extension methods to base additions
|
|
|
|
|
|
|
|
Copyright (C) 2010 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
|
|
|
|
|
|
|
*/
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
#import "GNUstepBase/NSLock+GNUstepBase.h"
|
|
|
|
#import "GNUstepBase/GSLock.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GNUstep specific (non-standard) additions to the NSLock class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static GSLazyRecursiveLock *local_lock = nil;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This class only exists to provide
|
|
|
|
a thread safe mechanism to initialize local_lock
|
|
|
|
as +initialize is called under a lock in ObjC runtimes.
|
|
|
|
User code should resort to GS_INITIALIZED_LOCK(),
|
|
|
|
which uses the +newLockAt: extension.
|
|
|
|
*/
|
|
|
|
|
|
|
|
@interface _GSLockInitializer : NSObject
|
|
|
|
@end
|
|
|
|
@implementation _GSLockInitializer
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (local_lock == nil)
|
|
|
|
{
|
|
|
|
/* As we do not know whether creating custom locks
|
|
|
|
may implicitly create other locks,
|
|
|
|
we use a recursive lock. */
|
|
|
|
local_lock = [GSLazyRecursiveLock new];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2010-02-25 05:09:44 +00:00
|
|
|
static inline id
|
2010-02-14 10:48:10 +00:00
|
|
|
newLockAt(Class self, SEL _cmd, id *location)
|
|
|
|
{
|
|
|
|
if (location == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"'%@' called with nil location",
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*location == nil)
|
|
|
|
{
|
|
|
|
if (local_lock == nil)
|
|
|
|
{
|
|
|
|
[_GSLockInitializer class];
|
|
|
|
}
|
|
|
|
|
|
|
|
[local_lock lock];
|
|
|
|
|
|
|
|
if (*location == nil)
|
|
|
|
{
|
|
|
|
*location = [[(id)self alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
[local_lock unlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
return *location;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-18 16:27:32 +00:00
|
|
|
@implementation NSLock (GNUstepBase)
|
2010-02-14 10:48:10 +00:00
|
|
|
+ (id) newLockAt: (id *)location
|
|
|
|
{
|
|
|
|
return newLockAt(self, _cmd, location);
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2010-02-18 16:27:32 +00:00
|
|
|
@implementation NSRecursiveLock (GNUstepBase)
|
2010-02-14 10:48:10 +00:00
|
|
|
+ (id) newLockAt: (id *)location
|
|
|
|
{
|
|
|
|
return newLockAt(self, _cmd, location);
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|