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
|
|
|
|
{
|
|
|
|
if (NO == [internal->dependencies containsObject: 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
|
|
|
|
{
|
|
|
|
if (YES == [internal->dependencies containsObject: op])
|
|
|
|
{
|
|
|
|
[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);
|
|
|
|
NSException *e = nil;
|
|
|
|
|
|
|
|
[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"];
|
|
|
|
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
if (NO == [self isCancelled])
|
|
|
|
{
|
|
|
|
double prio = [NSThread threadPriority];
|
|
|
|
|
|
|
|
[NSThread setThreadPriority: internal->threadPriority];
|
|
|
|
[self main];
|
|
|
|
[NSThread setThreadPriority: prio];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
e = localException;
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
{
|
|
|
|
if (e == nil)
|
|
|
|
{
|
|
|
|
e = localException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
[internal->lock unlock];
|
|
|
|
if (e != nil)
|
|
|
|
{
|
|
|
|
[e raise];
|
|
|
|
}
|
|
|
|
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;
|
2009-07-16 15:56:31 +00:00
|
|
|
NSMutableArray *operations;
|
|
|
|
BOOL suspended;
|
|
|
|
NSInteger count;
|
2009-07-21 09:40:48 +00:00
|
|
|
GS_END_INTERNAL(NSOperationQueue)
|
2009-07-16 15:56:31 +00:00
|
|
|
|
|
|
|
|
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];
|
|
|
|
if (NO == [internal->operations containsObject: op])
|
|
|
|
{
|
|
|
|
[internal->operations addObject: op];
|
|
|
|
// FIXME ... start if we can
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cancelAllOperations
|
|
|
|
{
|
|
|
|
NSEnumerator *en;
|
|
|
|
id o;
|
|
|
|
|
|
|
|
[internal->lock lock];
|
|
|
|
en = [internal->operations objectEnumerator];
|
|
|
|
while ((o = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[o cancel];
|
|
|
|
}
|
|
|
|
[internal->lock unlock];
|
|
|
|
}
|
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (_internal != nil)
|
|
|
|
{
|
|
|
|
[internal->operations 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-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-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-04 16:47:45 +00:00
|
|
|
internal->count = cnt;
|
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-04 16:47:45 +00:00
|
|
|
internal->suspended = flag;
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) waitUntilAllOperationsAreFinished
|
|
|
|
{
|
|
|
|
// not yet implemented...
|
|
|
|
}
|
|
|
|
@end
|