forked from jerrykrinock/ClassesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSYBlocker.m
55 lines (41 loc) · 1.1 KB
/
SSYBlocker.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#import "SSYBlocker.h"
#define SSY_BLOCKER_BLOCKED 0
#define SSY_BLOCKER_CLEARED 1
@interface SSYBlocker ()
@property (retain) NSConditionLock* lock ;
@end
@implementation SSYBlocker
@synthesize lock = m_lock ;
- (void)dealloc {
[m_lock release] ;
[super dealloc] ;
}
- (id)init {
self = [super init] ;
if (self) {
NSConditionLock* lock = [[NSConditionLock alloc] initWithCondition:SSY_BLOCKER_BLOCKED] ;
[self setLock:lock] ;
[lock release] ;
}
return self ;
}
- (void)lockLock {
[[self lock] lock] ;
}
- (void)blockForLock {
NSConditionLock* lock = [self lock] ;
BOOL workFinishedInTime = [lock lockWhenCondition:SSY_BLOCKER_CLEARED
beforeDate:[NSDate distantFuture]] ;
if (workFinishedInTime) {
[lock unlock] ;
}
}
- (void)unlockLock {
// If we send -unlockWithCondition to a condition lock which has already
// been unlocked, Cocoa raises an exception. To avoid that, we check
// that -unlockLock has not already been sent for this locking cycle.
if ([[self lock] condition] == SSY_BLOCKER_BLOCKED) {
[[self lock] unlockWithCondition:SSY_BLOCKER_CLEARED] ;
}
}
@end