IOSAD-157 Share locking code IOSAD-157
authorDirk Zimmermann <dz@pep.security>
Mon, 18 Nov 2019 15:54:57 +0100
branchIOSAD-157
changeset 13367b362fde775a
parent 1335 708d2520d5ba
child 1337 fe92ccdcec25
IOSAD-157 Share locking code
pEpObjCAdapter/PEPQueue.m
     1.1 --- a/pEpObjCAdapter/PEPQueue.m	Mon Nov 18 15:54:57 2019 +0100
     1.2 +++ b/pEpObjCAdapter/PEPQueue.m	Mon Nov 18 15:54:57 2019 +0100
     1.3 @@ -31,28 +31,35 @@
     1.4      return self;
     1.5  }
     1.6  
     1.7 -- (void)enqueue:(id)object
     1.8 +/// A block that gets called to modify the queue model.
     1.9 +typedef void (^queueOp)(NSMutableArray *queue);
    1.10 +
    1.11 +/// Lock the queue and calls the given block.
    1.12 +/// @param block The block to invoke once the queue is locked.
    1.13 +- (void)lockQueueAndUpdateWithBlock:(queueOp)block
    1.14  {
    1.15      [_cond lock];
    1.16 -    
    1.17 +
    1.18      if (_queue) {
    1.19 -        [_queue insertObject:object atIndex:0];
    1.20 +        block(_queue);
    1.21      }
    1.22 -    
    1.23 +
    1.24      [_cond signal];
    1.25      [_cond unlock];
    1.26  }
    1.27  
    1.28 +- (void)enqueue:(id)object
    1.29 +{
    1.30 +    [self lockQueueAndUpdateWithBlock:^(NSMutableArray *queue){
    1.31 +        [queue insertObject:object atIndex:0];
    1.32 +    }];
    1.33 +}
    1.34 +
    1.35  - (void)prequeue:(id)object
    1.36  {
    1.37 -    [_cond lock];
    1.38 -
    1.39 -    if (_queue) {
    1.40 -        [_queue addObject:object];
    1.41 -    }
    1.42 -
    1.43 -    [_cond signal];
    1.44 -    [_cond unlock];
    1.45 +    [self lockQueueAndUpdateWithBlock:^(NSMutableArray *queue){
    1.46 +        [queue addObject:object];
    1.47 +    }];
    1.48  }
    1.49  
    1.50  - (id)timedDequeue:(time_t*)timeout