Add support for thread naming

This commit is contained in:
Richard Frith-Macdonald 2022-07-06 15:05:36 +01:00
parent b23777adfa
commit 71f24ca382
5 changed files with 89 additions and 5 deletions

View file

@ -1,7 +1,12 @@
2022-07-06 Richard Frith-Macdonald <rfm@gnu.org>
2019-08-08 Richard Frith-Macdonald <rfm@gnu.org> 2019-08-08 Richard Frith-Macdonald <rfm@gnu.org>
* GSUniqued.m: * GSThreadPool.h:
Add special case code for handling objcts which can't be copied. * GSIOThreadPool.h:
* GSThreadPool.m:
* GSIOThreadPool.m:
Add support for naming the pools, with threads named sequentially (as
they are created) based on the pool name.
2019-04-16 Richard Frith-Macdonald <rfm@gnu.org> 2019-04-16 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -80,6 +80,8 @@ typedef unsigned int NSUInteger;
NSTimeInterval timeout; NSTimeInterval timeout;
NSUInteger maxThreads; NSUInteger maxThreads;
Class threadClass; Class threadClass;
NSString *poolName;
unsigned created;
} }
/** Returns an instance intended for sharing between sections of code which /** Returns an instance intended for sharing between sections of code which
@ -138,7 +140,7 @@ typedef unsigned int NSUInteger;
/** Releases a thread previously selected from the pool. This decreases the /** Releases a thread previously selected from the pool. This decreases the
* acquire count for the thread. If a thread has a zero acquire count, it is * acquire count for the thread. If a thread has a zero acquire count, it is
* a candidatre for termination and removal from the pool if/when the pool * a candidate for termination and removal from the pool if/when the pool
* has its size changed. * has its size changed.
*/ */
- (void) unacquireThread: (NSThread*)aThread; - (void) unacquireThread: (NSThread*)aThread;

View file

