Remove the useless init method from MP42Fifo interface and be safer in the implementation
This commit is contained in:
1 parent
fa39cf9f4b
commit
b70e19961c
2 files changed
+8
-9
No files matched your search
+1
-2
@@ -26,8 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
dispatch_semaphore_t _empty;
|
dispatch_semaphore_t _empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (instancetype)init;
|
- (instancetype)initWithCapacity:(NSUInteger)capacity NS_DESIGNATED_INITIALIZER;
|
||||||
- (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER;
|
|
||||||
|
|
||||||
- (void)enqueue:(ObjectType)item;
|
- (void)enqueue:(ObjectType)item;
|
||||||
- (nullable ObjectType)deque NS_RETURNS_RETAINED;
|
- (nullable ObjectType)deque NS_RETURNS_RETAINED;
|
||||||
|
|||||||
+7
-7
@@ -15,10 +15,10 @@
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (instancetype)initWithCapacity:(NSUInteger)numItems {
|
- (instancetype)initWithCapacity:(NSUInteger)capacity {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
if (self) {
|
||||||
_size = (int32_t)numItems;
|
_size = (int32_t)capacity;
|
||||||
_array = (id *) malloc(sizeof(id) * _size);
|
_array = (id *) malloc(sizeof(id) * _size);
|
||||||
_full = dispatch_semaphore_create(_size - 1);
|
_full = dispatch_semaphore_create(_size - 1);
|
||||||
_empty = dispatch_semaphore_create(0);
|
_empty = dispatch_semaphore_create(0);
|
||||||
@@ -40,11 +40,11 @@
|
|||||||
_tail = 0;
|
_tail = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
OSAtomicIncrement32(&_count);
|
OSAtomicIncrement32Barrier(&_count);
|
||||||
dispatch_semaphore_signal(_empty);
|
dispatch_semaphore_signal(_empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)deque {
|
- (nullable id)deque {
|
||||||
if (!_count) return nil;
|
if (!_count) return nil;
|
||||||
|
|
||||||
id item = _array[_head++];
|
id item = _array[_head++];
|
||||||
@@ -53,17 +53,17 @@
|
|||||||
_head = 0;
|
_head = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
OSAtomicDecrement32(&_count);
|
OSAtomicDecrement32Barrier(&_count);
|
||||||
dispatch_semaphore_signal(_full);
|
dispatch_semaphore_signal(_full);
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)dequeAndWait {
|
- (nullable id)dequeAndWait {
|
||||||
id item = [self deque];
|
id item = [self deque];
|
||||||
|
|
||||||
if (!item) {
|
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);
|
dispatch_semaphore_wait(_empty, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user