Improve chances of threads being removed from pool when the limit is decreased.

This commit is contained in:
Richard Frith-Macdonald 2023-02-03 16:50:35 +00:00
parent 7fa1246f24
commit 1a8c379be8
2 changed files with 17 additions and 2 deletions

View file

@ -1,3 +1,11 @@
2023-02-03 Richard Frith-Macdonald <rfm@gnu.org>
* GSIOThreadPool.m: If the maximum number of threads desired is
decreased so that we have more threads than we want, the excess
threads are now excluded from the set which may be acquired.
This should increase the chance of those threads being unacquired
so they can be terminated and removed from the pool.
2023-01-25 Wolfgang Lux <wolfgang.lux@gmail.com>
* GSCache.m: Raise exception when attempting to insert object with

View file

@ -190,14 +190,21 @@ static NSRecursiveLock *classLock = nil;
static GSIOThreadPool *shared = nil;
/* Return the thread with the lowest usage.
* If there are more threads in the array than we want to use,
* those excess threads are excluded from the check so that
* their usage can drop to zero and they can be terminated.
*/
static GSIOThread *
best(NSMutableArray *a)
best(NSMutableArray *a, NSUInteger max)
{
NSUInteger c = [a count];
NSUInteger l = NSNotFound;
GSIOThread *t = nil;
if (c > max)
{
c = max;
}
while (c-- > 0)
{
GSIOThread *o = [a objectAtIndex: c];
@ -254,7 +261,7 @@ best(NSMutableArray *a)
}
[classLock lock];
t = best(threads);
t = best(threads, maxThreads);
if (nil == t || ((c = [t _count]) > 0 && [threads count] < maxThreads))
{
NSString *n;