Guest User

Untitled

a guest
Jun 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. //
  2. // VFReverseQueue.m
  3. // Berlingske
  4. //
  5. // Created by Farcaller on 09.11.10.
  6. // Copyright 2010 Ciklum. All rights reserved.
  7. //
  8.  
  9. #import "VFReverseQueue.h"
  10.  
  11. @interface VFReverseQueue ()
  12.  
  13. @property (readwrite, retain) NSMutableArray *operations;
  14. @property (readwrite, retain) NSMutableArray *threads;
  15.  
  16. - (void)dequeueNext;
  17.  
  18. @end
  19.  
  20. @implementation VFReverseQueue
  21.  
  22. @synthesize operations, threads;
  23.  
  24. - (id)initWithTreadsCount:(int)aThreadsCount
  25. {
  26. if( (self = [super init]) ) {
  27. operations = [[NSMutableArray alloc] init];
  28. threads = [[NSMutableArray alloc] initWithCapacity:aThreadsCount];
  29. threadsCount = aThreadsCount;
  30. }
  31. return self;
  32. }
  33.  
  34. - (void)dealloc
  35. {
  36. [operations release];
  37. [threads release];
  38. [super dealloc];
  39. }
  40.  
  41. - (void)addOperation:(NSOperation *)operation
  42. {
  43. [self.operations insertObject:operation atIndex:0];
  44. [self dequeueNext];
  45. }
  46.  
  47. - (void)dequeueNext
  48. {
  49. @synchronized(self) {
  50. if([self.operations count]) {
  51. if([self.threads count] >= threadsCount) {
  52. NSLog(@"RQ delayed");
  53. return;
  54. }
  55. NSOperation *op = [[[self.operations objectAtIndex:0] retain] autorelease];
  56. [self.operations removeObjectAtIndex:0];
  57.  
  58. NSThread *t = [[[NSThread alloc] initWithTarget:self selector:@selector(runThread:) object:op] autorelease];
  59. [self.threads addObject:t];
  60. [t start];
  61. } else {
  62. NSLog(@"RQ operations drained");
  63. }
  64. }
  65. }
  66.  
  67. - (void)runThread:(NSOperation *)op
  68. {
  69. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  70.  
  71. NSLog(@"RQ for operation %@, %d operations pending", op, [self.operations count]);
  72. [op main];
  73.  
  74. [self.threads removeObject:[NSThread currentThread]];
  75. [self dequeueNext];
  76.  
  77. [pool release];
  78. }
  79.  
  80. @end
Add Comment
Please, Sign In to add comment