Skip to content

Commit

Permalink
feat(ble/ObjC): add countdownlatch impl in ObjC
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Nov 29, 2018
1 parent c2576ff commit 8e0c07b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions core/network/ble/CountDownLatch.h
@@ -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
38 changes: 38 additions & 0 deletions core/network/ble/CountDownLatch.m
@@ -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

0 comments on commit 8e0c07b

Please sign in to comment.