//
|
// GCDSemaphore.m
|
// GCD
|
// 用法
|
// http://www.cnblogs.com/YouXianMing/p/3659204.html
|
//
|
// Created by XianMingYou on 15/3/15.
|
// Copyright (c) 2015年 XianMingYou. All rights reserved.
|
//
|
|
#import "GCDSemaphore.h"
|
|
@interface GCDSemaphore ()
|
|
@property (strong, readwrite, nonatomic) dispatch_semaphore_t dispatchSemaphore;
|
|
@end
|
|
@implementation GCDSemaphore
|
|
- (instancetype)init {
|
|
self = [super init];
|
|
if (self) {
|
|
self.dispatchSemaphore = dispatch_semaphore_create(0);
|
}
|
|
return self;
|
}
|
|
- (instancetype)initWithValue:(long)value {
|
|
self = [super init];
|
|
if (self) {
|
|
self.dispatchSemaphore = dispatch_semaphore_create(value);
|
}
|
|
return self;
|
}
|
|
- (BOOL)signal {
|
|
return dispatch_semaphore_signal(self.dispatchSemaphore) != 0;
|
}
|
|
- (void)wait {
|
|
dispatch_semaphore_wait(self.dispatchSemaphore, DISPATCH_TIME_FOREVER);
|
}
|
|
- (BOOL)wait:(int64_t)delta {
|
|
return dispatch_semaphore_wait(self.dispatchSemaphore, dispatch_time(DISPATCH_TIME_NOW, delta)) == 0;
|
}
|
|
@end
|