GC tidyups

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@7509 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 2000-09-14 08:48:05 +00:00
parent 2b652c7503
commit 6f6debd1e6
3 changed files with 79 additions and 63 deletions

View file

@ -1,3 +1,9 @@
2000-09-14 Richard Frith-Macdonald <rfm@gnu.org>
* Headers/gnustep/base/NSLock.h: conform to GCFinalization protocol.
* Source/NSLock.m: deallocate mutex in gcFinalize. Don't raise an
exception if mutex deallocation fails.
2000-09-13 Adam Fedor <fedor@gnu.org> 2000-09-13 Adam Fedor <fedor@gnu.org>
* Remove dependance on Source/Foundation link, additional MINGW ports. * Remove dependance on Source/Foundation link, additional MINGW ports.

View file

@ -48,7 +48,7 @@
* NSLock class * NSLock class
* Simplest lock for protecting critical sections of code * Simplest lock for protecting critical sections of code
*/ */
@interface NSLock : NSObject <NSLocking> @interface NSLock : NSObject <NSLocking, GCFinalization>
{ {
@private @private
objc_mutex_t _mutex; objc_mutex_t _mutex;
@ -66,7 +66,7 @@
* NSConditionLock * NSConditionLock
* Allows locking and unlocking to be based upon a condition * Allows locking and unlocking to be based upon a condition
*/ */
@interface NSConditionLock : NSObject <NSLocking> @interface NSConditionLock : NSObject <NSLocking, GCFinalization>
{ {
@private @private
objc_condition_t _condition; objc_condition_t _condition;
@ -112,7 +112,7 @@
* 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.
*/ */
@interface NSRecursiveLock : NSObject <NSLocking> @interface NSRecursiveLock : NSObject <NSLocking, GCFinalization>
{ {
@private @private
objc_mutex_t _mutex; objc_mutex_t _mutex;

View file

@ -41,8 +41,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
if ((mutex)->owner == objc_thread_id()) \ if ((mutex)->owner == objc_thread_id()) \
{ \ { \
[NSException \ [NSException \
raise:NSLockException \ raise: NSLockException \
format:@"Thread attempted to recursively lock"]; \ format: @"Thread attempted to recursively lock"]; \
/* NOT REACHED */ \ /* NOT REACHED */ \
} \ } \
} }
@ -52,8 +52,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
if ((mutex)->owner == objc_thread_id()) \ if ((mutex)->owner == objc_thread_id()) \
{ \ { \
[NSException \ [NSException \
raise:NSConditionLockException \ raise: NSConditionLockException \
format:@"Thread attempted to recursively lock"]; \ format: @"Thread attempted to recursively lock"]; \
/* NOT REACHED */ \ /* NOT REACHED */ \
} \ } \
} }
@ -66,29 +66,35 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// Designated initializer // Designated initializer
- (id) init - (id) init
{ {
[super init]; self = [super init];
if (self != nil)
// Allocate the mutex from the runtime
_mutex = objc_mutex_allocate();
if (!_mutex)
{ {
NSLog(@"Failed to allocate a mutex"); // Allocate the mutex from the runtime
return nil; _mutex = objc_mutex_allocate();
if (_mutex == 0)
{
RELEASE(self);
NSLog(@"Failed to allocate a mutex");
return nil;
}
} }
return self; return self;
} }
- (void) dealloc - (void) dealloc
{
[self gcFinalize];
[super dealloc];
}
- (void) gcFinalize
{ {
// 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
if (objc_mutex_deallocate(_mutex) == -1) if (objc_mutex_deallocate(_mutex) == -1)
{ {
[NSException raise:NSLockException NSWarnMLog(@"objc_mutex_deallocate() failed");
format:@"invalid mutex"];
/* NOT REACHED */
} }
[super dealloc];
} }
// Try to acquire the lock // Try to acquire the lock
@ -154,8 +160,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// This will block // This will block
if (objc_mutex_lock(_mutex) == -1) if (objc_mutex_lock(_mutex) == -1)
{ {
[NSException raise:NSLockException [NSException raise: NSLockException
format:@"failed to lock mutex"]; format: @"failed to lock mutex"];
/* NOT REACHED */ /* NOT REACHED */
} }
} }
@ -165,8 +171,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// Ask the runtime to release a lock on the mutex // Ask the runtime to release a lock on the mutex
if (objc_mutex_unlock(_mutex) == -1) if (objc_mutex_unlock(_mutex) == -1)
{ {
[NSException raise:NSLockException [NSException raise: NSLockException
format:@"unlock: failed to unlock mutex"]; format: @"unlock: failed to unlock mutex"];
/* NOT REACHED */ /* NOT REACHED */
} }
} }
@ -209,22 +215,23 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
} }
- (void) dealloc - (void) dealloc
{
[self gcFinalize];
[super dealloc];
}
- (void) gcFinalize
{ {
// 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
if (objc_condition_deallocate(_condition) == -1) if (objc_condition_deallocate(_condition) == -1)
{ {
[NSException raise:NSConditionLockException NSWarnMLog(@"objc_condition_deallocate() failed");
format:@"dealloc: invalid condition"];
/* NOT REACHED */
} }
if (objc_mutex_deallocate(_mutex) == -1) if (objc_mutex_deallocate(_mutex) == -1)
{ {
[NSException raise:NSConditionLockException NSWarnMLog(@"objc_mutex_deallocate() failed");
format:@"dealloc: invalid mutex"];
/* NOT REACHED */
} }
[super dealloc];
} }
// Return the current condition of the lock // Return the current condition of the lock
@ -240,8 +247,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
if (objc_mutex_lock(_mutex) == -1) if (objc_mutex_lock(_mutex) == -1)
{ {
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"lockWhenCondition: failed to lock mutex"]; format: @"lockWhenCondition: failed to lock mutex"];
/* NOT REACHED */ /* NOT REACHED */
} }
@ -249,8 +256,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
{ {
if (objc_condition_wait(_condition, _mutex) == -1) if (objc_condition_wait(_condition, _mutex) == -1)
{ {
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"objc_condition_wait failed"]; format: @"objc_condition_wait failed"];
/* NOT REACHED */ /* NOT REACHED */
} }
} }
@ -266,8 +273,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// Another thread has the lock so abort // Another thread has the lock so abort
if (depth == -1) if (depth == -1)
{ {
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"unlockWithCondition: Tried to unlock someone else's lock"]; format: @"unlockWithCondition: Tried to unlock someone else's lock"];
/* NOT REACHED */ /* NOT REACHED */
} }
@ -275,8 +282,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// the lock above, bogus unlock so abort // the lock above, bogus unlock so abort
if (depth == 1) if (depth == 1)
{ {
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"unlockWithCondition: Unlock attempted without lock"]; format: @"unlockWithCondition: Unlock attempted without lock"];
/* NOT REACHED */ /* NOT REACHED */
} }
@ -286,8 +293,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// wake up blocked threads // wake up blocked threads
if (objc_condition_broadcast(_condition) == -1) if (objc_condition_broadcast(_condition) == -1)
{ {
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"unlockWithCondition: objc_condition_broadcast failed"]; format: @"unlockWithCondition: objc_condition_broadcast failed"];
/* NOT REACHED */ /* NOT REACHED */
} }
@ -295,8 +302,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
if ((objc_mutex_unlock(_mutex) == -1) if ((objc_mutex_unlock(_mutex) == -1)
|| (objc_mutex_unlock(_mutex) == -1)) || (objc_mutex_unlock(_mutex) == -1))
{ {
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"unlockWithCondition: failed to unlock mutex"]; format: @"unlockWithCondition: failed to unlock mutex"];
/* NOT REACHED */ /* NOT REACHED */
} }
} }
@ -369,8 +376,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
CHECK_RECURSIVE_CONDITION_LOCK(_mutex); CHECK_RECURSIVE_CONDITION_LOCK(_mutex);
if (-1 == objc_mutex_lock(_mutex)) if (-1 == objc_mutex_lock(_mutex))
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"lockWhenCondition: failed to lock mutex"]; format: @"lockWhenCondition: failed to lock mutex"];
if (_condition_value == condition_to_meet) if (_condition_value == condition_to_meet)
return YES; return YES;
@ -384,16 +391,16 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
{ {
switch (objc_condition_timedwait(_condition, _mutex, &endtime)) switch (objc_condition_timedwait(_condition, _mutex, &endtime))
{ {
case 0: case 0:
break; break;
case EINTR: case EINTR:
break; break;
case ETIMEDOUT : case ETIMEDOUT :
[self unlock]; [self unlock];
return NO; return NO;
default: default:
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"objc_condition_timedwait failed"]; format: @"objc_condition_timedwait failed"];
[self unlock]; [self unlock];
return NO; return NO;
} }
@ -412,8 +419,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// This will block // This will block
if (objc_mutex_lock(_mutex) == -1) if (objc_mutex_lock(_mutex) == -1)
{ {
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"lock: failed to lock mutex"]; format: @"lock: failed to lock mutex"];
/* NOT REACHED */ /* NOT REACHED */
} }
} }
@ -423,16 +430,16 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// wake up blocked threads // wake up blocked threads
if (objc_condition_broadcast(_condition) == -1) if (objc_condition_broadcast(_condition) == -1)
{ {
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"unlockWithCondition: objc_condition_broadcast failed"]; format: @"unlockWithCondition: objc_condition_broadcast failed"];
/* NOT REACHED */ /* NOT REACHED */
} }
// Ask the runtime to release a lock on the mutex // Ask the runtime to release a lock on the mutex
if (objc_mutex_unlock(_mutex) == -1) if (objc_mutex_unlock(_mutex) == -1)
{ {
[NSException raise:NSConditionLockException [NSException raise: NSConditionLockException
format:@"unlock: failed to unlock mutex"]; format: @"unlock: failed to unlock mutex"];
/* NOT REACHED */ /* NOT REACHED */
} }
} }
@ -465,16 +472,19 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
} }
- (void) dealloc - (void) dealloc
{
[self gcFinalize];
[super dealloc];
}
- (void) gcFinalize
{ {
// 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
if (objc_mutex_deallocate(_mutex) == -1) if (objc_mutex_deallocate(_mutex) == -1)
{ {
[NSException raise:NSRecursiveLockException NSWarnMLog(@"objc_mutex_deallocate() failed");
format:@"dealloc: invalid mutex"];
/* NOT REACHED */
} }
[super dealloc];
} }
// Try to acquire the lock // Try to acquire the lock
@ -516,8 +526,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// This will block // This will block
if (objc_mutex_lock(_mutex) == -1) if (objc_mutex_lock(_mutex) == -1)
{ {
[NSException raise:NSRecursiveLockException [NSException raise: NSRecursiveLockException
format:@"lock: failed to lock mutex"]; format: @"lock: failed to lock mutex"];
/* NOT REACHED */ /* NOT REACHED */
} }
} }
@ -527,8 +537,8 @@ NSString *NSRecursiveLockException = @"NSRecursiveLockException";
// Ask the runtime to release a lock on the mutex // Ask the runtime to release a lock on the mutex
if (objc_mutex_unlock(_mutex) == -1) if (objc_mutex_unlock(_mutex) == -1)
{ {
[NSException raise:NSRecursiveLockException [NSException raise: NSRecursiveLockException
format:@"unlock: failed to unlock mutex"]; format: @"unlock: failed to unlock mutex"];
/* NOT REACHED */ /* NOT REACHED */
} }
} }