home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / ThreadSafeQueue.m < prev    next >
Encoding:
Text File  |  1996-04-12  |  1.3 KB  |  70 lines

  1. /* ThreadSafeQueue.m created by blaine on Mon 01-Apr-1996 */
  2.  
  3. #import "ThreadSafeQueue.h"
  4.  
  5. @implementation ThreadSafeQueue
  6.  
  7. + instance {
  8.     ThreadSafeQueue *result = [self alloc];
  9.     result->lock = [NSLock new];
  10.     result->array = [NSMutableArray new];
  11.     return [result autorelease];
  12. }
  13.  
  14. - (void)dealloc {
  15.     [lock release];
  16.     [array release];
  17. }
  18.  
  19. - headItem {
  20.    id result;
  21.    [lock lock];
  22.    if ([array count] == 0)
  23.        result = nil;
  24.    else {
  25.        result = [array objectAtIndex:0];
  26.        // hold onto it in case the queue is the only retainer
  27.        [[result retain] autorelease];
  28.        [array removeObjectAtIndex:0];
  29.    }
  30.    [lock unlock];
  31.    return result;
  32. }
  33.  
  34. - tailItem {
  35.    id result;
  36.    [lock lock];
  37.    if ([array count] == 0)
  38.        result = nil;
  39.    else {
  40.        result = [array lastObject];
  41.        // hold onto it in case the queue is the only retainer
  42.        [[result retain] autorelease];
  43.        [array removeLastObject];
  44.    }
  45.    [lock unlock];
  46.    return result;
  47. }
  48.  
  49. - (void)insertItem:obj {
  50.    [lock lock];
  51.    [array insertObject:obj atIndex:0];
  52.    [lock unlock];
  53. }
  54.  
  55. - (void)appendItem:obj {
  56.    [lock lock];
  57.    [array addObject:obj];
  58.    [lock unlock];
  59. }
  60.  
  61. - (unsigned)count {
  62.     return [array count];
  63. }
  64.  
  65. - (NSArray *)snapshot {
  66.    return [[array copy] autorelease];
  67. }
  68.  
  69. @end
  70.