pEpObjCAdapter/PEPQueue.m
author Dirk Zimmermann <dirk@pep-project.org>
Wed, 17 May 2017 09:27:57 +0200
changeset 187 1f630a05e444
child 193 0d26e0a12324
permissions -rw-r--r--
IOSAD-15 more renames
     1 //
     2 //  PEPQueue.m
     3 //  pEpiOSAdapter
     4 //
     5 //  Created by Volker Birk on 15.07.15.
     6 //  Copyright (c) 2015 p≡p. All rights reserved.
     7 //
     8 
     9 #import "PEPQueue.h"
    10 
    11 
    12 @interface PEPQueue ()
    13 
    14 @property (nonatomic, strong) NSMutableArray *queue;
    15 @property (nonatomic, strong) NSCondition *cond;
    16 
    17 @end
    18 
    19 @implementation PEPQueue
    20 
    21 - (id)init
    22 {
    23     self = [super init];
    24     
    25     if (self)
    26     {
    27         self.queue = [[NSMutableArray alloc] init];
    28         self.cond = [[NSCondition alloc] init];
    29     }
    30     
    31     return self;
    32 }
    33 
    34 - (void)enqueue:(id)object
    35 {
    36 
    37     @synchronized(self) {
    38         if (_queue)
    39             [_queue insertObject:object atIndex:0];
    40     }
    41     
    42     [_cond signal];
    43     
    44 }
    45 
    46 - (BOOL)condwait
    47 {
    48     BOOL res;
    49     @synchronized(self) {
    50         res = _queue && _queue.count == 0;
    51     }
    52     return res;
    53 }
    54 
    55 - (id)dequeue
    56 {
    57     id tmp = nil;
    58     
    59     [_cond lock];
    60     
    61     while ([self condwait])
    62     {
    63         [_cond wait];
    64     }
    65     
    66     @synchronized(self) {
    67         if (_queue)
    68         {
    69             tmp = [_queue lastObject];
    70             
    71             [_queue removeLastObject];
    72         }
    73     }
    74     [_cond unlock];
    75     
    76     return tmp;
    77 }
    78 
    79 - (void)kill
    80 {
    81     _queue = nil;
    82     
    83     [_cond signal];
    84 }
    85 
    86 - (NSUInteger)count
    87 {
    88     return [_queue count];
    89 }
    90 
    91 - (void)dealloc
    92 {
    93     self.queue = nil;
    94     self.cond = nil;
    95 }
    96 
    97 @end