make -terminate: method usable from other threads

This commit is contained in:
Richard Frith-Macdonald 2017-07-13 08:56:55 +01:00
parent 29f1bab93e
commit 2378e82aaf
2 changed files with 42 additions and 22 deletions

View file

@ -1,3 +1,8 @@
2017-07-13 Richard Frith-Macdonald <rfm@gnu.org>
* GSIOThreadPool.m: Make the -terminate: method safe to call from
any thread (performs itself in the correct thread if it is executing).
2015-10-07 Richard Frith-Macdonald <rfm@gnu.org>
* GSFIFO.m: Add method for writing a whole block of data to a FIFO

View file

@ -120,34 +120,49 @@ static NSRecursiveLock *classLock = nil;
*/
- (void) terminate: (NSDate*)when
{
NSTimeInterval delay = 0.0;
if ([when isKindOfClass: [NSDate class]])
if (YES == [self isFinished])
{
delay = [when timeIntervalSinceNow];
return;
}
[_timer invalidate];
[classLock lock];
if (0 == _count || delay <= 0.0)
if ([NSThread currentThread] == self)
{
_count = NSNotFound; // Mark as terminating
_timer = nil;
delay = 0.0;
}
[classLock unlock];
NSTimeInterval delay = 0.0;
if (delay > 0.0)
{
_timer = [NSTimer scheduledTimerWithTimeInterval: delay
target: self
selector: @selector(_finish:)
userInfo: nil
repeats: NO];
if ([when isKindOfClass: [NSDate class]])
{
delay = [when timeIntervalSinceNow];
}
[_timer invalidate];
[classLock lock];
if (0 == _count || delay <= 0.0)
{
_count = NSNotFound; // Mark as terminating
_timer = nil;
delay = 0.0;
}
[classLock unlock];
if (delay > 0.0)
{
_timer = [NSTimer scheduledTimerWithTimeInterval: delay
target: self
selector: @selector(_finish:)
userInfo: nil
repeats: NO];
}
else
{
[self _finish: nil];
}
}
else
else if ([self isExecuting])
{
[self _finish: nil];
[self performSelector: _cmd
onThread: self
withObject: when
waitUntilDone: YES];
}
}
@end
@ -261,7 +276,7 @@ best(NSMutableArray *a)
onThread: thread
withObject: when
waitUntilDone: NO];
[threads removeLastObject];
[threads removeObjectIdenticalTo: thread];
}
[threads release];
[classLock unlock];