-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathasio-blockable.cpp
More file actions
288 lines (257 loc) · 9.85 KB
/
Copy pathasio-blockable.cpp
File metadata and controls
288 lines (257 loc) · 9.85 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/ext/asio/asio-blockable.h"
#include "hphp/runtime/ext/asio/ext_async-function-wait-handle.h"
#include "hphp/runtime/ext/asio/ext_async-generator-wait-handle.h"
#include "hphp/runtime/ext/asio/ext_await-all-wait-handle.h"
#include "hphp/runtime/ext/asio/ext_concurrent-wait-handle.h"
#include "hphp/runtime/ext/asio/ext_condition-wait-handle.h"
#include "hphp/runtime/ext/asio/ext_priority-bridge-wait-handle.h"
#include "hphp/runtime/vm/vm-regs.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
namespace {
using Kind = AsioBlockable::Kind;
template<class T>
inline T* getContainingObject(const AsioBlockable* blockable) {
return reinterpret_cast<T*>(
const_cast<char*>(
reinterpret_cast<const char*>(blockable) -
T::blockableOff()));
}
inline c_AsyncFunctionWaitHandle::Node* getAsyncFunctionWaitHandleNode(
const AsioBlockable* blockable
) {
assertx(blockable->getKind() == Kind::AsyncFunctionWaitHandleNode);
return getContainingObject<c_AsyncFunctionWaitHandle::Node>(blockable);
}
inline c_AsyncGeneratorWaitHandle* getAsyncGeneratorWaitHandle(
const AsioBlockable* blockable
) {
assertx(blockable->getKind() == Kind::AsyncGeneratorWaitHandle);
return getContainingObject<c_AsyncGeneratorWaitHandle>(blockable);
}
inline c_AwaitAllWaitHandle::Node* getAwaitAllWaitHandleNode(
const AsioBlockable* blockable
) {
assertx(blockable->getKind() == Kind::AwaitAllWaitHandleNode);
return getContainingObject<c_AwaitAllWaitHandle::Node>(blockable);
}
inline c_ConcurrentWaitHandle::Node* getConcurrentWaitHandleNode(
const AsioBlockable* blockable
) {
assertx(blockable->getKind() == Kind::ConcurrentWaitHandleNode);
return getContainingObject<c_ConcurrentWaitHandle::Node>(blockable);
}
inline c_ConditionWaitHandle* getConditionWaitHandle(
const AsioBlockable* blockable
) {
assertx(blockable->getKind() == Kind::ConditionWaitHandle);
return getContainingObject<c_ConditionWaitHandle>(blockable);
}
inline c_PriorityBridgeWaitHandle* getPriorityBridgeWaitHandle(
const AsioBlockable* blockable
) {
assertx(blockable->getKind() == Kind::PriorityBridgeWaitHandle);
return getContainingObject<c_PriorityBridgeWaitHandle>(blockable);
}
inline void exitContextImpl(
std::vector<AsioBlockableChain>& worklist,
c_WaitableWaitHandle* waitHandle,
ContextIndex contextIdx
) {
assertx(AsioSession::Get()->getContext(contextIdx));
assertx(!waitHandle->isFinished());
assertx(waitHandle->getContextIndex() <= contextIdx);
// Not in a context being exited.
if (waitHandle->getContextIndex() != contextIdx) {
return;
}
// Move the wait handle to the parent context.
waitHandle->setContextStateIndex(contextIdx.parent().toRegular());
// Request exit to the parent context for all parents.
worklist.emplace_back(waitHandle->getParentChain());
}
inline void exitContextImpl(
std::vector<AsioBlockableChain>& worklist,
c_AsyncFunctionWaitHandle::Node* node,
ContextIndex contextIdx
) {
if (node->isFirstUnfinishedChild()) {
exitContextImpl(worklist, node->getWaitHandle(), contextIdx);
}
}
inline void exitContextImpl(
std::vector<AsioBlockableChain>& worklist,
c_AwaitAllWaitHandle::Node* node,
ContextIndex contextIdx
) {
if (node->isFirstUnfinishedChild()) {
exitContextImpl(worklist, node->getWaitHandle(), contextIdx);
}
}
inline void exitContextImpl(
std::vector<AsioBlockableChain>& worklist,
c_ConcurrentWaitHandle::Node* node,
ContextIndex contextIdx
) {
if (node->isFirstUnfinishedChild()) {
exitContextImpl(worklist, node->getWaitHandle(), contextIdx);
}
}
inline void exitContextImpl(
std::vector<AsioBlockableChain>& worklist,
c_ConditionWaitHandle* waitHandle,
ContextIndex contextIdx
) {
// ConditionWaitHandle may finish before its children do.
if (waitHandle->isFinished()) {
return;
}
exitContextImpl(
worklist, static_cast<c_WaitableWaitHandle*>(waitHandle), contextIdx);
}
} // anon namespace
c_WaitableWaitHandle* AsioBlockable::getWaitHandle() const {
switch (getKind()) {
case Kind::AsyncFunctionWaitHandleNode:
return getAsyncFunctionWaitHandleNode(this)->getWaitHandle();
case Kind::AsyncGeneratorWaitHandle:
return getAsyncGeneratorWaitHandle(this);
case Kind::AwaitAllWaitHandleNode:
return getAwaitAllWaitHandleNode(this)->getWaitHandle();
case Kind::ConcurrentWaitHandleNode:
return getConcurrentWaitHandleNode(this)->getWaitHandle();
case Kind::ConditionWaitHandle:
return getConditionWaitHandle(this);
case Kind::PriorityBridgeWaitHandle:
return getPriorityBridgeWaitHandle(this);
}
not_reached();
}
void AsioBlockableChain::unblock() {
std::vector<AsioBlockableChain> worklist = { *this };
while (!worklist.empty()) {
auto const lastParent = worklist.back().m_lastParent;
worklist.pop_back();
for (AsioBlockable* cur = lastParent, *next; cur; cur = next) {
next = cur->getPrevParent();
cur->updatePrevParent(nullptr);
// the onUnblocked handler may free cur
switch (cur->getKind()) {
case Kind::AsyncFunctionWaitHandleNode:
getAsyncFunctionWaitHandleNode(cur)->onUnblocked();
break;
case Kind::AsyncGeneratorWaitHandle:
getAsyncGeneratorWaitHandle(cur)->onUnblocked();
break;
case Kind::AwaitAllWaitHandleNode:
getAwaitAllWaitHandleNode(cur)->onUnblocked(worklist);
break;
case Kind::ConcurrentWaitHandleNode:
getConcurrentWaitHandleNode(cur)->onUnblocked(worklist);
break;
case Kind::ConditionWaitHandle:
getConditionWaitHandle(cur)->onUnblocked(worklist);
break;
case Kind::PriorityBridgeWaitHandle:
getPriorityBridgeWaitHandle(cur)->onUnblocked(worklist);
break;
}
}
}
}
void AsioBlockableChain::exitContext(ContextIndex contextIdx) {
std::vector<AsioBlockableChain> worklist = { *this };
while (!worklist.empty()) {
auto const lastParent = worklist.back().m_lastParent;
worklist.pop_back();
for (auto cur = lastParent; cur; cur = cur->getPrevParent()) {
switch (cur->getKind()) {
case Kind::AsyncFunctionWaitHandleNode:
exitContextImpl(
worklist, getAsyncFunctionWaitHandleNode(cur), contextIdx);
break;
case Kind::AsyncGeneratorWaitHandle:
exitContextImpl(worklist, getAsyncGeneratorWaitHandle(cur), contextIdx);
break;
case Kind::AwaitAllWaitHandleNode:
exitContextImpl(worklist, getAwaitAllWaitHandleNode(cur), contextIdx);
break;
case Kind::ConcurrentWaitHandleNode:
exitContextImpl(worklist, getConcurrentWaitHandleNode(cur), contextIdx);
break;
case Kind::ConditionWaitHandle:
exitContextImpl(worklist, getConditionWaitHandle(cur), contextIdx);
break;
case Kind::PriorityBridgeWaitHandle:
exitContextImpl(worklist, getPriorityBridgeWaitHandle(cur), contextIdx);
break;
}
}
}
}
// Currently only AAWH and CCWH utilizes this to handle failures.
void AsioBlockableChain::removeFromChain(AsioBlockable* ab) {
AsioBlockable* next = nullptr;
for (AsioBlockable* cur = m_lastParent, *prev; cur; cur = prev) {
prev = cur->getPrevParent();
if (ab == cur) {
// Found the AAWH or CCWH we need to remove
assertx(cur->getKind() == Kind::AwaitAllWaitHandleNode ||
cur->getKind() == Kind::ConcurrentWaitHandleNode);
if (!next) {
m_lastParent = prev;
} else {
next->updatePrevParent(prev);
}
cur->updatePrevParent(nullptr);
return;
}
next = cur;
}
// We should always be able to find the parent.
assertx(false);
}
c_WaitableWaitHandle*
AsioBlockableChain::firstInContext(ContextStateIndex ctxStateIdx) {
// Returns the first wait handle in the parent chain that matches the given
// ContextStateIndex. If no match is found, returns the first wait handle with
// a matching ContextIndex (if there is one).
c_WaitableWaitHandle* result = nullptr;
c_WaitableWaitHandle* ctxResult = nullptr;
// Iterate through parents looking for a match. Continue until the last
// matching parent is found (i.e. first one that got enqueued) in order to
// maintain stacktrace consistency as more parents are added.
for (auto cur = m_lastParent; cur; cur = cur->getPrevParent()) {
auto const wh = cur->getWaitHandle();
if (!wh->isFinished() &&
wh->getContextStateIndex() == ctxStateIdx) {
result = wh;
}
if (!wh->isFinished() &&
wh->getContextIndex() == ctxStateIdx.contextIndex()) {
ctxResult = wh;
}
}
return result ? result : ctxResult;
}
void AsioBlockableChain::Unblock(AsioBlockableChain chain) {
chain.unblock();
}
///////////////////////////////////////////////////////////////////////////////
}