2001-12-17 14:31:42 +00:00
|
|
|
|
/** Mutual exclusion locking classes
|
1999-12-10 00:59:40 +00:00
|
|
|
|
Copyright (C) 1996 Free Software Foundation, Inc.
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
|
|
|
|
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
|
1999-12-10 00:59:40 +00:00
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
This library is distributed in the hope that it will be useful,
|
1996-02-13 15:40:05 +00:00
|
|
|
|
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
|
1999-12-10 00:59:40 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
|
|
<title>NSLock class reference</title>
|
|
|
|
|
$Date$ $Revision$
|
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>
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
1999-12-10 00:59:40 +00:00
|
|
|
|
#include <unistd.h>
|
2002-02-20 06:42:05 +00:00
|
|
|
|
#endif
|
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>
|
2000-09-14 09:37:13 +00:00
|
|
|
|
#include <Foundation/NSDebug.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 \
|
2000-09-14 08:48:05 +00:00
|
|
|
|
raise: NSLockException \
|
|
|
|
|
format: @"Thread attempted to recursively lock"]; \
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* NOT REACHED */ \
|
|
|
|
|
} \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define CHECK_RECURSIVE_CONDITION_LOCK(mutex) \
|
|
|
|
|
{ \
|
|
|
|
|
if ((mutex)->owner == objc_thread_id()) \
|
|
|
|
|
{ \
|
|
|
|
|
[NSException \
|
2000-09-14 08:48:05 +00:00
|
|
|
|
raise: NSConditionLockException \
|
|
|
|
|
format: @"Thread attempted to recursively lock"]; \
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* 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
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (id) init
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
self = [super init];
|
|
|
|
|
if (self != nil)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
// Allocate the mutex from the runtime
|
|
|
|
|
_mutex = objc_mutex_allocate();
|
|
|
|
|
if (_mutex == 0)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
NSLog(@"Failed to allocate a mutex");
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-04-20 14:13:19 +00:00
|
|
|
|
}
|
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
|
2000-09-14 08:48:05 +00:00
|
|
|
|
{
|
|
|
|
|
[self gcFinalize];
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) gcFinalize
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
if (_mutex != 0)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
// Ask the runtime to deallocate the mutex
|
|
|
|
|
// If there are outstanding locks then it will block
|
|
|
|
|
if (objc_mutex_deallocate(_mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
NSWarnMLog(@"objc_mutex_deallocate() failed");
|
|
|
|
|
}
|
1998-04-20 14:13:19 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2001-04-10 03:27:01 +00:00
|
|
|
|
/* Return NO if we're already locked */
|
|
|
|
|
if (_mutex->owner == objc_thread_id())
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_trylock(_mutex) == -1)
|
1999-12-13 12:14:01 +00:00
|
|
|
|
{
|
1999-12-10 00:59:40 +00:00
|
|
|
|
return NO;
|
1999-12-13 12:14:01 +00:00
|
|
|
|
}
|
2001-04-10 03:27:01 +00:00
|
|
|
|
return YES;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (BOOL) lockBeforeDate: (NSDate *)limit
|
1998-11-16 19:36:51 +00:00
|
|
|
|
{
|
1999-12-10 00:59:40 +00:00
|
|
|
|
int x;
|
|
|
|
|
|
2001-04-10 03:27:01 +00:00
|
|
|
|
/* This is really the behavior of OpenStep, if the current thread has
|
|
|
|
|
the lock, we just block until the time limit is up. Very odd */
|
|
|
|
|
while (_mutex->owner == objc_thread_id()
|
|
|
|
|
|| (x = objc_mutex_trylock(_mutex)) == -1)
|
1999-12-13 12:14:01 +00:00
|
|
|
|
{
|
|
|
|
|
NSDate *current = [NSDate date];
|
|
|
|
|
NSComparisonResult compare;
|
|
|
|
|
|
|
|
|
|
compare = [current compare: limit];
|
|
|
|
|
if (compare == NSOrderedSame || compare == NSOrderedDescending)
|
1999-12-10 00:59:40 +00:00
|
|
|
|
{
|
1999-12-13 12:14:01 +00:00
|
|
|
|
return NO;
|
1999-12-10 00:59:40 +00:00
|
|
|
|
}
|
1999-12-13 12:14:01 +00:00
|
|
|
|
/*
|
|
|
|
|
* This should probably be more accurate like usleep(250)
|
|
|
|
|
* but usleep is known to NOT be thread safe under all architectures.
|
|
|
|
|
*/
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
1999-12-10 00:59:40 +00:00
|
|
|
|
return YES;
|
1998-11-16 19:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
CHECK_RECURSIVE_LOCK(_mutex);
|
1998-04-20 14:13:19 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
|
|
|
|
// This will block
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_lock(_mutex) == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSLockException
|
|
|
|
|
format: @"failed to lock mutex"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (void) unlock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to release a lock on the mutex
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_unlock(_mutex) == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSLockException
|
2000-09-14 09:37:13 +00:00
|
|
|
|
format: @"unlock: failed to unlock mutex"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* 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
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (id) init
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
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
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (id) initWithCondition: (int)value
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
self = [super init];
|
|
|
|
|
if (self != nil)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
_condition_value = value;
|
|
|
|
|
|
|
|
|
|
// Allocate the mutex from the runtime
|
|
|
|
|
_condition = objc_condition_allocate ();
|
|
|
|
|
if (_condition == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Failed to allocate a condition");
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
_mutex = objc_mutex_allocate ();
|
|
|
|
|
if (_mutex == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Failed to allocate a mutex");
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-04-20 14:13:19 +00:00
|
|
|
|
}
|
1996-05-28 13:37:17 +00:00
|
|
|
|
return self;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (void) dealloc
|
2000-09-14 08:48:05 +00:00
|
|
|
|
{
|
|
|
|
|
[self gcFinalize];
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) gcFinalize
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
if (_condition != 0)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
// Ask the runtime to deallocate the condition
|
|
|
|
|
if (objc_condition_deallocate(_condition) == -1)
|
|
|
|
|
{
|
|
|
|
|
NSWarnMLog(@"objc_condition_deallocate() failed");
|
|
|
|
|
}
|
1998-04-20 14:13:19 +00:00
|
|
|
|
}
|
2000-09-14 09:37:13 +00:00
|
|
|
|
if (_mutex != 0)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
// Ask the runtime to deallocate the mutex
|
|
|
|
|
// If there are outstanding locks then it will block
|
|
|
|
|
if (objc_mutex_deallocate(_mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
NSWarnMLog(@"objc_mutex_deallocate() failed");
|
|
|
|
|
}
|
1998-04-20 14:13:19 +00:00
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return the current condition of the lock
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (int) condition
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +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
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK(_mutex);
|
1996-05-28 13:37:17 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_lock(_mutex) == -1)
|
1996-05-28 13:37:17 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"lockWhenCondition: failed to lock mutex"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
while (_condition_value != value)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_condition_wait(_condition, _mutex) == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"objc_condition_wait failed"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* 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
|
1999-09-16 07:21:34 +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
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"unlockWithCondition: Tried to unlock someone else's lock"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* 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
|
1999-12-10 00:59:40 +00:00
|
|
|
|
// the lock above, bogus unlock so abort
|
1996-05-28 13:37:17 +00:00
|
|
|
|
if (depth == 1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"unlockWithCondition: Unlock attempted without lock"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* 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
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_condition_value = value;
|
1998-04-20 14:13:19 +00:00
|
|
|
|
|
|
|
|
|
// wake up blocked threads
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_condition_broadcast(_condition) == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"unlockWithCondition: objc_condition_broadcast failed"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// and unlock twice
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if ((objc_mutex_unlock(_mutex) == -1)
|
|
|
|
|
|| (objc_mutex_unlock(_mutex) == -1))
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"unlockWithCondition: failed to unlock mutex"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* 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
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK(_mutex);
|
1998-04-20 14:13:19 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_trylock(_mutex) == -1)
|
1996-05-28 13:37:17 +00:00
|
|
|
|
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?
|
1999-09-16 07:21:34 +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
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (BOOL) lockBeforeDate: (NSDate*)limit
|
1999-12-10 00:59:40 +00:00
|
|
|
|
{
|
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK(_mutex);
|
|
|
|
|
|
1999-12-13 12:14:01 +00:00
|
|
|
|
while (objc_mutex_trylock(_mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
NSDate *current = [NSDate date];
|
|
|
|
|
NSComparisonResult compare;
|
|
|
|
|
|
|
|
|
|
compare = [current compare: limit];
|
|
|
|
|
if (compare == NSOrderedSame || compare == NSOrderedDescending)
|
1999-12-10 00:59:40 +00:00
|
|
|
|
{
|
1999-12-13 12:14:01 +00:00
|
|
|
|
return NO;
|
1999-12-10 00:59:40 +00:00
|
|
|
|
}
|
1999-12-13 12:14:01 +00:00
|
|
|
|
/*
|
|
|
|
|
* This should probably be more accurate like usleep(250)
|
|
|
|
|
* but usleep is known to NOT be thread safe under all architectures.
|
|
|
|
|
*/
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
1999-12-10 00:59:40 +00:00
|
|
|
|
return YES;
|
1998-11-16 19:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-09 02:56:20 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +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;
|
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK(_mutex);
|
1999-09-09 02:56:20 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (-1 == objc_mutex_lock(_mutex))
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"lockWhenCondition: failed to lock mutex"];
|
1999-09-09 02:56:20 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_condition_value == condition_to_meet)
|
1999-09-09 02:56:20 +00:00
|
|
|
|
return YES;
|
|
|
|
|
|
|
|
|
|
atimeinterval = [limitDate timeIntervalSince1970];
|
|
|
|
|
endtime.tv_sec =(unsigned int)atimeinterval; // 941883028;//
|
|
|
|
|
endtime.tv_nsec = (unsigned int)((atimeinterval - (float)endtime.tv_sec)
|
|
|
|
|
* 1000000000.0);
|
|
|
|
|
|
1999-12-13 12:14:01 +00:00
|
|
|
|
while (_condition_value != condition_to_meet)
|
1999-09-09 02:56:20 +00:00
|
|
|
|
{
|
1999-12-13 12:14:01 +00:00
|
|
|
|
switch (objc_condition_timedwait(_condition, _mutex, &endtime))
|
1999-09-09 02:56:20 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
case 0:
|
1999-12-13 12:14:01 +00:00
|
|
|
|
break;
|
2000-09-14 08:48:05 +00:00
|
|
|
|
case EINTR:
|
1999-12-13 12:14:01 +00:00
|
|
|
|
break;
|
2000-09-14 08:48:05 +00:00
|
|
|
|
case ETIMEDOUT :
|
1999-12-13 12:14:01 +00:00
|
|
|
|
[self unlock];
|
|
|
|
|
return NO;
|
2000-09-14 08:48:05 +00:00
|
|
|
|
default:
|
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"objc_condition_timedwait failed"];
|
1999-12-13 12:14:01 +00:00
|
|
|
|
[self unlock];
|
|
|
|
|
return NO;
|
1999-09-09 02:56:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
2001-02-27 15:54:17 +00:00
|
|
|
|
#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
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK(_mutex);
|
1998-04-20 14:13:19 +00:00
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
|
|
|
|
// This will block
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_lock(_mutex) == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"lock: failed to lock mutex"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (void) unlock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
1998-04-20 14:13:19 +00:00
|
|
|
|
// wake up blocked threads
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_condition_broadcast(_condition) == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"unlockWithCondition: objc_condition_broadcast failed"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to release a lock on the mutex
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_unlock(_mutex) == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"unlock: failed to unlock mutex"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* 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
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (id) init
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
self = [super init];
|
|
|
|
|
if (self != nil)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
// Allocate the mutex from the runtime
|
|
|
|
|
_mutex = objc_mutex_allocate();
|
|
|
|
|
if (_mutex == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Failed to allocate a mutex");
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-04-20 14:13:19 +00:00
|
|
|
|
}
|
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
|
2000-09-14 08:48:05 +00:00
|
|
|
|
{
|
|
|
|
|
[self gcFinalize];
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) gcFinalize
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
if (_mutex != 0)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 09:37:13 +00:00
|
|
|
|
// Ask the runtime to deallocate the mutex
|
|
|
|
|
// If there are outstanding locks then it will block
|
|
|
|
|
if (objc_mutex_deallocate(_mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
NSWarnMLog(@"objc_mutex_deallocate() failed");
|
|
|
|
|
}
|
1998-04-20 14:13:19 +00:00
|
|
|
|
}
|
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
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_trylock(_mutex) == -1)
|
1996-05-28 13:37:17 +00:00
|
|
|
|
return NO;
|
|
|
|
|
else
|
|
|
|
|
return YES;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
- (BOOL) lockBeforeDate: (NSDate *)limit
|
1998-11-16 19:36:51 +00:00
|
|
|
|
{
|
1999-12-13 12:14:01 +00:00
|
|
|
|
while (objc_mutex_trylock(_mutex) == -1)
|
|
|
|
|
{
|
|
|
|
|
NSDate *current = [NSDate date];
|
|
|
|
|
NSComparisonResult compare;
|
|
|
|
|
|
|
|
|
|
compare = [current compare: limit];
|
|
|
|
|
if (compare == NSOrderedSame || compare == NSOrderedDescending)
|
1999-12-10 00:59:40 +00:00
|
|
|
|
{
|
1999-12-13 12:14:01 +00:00
|
|
|
|
return NO;
|
1999-12-10 00:59:40 +00:00
|
|
|
|
}
|
1999-12-13 12:14:01 +00:00
|
|
|
|
/*
|
|
|
|
|
* This should probably be more accurate like usleep(250)
|
|
|
|
|
* but usleep is known to NOT be thread safe under all architectures.
|
|
|
|
|
*/
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
1999-12-10 00:59:40 +00:00
|
|
|
|
return YES;
|
1998-11-16 19:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_lock(_mutex) == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSRecursiveLockException
|
|
|
|
|
format: @"lock: failed to lock mutex"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-16 07:21:34 +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
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (objc_mutex_unlock(_mutex) == -1)
|
1998-04-20 14:13:19 +00:00
|
|
|
|
{
|
2000-09-14 08:48:05 +00:00
|
|
|
|
[NSException raise: NSRecursiveLockException
|
|
|
|
|
format: @"unlock: failed to unlock mutex"];
|
1998-04-20 14:13:19 +00:00
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|