Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // VFReverseQueue.m
- // Berlingske
- //
- // Created by Farcaller on 09.11.10.
- // Copyright 2010 Ciklum. All rights reserved.
- //
- #import "VFReverseQueue.h"
- @interface VFReverseQueue ()
- @property (readwrite, retain) NSMutableArray *operations;
- @property (readwrite, retain) NSMutableArray *threads;
- - (void)dequeueNext;
- @end
- @implementation VFReverseQueue
- @synthesize operations, threads;
- - (id)initWithTreadsCount:(int)aThreadsCount
- {
- if( (self = [super init]) ) {
- operations = [[NSMutableArray alloc] init];
- threads = [[NSMutableArray alloc] initWithCapacity:aThreadsCount];
- threadsCount = aThreadsCount;
- }
- return self;
- }
- - (void)dealloc
- {
- [operations release];
- [threads release];
- [super dealloc];
- }
- - (void)addOperation:(NSOperation *)operation
- {
- [self.operations insertObject:operation atIndex:0];
- [self dequeueNext];
- }
- - (void)dequeueNext
- {
- @synchronized(self) {
- if([self.operations count]) {
- if([self.threads count] >= threadsCount) {
- NSLog(@"RQ delayed");
- return;
- }
- NSOperation *op = [[[self.operations objectAtIndex:0] retain] autorelease];
- [self.operations removeObjectAtIndex:0];
- NSThread *t = [[[NSThread alloc] initWithTarget:self selector:@selector(runThread:) object:op] autorelease];
- [self.threads addObject:t];
- [t start];
- } else {
- NSLog(@"RQ operations drained");
- }
- }
- }
- - (void)runThread:(NSOperation *)op
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSLog(@"RQ for operation %@, %d operations pending", op, [self.operations count]);
- [op main];
- [self.threads removeObject:[NSThread currentThread]];
- [self dequeueNext];
- [pool release];
- }
- @end
Add Comment
Please, Sign In to add comment