-
Notifications
You must be signed in to change notification settings - Fork 9
/
MABaseFuture.m
79 lines (65 loc) · 1.34 KB
/
MABaseFuture.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#import "MABaseFuture.h"
#import <objc/runtime.h>
@implementation MABaseFuture
- (id)init
{
_lock = [[NSCondition alloc] init];
return self;
}
- (void)dealloc
{
[_value release];
[_lock release];
[super dealloc];
}
- (void)setFutureValue: (id)value
{
[_lock lock];
[self setFutureValueUnlocked: value];
[_lock unlock];
}
- (id)futureValue
{
// skip the usual retain/autorelease dance here
// because the setter is never called more than
// once, thus value lifetime is same as future
// lifetime
[_lock lock];
id value = _value;
[_lock unlock];
return value;
}
- (void)setFutureValueUnlocked: (id)value
{
[value retain];
[_value release];
_value = value;
_resolved = YES;
[_lock broadcast];
}
- (BOOL)futureHasResolved
{
return _resolved;
}
- (id)waitForFutureResolution
{
[_lock lock];
while(!_resolved)
[_lock wait];
[_lock unlock];
return _value;
}
- (id)resolveFuture
{
NSAssert1(NO, @"-[MABaseFuture resolveFuture] called, this should never happen! Did you forget to implement -[%@ resolveFuture]?", NSStringFromClass(isa));
__builtin_unreachable();
}
- (Class)class
{
Class c = [[self resolveFuture] class];
if([NSStringFromClass(c) hasPrefix: @"NSCF"])
return [c superclass];
else
return c;
}
@end