/
MPWaitTask.m
148 lines (116 loc) · 3.28 KB
/
MPWaitTask.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// MPWaitTask.m
// LentiLens
//
// Created by Mark Probst on 2/17/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "MPWaitTask.h"
@implementation MPWaitTask
- (id) init
{
self = [super init];
if (self != nil) {
condition = [[NSCondition alloc] init];
waiting = NO;
}
return self;
}
- (void) releaseBlocks
{
[completionBlock release];
completionBlock = nil;
[failureBlock release];
failureBlock = nil;
}
- (void) dealloc
{
[condition release];
[result release];
[error release];
[super dealloc];
}
+ (MPWaitTask*) waitTask
{
return [[[MPWaitTask alloc] init] autorelease];
}
- (BOOL) haveBlocks
{
return completionBlock != nil || failureBlock != nil;
}
- (void) completeWithResult: (id) _result
{
NSAssert (_result != nil, @"Result must not be nil");
[condition lock];
NSAssert (result == nil && error == nil, @"Complete twice or complete after fail");
if ([self haveBlocks]) {
// FIXME: unlock or try/finally
if (completionBlock != nil)
completionBlock (self, _result);
[self releaseBlocks];
} else {
result = [_result retain];
if (waiting)
[condition signal];
}
[condition unlock];
}
- (void) failWithError: (NSError*) _error
{
NSAssert (_error != nil, @"Error must not be nil");
[condition lock];
NSAssert (result == nil && error == nil, @"Fail twice or fail after complete");
if ([self haveBlocks]) {
// FIXME: unlock or try/finally
if (failureBlock != nil)
failureBlock (self, _error);
[self releaseBlocks];
} else {
error = [_error retain];
if (waiting)
[condition signal];
}
[condition unlock];
}
- (id) runSynchronouslyWithError: (NSError**) _error
{
[condition lock];
NSAssert (!waiting, @"Must not wait twice");
NSAssert (completionBlock == nil && failureBlock == nil, @"Must not run asynchronously and synchronously");
waiting = YES;
while (result == nil && error == nil)
[condition wait];
[condition unlock];
if (_error != NULL)
*_error = error;
if (result != nil)
return result;
return nil;
}
- (void) runAsynchronouslyWithCompletionBlock: (void (^)(NSObject<MPTask> *, id)) _completionBlock
failureBlock: (void (^)(NSObject<MPTask> *, NSError *)) _failureBlock
{
NSAssert (_completionBlock != nil || _failureBlock != nil, @"Must have completion or failure block");
[condition lock];
NSAssert (!waiting, @"Must not run synchronously and asynchronously");
NSAssert (completionBlock == nil && failureBlock == nil, @"Must not run twice");
// FIXME: run the blocks without the lock or add a try/finally
if (result != nil) {
if (_completionBlock != nil)
_completionBlock (self, result);
} else if (error != nil) {
if (_failureBlock != nil)
_failureBlock (self, error);
} else {
completionBlock = [_completionBlock copy];
failureBlock = [_failureBlock copy];
// FIXME: save the thread and always invoke on that one
}
[condition unlock];
}
- (void) cancel
{
// FIXME: implement
NSAssert (NO, @"Cannot cancel a wait task");
}
@end