Last active
December 24, 2015 21:59
-
-
Save odrobnik/6869306 to your computer and use it in GitHub Desktop.
Using NSConditionLock - is this correct?
This file contains hidden or 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
@implementation DTDrawingLock | |
{ | |
NSUInteger _numberActiveOperations; | |
NSConditionLock *_lock; | |
} | |
- (instancetype)init | |
{ | |
self = [super init]; | |
if (self) | |
{ | |
_lock = [[NSConditionLock alloc] initWithCondition:0]; | |
} | |
return self; | |
} | |
- (void)pushDrawingOperation | |
{ | |
[_lock lock]; | |
_numberActiveOperations++; | |
[_lock unlockWithCondition:_numberActiveOperations]; | |
} | |
- (void)popDrawingOperations | |
{ | |
[_lock lock]; | |
_numberActiveOperations--; | |
[_lock unlockWithCondition:_numberActiveOperations]; | |
} | |
- (void)performLockProtectedOperation:(void(^)(void))operation | |
{ | |
[_lock lockWhenCondition:0]; | |
operation(); | |
[_lock unlockWithCondition:0]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment