From b70e19961c4791fd3c416eceb9066414b9096869 Mon Sep 17 00:00:00 2001 From: galad87 Date: Sat, 29 Aug 2015 11:40:37 +0200 Subject: [PATCH] Remove the useless init method from MP42Fifo interface and be safer in the implementation --- MP42/MP42Fifo.h | 3 +-- MP42/MP42Fifo.m | 14 +++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/MP42/MP42Fifo.h b/MP42/MP42Fifo.h index ac568c7..bffd2c6 100644 --- a/MP42/MP42Fifo.h +++ b/MP42/MP42Fifo.h @@ -26,8 +26,7 @@ NS_ASSUME_NONNULL_BEGIN dispatch_semaphore_t _empty; } -- (instancetype)init; -- (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithCapacity:(NSUInteger)capacity NS_DESIGNATED_INITIALIZER; - (void)enqueue:(ObjectType)item; - (nullable ObjectType)deque NS_RETURNS_RETAINED; diff --git a/MP42/MP42Fifo.m b/MP42/MP42Fifo.m index a26b7a0..26a475d 100644 --- a/MP42/MP42Fifo.m +++ b/MP42/MP42Fifo.m @@ -15,10 +15,10 @@ return self; } -- (instancetype)initWithCapacity:(NSUInteger)numItems { +- (instancetype)initWithCapacity:(NSUInteger)capacity { self = [super init]; if (self) { - _size = (int32_t)numItems; + _size = (int32_t)capacity; _array = (id *) malloc(sizeof(id) * _size); _full = dispatch_semaphore_create(_size - 1); _empty = dispatch_semaphore_create(0); @@ -40,11 +40,11 @@ _tail = 0; } - OSAtomicIncrement32(&_count); + OSAtomicIncrement32Barrier(&_count); dispatch_semaphore_signal(_empty); } -- (id)deque { +- (nullable id)deque { if (!_count) return nil; id item = _array[_head++]; @@ -53,17 +53,17 @@ _head = 0; } - OSAtomicDecrement32(&_count); + OSAtomicDecrement32Barrier(&_count); dispatch_semaphore_signal(_full); return item; } -- (id)dequeAndWait { +- (nullable id)dequeAndWait { id item = [self deque]; if (!item) { - dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC/1000); + dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 1000); dispatch_semaphore_wait(_empty, time); }