@ -257,7 +257,15 @@ best(NSMutableArray *a)
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))
{ {
NSString *n;
t = [threadClass new]; t = [threadClass new];
if (nil == (n = poolName))
{
n = @"GSIOThreadPool";
}
n = [NSString stringWithFormat: @"%@-%u", n, ++created];
[t setName: n];
[threads addObject: t]; [threads addObject: t];
[t release]; [t release];
[t start]; [t start];
@ -299,6 +307,7 @@ best(NSMutableArray *a)
[threads release]; [threads release];
[classLock unlock]; [classLock unlock];
#endif #endif
DESTROY(poolName);
[super dealloc]; [super dealloc];
} }
@ -323,6 +332,30 @@ best(NSMutableArray *a)
return maxThreads; return maxThreads;
} }
- (NSString*) poolName
{
NSString *n;
[classLock lock];
n = RETAIN(poolName);
[classLock unlock];
return AUTORELEASE(n);
}
- (void) setPoolName: (NSString*)aName
{
NSString *s = nil;
if (aName)
{
s = AUTORELEASE([aName copy]);
NSAssert([s isKindOfClass: [NSString class]], NSInvalidArgumentException);
}
[classLock lock];
ASSIGN(poolName, s);
[classLock unlock];
}
- (void) setThreads: (NSUInteger)max - (void) setThreads: (NSUInteger)max
{ {
maxThreads = max; maxThreads = max;

View file

@ -38,6 +38,8 @@
@interface GSThreadPool : NSObject @interface GSThreadPool : NSObject
{ {
NSRecursiveLock *poolLock; NSRecursiveLock *poolLock;
NSString *poolName;
unsigned created;
BOOL shutdown; BOOL shutdown;
BOOL suspended; BOOL suspended;
NSUInteger maxThreads; NSUInteger maxThreads;
@ -88,6 +90,10 @@
*/ */
- (NSUInteger) maxThreads; - (NSUInteger) maxThreads;
/** Returns the name of the pool as set using the -setPoolName: method.
*/
- (NSString*) poolName;
/** Reverses the effect of -suspend. /** Reverses the effect of -suspend.
*/ */
- (void) resume; - (void) resume;
@ -112,6 +118,10 @@
*/ */
- (void) setOperations: (NSUInteger)max; - (void) setOperations: (NSUInteger)max;
/** Sets the pool name, used as a prefix for thread names.
*/
- (void) setPoolName: (NSString*)aName;
/** Specify the maximum number of threads in the pool (the actual number /** Specify the maximum number of threads in the pool (the actual number
* used may be lower than this value).<br /> * used may be lower than this value).<br />
* Default is 2.<br /> * Default is 2.<br />

View file

@ -114,6 +114,7 @@ static GSThreadPool *shared = nil;
[live release]; [live release];
live = nil; live = nil;
} }
[poolName release];
[poolLock unlock]; [poolLock unlock];
[poolLock release]; [poolLock release];
[super dealloc]; [super dealloc];
@ -125,10 +126,10 @@ static GSThreadPool *shared = nil;
[poolLock lock]; [poolLock lock];
result = [NSString stringWithFormat: result = [NSString stringWithFormat:
@"%@ queue: %"PRIuPTR"(%"PRIuPTR")" @"%@ %@ queue: %"PRIuPTR"(%"PRIuPTR")"
@" threads: %"PRIuPTR"(%"PRIuPTR")" @" threads: %"PRIuPTR"(%"PRIuPTR")"
@" active: %"PRIuPTR" processed: %"PRIuPTR"", @" active: %"PRIuPTR" processed: %"PRIuPTR"",
[super description], operations->count, maxOperations, [super description], poolName, operations->count, maxOperations,
idle->count + live->count, maxThreads, live->count, processed]; idle->count + live->count, maxThreads, live->count, processed];
[poolLock unlock]; [poolLock unlock];
return result; return result;
@ -170,6 +171,7 @@ static GSThreadPool *shared = nil;
if ((self = [super init]) != nil) if ((self = [super init]) != nil)
{ {
poolLock = [NSRecursiveLock new]; poolLock = [NSRecursiveLock new];
poolName = @"GSThreadPool";
idle = [GSLinkedList new]; idle = [GSLinkedList new];
live = [GSLinkedList new]; live = [GSLinkedList new];
operations = [GSLinkedList new]; operations = [GSLinkedList new];
@ -205,6 +207,16 @@ static GSThreadPool *shared = nil;
return maxThreads; return maxThreads;
} }
- (NSString*) poolName
{
NSString *n;
[poolLock lock];
n = RETAIN(poolName);
[poolLock unlock];
return AUTORELEASE(n);
}
- (void) resume - (void) resume
{ {
[poolLock lock]; [poolLock lock];
@ -284,6 +296,20 @@ static GSThreadPool *shared = nil;
maxOperations = max; maxOperations = max;
} }
- (void) setPoolName: (NSString*)aName
{
NSString *s = nil;
if (aName)
{
s = AUTORELEASE([aName copy]);
NSAssert([s isKindOfClass: [NSString class]], NSInvalidArgumentException);
}
[poolLock lock];
ASSIGN(poolName, s);
[poolLock unlock];
}
- (void) setThreads: (NSUInteger)max - (void) setThreads: (NSUInteger)max
{ {
[poolLock lock]; [poolLock lock];
@ -344,6 +370,7 @@ static GSThreadPool *shared = nil;
if (maxThreads > idle->count + live->count) if (maxThreads > idle->count + live->count)
{ {
NSThread *thread; NSThread *thread;
NSString *name;
/* Create a new link, add it to the idle list, and start the /* Create a new link, add it to the idle list, and start the
* thread which will work with it. * thread which will work with it.
@ -378,6 +405,13 @@ static GSThreadPool *shared = nil;
thread = [[NSThread alloc] initWithTarget: self thread = [[NSThread alloc] initWithTarget: self
selector: @selector(_run:) selector: @selector(_run:)
object: link]; object: link];
if (nil == (name = poolName))
{
name = @"GSThreadPool";
}
name = [NSString stringWithFormat: @"%@-%u",
name, ++created];
[thread setName: name];
[link setItem: thread]; [link setItem: thread];
[thread start]; [thread start];
[thread release]; // Retained by link [thread release]; // Retained by link