mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 16:30:41 +00:00
Include <gnustep/base/preface.h> first! Fix indentation.
([NSLock +initialize]): Method removed. ([NSLock -init]): Assert return value from objc_mutex_allocate(), don't just return nil. ([NSConditionLock -initWithCondition:]): Likewise. ([NSRecursiveLock -init]): Likewise. ([NSConditionLock +initialize]): Method removed. ([NSRecursiveLock +initialize]): Method removed. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1566 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
9f4e247b78
commit
28d87a29cd
1 changed files with 125 additions and 172 deletions
297
Source/NSLock.m
297
Source/NSLock.m
|
@ -1,12 +1,8 @@
|
||||||
/*
|
/* Mutual exclusion locking classes
|
||||||
NSLock.m
|
|
||||||
|
|
||||||
Mutual exclusion locking classes
|
|
||||||
|
|
||||||
Copyright (C) 1996 Free Software Foundation, Inc.
|
Copyright (C) 1996 Free Software Foundation, Inc.
|
||||||
|
|
||||||
Author: Scott Christley <scottc@net-community.com>
|
Author: Scott Christley <scottc@net-community.com>
|
||||||
Date: 1996
|
Created: 1996
|
||||||
|
|
||||||
This file is part of the GNUstep Objective-C Library.
|
This file is part of the GNUstep Objective-C Library.
|
||||||
|
|
||||||
|
@ -20,284 +16,241 @@
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
Library General Public License for more details.
|
Library General Public License for more details.
|
||||||
|
|
||||||
If you are interested in a warranty or support for this source code,
|
|
||||||
contact Scott Christley <scottc@net-community.com> for more information.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Library General Public
|
You should have received a copy of the GNU Library General Public
|
||||||
License along with this library; if not, write to the Free
|
License along with this library; if not, write to the Free
|
||||||
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <gnustep/base/preface.h>
|
||||||
#include <Foundation/NSLock.h>
|
#include <Foundation/NSLock.h>
|
||||||
|
|
||||||
//
|
|
||||||
// NSLock class
|
// NSLock class
|
||||||
// Simplest lock for protecting critical sections of code
|
// Simplest lock for protecting critical sections of code
|
||||||
//
|
|
||||||
@implementation NSLock
|
@implementation NSLock
|
||||||
|
|
||||||
// Class initialization
|
// Designated initializer
|
||||||
+ (void)initialize
|
|
||||||
{
|
|
||||||
if (self == [NSLock class])
|
|
||||||
{
|
|
||||||
// Initial version
|
|
||||||
[self setVersion:1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default initializer
|
|
||||||
- init
|
- init
|
||||||
{
|
{
|
||||||
[super init];
|
[super init];
|
||||||
|
|
||||||
// Allocate the mutex from the runtime
|
// Allocate the mutex from the runtime
|
||||||
mutex = objc_mutex_allocate();
|
mutex = objc_mutex_allocate();
|
||||||
if (!mutex)
|
NSAssertParameter (mutex);
|
||||||
return nil;
|
return self;
|
||||||
else
|
|
||||||
return self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)dealloc
|
- (void) dealloc
|
||||||
{
|
{
|
||||||
// Ask the runtime to deallocate the mutex
|
// Ask the runtime to deallocate the mutex
|
||||||
// If there are outstanding locks then it will block
|
// If there are outstanding locks then it will block
|
||||||
objc_mutex_deallocate(mutex);
|
objc_mutex_deallocate (mutex);
|
||||||
|
[super dealloc];
|
||||||
[super dealloc];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to acquire the lock
|
// Try to acquire the lock
|
||||||
// Does not block
|
// Does not block
|
||||||
- (BOOL)tryLock
|
- (BOOL) tryLock
|
||||||
{
|
{
|
||||||
// Ask the runtime to acquire a lock on the mutex
|
// Ask the runtime to acquire a lock on the mutex
|
||||||
if (objc_mutex_trylock(mutex) == -1)
|
if (objc_mutex_trylock (mutex) == -1)
|
||||||
return NO;
|
return NO;
|
||||||
else
|
else
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NSLocking protocol
|
// NSLocking protocol
|
||||||
- (void)lock
|
- (void) lock
|
||||||
{
|
{
|
||||||
// Ask the runtime to acquire a lock on the mutex
|
// Ask the runtime to acquire a lock on the mutex
|
||||||
// This will block
|
// This will block
|
||||||
objc_mutex_lock(mutex);
|
objc_mutex_lock (mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)unlock
|
- (void)unlock
|
||||||
{
|
{
|
||||||
// Ask the runtime to release a lock on the mutex
|
// Ask the runtime to release a lock on the mutex
|
||||||
objc_mutex_unlock(mutex);
|
objc_mutex_unlock (mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
//
|
|
||||||
// NSConditionLock
|
// NSConditionLock
|
||||||
// Allows locking and unlocking to be based upon a condition
|
// Allows locking and unlocking to be based upon an integer condition
|
||||||
//
|
|
||||||
@implementation NSConditionLock
|
|
||||||
|
|
||||||
// Class initialization
|
@implementation NSConditionLock
|
||||||
+ (void)initialize
|
|
||||||
{
|
|
||||||
if (self == [NSConditionLock class])
|
|
||||||
{
|
|
||||||
// Initial version
|
|
||||||
[self setVersion:1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
- init
|
- init
|
||||||
{
|
{
|
||||||
return [self initWithCondition:0];
|
return [self initWithCondition: 0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default initializer
|
// Designated initializer
|
||||||
// Initialize lock with condition
|
// Initialize lock with condition
|
||||||
- (id)initWithCondition:(int)value
|
- (id)initWithCondition:(int)value
|
||||||
{
|
{
|
||||||
[super init];
|
[super init];
|
||||||
|
|
||||||
|
condition = value;
|
||||||
|
|
||||||
condition = value;
|
// Allocate the mutex from the runtime
|
||||||
|
mutex = objc_mutex_allocate ();
|
||||||
// Allocate the mutex from the runtime
|
NSParameterAssert (mutex);
|
||||||
mutex = objc_mutex_allocate();
|
return self;
|
||||||
if (!mutex)
|
|
||||||
return nil;
|
|
||||||
else
|
|
||||||
return self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)dealloc
|
- (void)dealloc
|
||||||
{
|
{
|
||||||
// Ask the runtime to deallocate the mutex
|
// Ask the runtime to deallocate the mutex
|
||||||
// If there are outstanding locks then it will block
|
// If there are outstanding locks then it will block
|
||||||
objc_mutex_deallocate(mutex);
|
objc_mutex_deallocate (mutex);
|
||||||
|
[super dealloc];
|
||||||
[super dealloc];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the current condition of the lock
|
// Return the current condition of the lock
|
||||||
- (int)condition
|
- (int)condition
|
||||||
{
|
{
|
||||||
return condition;
|
return condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Acquiring and release the lock
|
// Acquiring and release the lock
|
||||||
- (void)lockWhenCondition:(int)value
|
- (void) lockWhenCondition: (int)value
|
||||||
{
|
{
|
||||||
BOOL done;
|
BOOL done;
|
||||||
|
|
||||||
done = NO;
|
done = NO;
|
||||||
while (!done)
|
while (!done)
|
||||||
{
|
{
|
||||||
// Try to get the lock
|
// Try to get the lock
|
||||||
[self lock];
|
[self lock];
|
||||||
|
|
||||||
// Is it in the condition we are looking for?
|
// Is it in the condition we are looking for?
|
||||||
if (condition == value)
|
if (condition == value)
|
||||||
done = YES;
|
done = YES;
|
||||||
else
|
else
|
||||||
// Release the lock and keep waiting
|
// Release the lock and keep waiting
|
||||||
[self unlock];
|
[self unlock];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)unlockWithCondition:(int)value
|
- (void) unlockWithCondition: (int)value
|
||||||
{
|
{
|
||||||
int depth;
|
int depth;
|
||||||
|
|
||||||
// First check to make sure we have the lock
|
// First check to make sure we have the lock
|
||||||
depth= objc_mutex_trylock(mutex);
|
depth= objc_mutex_trylock (mutex);
|
||||||
|
|
||||||
// Another thread has the lock so abort
|
// Another thread has the lock so abort
|
||||||
if (depth == -1)
|
if (depth == -1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// If the depth is only 1 then we just acquired
|
// If the depth is only 1 then we just acquired
|
||||||
// the lock above, bogus unlock so abort
|
// the lock above, bogus unlock so abort
|
||||||
if (depth == 1)
|
if (depth == 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// This is a valid unlock so set the condition
|
// This is a valid unlock so set the condition
|
||||||
// and unlock twice
|
// and unlock twice
|
||||||
condition = value;
|
condition = value;
|
||||||
objc_mutex_unlock(mutex);
|
objc_mutex_unlock (mutex);
|
||||||
objc_mutex_unlock(mutex);
|
objc_mutex_unlock (mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)tryLock
|
- (BOOL) tryLock
|
||||||
{
|
{
|
||||||
// Ask the runtime to acquire a lock on the mutex
|
// Ask the runtime to acquire a lock on the mutex
|
||||||
if (objc_mutex_trylock(mutex) == -1)
|
if (objc_mutex_trylock(mutex) == -1)
|
||||||
return NO;
|
return NO;
|
||||||
else
|
else
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)tryLockWhenCondition:(int)value
|
- (BOOL) tryLockWhenCondition: (int)value
|
||||||
{
|
{
|
||||||
// First can we even get the lock?
|
// First can we even get the lock?
|
||||||
if (![self tryLock])
|
if (![self tryLock])
|
||||||
return NO;
|
return NO;
|
||||||
|
|
||||||
// If we got the lock is it the right condition?
|
// If we got the lock is it the right condition?
|
||||||
if (condition == value)
|
if (condition == value)
|
||||||
return YES;
|
return YES;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Wrong condition so release the lock
|
// Wrong condition so release the lock
|
||||||
[self unlock];
|
[self unlock];
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NSLocking protocol
|
// NSLocking protocol
|
||||||
// These methods ignore the condition
|
// These methods ignore the condition
|
||||||
- (void)lock
|
- (void) lock
|
||||||
{
|
{
|
||||||
// Ask the runtime to acquire a lock on the mutex
|
// Ask the runtime to acquire a lock on the mutex
|
||||||
// This will block
|
// This will block
|
||||||
objc_mutex_lock(mutex);
|
objc_mutex_lock (mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)unlock
|
- (void)unlock
|
||||||
{
|
{
|
||||||
// Ask the runtime to release a lock on the mutex
|
// Ask the runtime to release a lock on the mutex
|
||||||
objc_mutex_unlock(mutex);
|
objc_mutex_unlock (mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
//
|
|
||||||
// NSRecursiveLock
|
// NSRecursiveLock
|
||||||
// Allows the lock to be recursively acquired by the same thread
|
// Allows the lock to be recursively acquired by the same thread
|
||||||
//
|
//
|
||||||
// If the same thread locks the mutex (n) times then that same
|
// If the same thread locks the mutex (n) times then that same
|
||||||
// thread must also unlock it (n) times before another thread
|
// thread must also unlock it (n) times before another thread
|
||||||
// can acquire the lock.
|
// can acquire the lock.
|
||||||
//
|
|
||||||
@implementation NSRecursiveLock
|
|
||||||
|
|
||||||
// Class initialization
|
@implementation NSRecursiveLock
|
||||||
+ (void)initialize
|
|
||||||
{
|
|
||||||
if (self == [NSRecursiveLock class])
|
|
||||||
{
|
|
||||||
// Initial version
|
|
||||||
[self setVersion:1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default initializer
|
// Default initializer
|
||||||
- init
|
- init
|
||||||
{
|
{
|
||||||
[super init];
|
[super init];
|
||||||
|
// Allocate the mutex from the runtime
|
||||||
// Allocate the mutex from the runtime
|
mutex = objc_mutex_allocate();
|
||||||
mutex = objc_mutex_allocate();
|
NSParameterAssert (mutex);
|
||||||
if (!mutex)
|
return self;
|
||||||
return nil;
|
|
||||||
else
|
|
||||||
return self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)dealloc
|
- (void) dealloc
|
||||||
{
|
{
|
||||||
// Ask the runtime to deallocate the mutex
|
// Ask the runtime to deallocate the mutex
|
||||||
// If there are outstanding locks then it will block.
|
// If there are outstanding locks then it will block.
|
||||||
objc_mutex_deallocate(mutex);
|
objc_mutex_deallocate(mutex);
|
||||||
|
[super dealloc];
|
||||||
[super dealloc];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to acquire the lock
|
// Try to acquire the lock
|
||||||
// Does not block
|
// Does not block
|
||||||
- (BOOL)tryLock
|
- (BOOL) tryLock
|
||||||
{
|
{
|
||||||
// Ask the runtime to acquire a lock on the mutex
|
// Ask the runtime to acquire a lock on the mutex
|
||||||
if (objc_mutex_trylock(mutex) == -1)
|
if (objc_mutex_trylock (mutex) == -1)
|
||||||
return NO;
|
return NO;
|
||||||
else
|
else
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NSLocking protocol
|
// NSLocking protocol
|
||||||
- (void)lock
|
- (void) lock
|
||||||
{
|
{
|
||||||
// Ask the runtime to acquire a lock on the mutex
|
// Ask the runtime to acquire a lock on the mutex
|
||||||
// This will block
|
// This will block
|
||||||
objc_mutex_lock(mutex);
|
objc_mutex_lock (mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)unlock
|
- (void) unlock
|
||||||
{
|
{
|
||||||
// Ask the runtime to release a lock onthe mutex
|
// Ask the runtime to release a lock onthe mutex
|
||||||
objc_mutex_unlock(mutex);
|
objc_mutex_unlock (mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue