2009-07-17 05:13:52 +00:00
|
|
|
/**Implementation for NSOperation for GNUStep
|
2010-02-04 16:47:45 +00:00
|
|
|
Copyright (C) 2009,2010 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>
|
|
|
|
Date: 2009,2010
|
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.
|
|
|
|
|
|
|
|
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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
|
|
|
|
2009-07-17 05:13:52 +00:00
|
|
|
<title>NSOperation class reference</title>
|
|
|
|
$Date: 2008-06-08 11:38:33 +0100 (Sun, 08 Jun 2008) $ $Revision: 26606 $
|
2009-07-13 18:14:42 +00:00
|
|
|
*/
|
|
|
|
|
2009-07-17 05:13:52 +00:00
|
|
|
#import "config.h"
|
|
|
|
#import "Foundation/NSOperation.h"
|
|
|
|
#import "Foundation/NSArray.h"
|
2010-02-04 16:47:45 +00:00
|
|
|
#import "Foundation/NSAutoreleasePool.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/NSLock.h"
|
|
|
|
#import "Foundation/NSKeyValueObserving.h"
|
|
|
|
#import "Foundation/NSThread.h"
|
2009-07-13 18:14:42 +00:00
|
|
|
|
2009-07-21 09:40:48 +00:00
|
|
|
#define GSInternal NSOperationInternal
|
|
|
|
#include "GSInternal.h"
|
|
|
|
GS_BEGIN_INTERNAL(NSOperation)
|
2010-02-04 16:47:45 +00:00
|
|
|
NSRecursiveLock *lock;
|
|
|
|
NSConditionLock *cond;
|
2009-07-16 15:56:31 +00:00
|
|
|
NSOperationQueuePriority priority;
|
2010-02-04 16:47:45 +00:00
|
|
|
double threadPriority;
|
2009-07-16 15:56:31 +00:00
|
|
|
BOOL cancelled;
|
|
|
|
BOOL concurrent;
|
|
|
|
BOOL executing;
|
|
|
|
BOOL finished;
|
|
|
|
BOOL ready;
|
|
|
|
NSMutableArray *dependencies;
|
2009-07-21 09:40:48 +00:00
|
|
|
GS_END_INTERNAL(NSOperation)
|
2009-07-16 15:56:31 +00:00
|
|
|
|
2010-02-04 16:47:45 +00:00
|
|
|
static NSArray *empty = nil;
|
2009-07-16 15:56:31 +00:00
|
|
|
|
2009-07-13 18:14:42 +00:00
|
|
|
@implementation NSOperation : NSObject
|
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) initialize
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
empty = [NSArray new];
|
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
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
if (NO == [op isKindOfClass: [self 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
|
|
|
|
context: NULL];
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (internal != nil)
|
|
|
|
{
|
|
|
|
NSOperation *op;
|
2009-07-13 18:14:42 +00:00
|
|
|
|
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);
|
|
|
|
GS_DESTROY_INTERNAL(NSOperation);
|
|
|
|
}
|
|
|
|
[super 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)
|
|
|
|
{
|
|
|
|
a = empty; // OSX return an empty array
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
internal->cond = [[NSConditionLock alloc] initWithCondition: 0];
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-02-04 16:47:45 +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
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
/* Some dependency has finished (or been removed) ...
|
|
|
|
* so we need to check to see if we are now ready unless we know we are.
|
|
|
|
* This is protected by locks so that an update due to an observed
|
|
|
|
* change in one thread won't interrupt anything in another thread.
|
|
|
|
*/
|
|
|
|
[internal->lock lock];
|
|
|
|
if (NO == internal->ready)
|
|
|
|
{
|
|
|
|
NSEnumerator *en;
|
|
|
|
NSOperation *op;
|
|
|
|
|
|
|
|
en = [internal->dependencies objectEnumerator];
|
|
|
|
while ((op = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
if (NO == [op isReady])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (op == nil)
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"isReady"];
|
|
|
|
internal->ready = YES;
|
|
|
|
[self didChangeValueForKey: @"isReady"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
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
|
|
|
{
|
|
|
|
[op removeObserver: self
|
|
|
|
forKeyPath: @"isFinished"];
|
|
|
|
[self willChangeValueForKey: @"dependencies"];
|
|
|
|
[internal->dependencies removeObject: op];
|
|
|
|
if (NO == internal->ready)
|
|
|
|
{
|
|
|
|
/* The dependency may cause us to become ready ...
|
|
|
|
* fake an observation so we can deal with that.
|
|
|
|
*/
|
|
|
|
[self observeValueForKeyPath: @"isFinished"
|
|
|
|
ofObject: op
|
|
|
|
change: nil
|
|
|
|
context: nil];
|
|
|
|
}
|
|
|
|
[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
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setQueuePriority: (NSOperationQueuePriority)pri
|
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
if (pri < NSOperationQueuePriorityVeryLow)
|
|
|
|
pri = NSOperationQueuePriorityVeryLow;
|
|
|
|
else if (pri < NSOperationQueuePriorityLow)
|
|
|
|
pri = NSOperationQueuePriorityLow;
|
|
|
|
else if (pri < NSOperationQueuePriorityNormal)
|
|
|
|
pri = NSOperationQueuePriorityNormal;
|
|
|
|
else if (pri > NSOperationQueuePriorityVeryHigh)
|
|
|
|
pri = NSOperationQueuePriorityVeryHigh;
|
|
|
|
else if (pri > NSOperationQueuePriorityHigh)
|
|
|
|
pri = NSOperationQueuePriorityHigh;
|
|
|
|
else if (pri > NSOperationQueuePriorityNormal)
|
|
|
|
pri = NSOperationQueuePriorityNormal;
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
CREATE_AUTORELEASE_POOL(pool);
|
2010-02-05 11:41:24 +00:00
|
|
|
double prio = [NSThread threadPriority];
|
2010-02-04 16:47:45 +00:00
|
|
|
|
|
|
|
[internal->lock lock];
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
if (YES == [self isConcurrent])
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] called on concurrent operation",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
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)];
|
|
|
|
}
|
|
|
|
|
|
|
|
[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
|
|
|
|
2010-02-05 11:41:24 +00:00
|
|
|
[internal->lock lock];
|
|
|
|
NS_DURING
|
|
|
|
{
|
2010-02-04 16:47:45 +00:00
|
|
|
/* Notify KVO system of changes to isExecuting and isFinished
|
|
|
|
*/
|
|
|
|
[self willChangeValueForKey: @"isExecuting"];
|
|
|
|
[self willChangeValueForKey: @"isFinished"];
|
|
|
|
internal->executing = NO;
|
|
|
|
internal->finished = YES;
|
|
|
|
[self didChangeValueForKey: @"isFinished"];
|
|
|
|
[self didChangeValueForKey: @"isExecuting"];
|
|
|
|
[internal->cond lock];
|
|
|
|
[internal->cond unlockWithCondition: 1];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
2010-02-05 11:41:24 +00:00
|
|
|
[internal->lock unlock];
|
|
|
|
[localException raise];
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
[internal->lock unlock];
|
|
|
|
RELEASE(pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (double) threadPriority
|
|
|
|
{
|
|
|
|
return internal->threadPriority;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) waitUntilFinished
|
|
|
|
{
|
|
|
|
[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
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
|
2009-07-21 09:40:48 +00:00
|
|
|
#undef GSInternal
|
|
|
|
#define GSInternal NSOperationQueueInternal
|
|
|
|
#include "GSInternal.h"
|
|
|
|
GS_BEGIN_INTERNAL(NSOperationQueue)
|
2010-02-04 16:47:45 +00:00
|
|
|
NSRecursiveLock *lock;
|
2010-02-06 17:10:16 +00:00
|
|
|
NSConditionLock *cond;
|
2009-07-16 15:56:31 +00:00
|
|
|
NSMutableArray *operations;
|
2010-02-06 17:10:16 +00:00
|
|
|
NSMutableArray *waiting;
|
|
|
|
NSString *name;
|
2009-07-16 15:56:31 +00:00
|
|
|
BOOL suspended;
|
2010-02-06 17:10:16 +00:00
|
|
|
NSInteger threads; // number of threads allocated
|
|
|
|
NSInteger idle; // threads waiting for an op to do
|
|
|
|
NSInteger count; // max executing operations
|
2009-07-21 09:40:48 +00:00
|
|
|
GS_END_INTERNAL(NSOperationQueue)
|
2009-07-16 15:56:31 +00:00
|
|
|
|
|
|
|
|
2010-02-06 17:10:16 +00:00
|
|
|
@interface NSOperationQueue (Private)
|
|
|
|
- (void) observeValueForKeyPath: (NSString *)keyPath
|
|
|
|
ofObject: (id)object
|
|
|
|
change: (NSDictionary *)change
|
|
|
|
context: (void *)context;
|
|
|
|
- (void) _thread;
|
|
|
|
- (void) _update;
|
|
|
|
@end
|
|
|
|
|
|
|
|
static NSInteger maxThreads = 200; // FIXME ... how many really?
|
|
|
|
|
|
|
|
static NSComparisonResult
|
|
|
|
sortFunc(id o1, id o2, void *ctxt)
|
|
|
|
{
|
|
|
|
NSOperationQueuePriority p1 = [o1 queuePriority];
|
|
|
|
NSOperationQueuePriority p2 = [o2 queuePriority];
|
|
|
|
|
|
|
|
if (p1 < p2) return NSOrderedDescending;
|
|
|
|
if (p1 > p2) return NSOrderedAscending;
|
|
|
|
return NSOrderedSame;
|
|
|
|
}
|
|
|
|
|
2009-07-15 00:02:34 +00:00
|
|
|
@implementation NSOperationQueue
|
|
|
|
|
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 isCancelled]
|
|
|
|
&& NO == [op isExecuting]
|
|
|
|
&& 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
|
|
|
|
context: NULL];
|
|
|
|
[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
|
|
|
|
context: nil];
|
|
|
|
}
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cancelAllOperations
|
|
|
|
{
|
2010-02-06 17:10:16 +00:00
|
|
|
NSUInteger index;
|
|
|
|
NSOperation *o;
|
2010-02-04 16:47:45 +00:00
|
|
|
|
2010-02-06 17:10:16 +00:00
|
|
|
[internal->cond lock];
|
|
|
|
while ((o = [internal->waiting lastObject]) != nil)
|
2010-02-04 16:47:45 +00:00
|
|
|
{
|
2010-02-06 17:10:16 +00:00
|
|
|
[o removeObserver: self
|
|
|
|
forKeyPath: @"isReady"];
|
2010-02-04 16:47:45 +00:00
|
|
|
[o cancel];
|
2010-02-06 17:10:16 +00:00
|
|
|
[internal->waiting removeLastObject];
|
|
|
|
}
|
|
|
|
[internal->cond unlockWithCondition: 0]; // Nothing waiting to execute
|
|
|
|
|
|
|
|
[internal->lock lock];
|
|
|
|
index = [internal->operations count];
|
|
|
|
while (index-- > 0)
|
|
|
|
{
|
|
|
|
NSOperation *o;
|
|
|
|
|
|
|
|
o = [internal->operations objectAtIndex: index];
|
|
|
|
if (NO == [o isCancelled])
|
|
|
|
{
|
|
|
|
[o removeObserver: self
|
|
|
|
forKeyPath: @"isReady"];
|
|
|
|
[o cancel];
|
|
|
|
}
|
2010-02-04 16:47:45 +00:00
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (_internal != nil)
|
|
|
|
{
|
|
|
|
[internal->operations release];
|
2010-02-06 17:10:16 +00:00
|
|
|
[internal->waiting release];
|
|
|
|
[internal->name release];
|
|
|
|
[internal->cond release];
|
2010-02-04 16:47:45 +00:00
|
|
|
[internal->lock release];
|
2009-07-21 09:40:48 +00:00
|
|
|
GS_DESTROY_INTERNAL(NSOperationQueue);
|
2009-07-16 15:56:31 +00:00
|
|
|
}
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
internal->count = NSOperationQueueDefaultMaxConcurrentOperationCount;
|
|
|
|
internal->operations = [NSMutableArray new];
|
2010-02-06 17:10:16 +00:00
|
|
|
internal->waiting = [NSMutableArray new];
|
|
|
|
internal->cond = [[NSConditionLock alloc] initWithCondition: 0];
|
2010-02-04 16:47:45 +00:00
|
|
|
internal->lock = [NSRecursiveLock new];
|
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
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
return internal->count;
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
2010-02-06 17:10:16 +00:00
|
|
|
- (NSString*) name
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
[internal->lock lock];
|
|
|
|
if (internal->name == nil)
|
|
|
|
{
|
|
|
|
internal->name
|
|
|
|
= [[NSString alloc] initWithFormat: @"NSOperation %p", self];
|
|
|
|
}
|
|
|
|
s = [internal->name retain];
|
|
|
|
[internal->lock unlock];
|
|
|
|
return [s autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|
|
|
|
format: @"[%@-%@] cannot set negative (%d) count",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd), cnt];
|
|
|
|
}
|
|
|
|
[internal->lock lock];
|
|
|
|
if (cnt != internal->count)
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"maxConcurrentOperationCount"];
|
|
|
|
internal->count = cnt;
|
|
|
|
[self didChangeValueForKey: @"maxConcurrentOperationCount"];
|
|
|
|
[self _update];
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setName: (NSString*)s
|
|
|
|
{
|
|
|
|
if (s == nil) s = @"";
|
|
|
|
[internal->lock lock];
|
|
|
|
if (NO == [internal->name isEqual: s])
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"name"];
|
|
|
|
[internal->name release];
|
|
|
|
internal->name = [s copy];
|
|
|
|
[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"];
|
|
|
|
[self _update];
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
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)
|
|
|
|
{
|
|
|
|
[op retain];
|
|
|
|
[internal->lock unlock];
|
|
|
|
[op waitUntilFinished];
|
|
|
|
[op release];
|
|
|
|
[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
|
|
|
|
{
|
|
|
|
[internal->cond lock];
|
|
|
|
if (YES == [object isReady])
|
|
|
|
{
|
|
|
|
if ([internal->waiting indexOfObjectIdenticalTo: object] == NSNotFound)
|
|
|
|
{
|
|
|
|
[internal->waiting addObject: object];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSUInteger index;
|
|
|
|
|
|
|
|
index = [internal->waiting indexOfObjectIdenticalTo: object];
|
|
|
|
if (index != NSNotFound)
|
|
|
|
{
|
|
|
|
[internal->waiting removeObjectAtIndex: index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ([internal->waiting count] > 0)
|
|
|
|
{
|
|
|
|
[internal->cond unlockWithCondition: 1];
|
|
|
|
[internal->lock lock];
|
|
|
|
[self _update];
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[internal->cond unlockWithCondition: 0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _thread
|
|
|
|
{
|
|
|
|
[internal->lock lock];
|
|
|
|
while ([internal->operations count] > 0)
|
|
|
|
{
|
|
|
|
NSOperation *op;
|
|
|
|
NSUInteger index;
|
|
|
|
|
|
|
|
/* Unlock the queue while we are waiting for another operation
|
|
|
|
* to perform.
|
|
|
|
*/
|
|
|
|
[internal->lock unlock];
|
|
|
|
|
|
|
|
/* Wait for an operation to become available.
|
|
|
|
*/
|
|
|
|
[internal->cond lockWhenCondition: 1];
|
|
|
|
[internal->waiting sortUsingFunction: sortFunc context: 0];
|
|
|
|
op = [[internal->waiting lastObject] retain];
|
|
|
|
[internal->waiting removeLastObject];
|
|
|
|
if ([internal->waiting count] == 0)
|
|
|
|
{
|
|
|
|
[internal->cond unlockWithCondition: 0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[internal->cond unlockWithCondition: 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the queue lock so we can track the idle count.
|
|
|
|
*/
|
|
|
|
[internal->lock lock];
|
|
|
|
if (YES == [op isReady])
|
|
|
|
{
|
|
|
|
if (NO == [op isCancelled])
|
|
|
|
{
|
|
|
|
internal->idle--;
|
|
|
|
|
|
|
|
/* Unlock the queue while the operation is executing.
|
|
|
|
*/
|
|
|
|
[internal->lock unlock];
|
|
|
|
[op start];
|
|
|
|
[op waitUntilFinished];
|
|
|
|
|
|
|
|
/* Lock the queue again to perform cleanup etc.
|
|
|
|
*/
|
|
|
|
[internal->lock lock];
|
|
|
|
internal->idle++;
|
|
|
|
}
|
|
|
|
[self willChangeValueForKey: @"operations"];
|
|
|
|
[self willChangeValueForKey: @"operationCount"];
|
|
|
|
[internal->operations removeObjectIdenticalTo: op];
|
|
|
|
[self didChangeValueForKey: @"operationCount"];
|
|
|
|
[self didChangeValueForKey: @"operations"];
|
|
|
|
}
|
|
|
|
[op release];
|
|
|
|
|
|
|
|
/* And now make sure we clean up any finished operations.
|
|
|
|
*/
|
|
|
|
index = [internal->operations count];
|
|
|
|
while (index-- > 0)
|
|
|
|
{
|
|
|
|
op = [internal->operations objectAtIndex: index];
|
|
|
|
if (YES == [op isFinished])
|
|
|
|
{
|
|
|
|
[self willChangeValueForKey: @"operations"];
|
|
|
|
[self willChangeValueForKey: @"operationCount"];
|
|
|
|
[internal->operations removeObjectAtIndex: index];
|
|
|
|
[self didChangeValueForKey: @"operationCount"];
|
|
|
|
[self didChangeValueForKey: @"operations"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
internal->idle--;
|
|
|
|
internal->threads--;
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NB. This must only be called from a locked section of code!
|
|
|
|
* It's just to check to see if a new thread needs to be started.
|
|
|
|
*/
|
|
|
|
- (void) _update
|
|
|
|
{
|
|
|
|
if (0 == internal->idle
|
|
|
|
&& NO == [self isSuspended]
|
|
|
|
&& [self maxConcurrentOperationCount] != 0
|
|
|
|
&& [internal->waiting count] > 0)
|
|
|
|
{
|
|
|
|
NSInteger count = internal->count;
|
|
|
|
|
|
|
|
if (count == NSOperationQueueDefaultMaxConcurrentOperationCount)
|
|
|
|
{
|
|
|
|
count = maxThreads; // Limit number of allowed threads
|
|
|
|
}
|
|
|
|
if (internal->threads < count)
|
|
|
|
{
|
|
|
|
/* All threads are in use, but we don't have the maximum
|
|
|
|
* number of threads, so we can create a new one for the
|
|
|
|
* waiting operation.
|
|
|
|
*/
|
|
|
|
internal->threads++;
|
|
|
|
internal->idle++;
|
|
|
|
[NSThread detachNewThreadSelector: @selector(_thread)
|
|
|
|
toTarget: self
|
|
|
|
withObject: nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|