1996-05-28 13:37:17 +00:00
|
|
|
|
/* Mutual exclusion locking classes
|
1996-02-13 15:40:05 +00:00
|
|
|
|
Copyright (C) 1996 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
Author: Scott Christley <scottc@net-community.com>
|
1996-05-28 13:37:17 +00:00
|
|
|
|
Created: 1996
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
|
|
|
|
This file is part of the GNUstep Objective-C Library.
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Library 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 Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
1996-02-13 15:40:05 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
|
#include <config.h>
|
1999-09-09 02:56:20 +00:00
|
|
|
|
#include <errno.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
|
#include <base/preface.h>
|
1996-02-13 15:40:05 +00:00
|
|
|
|
#include <Foundation/NSLock.h>
|
1996-05-31 17:27:45 +00:00
|
|
|
|
#include <Foundation/NSException.h>
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
1998-04-20 14:13:19 +00:00
|
|
|
|
// Exceptions
|
|
|
|
|
|
|
|
|
|
NSString *NSLockException = @"NSLockException";
|
|
|
|
|
NSString *NSConditionLockException = @"NSConditionLockException";
|
|
|
|
|
NSString *NSRecursiveLockException = @"NSRecursiveLockException";
|
|
|
|
|
|
|
|
|
|
// Macros
|
|
|
|
|
|
|
|
|
|
#define CHECK_RECURSIVE_LOCK(mutex) \
|
|
|
|
|
{ \
|
|
|
|
|
if ((mutex)->owner == objc_thread_id()) \
|
|
|
|
|
{ \
|
|
|
|
|
[NSException \
|
|
|
|
|
raise:NSLockException \
|
|
|
|
|
format:@"Thread attempted to recursively lock"]; \
|
|
|
|
|
/* NOT REACHED */ \
|
|
|
|
|
} \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define CHECK_RECURSIVE_CONDITION_LOCK(mutex) \
|
|
|
|
|
{ \
|
|
|
|
|
if ((mutex)->owner == objc_thread_id()) \
|
|
|
|
|
{ \
|
|
|
|
|
[NSException \
|
|
|
|
|
raise:NSConditionLockException \
|
|
|
|
|
format:@"Thread attempted to recursively lock"]; \
|
|
|
|
|
/* NOT REACHED */ \
|
|
|
|
|
} \
|
|
|
|
|
}
|
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
|
// NSLock class
|
|
|
|
|
// Simplest lock for protecting critical sections of code
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
@implementation NSLock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Designated initializer
|
1996-02-13 15:40:05 +00:00
|
|
|
|
- init
|
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
[super init];
|
|
|
|
|
|
|
|
|
|
// Allocate the mutex from the runtime
|
|
|
|
|
mutex = objc_mutex_allocate();
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (!mutex)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Failed to allocate a mutex");
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1996-05-28 13:37:17 +00:00
|
|
|
|
return self;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (void) dealloc
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to deallocate the mutex
|
|
|
|
|
// If there are outstanding locks then it will block
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (objc_mutex_deallocate (mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSLockException
|
|
|
|
|
format:@"invalid mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-05-28 13:37:17 +00:00
|
|
|
|
[super dealloc];
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to acquire the lock
|
|
|
|
|
// Does not block
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (BOOL) tryLock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
CHECK_RECURSIVE_LOCK (mutex);
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
|
|
|
|
if (objc_mutex_trylock (mutex) == -1)
|
|
|
|
|
return NO;
|
|
|
|
|
else
|
|
|
|
|
return YES;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-11-16 19:36:51 +00:00
|
|
|
|
- (BOOL)lockBeforeDate:(NSDate *)limit
|
|
|
|
|
{
|
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
|
// NSLocking protocol
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (void) lock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
CHECK_RECURSIVE_LOCK (mutex);
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
|
|
|
|
// This will block
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (objc_mutex_lock (mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSLockException
|
|
|
|
|
format:@"failed to lock mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)unlock
|
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to release a lock on the mutex
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (objc_mutex_unlock (mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSLockException
|
|
|
|
|
format:@"unlock: failed to unlock mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
|
// NSConditionLock
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Allows locking and unlocking to be based upon an integer condition
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
@implementation NSConditionLock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
|
|
|
|
- init
|
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
return [self initWithCondition: 0];
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Designated initializer
|
1996-02-13 15:40:05 +00:00
|
|
|
|
// Initialize lock with condition
|
|
|
|
|
- (id)initWithCondition:(int)value
|
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
[super init];
|
|
|
|
|
|
1998-04-20 14:13:19 +00:00
|
|
|
|
condition_value = value;
|
1996-05-28 13:37:17 +00:00
|
|
|
|
|
|
|
|
|
// Allocate the mutex from the runtime
|
1998-04-20 14:13:19 +00:00
|
|
|
|
condition = objc_condition_allocate ();
|
|
|
|
|
if (!condition)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Failed to allocate a condition");
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1996-05-28 13:37:17 +00:00
|
|
|
|
mutex = objc_mutex_allocate ();
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (!mutex)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Failed to allocate a mutex");
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1996-05-28 13:37:17 +00:00
|
|
|
|
return self;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to deallocate the mutex
|
|
|
|
|
// If there are outstanding locks then it will block
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (objc_condition_deallocate (condition) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"dealloc: invalid condition"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
if (objc_mutex_deallocate (mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"dealloc: invalid mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-05-28 13:37:17 +00:00
|
|
|
|
[super dealloc];
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return the current condition of the lock
|
|
|
|
|
- (int)condition
|
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
return condition_value;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Acquiring and release the lock
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (void) lockWhenCondition: (int)value
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK (mutex);
|
1996-05-28 13:37:17 +00:00
|
|
|
|
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (objc_mutex_lock (mutex) == -1)
|
1996-05-28 13:37:17 +00:00
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"lockWhenCondition: failed to lock mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (condition_value != value)
|
|
|
|
|
{
|
|
|
|
|
if (objc_condition_wait (condition,mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"objc_condition_wait failed"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-05-28 13:37:17 +00:00
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (void) unlockWithCondition: (int)value
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
int depth;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// First check to make sure we have the lock
|
1998-04-20 14:13:19 +00:00
|
|
|
|
depth = objc_mutex_trylock (mutex);
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Another thread has the lock so abort
|
|
|
|
|
if (depth == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"unlockWithCondition: Tried to unlock someone else's lock"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// If the depth is only 1 then we just acquired
|
|
|
|
|
// the lock above, bogus unlock so abort
|
|
|
|
|
if (depth == 1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"unlockWithCondition: Unlock attempted without lock"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// This is a valid unlock so set the condition
|
1998-04-20 14:13:19 +00:00
|
|
|
|
condition_value = value;
|
|
|
|
|
|
|
|
|
|
// wake up blocked threads
|
|
|
|
|
if (objc_condition_broadcast(condition) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"unlockWithCondition: objc_condition_broadcast failed"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// and unlock twice
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if ((objc_mutex_unlock (mutex) == -1)
|
|
|
|
|
|| (objc_mutex_unlock (mutex) == -1))
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"unlockWithCondition: failed to unlock mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (BOOL) tryLock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK (mutex);
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
|
|
|
|
if (objc_mutex_trylock(mutex) == -1)
|
|
|
|
|
return NO;
|
|
|
|
|
else
|
|
|
|
|
return YES;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (BOOL) tryLockWhenCondition: (int)value
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
// tryLock message will check for recursive locks
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// First can we even get the lock?
|
|
|
|
|
if (![self tryLock])
|
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
|
|
// If we got the lock is it the right condition?
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (condition_value == value)
|
1996-05-28 13:37:17 +00:00
|
|
|
|
return YES;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Wrong condition so release the lock
|
|
|
|
|
[self unlock];
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-11-16 19:36:51 +00:00
|
|
|
|
// Acquiring the lock with a date condition
|
|
|
|
|
- (BOOL)lockBeforeDate:(NSDate *)limit
|
|
|
|
|
{
|
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
1999-09-09 02:56:20 +00:00
|
|
|
|
|
|
|
|
|
- (BOOL)lockWhenCondition:(int)condition_to_meet
|
|
|
|
|
beforeDate:(NSDate *)limitDate
|
1998-11-16 19:36:51 +00:00
|
|
|
|
{
|
1999-09-09 02:56:20 +00:00
|
|
|
|
#ifndef HAVE_OBJC_CONDITION_TIMEDWAIT
|
1998-11-16 19:36:51 +00:00
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
return NO;
|
1999-09-09 02:56:20 +00:00
|
|
|
|
#else
|
|
|
|
|
NSTimeInterval atimeinterval;
|
|
|
|
|
struct timespec endtime;
|
|
|
|
|
|
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK (mutex);
|
|
|
|
|
|
|
|
|
|
if( -1 == objc_mutex_lock(mutex) )
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"lockWhenCondition: failed to lock mutex"];
|
|
|
|
|
|
|
|
|
|
if( condition_value == condition_to_meet )
|
|
|
|
|
return YES;
|
|
|
|
|
|
|
|
|
|
atimeinterval = [limitDate timeIntervalSince1970];
|
|
|
|
|
endtime.tv_sec =(unsigned int)atimeinterval; // 941883028;//
|
|
|
|
|
endtime.tv_nsec = (unsigned int)((atimeinterval - (float)endtime.tv_sec)
|
|
|
|
|
* 1000000000.0);
|
|
|
|
|
|
|
|
|
|
while( condition_value != condition_to_meet )
|
|
|
|
|
{
|
|
|
|
|
switch(objc_condition_timedwait(condition,mutex,&endtime))
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
break;
|
|
|
|
|
case EINTR:
|
|
|
|
|
break;
|
|
|
|
|
case ETIMEDOUT :
|
|
|
|
|
[self unlock];
|
|
|
|
|
return NO;
|
|
|
|
|
default:
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"objc_condition_timedwait failed"];
|
|
|
|
|
[self unlock];
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
#endif HAVE__OBJC_CONDITION_TIMEDWAIT
|
1998-11-16 19:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
|
// NSLocking protocol
|
|
|
|
|
// These methods ignore the condition
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (void) lock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK (mutex);
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
|
|
|
|
// This will block
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (objc_mutex_lock (mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"lock: failed to lock mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)unlock
|
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
// wake up blocked threads
|
|
|
|
|
if (objc_condition_broadcast(condition) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"unlockWithCondition: objc_condition_broadcast failed"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to release a lock on the mutex
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (objc_mutex_unlock (mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSConditionLockException
|
|
|
|
|
format:@"unlock: failed to unlock mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
|
// NSRecursiveLock
|
|
|
|
|
// Allows the lock to be recursively acquired by the same thread
|
|
|
|
|
//
|
|
|
|
|
// If the same thread locks the mutex (n) times then that same
|
|
|
|
|
// thread must also unlock it (n) times before another thread
|
|
|
|
|
// can acquire the lock.
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
@implementation NSRecursiveLock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
1998-04-20 14:13:19 +00:00
|
|
|
|
// Designated initializer
|
1996-02-13 15:40:05 +00:00
|
|
|
|
- init
|
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
[super init];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Allocate the mutex from the runtime
|
|
|
|
|
mutex = objc_mutex_allocate();
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (!mutex)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Failed to allocate a mutex");
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1996-05-28 13:37:17 +00:00
|
|
|
|
return self;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (void) dealloc
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to deallocate the mutex
|
1998-04-20 14:13:19 +00:00
|
|
|
|
// If there are outstanding locks then it will block
|
|
|
|
|
if (objc_mutex_deallocate (mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSRecursiveLockException
|
|
|
|
|
format:@"dealloc: invalid mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-05-28 13:37:17 +00:00
|
|
|
|
[super dealloc];
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to acquire the lock
|
|
|
|
|
// Does not block
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (BOOL) tryLock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
|
|
|
|
if (objc_mutex_trylock (mutex) == -1)
|
|
|
|
|
return NO;
|
|
|
|
|
else
|
|
|
|
|
return YES;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-11-16 19:36:51 +00:00
|
|
|
|
- (BOOL)lockBeforeDate:(NSDate *)limit
|
|
|
|
|
{
|
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
|
// NSLocking protocol
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (void) lock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
|
|
|
|
// This will block
|
1998-04-20 14:13:19 +00:00
|
|
|
|
if (objc_mutex_lock (mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSRecursiveLockException
|
|
|
|
|
format:@"lock: failed to lock mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-04-20 14:13:19 +00:00
|
|
|
|
- (void)unlock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
// Ask the runtime to release a lock on the mutex
|
|
|
|
|
if (objc_mutex_unlock (mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSRecursiveLockException
|
|
|
|
|
format:@"unlock: failed to unlock mutex"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|