pEpObjCAdapter/PEPQueue.m
author Dirk Zimmermann <dz@pep.security>
Fri, 20 Sep 2019 10:01:48 +0200
branchIOSAD-141
changeset 1250 b6a4d30abd26
parent 785 3c37225acd2b
child 1253 0532de0ff84a
permissions -rw-r--r--
IOSAD-141 Test for "hammering" the engine.
     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     [_cond lock];
    37     
    38     if (_queue) {
    39         [_queue insertObject:object atIndex:0];
    40     }
    41     
    42     [_cond signal];
    43     
    44     [_cond unlock];
    45     
    46 }
    47 
    48 - (id)timedDequeue:(time_t*)timeout
    49 {
    50     id tmp = nil;
    51     
    52     [_cond lock];
    53     
    54     if (_queue && _queue.count == 0)
    55     {
    56         if (*timeout == 0)
    57         {
    58             [_cond wait];
    59         }
    60         else
    61         {
    62             NSDate *end = [NSDate dateWithTimeIntervalSinceNow: *timeout];
    63             
    64             [_cond waitUntilDate:end];
    65             
    66             NSTimeInterval remaining = [end timeIntervalSinceNow];
    67             
    68             if (remaining > 0)
    69                 *timeout = remaining;
    70             else
    71                 *timeout = 0;
    72         }
    73     }
    74     
    75     if (_queue)
    76     {
    77         tmp = [_queue lastObject];
    78         
    79         [_queue removeLastObject];
    80     }
    81 
    82     [_cond unlock];
    83 
    84     return tmp;
    85 }
    86 
    87 - (id)dequeue
    88 {
    89     time_t zeroTimeout = 0;
    90     return [self timedDequeue:&zeroTimeout];
    91 }
    92 
    93 - (void)kill
    94 {
    95     [_cond lock];
    96 
    97     _queue = nil;
    98     
    99     [_cond signal];
   100     [_cond unlock];
   101 
   102 }
   103 
   104 - (void)purge:(deleteOp)del
   105 {
   106     [_cond lock];
   107 
   108     id item;
   109     for (item in _queue)
   110     {
   111         del(item);
   112     }
   113     _queue = nil;
   114     
   115     [_cond signal];
   116     [_cond unlock];
   117 }
   118 
   119 
   120 - (void)dealloc
   121 {
   122     self.queue = nil;
   123     self.cond = nil;
   124 }
   125 
   126 @end