2001-12-17 14:31:42 +00:00
|
|
|
|
/** Mutual exclusion locking classes
|
2003-07-20 06:37:25 +00:00
|
|
|
|
Copyright (C) 1996,2003 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
|
2003-07-20 06:37:25 +00:00
|
|
|
|
Author: Richard Frith-Macdonald <rfm@gnu.org>
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
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
|
2007-09-14 11:36:11 +00:00
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1996-02-13 15:40:05 +00:00
|
|
|
|
License as published by the Free Software Foundation; either
|
2007-09-14 11:36:11 +00:00
|
|
|
|
version 3 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +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.
|
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1999-12-10 00:59:40 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
2006-10-20 10:56:27 +00:00
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
|
|
<title>NSLock class reference</title>
|
|
|
|
|
$Date$ $Revision$
|
2005-02-22 11:22:44 +00:00
|
|
|
|
*/
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
2003-06-07 01:24:41 +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
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/preface.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "Foundation/NSLock.h"
|
|
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
|
#include "Foundation/NSDebug.h"
|
2003-07-20 06:37:25 +00:00
|
|
|
|
#include "Foundation/NSThread.h"
|
2004-02-17 12:55:02 +00:00
|
|
|
|
#ifdef NeXT_RUNTIME
|
|
|
|
|
#include "thr-mach.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define _MUTEX ((objc_mutex_t)_mutex)
|
|
|
|
|
#define _CONDITION ((objc_condition_t)_condition)
|
2003-07-20 06:37:25 +00:00
|
|
|
|
|
|
|
|
|
extern void GSSleepUntilIntervalSinceReferenceDate(NSTimeInterval);
|
|
|
|
|
extern NSTimeInterval GSTimeNow();
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
NSTimeInterval end;
|
|
|
|
|
NSTimeInterval i0;
|
|
|
|
|
NSTimeInterval i1;
|
|
|
|
|
NSTimeInterval max;
|
|
|
|
|
} GSSleepInfo;
|
|
|
|
|
|
|
|
|
|
static void GSSleepInit(NSDate *limit, GSSleepInfo *context)
|
|
|
|
|
{
|
|
|
|
|
context->end = [limit timeIntervalSinceReferenceDate];
|
|
|
|
|
context->i0 = 0.0;
|
|
|
|
|
context->i1 = 0.0001; // Initial pause interval.
|
|
|
|
|
context->max = 0.25; // Maximum pause interval.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>Using a pointer to a context structure initialised using GSSleepInit()
|
|
|
|
|
* we either pause for a while and return YES or, if the limit date
|
|
|
|
|
* has passed, return NO.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>The pause intervals start off very small, but rapidly increase
|
|
|
|
|
* (following a fibonacci sequence) up to a maximum value.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>We use the GSSleepUntilIntervalSinceReferenceDate() function to
|
|
|
|
|
* avoid objc runtime messaging overheads and overheads of creating and
|
|
|
|
|
* destroying temporary date objects.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
|
|
|
|
static BOOL GSSleepOrFail(GSSleepInfo *context)
|
|
|
|
|
{
|
|
|
|
|
NSTimeInterval when = GSTimeNow();
|
|
|
|
|
NSTimeInterval tmp;
|
|
|
|
|
|
|
|
|
|
if (when >= context->end)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
tmp = context->i0 + context->i1;
|
|
|
|
|
context->i0 = context->i1;
|
|
|
|
|
context->i1 = tmp;
|
|
|
|
|
if (tmp > context->max)
|
|
|
|
|
{
|
|
|
|
|
tmp = context->max;
|
|
|
|
|
}
|
|
|
|
|
when += tmp;
|
|
|
|
|
if (when > context->end)
|
|
|
|
|
{
|
|
|
|
|
when = context->end;
|
|
|
|
|
}
|
|
|
|
|
GSSleepUntilIntervalSinceReferenceDate(when);
|
|
|
|
|
return YES; // Paused.
|
|
|
|
|
}
|
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
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* An <code>NSLock</code> is used in multi-threaded applications to protect
|
|
|
|
|
* critical pieces of code. While one thread holds a lock within a piece of
|
|
|
|
|
* code, another thread cannot execute that code until the first thread has
|
|
|
|
|
* given up its hold on the lock. The limitation of <code>NSLock</code> is
|
|
|
|
|
* that you can only lock an <code>NSLock</code> once and it must be unlocked
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* before it can be acquired again.<br /> Other lock classes, notably
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* [NSRecursiveLock], have different restrictions.
|
2002-08-20 10:22:05 +00:00
|
|
|
|
*/
|
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
|
2004-02-17 12:55:02 +00:00
|
|
|
|
if (objc_mutex_deallocate(_MUTEX) == -1)
|
2000-09-14 09:37:13 +00:00
|
|
|
|
{
|
|
|
|
|
NSWarnMLog(@"objc_mutex_deallocate() failed");
|
|
|
|
|
}
|
2002-09-25 04:55:40 +00:00
|
|
|
|
_mutex = 0;
|
1998-04-20 14:13:19 +00:00
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Attempts to acquire a lock, but returns immediately if the lock
|
|
|
|
|
* cannot be acquired. It returns YES if the lock is acquired. It returns
|
|
|
|
|
* NO if the lock cannot be acquired or if the current thread already has
|
2002-08-20 10:22:05 +00:00
|
|
|
|
* the lock.
|
|
|
|
|
*/
|
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 */
|
2004-02-17 12:55:02 +00:00
|
|
|
|
if (_MUTEX->owner == objc_thread_id())
|
2001-04-10 03:27:01 +00:00
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
1996-05-28 13:37:17 +00:00
|
|
|
|
// Ask the runtime to acquire a lock on the mutex
|
2004-02-17 12:55:02 +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
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Attempts to acquire a lock before the date limit passes. It returns YES
|
2002-08-20 10:22:05 +00:00
|
|
|
|
* if it can. It returns NO if it cannot, or if the current thread already
|
|
|
|
|
* has the lock (but it waits until the time limit is up before returning
|
|
|
|
|
* NO).
|
|
|
|
|
*/
|
2003-07-20 06:37:25 +00:00
|
|
|
|
- (BOOL) lockBeforeDate: (NSDate*)limit
|
1998-11-16 19:36:51 +00:00
|
|
|
|
{
|
2003-07-20 06:37:25 +00:00
|
|
|
|
int x;
|
|
|
|
|
GSSleepInfo ctxt;
|
|
|
|
|
|
|
|
|
|
GSSleepInit(limit, &ctxt);
|
1999-12-10 00:59:40 +00:00
|
|
|
|
|
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 */
|
2004-02-17 12:55:02 +00:00
|
|
|
|
while (_MUTEX->owner == objc_thread_id()
|
|
|
|
|
|| (x = objc_mutex_trylock(_MUTEX)) == -1)
|
1999-12-13 12:14:01 +00:00
|
|
|
|
{
|
2003-07-20 06:37:25 +00:00
|
|
|
|
if (GSSleepOrFail(&ctxt) == NO)
|
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
|
|
|
|
}
|
1999-12-10 00:59:40 +00:00
|
|
|
|
return YES;
|
1998-11-16 19:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Attempts to acquire a lock, and waits until it can do so.
|
2002-08-20 10:22:05 +00:00
|
|
|
|
*/
|
1996-05-28 13:37:17 +00:00
|
|
|
|
- (void) lock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +00:00
|
|
|
|
if (objc_condition_deallocate(_CONDITION) == -1)
|
2000-09-14 09:37:13 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
2004-02-17 12:55:02 +00:00
|
|
|
|
if (objc_mutex_deallocate(_MUTEX) == -1)
|
2000-09-14 09:37:13 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
{
|
2004-02-17 12:55:02 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK(_MUTEX);
|
1996-05-28 13:37:17 +00:00
|
|
|
|
|
2004-02-17 12:55:02 +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
|
|
|
|
{
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|
|
|
|
{
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|
2005-02-22 11:22:44 +00:00
|
|
|
|
{
|
2003-07-20 06:37:25 +00:00
|
|
|
|
GSSleepInfo ctxt;
|
|
|
|
|
|
2004-02-17 12:55:02 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK(_MUTEX);
|
1999-12-10 00:59:40 +00:00
|
|
|
|
|
2003-07-20 06:37:25 +00:00
|
|
|
|
GSSleepInit(limit, &ctxt);
|
|
|
|
|
|
2004-02-17 12:55:02 +00:00
|
|
|
|
while (objc_mutex_trylock(_MUTEX) == -1)
|
1999-12-13 12:14:01 +00:00
|
|
|
|
{
|
2003-07-20 06:37:25 +00:00
|
|
|
|
if (GSSleepOrFail(&ctxt) == NO)
|
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
|
|
|
|
}
|
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
|
2003-10-30 13:44:55 +00:00
|
|
|
|
GSSleepInfo ctxt;
|
|
|
|
|
|
2004-02-17 12:55:02 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK(_MUTEX);
|
2003-10-30 13:44:55 +00:00
|
|
|
|
|
|
|
|
|
GSSleepInit(limitDate, &ctxt);
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if (_condition_value == condition_to_meet)
|
|
|
|
|
{
|
2004-02-17 12:55:02 +00:00
|
|
|
|
while (objc_mutex_trylock(_MUTEX) == -1)
|
2003-10-30 13:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
if (GSSleepOrFail(&ctxt) == NO)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (_condition_value == condition_to_meet)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2004-02-17 12:55:02 +00:00
|
|
|
|
if (objc_mutex_unlock(_MUTEX) == -1)
|
2003-10-30 13:44:55 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSConditionLockException
|
|
|
|
|
format: @"%s failed to unlock mutex",
|
|
|
|
|
GSNameFromSelector(_cmd)];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
}
|
2003-10-30 13:44:55 +00:00
|
|
|
|
}
|
|
|
|
|
while (GSSleepOrFail(&ctxt) == YES);
|
|
|
|
|
|
1998-11-16 19:36:51 +00:00
|
|
|
|
return NO;
|
2003-10-30 13:44:55 +00:00
|
|
|
|
|
1999-09-09 02:56:20 +00:00
|
|
|
|
#else
|
|
|
|
|
NSTimeInterval atimeinterval;
|
|
|
|
|
struct timespec endtime;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2004-02-17 12:55:02 +00:00
|
|
|
|
CHECK_RECURSIVE_CONDITION_LOCK(_MUTEX);
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2004-02-17 12:55:02 +00:00
|
|
|
|
if (-1 == objc_mutex_lock(_MUTEX))
|
2005-02-22 11:22:44 +00:00
|
|
|
|
[NSException raise: NSConditionLockException
|
2000-09-14 08:48:05 +00:00
|
|
|
|
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;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
1999-09-09 02:56:20 +00:00
|
|
|
|
atimeinterval = [limitDate timeIntervalSince1970];
|
|
|
|
|
endtime.tv_sec =(unsigned int)atimeinterval; // 941883028;//
|
|
|
|
|
endtime.tv_nsec = (unsigned int)((atimeinterval - (float)endtime.tv_sec)
|
|
|
|
|
* 1000000000.0);
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
1999-12-13 12:14:01 +00:00
|
|
|
|
while (_condition_value != condition_to_meet)
|
1999-09-09 02:56:20 +00:00
|
|
|
|
{
|
2004-02-17 12:55:02 +00:00
|
|
|
|
switch (objc_condition_timedwait(_CONDITION, _MUTEX, &endtime))
|
1999-09-09 02:56:20 +00:00
|
|
|
|
{
|
2005-02-22 11:22:44 +00:00
|
|
|
|
case 0:
|
1999-12-13 12:14:01 +00:00
|
|
|
|
break;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
case EINTR:
|
1999-12-13 12:14:01 +00:00
|
|
|
|
break;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
case ETIMEDOUT :
|
1999-12-13 12:14:01 +00:00
|
|
|
|
[self unlock];
|
|
|
|
|
return NO;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
default:
|
|
|
|
|
[NSException raise: NSConditionLockException
|
2000-09-14 08:48:05 +00:00
|
|
|
|
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
|
|
|
|
{
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* See [NSLock] for more information about what a lock is. A recursive
|
|
|
|
|
* lock extends [NSLock] in that you can lock a recursive lock multiple
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* times. Each lock must be balanced by a corresponding unlock, and the
|
|
|
|
|
* lock is not released for another thread to acquire until the last
|
2002-08-20 10:22:05 +00:00
|
|
|
|
* unlock call is made (corresponding to the first lock message).
|
|
|
|
|
*/
|
1996-05-28 13:37:17 +00:00
|
|
|
|
@implementation NSRecursiveLock
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/** <init />
|
|
|
|
|
*/
|
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
|
2004-02-17 12:55:02 +00:00
|
|
|
|
if (objc_mutex_deallocate(_MUTEX) == -1)
|
2000-09-14 09:37:13 +00:00
|
|
|
|
{
|
|
|
|
|
NSWarnMLog(@"objc_mutex_deallocate() failed");
|
|
|
|
|
}
|
2002-09-25 04:55:40 +00:00
|
|
|
|
_mutex = 0;
|
1998-04-20 14:13:19 +00:00
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Attempts to acquire a lock, but returns NO immediately if the lock
|
|
|
|
|
* cannot be acquired. It returns YES if the lock is acquired. Can be
|
2002-08-20 10:22:05 +00:00
|
|
|
|
* called multiple times to make nested locks.
|
|
|
|
|
*/
|
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
|
2004-02-17 12:55:02 +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
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Attempts to acquire a lock before the date limit passes. It returns
|
2002-08-20 10:22:05 +00:00
|
|
|
|
* YES if it can. It returns NO if it cannot
|
|
|
|
|
* (but it waits until the time limit is up before returning NO).
|
|
|
|
|
*/
|
2003-07-20 06:37:25 +00:00
|
|
|
|
- (BOOL) lockBeforeDate: (NSDate*)limit
|
1998-11-16 19:36:51 +00:00
|
|
|
|
{
|
2003-07-20 06:37:25 +00:00
|
|
|
|
GSSleepInfo ctxt;
|
|
|
|
|
|
|
|
|
|
GSSleepInit(limit, &ctxt);
|
2004-02-17 12:55:02 +00:00
|
|
|
|
while (objc_mutex_trylock(_MUTEX) == -1)
|
1999-12-13 12:14:01 +00:00
|
|
|
|
{
|
2003-07-20 06:37:25 +00:00
|
|
|
|
if (GSSleepOrFail(&ctxt) == NO)
|
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
|
|
|
|
}
|
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
|
2004-02-17 12:55:02 +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
|
2004-02-17 12:55:02 +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
|