2009-07-17 05:13:52 +00:00
|
|
|
/**Implementation for NSOperation for GNUStep
|
2023-09-20 23:27:10 +00:00
|
|
|
Copyright (C) 2008-2023 Free Software Foundation, Inc.
|
2009-07-13 18:14:42 +00:00
|
|
|
|
|
|
|
Written by: Gregory Casamento <greg.casamento@gmail.com>
|
2010-02-04 16:47:45 +00:00
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
2012-03-26 14:47:07 +00:00
|
|
|
|
2009-07-13 18:14:42 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
2012-03-26 14:47:07 +00:00
|
|
|
|
2009-07-13 18:14:42 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2012-03-26 14:47:07 +00:00
|
|
|
|
2009-07-13 18:14:42 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
2024-11-07 13:37:59 +00:00
|
|
|
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
|
2009-07-13 18:14:42 +00:00
|
|
|
|
2009-07-17 05:13:52 +00:00
|
|
|
<title>NSOperation class reference</title>
|
2023-09-20 23:27:10 +00:00
|
|
|
Created: 2008-06-08 11:38:33 +0100 (Sun, 08 Jun 2008)
|
2012-03-26 14:47:07 +00:00
|
|
|
*/
|
2009-07-13 18:14:42 +00:00
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2010-02-26 10:25:35 +00:00
|
|
|
|
2010-02-26 11:04:14 +00:00
|
|
|
#import "Foundation/NSLock.h"
|
|
|
|
|
2010-02-26 10:25:35 +00:00
|
|
|
#define GS_NSOperation_IVARS \
|
|
|
|
NSRecursiveLock *lock; \
|
|
|
|
NSConditionLock *cond; \
|
|
|
|
NSOperationQueuePriority priority; \
|
|
|
|
double threadPriority; \
|
|
|
|
BOOL cancelled; \
|
|
|
|
BOOL concurrent; \
|
|
|
|
BOOL executing; \
|
|
|
|
BOOL finished; \
|
2011-03-03 10:56:47 +00:00
|
|
|
BOOL blocked; \
|
2010-02-26 10:25:35 +00:00
|
|
|
BOOL ready; \
|
2012-03-26 14:47:07 +00:00
|
|
|
NSMutableArray *dependencies; \
|
2020-12-07 12:23:30 +00:00
|
|
|
id completionBlock;
|
2010-02-26 10:25:35 +00:00
|
|
|
|
|
|
|
#define GS_NSOperationQueue_IVARS \
|
|
|
|
NSRecursiveLock *lock; \
|
2011-03-03 15:14:29 +00:00
|
|
|
NSConditionLock *cond; \
|
2010-02-26 10:25:35 +00:00
|
|
|
NSMutableArray *operations; \
|
|
|
|
NSMutableArray *waiting; \
|
2011-03-03 15:14:29 +00:00
|
|
|
NSMutableArray *starting; \
|
2010-02-26 10:25:35 +00:00
|
|
|
NSString *name; \
|
2024-09-08 14:54:01 +00:00
|
|
|
NSString *threadName; \
|
2010-02-26 10:25:35 +00:00
|
|
|
BOOL suspended; \
|
2011-03-03 10:56:47 +00:00
|
|
|
NSInteger executing; \
|
2011-03-03 15:14:29 +00:00
|
|
|
NSInteger threadCount; \
|
2022-08-16 08:36:27 +00:00
|
|
|
NSInteger maxThreads;
|
2010-02-26 10:25:35 +00:00
|
|
|
|
2009-07-17 05:13:52 +00:00
|
|
|
#import "Foundation/NSOperation.h"
|
|
|
|
#import "Foundation/NSArray.h"
|
2010-02-04 16:47:45 +00:00
|
|
|
#import "Foundation/NSAutoreleasePool.h"
|
2010-02-18 16:18:54 +00:00
|
|
|
#import "Foundation/NSDictionary.h"
|
2009-07-17 05:13:52 +00:00
|
|
|
#import "Foundation/NSEnumerator.h"
|
|
|
|
#import "Foundation/NSException.h"
|
2010-02-04 16:47:45 +00:00
|
|
|
#import "Foundation/NSKeyValueObserving.h"
|
|
|
|
#import "Foundation/NSThread.h"
|
2024-09-08 14:54:01 +00:00
|
|
|
#import "Foundation/NSValue.h"
|
2016-07-18 09:51:35 +00:00
|
|
|
#import "GNUstepBase/NSArray+GNUstepBase.h"
|
2010-02-08 10:34:27 +00:00
|
|
|
#import "GSPrivate.h"
|
2009-07-13 18:14:42 +00:00
|
|
|
|
2009-07-21 09:40:48 +00:00
|
|
|
#define GSInternal NSOperationInternal
|
|
|
|
#include "GSInternal.h"
|
2010-02-26 10:25:35 +00:00
|
|
|
GS_PRIVATE_INTERNAL(NSOperation)
|
2009-07-16 15:56:31 +00:00
|
|
|
|
2016-07-18 09:51:35 +00:00
|
|
|
static void *isFinishedCtxt = (void*)"isFinished";
|
|
|
|
static void *isReadyCtxt = (void*)"isReady";
|
|
|
|
static void *queuePriorityCtxt = (void*)"queuePriority";
|
|
|
|
|
2011-03-03 10:56:47 +00:00
|
|
|
@interface NSOperation (Private)
|
|
|
|
- (void) _finish;
|
2022-01-04 12:57:55 +00:00
|
|
|
- (void) _updateReadyState;
|
2011-03-03 10:56:47 +00:00
|
|
|
@end
|
|
|
|
|
2010-02-14 10:48:10 +00:00
|
|
|
@implementation NSOperation
|
2009-07-16 15:56:31 +00:00
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
+ (BOOL) automaticallyNotifiesObserversForKey: (NSString*)theKey
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
/* Handle all KVO manually
|
|
|
|
*/
|
|
|
|
return NO;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (void) addDependency: (NSOperation *)op
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2011-02-27 22:37:52 +00:00
|
|
|
if (NO == [op isKindOfClass: [NSOperation class]])
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] dependency is not an NSOperation",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
2010-02-04 16:47:45 +00:00
|
|
|
if (op == self)
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] attempt to add dependency on self",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
[internal->lock lock];
|
|
|
|
if (internal->dependencies == nil)
|
|
|
|
{
|
|
|
|
internal->dependencies = [[NSMutableArray alloc] initWithCapacity: 5];
|
|
|
|
}
|
|
|
|
NS_DURING
|
|
|
|
{
|
2010-02-06 17:10:16 +00:00
|
|
|
if (NSNotFound == [internal->dependencies indexOfObjectIdenticalTo: op])
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
[self willChangeValueForKey: @"dependencies"];
|
|
|
|
[internal->dependencies addObject: op];
|
|
|
|
/* We only need to watch for changes if it's possible for them to
|
|
|
|
* happen and make a difference.
|
|
|
|
*/
|
|
|
|
if (NO == [op isFinished]
|
|
|
|
&& NO == [self isCancelled]
|
|
|
|
&& NO == [self isExecuting]
|
|
|
|
&& NO == [self isFinished])
|
|
|
|
{
|
|
|
|
/* Can change readiness if we are neither cancelled nor
|
|
|
|
* executing nor finished. So we need to observe for the
|
|
|
|
* finish of the dependency.
|
|
|
|
*/
|
|
|
|
[op addObserver: self
|
|
|
|
forKeyPath: @"isFinished"
|
|
|
|
options: NSKeyValueObservingOptionNew
|
2016-07-18 09:51:35 +00:00
|
|
|
context: isFinishedCtxt];
|
2010-02-04 16:47:45 +00:00
|
|
|
if (internal->ready == YES)
|
|
|
|
{
|
|
|
|
/* The new dependency stops us being ready ...
|
|
|
|
* change state.
|
|
|
|
*/
|
|
|
|
[self willChangeValueForKey: @"isReady"];
|
|
|
|
internal->ready = NO;
|
|
|
|
[self didChangeValueForKey: @"isReady"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[self didChangeValueForKey: @"dependencies"];
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[internal->lock unlock];
|
|
|
|
NSLog(@"Problem adding dependency: %@", localException);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cancel
|
|
|
|
{
|
|
|
|
if (NO == internal->cancelled && NO == [self isFinished])
|
|
|
|
{
|
|
|
|
[internal->lock lock];
|
|
|
|
if (NO == internal->cancelled && NO == [self isFinished])
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"isCancelled"];
|
|
|
|
internal->cancelled = YES;
|
|
|
|
if (NO == internal->ready)
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"isReady"];
|
|
|
|
internal->ready = YES;
|
|
|
|
[self didChangeValueForKey: @"isReady"];
|
|
|
|
}
|
|
|
|
[self didChangeValueForKey: @"isCancelled"];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[internal->lock unlock];
|
|
|
|
NSLog(@"Problem cancelling operation: %@", localException);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
2010-02-04 16:47:45 +00:00
|
|
|
[internal->lock unlock];
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 10:03:40 +00:00
|
|
|
- (GSOperationCompletionBlock) completionBlock
|
|
|
|
{
|
2020-12-07 12:23:30 +00:00
|
|
|
return (GSOperationCompletionBlock)internal->completionBlock;
|
2012-03-27 10:03:40 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2024-11-14 17:57:55 +00:00
|
|
|
/* Only clean up if ivars have been initialised
|
|
|
|
*/
|
2024-11-17 16:55:54 +00:00
|
|
|
if (GS_EXISTS_INTERNAL && internal->lock != nil)
|
2010-02-04 16:47:45 +00:00
|
|
|
{
|
|
|
|
NSOperation *op;
|
2009-07-13 18:14:42 +00:00
|
|
|
|
2019-08-12 17:30:25 +00:00
|
|
|
if (!internal->finished)
|
|
|
|
{
|
|
|
|
[self removeObserver: self forKeyPath: @"isFinished"];
|
|
|
|
}
|
2010-02-04 16:47:45 +00:00
|
|
|
while ((op = [internal->dependencies lastObject]) != nil)
|
|
|
|
{
|
|
|
|
[self removeDependency: op];
|
|
|
|
}
|
|
|
|
RELEASE(internal->dependencies);
|
|
|
|
RELEASE(internal->cond);
|
|
|
|
RELEASE(internal->lock);
|
2020-03-30 13:47:50 +00:00
|
|
|
RELEASE(internal->completionBlock);
|
2010-02-04 16:47:45 +00:00
|
|
|
GS_DESTROY_INTERNAL(NSOperation);
|
|
|
|
}
|
2024-11-17 16:55:54 +00:00
|
|
|
DEALLOC
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (NSArray *) dependencies
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
NSArray *a;
|
|
|
|
|
|
|
|
if (internal->dependencies == nil)
|
|
|
|
{
|
2024-11-15 20:48:09 +00:00
|
|
|
a = [NSArray array]; // OSX return an empty array
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[internal->lock lock];
|
|
|
|
a = [NSArray arrayWithArray: internal->dependencies];
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
return a;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (id) init
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
GS_CREATE_INTERNAL(NSOperation);
|
|
|
|
internal->priority = NSOperationQueuePriorityNormal;
|
|
|
|
internal->threadPriority = 0.5;
|
|
|
|
internal->ready = YES;
|
|
|
|
internal->lock = [NSRecursiveLock new];
|
2018-03-26 14:05:01 +00:00
|
|
|
[internal->lock setName:
|
|
|
|
[NSString stringWithFormat: @"lock-for-opqueue-%p", self]];
|
2016-07-02 18:56:02 +00:00
|
|
|
internal->cond = [[NSConditionLock alloc] initWithCondition: 0];
|
2018-03-26 14:05:01 +00:00
|
|
|
[internal->cond setName:
|
|
|
|
[NSString stringWithFormat: @"cond-for-opqueue-%p", self]];
|
2016-07-02 18:56:02 +00:00
|
|
|
[self addObserver: self
|
|
|
|
forKeyPath: @"isFinished"
|
|
|
|
options: NSKeyValueObservingOptionNew
|
2016-07-18 09:51:35 +00:00
|
|
|
context: isFinishedCtxt];
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
return self;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isCancelled
|
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
return internal->cancelled;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isExecuting
|
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
return internal->executing;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isFinished
|
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
return internal->finished;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isConcurrent
|
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
return internal->concurrent;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isReady
|
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
return internal->ready;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 22:53:52 +00:00
|
|
|
- (void) main
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
return; // OSX default implementation does nothing
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (void) observeValueForKeyPath: (NSString *)keyPath
|
|
|
|
ofObject: (id)object
|
|
|
|
change: (NSDictionary *)change
|
|
|
|
context: (void *)context
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2021-12-26 15:06:48 +00:00
|
|
|
NSOperation *op = object;
|
|
|
|
if (NO == [op isFinished])
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-04-03 20:23:31 +00:00
|
|
|
|
2011-03-03 10:56:47 +00:00
|
|
|
if (object == self)
|
|
|
|
{
|
2021-12-26 15:06:48 +00:00
|
|
|
[internal->lock lock];
|
|
|
|
|
|
|
|
/* We only observe isFinished changes, and we can remove self as an
|
|
|
|
* observer once we know the operation has finished since it can never
|
|
|
|
* become unfinished.
|
|
|
|
*/
|
|
|
|
[object removeObserver: self forKeyPath: @"isFinished"];
|
|
|
|
|
|
|
|
/* Concurrent operations: Call completion block and set internal finished
|
|
|
|
* state so we don't try removing the observer again in -dealloc. */
|
|
|
|
if (YES == [self isConcurrent])
|
|
|
|
{
|
|
|
|
internal->finished = YES;
|
|
|
|
CALL_BLOCK_NO_ARGS(
|
|
|
|
((GSOperationCompletionBlock)internal->completionBlock));
|
|
|
|
}
|
|
|
|
|
2011-03-03 10:56:47 +00:00
|
|
|
/* We have finished and need to unlock the condition lock so that
|
|
|
|
* any waiting thread can continue.
|
|
|
|
*/
|
|
|
|
[internal->cond lock];
|
|
|
|
[internal->cond unlockWithCondition: 1];
|
2021-12-26 15:06:48 +00:00
|
|
|
|
2016-07-02 18:56:02 +00:00
|
|
|
[internal->lock unlock];
|
2011-03-03 10:56:47 +00:00
|
|
|
}
|
2021-12-26 15:06:48 +00:00
|
|
|
else
|
2010-02-04 16:47:45 +00:00
|
|
|
{
|
2021-12-26 15:06:48 +00:00
|
|
|
/* Some dependency has finished ...
|
2011-03-03 10:56:47 +00:00
|
|
|
*/
|
2021-12-26 15:06:48 +00:00
|
|
|
[self _updateReadyState];
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (NSOperationQueuePriority) queuePriority
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
return internal->priority;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (void) removeDependency: (NSOperation *)op
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
[internal->lock lock];
|
|
|
|
NS_DURING
|
|
|
|
{
|
2010-02-06 17:10:16 +00:00
|
|
|
if (NSNotFound != [internal->dependencies indexOfObjectIdenticalTo: op])
|
2010-02-04 16:47:45 +00:00
|
|
|
{
|
2018-12-04 11:09:18 +00:00
|
|
|
[op removeObserver: self forKeyPath: @"isFinished"];
|
2010-02-04 16:47:45 +00:00
|
|
|
[self willChangeValueForKey: @"dependencies"];
|
|
|
|
[internal->dependencies removeObject: op];
|
|
|
|
if (NO == internal->ready)
|
|
|
|
{
|
|
|
|
/* The dependency may cause us to become ready ...
|
|
|
|
*/
|
2021-12-26 15:06:48 +00:00
|
|
|
[self _updateReadyState];
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
[self didChangeValueForKey: @"dependencies"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[internal->lock unlock];
|
|
|
|
NSLog(@"Problem removing dependency: %@", localException);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
[internal->lock unlock];
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 10:03:40 +00:00
|
|
|
- (void) setCompletionBlock: (GSOperationCompletionBlock)aBlock
|
|
|
|
{
|
2020-12-07 12:23:30 +00:00
|
|
|
ASSIGNCOPY(internal->completionBlock, (id)aBlock);
|
2012-03-27 10:03:40 +00:00
|
|
|
}
|
|
|
|
|
2009-07-13 18:14:42 +00:00
|
|
|
- (void) setQueuePriority: (NSOperationQueuePriority)pri
|
|
|
|
{
|
2010-02-08 10:34:27 +00:00
|
|
|
if (pri <= NSOperationQueuePriorityVeryLow)
|
2010-02-04 16:47:45 +00:00
|
|
|
pri = NSOperationQueuePriorityVeryLow;
|
2010-02-08 10:34:27 +00:00
|
|
|
else if (pri <= NSOperationQueuePriorityLow)
|
2010-02-04 16:47:45 +00:00
|
|
|
pri = NSOperationQueuePriorityLow;
|
2010-02-08 10:34:27 +00:00
|
|
|
else if (pri < NSOperationQueuePriorityHigh)
|
2010-02-04 16:47:45 +00:00
|
|
|
pri = NSOperationQueuePriorityNormal;
|
2010-02-08 10:34:27 +00:00
|
|
|
else if (pri < NSOperationQueuePriorityVeryHigh)
|
2010-02-04 16:47:45 +00:00
|
|
|
pri = NSOperationQueuePriorityHigh;
|
2010-02-08 10:34:27 +00:00
|
|
|
else
|
|
|
|
pri = NSOperationQueuePriorityVeryHigh;
|
2010-02-04 16:47:45 +00:00
|
|
|
|
|
|
|
if (pri != internal->priority)
|
|
|
|
{
|
|
|
|
[internal->lock lock];
|
|
|
|
if (pri != internal->priority)
|
|
|
|
{
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"queuePriority"];
|
|
|
|
internal->priority = pri;
|
|
|
|
[self didChangeValueForKey: @"queuePriority"];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[internal->lock unlock];
|
|
|
|
NSLog(@"Problem setting priority: %@", localException);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setThreadPriority: (double)pri
|
|
|
|
{
|
|
|
|
if (pri > 1) pri = 1;
|
|
|
|
else if (pri < 0) pri = 0;
|
|
|
|
internal->threadPriority = pri;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) start
|
|
|
|
{
|
2020-06-05 16:43:46 +00:00
|
|
|
ENTER_POOL
|
|
|
|
|
|
|
|
double prio = [NSThread threadPriority];
|
2010-02-04 16:47:45 +00:00
|
|
|
|
2016-06-22 07:54:16 +00:00
|
|
|
AUTORELEASE(RETAIN(self)); // Make sure we exist while running.
|
2010-02-04 16:47:45 +00:00
|
|
|
[internal->lock lock];
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
if (YES == [self isExecuting])
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] called on executing operation",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
if (YES == [self isFinished])
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] called on finished operation",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
if (NO == [self isReady])
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] called on operation which is not ready",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
2011-03-03 10:56:47 +00:00
|
|
|
if (NO == internal->executing)
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"isExecuting"];
|
|
|
|
internal->executing = YES;
|
|
|
|
[self didChangeValueForKey: @"isExecuting"];
|
|
|
|
}
|
2010-02-05 11:41:24 +00:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[internal->lock unlock];
|
|
|
|
[localException raise];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
[internal->lock unlock];
|
2010-02-04 16:47:45 +00:00
|
|
|
|
2010-02-05 11:41:24 +00:00
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
if (NO == [self isCancelled])
|
2010-02-04 16:47:45 +00:00
|
|
|
{
|
2010-02-05 11:41:24 +00:00
|
|
|
[NSThread setThreadPriority: internal->threadPriority];
|
|
|
|
[self main];
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
2010-02-05 11:41:24 +00:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[NSThread setThreadPriority: prio];
|
|
|
|
[localException raise];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
2010-02-04 16:47:45 +00:00
|
|
|
|
2011-03-05 06:11:50 +00:00
|
|
|
[self _finish];
|
2020-06-05 16:43:46 +00:00
|
|
|
LEAVE_POOL
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (double) threadPriority
|
|
|
|
{
|
|
|
|
return internal->threadPriority;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) waitUntilFinished
|
|
|
|
{
|
2016-07-02 18:56:02 +00:00
|
|
|
[internal->cond lockWhenCondition: 1]; // Wait for finish
|
|
|
|
[internal->cond unlockWithCondition: 1]; // Signal any other watchers
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
@end
|
2009-07-15 00:02:34 +00:00
|
|
|
|
2011-03-03 10:56:47 +00:00
|
|
|
@implementation NSOperation (Private)
|
2020-09-30 09:08:36 +00:00
|
|
|
/* NB code calling this method must ensure that the receiver is retained
|
|
|
|
* until after the method returns.
|
|
|
|
*/
|
2011-03-03 10:56:47 +00:00
|
|
|
- (void) _finish
|
|
|
|
{
|
|
|
|
[internal->lock lock];
|
|
|
|
if (NO == internal->finished)
|
|
|
|
{
|
2014-03-25 10:29:35 +00:00
|
|
|
if (YES == internal->executing)
|
2011-03-03 10:56:47 +00:00
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"isExecuting"];
|
|
|
|
[self willChangeValueForKey: @"isFinished"];
|
|
|
|
internal->executing = NO;
|
|
|
|
internal->finished = YES;
|
|
|
|
[self didChangeValueForKey: @"isFinished"];
|
|
|
|
[self didChangeValueForKey: @"isExecuting"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"isFinished"];
|
|
|
|
internal->finished = YES;
|
|
|
|
[self didChangeValueForKey: @"isFinished"];
|
|
|
|
}
|
2022-01-04 12:57:55 +00:00
|
|
|
CALL_BLOCK_NO_ARGS(
|
|
|
|
((GSOperationCompletionBlock)internal->completionBlock));
|
2011-03-03 10:56:47 +00:00
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
2022-01-04 12:57:55 +00:00
|
|
|
- (void) _updateReadyState
|
2021-12-26 15:06:48 +00:00
|
|
|
{
|
|
|
|
[internal->lock lock];
|
|
|
|
if (NO == internal->ready)
|
|
|
|
{
|
|
|
|
NSEnumerator *en;
|
|
|
|
NSOperation *op;
|
|
|
|
|
|
|
|
/* After a dependency has finished or was removed we need to check
|
|
|
|
* to see if we are now ready.
|
|
|
|
* This is protected by locks so that an update due to an observed
|
|
|
|
* change in one thread won't interrupt anything in another thread.
|
|
|
|
*/
|
|
|
|
en = [internal->dependencies objectEnumerator];
|
|
|
|
while ((op = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
if (NO == [op isFinished])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (op == nil)
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"isReady"];
|
|
|
|
internal->ready = YES;
|
|
|
|
[self didChangeValueForKey: @"isReady"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
2011-03-03 10:56:47 +00:00
|
|
|
@end
|
2009-07-16 15:56:31 +00:00
|
|
|
|
2020-09-30 08:56:24 +00:00
|
|
|
|
2019-08-02 09:20:59 +00:00
|
|
|
@implementation NSBlockOperation
|
|
|
|
|
2020-06-05 16:43:46 +00:00
|
|
|
+ (instancetype) blockOperationWithBlock: (GSBlockOperationBlock)block
|
2019-08-02 09:20:59 +00:00
|
|
|
{
|
2020-06-05 16:43:46 +00:00
|
|
|
NSBlockOperation *op = [[self alloc] init];
|
2019-08-02 09:20:59 +00:00
|
|
|
|
2020-06-05 16:43:46 +00:00
|
|
|
[op addExecutionBlock: block];
|
|
|
|
return AUTORELEASE(op);
|
2019-08-02 09:20:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:43:46 +00:00
|
|
|
- (void) addExecutionBlock: (GSBlockOperationBlock)block
|
2019-08-02 09:20:59 +00:00
|
|
|
{
|
2020-12-07 12:23:30 +00:00
|
|
|
id blockCopy = (id)Block_copy(block);
|
2020-06-05 16:43:46 +00:00
|
|
|
|
|
|
|
[_executionBlocks addObject: blockCopy];
|
|
|
|
RELEASE(blockCopy);
|
2019-08-02 09:20:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:43:46 +00:00
|
|
|
- (void) dealloc
|
2019-08-02 09:20:59 +00:00
|
|
|
{
|
2020-06-05 16:43:46 +00:00
|
|
|
RELEASE(_executionBlocks);
|
2024-11-17 16:55:54 +00:00
|
|
|
DEALLOC
|
2019-08-02 09:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) executionBlocks
|
|
|
|
{
|
2019-08-05 04:00:55 +00:00
|
|
|
return _executionBlocks;
|
2019-08-02 09:20:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:43:46 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil)
|
|
|
|
{
|
|
|
|
_executionBlocks = [[NSMutableArray alloc] initWithCapacity: 1];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-08-02 09:20:59 +00:00
|
|
|
- (void) main
|
|
|
|
{
|
2024-08-16 12:08:41 +00:00
|
|
|
NSEnumerator *en = [_executionBlocks objectEnumerator];
|
2019-08-02 09:20:59 +00:00
|
|
|
GSBlockOperationBlock theBlock;
|
2020-06-05 16:43:46 +00:00
|
|
|
|
2020-12-07 12:23:30 +00:00
|
|
|
while ((theBlock = (GSBlockOperationBlock)[en nextObject]) != NULL)
|
2019-08-02 09:20:59 +00:00
|
|
|
{
|
2021-11-17 13:35:49 +00:00
|
|
|
CALL_NON_NULL_BLOCK_NO_ARGS(theBlock);
|
2019-08-02 09:20:59 +00:00
|
|
|
}
|
2024-08-16 12:08:41 +00:00
|
|
|
|
|
|
|
[_executionBlocks removeAllObjects];
|
2019-08-02 09:20:59 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2009-07-21 09:40:48 +00:00
|
|
|
#undef GSInternal
|
|
|
|
#define GSInternal NSOperationQueueInternal
|
|
|
|
#include "GSInternal.h"
|
2010-02-26 10:25:35 +00:00
|
|
|
GS_PRIVATE_INTERNAL(NSOperationQueue)
|
2009-07-16 15:56:31 +00:00
|
|
|
|
|
|
|
|
2010-02-06 17:10:16 +00:00
|
|
|
@interface NSOperationQueue (Private)
|
2011-03-03 10:56:47 +00:00
|
|
|
- (void) _execute;
|
2024-09-08 14:54:01 +00:00
|
|
|
- (void) _thread: (NSNumber *) threadNumber;
|
2010-02-06 17:10:16 +00:00
|
|
|
- (void) observeValueForKeyPath: (NSString *)keyPath
|
|
|
|
ofObject: (id)object
|
|
|
|
change: (NSDictionary *)change
|
|
|
|
context: (void *)context;
|
|
|
|
@end
|
|
|
|
|
2022-08-16 08:36:27 +00:00
|
|
|
static NSInteger maxConcurrent = 8; // Thread pool size
|
2010-02-06 17:10:16 +00:00
|
|
|
|
|
|
|
static NSComparisonResult
|
|
|
|
sortFunc(id o1, id o2, void *ctxt)
|
|
|
|
{
|
|
|
|
NSOperationQueuePriority p1 = [o1 queuePriority];
|
|
|
|
NSOperationQueuePriority p2 = [o2 queuePriority];
|
2012-03-26 14:47:07 +00:00
|
|
|
|
2010-02-06 17:10:16 +00:00
|
|
|
if (p1 < p2) return NSOrderedDescending;
|
|
|
|
if (p1 > p2) return NSOrderedAscending;
|
|
|
|
return NSOrderedSame;
|
|
|
|
}
|
|
|
|
|
2010-02-08 10:34:27 +00:00
|
|
|
static NSString *threadKey = @"NSOperationQueue";
|
|
|
|
static NSOperationQueue *mainQueue = nil;
|
|
|
|
|
2009-07-15 00:02:34 +00:00
|
|
|
@implementation NSOperationQueue
|
|
|
|
|
2010-02-08 10:34:27 +00:00
|
|
|
+ (id) currentQueue
|
|
|
|
{
|
2014-01-07 16:15:33 +00:00
|
|
|
if ([NSThread isMainThread])
|
|
|
|
{
|
|
|
|
return mainQueue;
|
|
|
|
}
|
2010-02-08 10:34:27 +00:00
|
|
|
return [[[NSThread currentThread] threadDictionary] objectForKey: threadKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
2014-01-07 16:15:33 +00:00
|
|
|
if (nil == mainQueue)
|
2010-02-08 10:34:27 +00:00
|
|
|
{
|
2014-01-07 16:15:33 +00:00
|
|
|
mainQueue = [self new];
|
2010-02-08 10:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) mainQueue
|
|
|
|
{
|
|
|
|
return mainQueue;
|
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (void) addOperation: (NSOperation *)op
|
|
|
|
{
|
|
|
|
if (op == nil || NO == [op isKindOfClass: [NSOperation class]])
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] object is not an NSOperation",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
[internal->lock lock];
|
2010-02-06 17:10:16 +00:00
|
|
|
if (NSNotFound == [internal->operations indexOfObjectIdenticalTo: op]
|
|
|
|
&& NO == [op isFinished])
|
2010-02-04 16:47:45 +00:00
|
|
|
{
|
2010-02-06 17:10:16 +00:00
|
|
|
[op addObserver: self
|
|
|
|
forKeyPath: @"isReady"
|
|
|
|
options: NSKeyValueObservingOptionNew
|
2016-07-18 09:51:35 +00:00
|
|
|
context: isReadyCtxt];
|
2010-02-06 17:10:16 +00:00
|
|
|
[self willChangeValueForKey: @"operations"];
|
|
|
|
[self willChangeValueForKey: @"operationCount"];
|
2010-02-04 16:47:45 +00:00
|
|
|
[internal->operations addObject: op];
|
2010-02-06 17:10:16 +00:00
|
|
|
[self didChangeValueForKey: @"operationCount"];
|
|
|
|
[self didChangeValueForKey: @"operations"];
|
|
|
|
if (YES == [op isReady])
|
|
|
|
{
|
|
|
|
[self observeValueForKeyPath: @"isReady"
|
|
|
|
ofObject: op
|
|
|
|
change: nil
|
2016-07-18 09:51:35 +00:00
|
|
|
context: isReadyCtxt];
|
2010-02-06 17:10:16 +00:00
|
|
|
}
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
2019-08-02 17:29:42 +00:00
|
|
|
- (void) addOperationWithBlock: (GSBlockOperationBlock)block
|
|
|
|
{
|
|
|
|
NSBlockOperation *bop = [NSBlockOperation blockOperationWithBlock: block];
|
|
|
|
[self addOperation: bop];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-08 10:34:27 +00:00
|
|
|
- (void) addOperations: (NSArray *)ops
|
|
|
|
waitUntilFinished: (BOOL)shouldWait
|
|
|
|
{
|
2010-02-26 11:04:14 +00:00
|
|
|
NSUInteger total;
|
2010-02-09 08:08:26 +00:00
|
|
|
NSUInteger index;
|
2010-02-08 10:34:27 +00:00
|
|
|
|
|
|
|
if (ops == nil || NO == [ops isKindOfClass: [NSArray class]])
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] object is not an NSArray",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
2010-02-26 11:04:14 +00:00
|
|
|
total = [ops count];
|
|
|
|
if (total > 0)
|
2010-02-08 10:34:27 +00:00
|
|
|
{
|
|
|
|
BOOL invalidArg = NO;
|
2010-02-26 11:04:14 +00:00
|
|
|
NSUInteger toAdd = total;
|
2011-02-11 14:07:49 +00:00
|
|
|
GS_BEGINITEMBUF(buf, total, id)
|
2010-02-08 10:34:27 +00:00
|
|
|
|
|
|
|
[ops getObjects: buf];
|
2010-02-26 11:04:14 +00:00
|
|
|
for (index = 0; index < total; index++)
|
2010-02-08 10:34:27 +00:00
|
|
|
{
|
|
|
|
NSOperation *op = buf[index];
|
|
|
|
|
|
|
|
if (NO == [op isKindOfClass: [NSOperation class]])
|
|
|
|
{
|
|
|
|
invalidArg = YES;
|
|
|
|
toAdd = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (YES == [op isFinished])
|
|
|
|
{
|
|
|
|
buf[index] = nil;
|
|
|
|
toAdd--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (toAdd > 0)
|
|
|
|
{
|
|
|
|
[internal->lock lock];
|
|
|
|
[self willChangeValueForKey: @"operationCount"];
|
|
|
|
[self willChangeValueForKey: @"operations"];
|
2010-02-26 11:04:14 +00:00
|
|
|
for (index = 0; index < total; index++)
|
2010-02-08 10:34:27 +00:00
|
|
|
{
|
|
|
|
NSOperation *op = buf[index];
|
|
|
|
|
|
|
|
if (op == nil)
|
|
|
|
{
|
|
|
|
continue; // Not added
|
|
|
|
}
|
|
|
|
if (NSNotFound
|
|
|
|
!= [internal->operations indexOfObjectIdenticalTo: op])
|
|
|
|
{
|
|
|
|
buf[index] = nil; // Not added
|
|
|
|
toAdd--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
[op addObserver: self
|
|
|
|
forKeyPath: @"isReady"
|
|
|
|
options: NSKeyValueObservingOptionNew
|
2016-07-18 09:51:35 +00:00
|
|
|
context: isReadyCtxt];
|
2010-02-08 10:34:27 +00:00
|
|
|
[internal->operations addObject: op];
|
|
|
|
if (NO == [op isReady])
|
|
|
|
{
|
|
|
|
buf[index] = nil; // Not yet ready
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[self didChangeValueForKey: @"operationCount"];
|
|
|
|
[self didChangeValueForKey: @"operations"];
|
2010-02-26 11:04:14 +00:00
|
|
|
for (index = 0; index < total; index++)
|
2010-02-08 10:34:27 +00:00
|
|
|
{
|
|
|
|
NSOperation *op = buf[index];
|
|
|
|
|
|
|
|
if (op != nil)
|
|
|
|
{
|
|
|
|
[self observeValueForKeyPath: @"isReady"
|
|
|
|
ofObject: op
|
|
|
|
change: nil
|
2016-07-18 09:51:35 +00:00
|
|
|
context: isReadyCtxt];
|
2010-02-08 10:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
GS_ENDITEMBUF()
|
|
|
|
if (YES == invalidArg)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
2013-07-02 15:46:26 +00:00
|
|
|
format: @"[%@-%@] object at index %"PRIuPTR" is not an NSOperation",
|
2010-02-08 10:34:27 +00:00
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd),
|
|
|
|
index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (YES == shouldWait)
|
|
|
|
{
|
|
|
|
[self waitUntilAllOperationsAreFinished];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (void) cancelAllOperations
|
|
|
|
{
|
2011-03-03 10:56:47 +00:00
|
|
|
[[self operations] makeObjectsPerformSelector: @selector(cancel)];
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2024-11-17 16:50:16 +00:00
|
|
|
if (GS_EXISTS_INTERNAL && internal->lock != nil)
|
2024-11-17 16:41:59 +00:00
|
|
|
{
|
2024-11-17 20:57:04 +00:00
|
|
|
[self cancelAllOperations];
|
2024-11-17 16:41:59 +00:00
|
|
|
DESTROY(internal->operations);
|
|
|
|
DESTROY(internal->starting);
|
|
|
|
DESTROY(internal->waiting);
|
|
|
|
DESTROY(internal->name);
|
|
|
|
DESTROY(internal->cond);
|
|
|
|
DESTROY(internal->lock);
|
|
|
|
GS_DESTROY_INTERNAL(NSOperationQueue);
|
|
|
|
}
|
2024-11-17 16:55:54 +00:00
|
|
|
DEALLOC
|
2009-07-16 15:56:31 +00:00
|
|
|
}
|
|
|
|
|
2009-07-15 00:02:34 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
if ((self = [super init]) != nil)
|
2009-07-15 00:02:34 +00:00
|
|
|
{
|
2009-07-21 09:40:48 +00:00
|
|
|
GS_CREATE_INTERNAL(NSOperationQueue);
|
2009-07-16 15:56:31 +00:00
|
|
|
internal->suspended = NO;
|
2022-08-16 08:36:27 +00:00
|
|
|
internal->maxThreads = NSOperationQueueDefaultMaxConcurrentOperationCount;
|
2009-07-16 15:56:31 +00:00
|
|
|
internal->operations = [NSMutableArray new];
|
2011-03-03 15:14:29 +00:00
|
|
|
internal->starting = [NSMutableArray new];
|
2010-02-06 17:10:16 +00:00
|
|
|
internal->waiting = [NSMutableArray new];
|
2010-02-04 16:47:45 +00:00
|
|
|
internal->lock = [NSRecursiveLock new];
|
2018-03-26 14:05:01 +00:00
|
|
|
[internal->lock setName:
|
|
|
|
[NSString stringWithFormat: @"lock-for-op-%p", self]];
|
2011-03-03 15:14:29 +00:00
|
|
|
internal->cond = [[NSConditionLock alloc] initWithCondition: 0];
|
2018-03-26 14:05:01 +00:00
|
|
|
[internal->cond setName:
|
|
|
|
[NSString stringWithFormat: @"cond-for-op-%p", self]];
|
2024-11-14 17:57:55 +00:00
|
|
|
internal->name
|
|
|
|
= [[NSString alloc] initWithFormat: @"NSOperationQueue %p", self];
|
2024-09-08 14:54:01 +00:00
|
|
|
|
|
|
|
/* Ensure that default thread name can be displayed on systems with a
|
|
|
|
* limited thread name length.
|
|
|
|
*
|
|
|
|
* This value is set to internal->name, when altered with -setName:
|
|
|
|
* Worker threads are not renamed during their lifetime.
|
|
|
|
*/
|
|
|
|
internal->threadName = @"NSOperationQ";
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isSuspended
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
return internal->suspended;
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger) maxConcurrentOperationCount
|
|
|
|
{
|
2022-08-16 08:36:27 +00:00
|
|
|
return internal->maxThreads;
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
2010-02-06 17:10:16 +00:00
|
|
|
- (NSString*) name
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
[internal->lock lock];
|
2024-09-08 14:54:01 +00:00
|
|
|
s = [internal->name copy];
|
2010-02-06 17:10:16 +00:00
|
|
|
[internal->lock unlock];
|
2024-09-08 14:54:01 +00:00
|
|
|
|
2020-06-05 16:43:46 +00:00
|
|
|
return AUTORELEASE(s);
|
2010-02-06 17:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger) operationCount
|
|
|
|
{
|
|
|
|
NSUInteger c;
|
|
|
|
|
|
|
|
[internal->lock lock];
|
|
|
|
c = [internal->operations count];
|
|
|
|
[internal->lock unlock];
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (NSArray *) operations
|
2009-07-15 00:02:34 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
NSArray *a;
|
2009-07-15 00:02:34 +00:00
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
[internal->lock lock];
|
|
|
|
a = [NSArray arrayWithArray: internal->operations];
|
|
|
|
[internal->lock unlock];
|
|
|
|
return a;
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (void) setMaxConcurrentOperationCount: (NSInteger)cnt
|
2009-07-15 00:02:34 +00:00
|
|
|
{
|
2010-02-06 17:10:16 +00:00
|
|
|
if (cnt < 0
|
|
|
|
&& cnt != NSOperationQueueDefaultMaxConcurrentOperationCount)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
2013-07-02 15:46:26 +00:00
|
|
|
format: @"[%@-%@] cannot set negative (%"PRIdPTR") count",
|
2010-02-06 17:10:16 +00:00
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd), cnt];
|
|
|
|
}
|
|
|
|
[internal->lock lock];
|
2022-08-16 08:36:27 +00:00
|
|
|
if (cnt != internal->maxThreads)
|
2010-02-06 17:10:16 +00:00
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"maxConcurrentOperationCount"];
|
2022-08-16 08:36:27 +00:00
|
|
|
internal->maxThreads = cnt;
|
2010-02-06 17:10:16 +00:00
|
|
|
[self didChangeValueForKey: @"maxConcurrentOperationCount"];
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
2011-03-03 10:56:47 +00:00
|
|
|
[self _execute];
|
2010-02-06 17:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setName: (NSString*)s
|
|
|
|
{
|
2024-09-08 14:54:01 +00:00
|
|
|
if (s == nil) return;
|
|
|
|
|
2010-02-06 17:10:16 +00:00
|
|
|
[internal->lock lock];
|
|
|
|
if (NO == [internal->name isEqual: s])
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"name"];
|
2020-06-05 16:43:46 +00:00
|
|
|
RELEASE(internal->name);
|
2010-02-06 17:10:16 +00:00
|
|
|
internal->name = [s copy];
|
2024-09-08 14:54:01 +00:00
|
|
|
// internal->threadName is unretained
|
|
|
|
internal->threadName = internal->name;
|
2010-02-06 17:10:16 +00:00
|
|
|
[self didChangeValueForKey: @"name"];
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
- (void) setSuspended: (BOOL)flag
|
2009-07-15 00:02:34 +00:00
|
|
|
{
|
2010-02-06 17:10:16 +00:00
|
|
|
[internal->lock lock];
|
|
|
|
if (flag != internal->suspended)
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"suspended"];
|
|
|
|
internal->suspended = flag;
|
|
|
|
[self didChangeValueForKey: @"suspended"];
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
2011-03-03 10:56:47 +00:00
|
|
|
[self _execute];
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) waitUntilAllOperationsAreFinished
|
|
|
|
{
|
2010-02-06 17:10:16 +00:00
|
|
|
NSOperation *op;
|
|
|
|
|
|
|
|
[internal->lock lock];
|
|
|
|
while ((op = [internal->operations lastObject]) != nil)
|
|
|
|
{
|
2020-06-05 16:43:46 +00:00
|
|
|
RETAIN(op);
|
2010-02-06 17:10:16 +00:00
|
|
|
[internal->lock unlock];
|
|
|
|
[op waitUntilFinished];
|
2020-06-05 16:43:46 +00:00
|
|
|
RELEASE(op);
|
2010-02-06 17:10:16 +00:00
|
|
|
[internal->lock lock];
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
@end
|
2010-02-06 17:10:16 +00:00
|
|
|
|
|
|
|
@implementation NSOperationQueue (Private)
|
|
|
|
|
|
|
|
- (void) observeValueForKeyPath: (NSString *)keyPath
|
|
|
|
ofObject: (id)object
|
|
|
|
change: (NSDictionary *)change
|
|
|
|
context: (void *)context
|
|
|
|
{
|
2016-07-18 09:51:35 +00:00
|
|
|
/* We observe three properties in sequence ...
|
|
|
|
* isReady (while we wait for an operation to be ready)
|
|
|
|
* queuePriority (when priority of a ready operation may change)
|
|
|
|
* isFinished (to see if an executing operation is over).
|
|
|
|
*/
|
|
|
|
if (context == isFinishedCtxt)
|
2010-02-06 17:10:16 +00:00
|
|
|
{
|
2021-12-26 15:06:48 +00:00
|
|
|
NSOperation *op = object;
|
|
|
|
if (YES == [op isFinished])
|
|
|
|
{
|
|
|
|
[internal->lock lock];
|
|
|
|
internal->executing--;
|
|
|
|
[object removeObserver: self forKeyPath: @"isFinished"];
|
|
|
|
[internal->lock unlock];
|
|
|
|
[self willChangeValueForKey: @"operations"];
|
|
|
|
[self willChangeValueForKey: @"operationCount"];
|
|
|
|
[internal->lock lock];
|
|
|
|
[internal->operations removeObjectIdenticalTo: object];
|
|
|
|
[internal->lock unlock];
|
|
|
|
[self didChangeValueForKey: @"operationCount"];
|
|
|
|
[self didChangeValueForKey: @"operations"];
|
|
|
|
}
|
2010-02-06 17:10:16 +00:00
|
|
|
}
|
2016-07-18 09:51:35 +00:00
|
|
|
else if (context == queuePriorityCtxt || context == isReadyCtxt)
|
2010-02-06 17:10:16 +00:00
|
|
|
{
|
2016-07-18 09:51:35 +00:00
|
|
|
NSInteger pos;
|
|
|
|
|
|
|
|
[internal->lock lock];
|
|
|
|
if (context == queuePriorityCtxt)
|
|
|
|
{
|
|
|
|
[internal->waiting removeObjectIdenticalTo: object];
|
|
|
|
}
|
|
|
|
if (context == isReadyCtxt)
|
|
|
|
{
|
|
|
|
[object removeObserver: self forKeyPath: @"isReady"];
|
|
|
|
[object addObserver: self
|
|
|
|
forKeyPath: @"queuePriority"
|
|
|
|
options: NSKeyValueObservingOptionNew
|
|
|
|
context: queuePriorityCtxt];
|
|
|
|
}
|
|
|
|
pos = [internal->waiting insertionPosition: object
|
|
|
|
usingFunction: sortFunc
|
|
|
|
context: 0];
|
|
|
|
[internal->waiting insertObject: object atIndex: pos];
|
2015-08-30 06:40:40 +00:00
|
|
|
[internal->lock unlock];
|
2010-02-06 17:10:16 +00:00
|
|
|
}
|
2011-03-03 10:56:47 +00:00
|
|
|
[self _execute];
|
2010-02-06 17:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-09-08 14:54:01 +00:00
|
|
|
- (void) _thread: (NSNumber *) threadNumber
|
2011-03-03 15:14:29 +00:00
|
|
|
{
|
2024-09-08 14:54:01 +00:00
|
|
|
NSString *tName;
|
|
|
|
NSThread *current;
|
|
|
|
|
2020-10-11 10:24:49 +00:00
|
|
|
CREATE_AUTORELEASE_POOL(arp);
|
2011-03-03 15:14:29 +00:00
|
|
|
|
2024-09-08 14:54:01 +00:00
|
|
|
current = [NSThread currentThread];
|
|
|
|
|
|
|
|
[internal->lock lock];
|
|
|
|
tName = [internal->threadName stringByAppendingFormat: @"_%@", threadNumber];
|
|
|
|
[internal->lock unlock];
|
|
|
|
|
|
|
|
[[current threadDictionary] setObject: self forKey: threadKey];
|
|
|
|
[current setName: tName];
|
|
|
|
|
2011-03-03 15:14:29 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2020-12-07 12:23:30 +00:00
|
|
|
NSOperation *op;
|
|
|
|
NSDate *when;
|
|
|
|
BOOL found;
|
2020-09-30 08:56:24 +00:00
|
|
|
/* We use a pool for each operation in case releasing the operation
|
|
|
|
* causes it to be deallocated, and the deallocation of the operation
|
|
|
|
* autoreleases something which needs to be cleaned up.
|
|
|
|
*/
|
2020-10-11 10:24:49 +00:00
|
|
|
RECREATE_AUTORELEASE_POOL(arp);
|
2011-03-03 15:14:29 +00:00
|
|
|
|
|
|
|
when = [[NSDate alloc] initWithTimeIntervalSinceNow: 5.0];
|
|
|
|
found = [internal->cond lockWhenCondition: 1 beforeDate: when];
|
2016-06-19 07:37:58 +00:00
|
|
|
RELEASE(when);
|
2011-03-03 15:14:29 +00:00
|
|
|
if (NO == found)
|
|
|
|
{
|
2025-01-06 20:55:55 +00:00
|
|
|
[internal->cond lock];
|
|
|
|
if ([internal->starting count] == 0)
|
|
|
|
{
|
|
|
|
// Idle for 5 seconds ... exit thread.
|
2025-04-08 14:06:29 +00:00
|
|
|
[internal->cond unlock];
|
2025-01-06 20:55:55 +00:00
|
|
|
[[[NSThread currentThread] threadDictionary]
|
|
|
|
removeObjectForKey: threadKey];
|
|
|
|
[internal->lock lock];
|
|
|
|
internal->threadCount--;
|
|
|
|
[internal->lock unlock];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* An operation was added in the gap between the failed wait for
|
|
|
|
* the condition and us unconditionally locking the condition, so
|
|
|
|
* we fall through to execute that operation.
|
|
|
|
*/
|
2011-03-03 15:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ([internal->starting count] > 0)
|
|
|
|
{
|
2016-06-19 07:37:58 +00:00
|
|
|
op = RETAIN([internal->starting objectAtIndex: 0]);
|
2011-03-03 15:14:29 +00:00
|
|
|
[internal->starting removeObjectAtIndex: 0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
op = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([internal->starting count] > 0)
|
|
|
|
{
|
|
|
|
// Signal any other idle threads,
|
|
|
|
[internal->cond unlockWithCondition: 1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// There are no more operations starting.
|
|
|
|
[internal->cond unlockWithCondition: 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nil != op)
|
|
|
|
{
|
|
|
|
NS_DURING
|
|
|
|
{
|
2020-06-05 16:43:46 +00:00
|
|
|
ENTER_POOL
|
2018-12-04 11:09:18 +00:00
|
|
|
[NSThread setThreadPriority: [op threadPriority]];
|
|
|
|
[op start];
|
2020-06-05 16:43:46 +00:00
|
|
|
LEAVE_POOL
|
2011-03-03 15:14:29 +00:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSLog(@"Problem running operation %@ ... %@",
|
|
|
|
op, localException);
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
[op _finish];
|
2016-06-19 07:37:58 +00:00
|
|
|
RELEASE(op);
|
2011-03-03 15:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 10:24:49 +00:00
|
|
|
DESTROY(arp);
|
2011-03-03 15:14:29 +00:00
|
|
|
[NSThread exit];
|
|
|
|
}
|
|
|
|
|
2011-03-03 10:56:47 +00:00
|
|
|
/* Check for operations which can be executed and start them.
|
|
|
|
*/
|
|
|
|
- (void) _execute
|
2010-02-06 17:10:16 +00:00
|
|
|
{
|
2011-03-03 10:56:47 +00:00
|
|
|
NSInteger max;
|
2010-02-08 10:34:27 +00:00
|
|
|
|
2010-02-06 17:10:16 +00:00
|
|
|
[internal->lock lock];
|
2011-03-03 10:56:47 +00:00
|
|
|
|
|
|
|
max = [self maxConcurrentOperationCount];
|
|
|
|
if (NSOperationQueueDefaultMaxConcurrentOperationCount == max)
|
|
|
|
{
|
|
|
|
max = maxConcurrent;
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:58:55 +00:00
|
|
|
NS_DURING
|
2011-03-03 10:56:47 +00:00
|
|
|
while (NO == [self isSuspended]
|
|
|
|
&& max > internal->executing
|
|
|
|
&& [internal->waiting count] > 0)
|
2010-02-06 17:10:16 +00:00
|
|
|
{
|
|
|
|
NSOperation *op;
|
|
|
|
|
2011-03-03 10:56:47 +00:00
|
|
|
/* Take the first operation from the queue and start it executing.
|
|
|
|
* We set ourselves up as an observer for the operating finishing
|
|
|
|
* and we keep track of the count of operations we have started,
|
|
|
|
* but the actual startup is left to the NSOperation -start method.
|
2010-02-06 17:10:16 +00:00
|
|
|
*/
|
2011-03-03 10:56:47 +00:00
|
|
|
op = [internal->waiting objectAtIndex: 0];
|
|
|
|
[internal->waiting removeObjectAtIndex: 0];
|
2016-07-18 09:51:35 +00:00
|
|
|
[op removeObserver: self forKeyPath: @"queuePriority"];
|
2011-03-03 10:56:47 +00:00
|
|
|
[op addObserver: self
|
|
|
|
forKeyPath: @"isFinished"
|
|
|
|
options: NSKeyValueObservingOptionNew
|
2016-07-18 09:51:35 +00:00
|
|
|
context: isFinishedCtxt];
|
2011-03-03 10:56:47 +00:00
|
|
|
internal->executing++;
|
|
|
|
if (YES == [op isConcurrent])
|
2010-02-06 17:10:16 +00:00
|
|
|
{
|
2011-03-03 10:56:47 +00:00
|
|
|
[op start];
|
2010-02-08 10:34:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-03 15:14:29 +00:00
|
|
|
[internal->cond lock];
|
|
|
|
[internal->starting addObject: op];
|
|
|
|
|
|
|
|
/* Create a new thread if all existing threads are busy and
|
|
|
|
* we haven't reached the pool limit.
|
|
|
|
*/
|
2024-01-29 19:50:44 +00:00
|
|
|
if (internal->threadCount < max)
|
2011-03-03 15:14:29 +00:00
|
|
|
{
|
2025-01-06 20:55:55 +00:00
|
|
|
NSInteger count = internal->threadCount++;
|
|
|
|
NSNumber *threadNumber = [NSNumber numberWithInteger: count];
|
|
|
|
|
2020-04-16 21:48:25 +00:00
|
|
|
NS_DURING
|
|
|
|
{
|
2024-09-08 14:54:01 +00:00
|
|
|
[NSThread detachNewThreadSelector: @selector(_thread:)
|
2020-04-16 21:48:25 +00:00
|
|
|
toTarget: self
|
2024-09-08 14:54:01 +00:00
|
|
|
withObject: threadNumber];
|
2020-04-16 21:48:25 +00:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
2025-01-06 20:55:55 +00:00
|
|
|
NSLog(@"Failed to create thread %@ for %@: %@",
|
|
|
|
threadNumber, self, localException);
|
2020-04-16 21:48:25 +00:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
2011-03-03 15:14:29 +00:00
|
|
|
}
|
|
|
|
/* Tell the thread pool that there is an operation to start.
|
|
|
|
*/
|
|
|
|
[internal->cond unlockWithCondition: 1];
|
2010-02-06 17:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-01 20:58:55 +00:00
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[internal->lock unlock];
|
|
|
|
[localException raise];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
2010-02-08 10:34:27 +00:00
|
|
|
[internal->lock unlock];
|
2010-02-06 17:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|