2009-07-17 05:13:52 +00:00
|
|
|
/**Implementation for NSOperation for GNUStep
|
2009-07-13 18:14:42 +00:00
|
|
|
Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Gregory Casamento <greg.casamento@gmail.com>
|
|
|
|
Date: 2009
|
|
|
|
|
|
|
|
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"
|
|
|
|
#import "Foundation/NSEnumerator.h"
|
|
|
|
#import "Foundation/NSException.h"
|
2009-07-13 18:14:42 +00:00
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
@interface NSOperationInternal : NSObject
|
|
|
|
{
|
|
|
|
@public
|
|
|
|
NSOperationQueuePriority priority;
|
|
|
|
BOOL cancelled;
|
|
|
|
BOOL concurrent;
|
|
|
|
BOOL executing;
|
|
|
|
BOOL finished;
|
|
|
|
BOOL ready;
|
|
|
|
NSMutableArray *dependencies;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSOperationInternal
|
|
|
|
/* As long as this class does nothing but act as a container for ivars,
|
|
|
|
* we can easily remove it at a later date using a global substitution
|
|
|
|
* to replace all the code of the form 'internal->ivar' with 'ivar'.
|
|
|
|
*/
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2009-07-13 18:14:42 +00:00
|
|
|
@implementation NSOperation : NSObject
|
2009-07-16 15:56:31 +00:00
|
|
|
|
|
|
|
#define internal ((NSOperationInternal*)_internal)
|
|
|
|
|
|
|
|
- (void) dealloc
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
if (_internal != nil)
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
RELEASE(internal->dependencies);
|
|
|
|
[_internal release];
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
2009-07-16 15:56:31 +00:00
|
|
|
[super dealloc];
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
- (id) init
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_internal = [NSOperationInternal new];
|
|
|
|
internal->priority = NSOperationQueuePriorityNormal;
|
|
|
|
internal->dependencies = [[NSMutableArray alloc] initWithCapacity: 5];
|
|
|
|
}
|
|
|
|
return self;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
|
2009-07-13 18:14:42 +00:00
|
|
|
// Executing the operation
|
|
|
|
- (void) start
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
internal->executing = YES;
|
|
|
|
internal->finished = NO;
|
2009-07-13 18:14:42 +00:00
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
if ([self isConcurrent])
|
2009-07-13 18:14:42 +00:00
|
|
|
{
|
|
|
|
[self main];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[self main];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSLog(@"%@",[localException reason]);
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
|
|
|
}
|
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
internal->executing = NO;
|
|
|
|
internal->finished = YES;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) main;
|
|
|
|
{
|
|
|
|
// subclass responsibility...
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancelling the operation
|
|
|
|
- (void) cancel
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getting the operation status
|
|
|
|
- (BOOL) isCancelled
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isExecuting
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isFinished
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isConcurrent
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isReady
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Managing dependencies
|
|
|
|
- (void) addDependency: (NSOperation *)op
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
[internal->dependencies addObject: op];
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeDependency: (NSOperation *)op
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
[internal->dependencies removeObject: op];
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)dependencies
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
return [NSArray arrayWithArray: internal->dependencies];
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prioritization
|
|
|
|
- (NSOperationQueuePriority) queuePriority
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
return internal->priority;
|
2009-07-13 18:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setQueuePriority: (NSOperationQueuePriority)pri
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
internal->priority = pri;
|
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
|
|
|
|
|
|
|
@interface NSOperationQueueInternal : NSObject
|
|
|
|
{
|
|
|
|
@public
|
|
|
|
NSMutableArray *operations;
|
|
|
|
BOOL suspended;
|
|
|
|
NSInteger count;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSOperationQueueInternal : NSObject
|
|
|
|
/* As long as this class does nothing but act as a container for ivars,
|
|
|
|
* we can easily remove it at a later date using a global substitution
|
|
|
|
* to replace all the code of the form 'internal->ivar' with 'ivar'.
|
|
|
|
*/
|
|
|
|
@end
|
|
|
|
|
2009-07-15 00:02:34 +00:00
|
|
|
@implementation NSOperationQueue
|
|
|
|
|
2009-07-16 15:56:31 +00:00
|
|
|
#undef internal
|
|
|
|
#define internal ((NSOperationQueueInternal*)_internal)
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (_internal != nil)
|
|
|
|
{
|
|
|
|
[internal->operations release];
|
|
|
|
[_internal release];
|
|
|
|
}
|
|
|
|
[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-16 15:56:31 +00:00
|
|
|
_internal = [NSOperationQueueInternal new];
|
|
|
|
internal->suspended = NO;
|
|
|
|
internal->count = NSOperationQueueDefaultMaxConcurrentOperationCount;
|
|
|
|
internal->operations = [NSMutableArray new];
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
// status
|
|
|
|
- (BOOL) isSuspended
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
return internal->suspended;
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setSuspended: (BOOL)flag
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
internal->suspended = flag;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setMaxConcurrentOperationCount: (NSInteger)cnt
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
internal->count = cnt;
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// operations
|
|
|
|
- (void) addOperation: (NSOperation *) op
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
[internal->operations addObject: op];
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) operations
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
return [NSArray arrayWithArray: internal->operations];
|
2009-07-15 00:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cancelAllOperations
|
|
|
|
{
|
2009-07-16 15:56:31 +00:00
|
|
|
NSEnumerator *en = [internal->operations objectEnumerator];
|
2009-07-15 00:02:34 +00:00
|
|
|
id o = nil;
|
2009-07-16 15:56:31 +00:00
|
|
|
|
|
|
|
while ((o = [en nextObject]) != nil )
|
2009-07-15 00:02:34 +00:00
|
|
|
{
|
|
|
|
[o cancel];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) waitUntilAllOperationsAreFinished
|
|
|
|
{
|
|
|
|
// not yet implemented...
|
|
|
|
}
|
|
|
|
@end
|