forked from viewfinderco/viewfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncState.cc
244 lines (215 loc) · 5.18 KB
/
AsyncState.cc
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2012 Viewfinder. All rights reserved.
// Author: Peter Mattis.
#import "AsyncState.h"
#import "Utils.h"
AsyncState::Impl::Impl()
: inflight_(0),
running_(0),
alive_(true) {
}
AsyncState::AsyncBlock* AsyncState::Impl::QueueAsyncBlock(
const DispatchBlock& block, bool force) {
AsyncBlock* ab = new AsyncBlock(block);
if (Enter(ab, force)) {
return ab;
}
delete ab;
return NULL;
}
bool AsyncState::Impl::Enter(AsyncBlock* ab, bool force) {
MutexLock l(&mu_);
if (!force && !alive_) {
return false;
}
++inflight_;
if (ab) {
blocks_.insert(ab);
}
return true;
}
void AsyncState::Impl::Kill() {
mu_.Lock();
// Set alive to false to prevent new ops from entering the running state.
alive_ = false;
// Bump the inflight count to prevent deletion while we're waiting for the
// running ops to finish.
++inflight_;
mu_.Wait([this] {
return running_ == 0;
});
CHECK_EQ(0, running_);
--inflight_;
// Loop over any inflight (but not running) blocks and clear the block
// variable in order to release any memory associated with the block.
for (BlockMap::iterator iter(blocks_.begin());
iter != blocks_.end();
++iter) {
AsyncBlock* ab = *iter;
ab->clear();
}
const bool del = !inflight_;
mu_.Unlock();
if (del) {
delete this;
}
}
void AsyncState::Impl::Run(AsyncBlock* ab, bool force) {
dispatch_autoreleasepool([this, ab, force] {
if (Start(ab, force)) {
Finish();
}
});
}
bool AsyncState::Impl::Start(AsyncBlock* ab, bool force) {
mu_.Lock();
--inflight_;
const bool alive = force || alive_;
if (alive) {
++running_;
}
if (ab) {
if (!alive) {
// If the AsyncState is being killed, clear the block before we release
// the lock in order to release any memory associated with the block.
ab->clear();
}
blocks_.erase(ab);
}
const bool del = !alive && !inflight_ && !running_;
mu_.Unlock();
if (del) {
delete this;
}
if (ab && ab->valid()) {
(*ab)();
}
delete ab;
return alive;
}
bool AsyncState::Impl::Finish() {
mu_.Lock();
--running_;
const bool alive = alive_;
const bool del = !alive && !inflight_ && !running_;
mu_.Unlock();
if (del) {
delete this;
}
return alive;
}
void AsyncState::Impl::dispatch_main(const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block);
if (!ab) {
return;
}
::dispatch_main([this, ab] { Run(ab); });
}
void AsyncState::Impl::dispatch_main_async(const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block);
if (!ab) {
return;
}
::dispatch_main_async([this, ab] { Run(ab); });
}
void AsyncState::Impl::dispatch_network(bool force, const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block, force);
if (!ab) {
return;
}
::dispatch_network([this, ab, force] { Run(ab, force); });
}
void AsyncState::Impl::dispatch_high_priority(const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block);
if (!ab) {
return;
}
::dispatch_high_priority([this, ab] { Run(ab); });
}
void AsyncState::Impl::dispatch_low_priority(const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block);
if (!ab) {
return;
}
::dispatch_low_priority([this, ab] { Run(ab); });
}
void AsyncState::Impl::dispatch_background(const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block);
if (!ab) {
return;
}
::dispatch_background([this, ab] { Run(ab); });
}
void AsyncState::Impl::dispatch_after_main(
double delay, const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block);
if (!ab) {
return;
}
::dispatch_after_main(delay, [this, ab] { Run(ab); });
}
void AsyncState::Impl::dispatch_after_network(
double delay, const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block);
if (!ab) {
return;
}
::dispatch_after_network(delay, [this, ab] { Run(ab); });
}
void AsyncState::Impl::dispatch_after_low_priority(
double delay, const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block);
if (!ab) {
return;
}
::dispatch_after_low_priority(delay, [this, ab] { Run(ab); });
}
void AsyncState::Impl::dispatch_after_background(
double delay, const DispatchBlock& block) {
AsyncBlock* ab = QueueAsyncBlock(block);
if (!ab) {
return;
}
::dispatch_after_background(delay, [this, ab] { Run(ab); });
}
AsyncState::AsyncState()
: impl_(new Impl),
parent_(NULL) {
}
AsyncState::AsyncState(AsyncState* parent)
: impl_(new Impl),
parent_(parent) {
parent_->children_.insert(this);
}
AsyncState::~AsyncState() {
Kill();
}
bool AsyncState::Enter() {
if (!impl_->Enter(NULL)) {
return false;
}
return impl_->Start(NULL);
}
bool AsyncState::Exit() {
return impl_->Finish();
}
void AsyncState::Kill() {
ChildMap tmp_children;
tmp_children.swap(children_);
for (ChildMap::iterator iter(tmp_children.begin());
iter != tmp_children.end();
++iter) {
AsyncState* child = *iter;
child->parent_ = NULL;
child->Kill();
}
if (parent_) {
parent_->children_.erase(this);
}
if (impl_) {
impl_->Kill();
impl_ = NULL;
}
}
// local variables:
// mode: c++
// end: