// // FakeOperationQueue.m // Aurora2D // // Created by Joachim Bengtsson on 2008-01-28. // Copyright 2008 Joachim Bengtsson. All rights reserved. // #import "FakeOperationQueue.h" #import #define DataAvailable 1 #define QueueEmpty 0 #define ShutdownComplete 1 @implementation FakeOperationQueue -init; { if( ! [super init] ) return nil; fakeQueue = [NSMutableArray new]; workLock = [[NSConditionLock alloc] initWithCondition:QueueEmpty]; [self start]; return self; } -(void)dealloc; { [self stop]; [fakeQueue release]; [workLock release]; [super dealloc]; } -(void)finalize { [self stop]; [super finalize]; } - (void)addOperation:(NSOperation *)op; { [workLock lock]; [fakeQueue addObject:op]; [workLock unlockWithCondition:DataAvailable]; } - (void)setMaxConcurrentOperationCount:(NSInteger)cnt; {} -(void)setSuspended:(BOOL)willSuspend; { if(willSuspend) [self stop]; else [self start]; } -(BOOL)isSuspended; { return isOn; } -(void)start; { isOn = YES; [NSThread detachNewThreadSelector:@selector(queueMain) toTarget:self withObject:nil]; return; } -(void)stop; { if(!isOn) return; isOn = NO; while(isOn != ShutdownComplete) {} isOn = NO; } -(void)cancelAllOperations; { [workLock lock]; [fakeQueue removeAllObjects]; [workLock unlock]; } -(void)queueMain; { while(isOn) { NSAutoreleasePool *loopPool = [NSAutoreleasePool new]; [workLock lockWhenCondition:DataAvailable]; NSArray *ops = [fakeQueue copy]; [fakeQueue removeAllObjects]; [workLock unlockWithCondition:QueueEmpty]; [CATransaction begin]; for (NSOperation *op in ops) { [op main]; } [CATransaction commit]; [loopPool release]; } isOn = ShutdownComplete; } @end