Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ble/ObjC): add countdownlatch impl in ObjC
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// CountDownLatch.h | ||
// ble | ||
// | ||
// Created by sacha on 22/11/2018. | ||
// Copyright © 2018 berty. All rights reserved. | ||
// | ||
|
||
|
||
|
||
#ifndef CountDownLatch_h | ||
#define CountDownLatch_h | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface CountDownLatch : NSObject | ||
|
||
@property (nonatomic, assign) int count; | ||
@property (atomic, readwrite, strong) dispatch_semaphore_t semaphore; | ||
@property (nonatomic, strong) dispatch_queue_t dispatch_queue; | ||
|
||
- (instancetype)init:(int)count; | ||
- (void)coundDown; | ||
- (void)await; | ||
|
||
@end | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// CountDownLatch.m | ||
// ble | ||
// | ||
// Created by sacha on 22/11/2018. | ||
// Copyright © 2018 berty. All rights reserved. | ||
// | ||
|
||
#import "CountDownLatch.h" | ||
|
||
@implementation CountDownLatch | ||
|
||
- (instancetype)init:(int)count { | ||
if (count < 0) { | ||
return nil; | ||
} | ||
|
||
self.count = count; | ||
self.semaphore = dispatch_semaphore_create(0); | ||
self.dispatch_queue = dispatch_queue_create("CountDownLatchQueue", DISPATCH_QUEUE_CONCURRENT); | ||
[super self]; | ||
return self; | ||
} | ||
|
||
- (void)coundDown { | ||
dispatch_async(self.dispatch_queue, ^{ | ||
self.count--; | ||
if (self.count == 0) { | ||
while (dispatch_semaphore_signal(self.semaphore) != 0) {}; | ||
} | ||
}); | ||
} | ||
|
||
- (void)await { | ||
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER); | ||
} | ||
|
||
@end |