simplify GSIOThread exposure and locking

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/performance/trunk@38511 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2015-05-19 14:03:31 +00:00
parent 4b5fcddbc8
commit f2fec29ccc
2 changed files with 18 additions and 26 deletions

View file

@ -29,7 +29,6 @@
typedef unsigned int NSUInteger; typedef unsigned int NSUInteger;
#endif #endif
@class NSLock;
@class NSTimer; @class NSTimer;
/** This is the class for threads in the pool.<br /> /** This is the class for threads in the pool.<br />
@ -62,7 +61,6 @@ typedef unsigned int NSUInteger;
*/ */
@interface GSIOThreadPool : NSObject @interface GSIOThreadPool : NSObject
{ {
NSLock *poolLock;
NSMutableArray *threads; NSMutableArray *threads;
NSTimeInterval timeout; NSTimeInterval timeout;
NSUInteger maxThreads; NSUInteger maxThreads;

View file

@ -33,7 +33,7 @@
/* Protect changes to a thread's counter /* Protect changes to a thread's counter
*/ */
static NSLock *countLock = nil; static NSRecursiveLock *classLock = nil;
@interface GSIOThread (Private) @interface GSIOThread (Private)
- (NSUInteger) _count; - (NSUInteger) _count;
@ -46,9 +46,9 @@ static NSLock *countLock = nil;
+ (void) initialize + (void) initialize
{ {
if (nil == countLock) if (nil == classLock)
{ {
countLock = [NSLock new]; classLock = [NSRecursiveLock new];
} }
} }
@ -116,14 +116,14 @@ static NSLock *countLock = nil;
} }
[_timer invalidate]; [_timer invalidate];
[countLock lock]; [classLock lock];
if (0 == _count || delay <= 0.0) if (0 == _count || delay <= 0.0)
{ {
_count = NSNotFound; // Mark as terminating _count = NSNotFound; // Mark as terminating
_timer = nil; _timer = nil;
delay = 0.0; delay = 0.0;
} }
[countLock unlock]; [classLock unlock];
if (delay > 0.0) if (delay > 0.0)
{ {
@ -182,6 +182,7 @@ best(NSMutableArray *a)
{ {
NSInteger size; NSInteger size;
[GSIOThread class];
size = [[NSUserDefaults standardUserDefaults] size = [[NSUserDefaults standardUserDefaults]
integerForKey: @"GSIOThreadPoolSize"]; integerForKey: @"GSIOThreadPoolSize"];
if (size < 0) if (size < 0)
@ -203,13 +204,12 @@ best(NSMutableArray *a)
GSIOThread *t; GSIOThread *t;
NSUInteger c; NSUInteger c;
[poolLock lock]; [classLock lock];
if (0 == maxThreads) if (0 == maxThreads)
{ {
[poolLock unlock]; [classLock unlock];
return [NSThread mainThread]; return [NSThread mainThread];
} }
[countLock lock];
t = best(threads); t = best(threads);
if (nil == t || ((c = [t _count]) > 0 && [threads count] < maxThreads)) if (nil == t || ((c = [t _count]) > 0 && [threads count] < maxThreads))
{ {
@ -219,8 +219,7 @@ best(NSMutableArray *a)
c = 0; c = 0;
} }
[t _setCount: c + 1]; [t _setCount: c + 1];
[countLock unlock]; [classLock unlock];
[poolLock unlock];
return t; return t;
} }
@ -228,12 +227,12 @@ best(NSMutableArray *a)
{ {
NSUInteger count = 0; NSUInteger count = 0;
[poolLock lock]; [classLock lock];
if ([threads indexOfObjectIdenticalTo: aThread] != NSNotFound) if ([threads indexOfObjectIdenticalTo: aThread] != NSNotFound)
{ {
count = [((GSIOThread*)aThread) _count]; count = [((GSIOThread*)aThread) _count];
} }
[poolLock unlock]; [classLock unlock];
return count; return count;
} }
@ -243,7 +242,7 @@ best(NSMutableArray *a)
GSIOThread *thread; GSIOThread *thread;
NSDate *when = [NSDate dateWithTimeIntervalSinceNow: timeout]; NSDate *when = [NSDate dateWithTimeIntervalSinceNow: timeout];
[poolLock lock]; [classLock lock];
while ((thread = [threads lastObject]) != nil) while ((thread = [threads lastObject]) != nil)
{ {
[thread performSelector: @selector(terminate:) [thread performSelector: @selector(terminate:)
@ -253,8 +252,7 @@ best(NSMutableArray *a)
[threads removeLastObject]; [threads removeLastObject];
} }
[threads release]; [threads release];
[poolLock unlock]; [classLock unlock];
[poolLock release];
#endif #endif
[super dealloc]; [super dealloc];
} }
@ -264,7 +262,6 @@ best(NSMutableArray *a)
#if defined(GNUSTEP) || (MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_4) #if defined(GNUSTEP) || (MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_4)
if ((self = [super init]) != nil) if ((self = [super init]) != nil)
{ {
poolLock = [NSLock new];
threads = [NSMutableArray new]; threads = [NSMutableArray new];
} }
#else #else
@ -282,9 +279,9 @@ best(NSMutableArray *a)
- (void) setThreads: (NSUInteger)max - (void) setThreads: (NSUInteger)max
{ {
[poolLock lock]; [classLock lock];
maxThreads = max; maxThreads = max;
[poolLock unlock]; [classLock unlock];
} }
- (void) setTimeout: (NSTimeInterval)t - (void) setTimeout: (NSTimeInterval)t
@ -299,22 +296,19 @@ best(NSMutableArray *a)
- (void) unacquireThread: (NSThread*)aThread - (void) unacquireThread: (NSThread*)aThread
{ {
[poolLock lock]; [classLock lock];
if ([threads indexOfObjectIdenticalTo: aThread] != NSNotFound) if ([threads indexOfObjectIdenticalTo: aThread] != NSNotFound)
{ {
NSUInteger c; NSUInteger c;
[countLock lock];
c = [((GSIOThread*)aThread) _count]; c = [((GSIOThread*)aThread) _count];
if (0 == c) if (0 == c)
{ {
[countLock unlock]; [classLock unlock];
[poolLock unlock];
[NSException raise: NSInternalInconsistencyException [NSException raise: NSInternalInconsistencyException
format: @"-unacquireThread: called too many times"]; format: @"-unacquireThread: called too many times"];
} }
[((GSIOThread*)aThread) _setCount: --c]; [((GSIOThread*)aThread) _setCount: --c];
[countLock unlock];
if (0 == c && [threads count] > maxThreads) if (0 == c && [threads count] > maxThreads)
{ {
[aThread retain]; [aThread retain];
@ -326,7 +320,7 @@ best(NSMutableArray *a)
[aThread release]; [aThread release];
} }
} }
[poolLock unlock]; [classLock unlock];
} }
@end @end