-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathext_await-all-wait-handle.cpp
More file actions
228 lines (192 loc) · 7.89 KB
/
Copy pathext_await-all-wait-handle.cpp
File metadata and controls
228 lines (192 loc) · 7.89 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
/*
+----------------------------------------------------------------------+
| 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/ext_await-all-wait-handle.h"
#include "hphp/runtime/base/array-init.h"
#include "hphp/runtime/base/array-iterator.h"
#include "hphp/runtime/base/vanilla-dict.h"
#include "hphp/runtime/base/vanilla-vec.h"
#include "hphp/runtime/ext/asio/asio-blockable.h"
#include "hphp/runtime/ext/asio/asio-session.h"
#include "hphp/runtime/ext/asio/ext_asio.h"
#include "hphp/runtime/ext/asio/ext_static-wait-handle.h"
#include "hphp/runtime/ext/asio/ext_wait-handle.h"
#include "hphp/system/systemlib.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
req::ptr<c_AwaitAllWaitHandle> c_AwaitAllWaitHandle::Alloc(int32_t cnt) {
auto size = c_AwaitAllWaitHandle::heapSize(cnt);
auto mem = tl_heap->objMalloc(size);
auto handle = new (mem) c_AwaitAllWaitHandle(cnt);
assertx(handle->hasExactlyOneRef());
return req::ptr<c_AwaitAllWaitHandle>::attach(handle);
}
///////////////////////////////////////////////////////////////////////////////
namespace {
StaticString s_awaitAll("<await-all>");
[[noreturn]] NEVER_INLINE
void failWaitHandle() {
SystemLib::throwInvalidArgumentExceptionObject(
"Expected dependencies to be a collection of WaitHandle instances");
}
c_StaticWaitHandle* returnEmpty() {
return c_StaticWaitHandle::CreateSucceeded(make_tv<KindOfNull>());
}
void prepareChild(TypedValue src, ContextStateIndex& ctxStateIdx, uint32_t& cnt) {
auto const waitHandle = c_Awaitable::fromTV(src);
if (UNLIKELY(!waitHandle)) failWaitHandle();
if (waitHandle->isFinished()) return;
assertx(isa<c_WaitableWaitHandle>(waitHandle));
auto const child = static_cast<c_WaitableWaitHandle*>(waitHandle);
ctxStateIdx = std::min(ctxStateIdx, child->getContextStateIndex());
++cnt;
}
bool addChild(TypedValue src, c_AwaitAllWaitHandle::Node*& dst, uint32_t& idx) {
auto const waitHandle = c_Awaitable::fromTVAssert(src);
if (waitHandle->isFinished()) return false;
waitHandle->incRefCount();
(--dst)->m_child = static_cast<c_WaitableWaitHandle*>(waitHandle);
dst->m_index = idx--;
dst->m_child->getParentChain().addParent(dst->m_blockable,
AsioBlockable::Kind::AwaitAllWaitHandleNode);
return true;
}
}
void HHVM_STATIC_METHOD(AwaitAllWaitHandle, setOnCreateCallback,
const Variant& callback) {
AsioSession::Get()->setOnAwaitAllCreate(callback);
}
template<typename Iter>
Object c_AwaitAllWaitHandle::Create(Iter iter) {
auto ctxStateIdx = ContextStateIndex::max();
uint32_t cnt = 0;
iter([&](TypedValue v) { prepareChild(v, ctxStateIdx, cnt); });
if (!cnt) {
return Object{returnEmpty()};
}
auto result = Alloc(cnt);
auto next = &result->m_children[cnt];
uint32_t idx = cnt - 1;
iter([&](TypedValue v) { addChild(v, next, idx); });
assertx(next == &result->m_children[0]);
result->initialize(ctxStateIdx);
return Object{std::move(result)};
}
Object c_AwaitAllWaitHandle::fromArrLike(const ArrayData* ad) {
return c_AwaitAllWaitHandle::Create([=](auto fn) { IterateV(ad, fn); });
}
Object HHVM_STATIC_METHOD(AwaitAllWaitHandle, fromVec,
const Array& dependencies) {
auto ad = dependencies.get();
assertx(ad);
if (!ad->size()) return Object{returnEmpty()};
if (!ad->isVanilla()) return c_AwaitAllWaitHandle::fromArrLike(ad);
assertx(ad->isVanillaVec());
return c_AwaitAllWaitHandle::Create([=](auto fn) {
VanillaVec::IterateV(ad, fn);
});
}
Object HHVM_STATIC_METHOD(AwaitAllWaitHandle, fromDict,
const Array& dependencies) {
auto ad = dependencies.get();
assertx(ad);
if (!ad->size()) return Object{returnEmpty()};
if (!ad->isVanilla()) return c_AwaitAllWaitHandle::fromArrLike(ad);
assertx(ad->isVanillaDict());
return c_AwaitAllWaitHandle::Create([=](auto fn) {
VanillaDict::IterateV(VanillaDict::as(ad), fn);
});
}
void c_AwaitAllWaitHandle::initialize(ContextStateIndex ctxStateIdx) {
setState(STATE_BLOCKED);
setContextStateIndex(ctxStateIdx);
// For AAWH not being finished, accounts for edges from all children.
incRefCount();
if (UNLIKELY(AsioSession::Get()->hasOnAwaitAllCreate())) {
VecInit dependencies(m_cap);
for (int32_t idx = m_cap - 1; idx >= 0; --idx) {
auto const child = make_tv<KindOfObject>(m_children[idx].m_child);
dependencies.append(child);
}
AsioSession::Get()->onAwaitAllCreate(this, dependencies.toArray());
}
}
void c_AwaitAllWaitHandle::onUnblocked(uint32_t idx, std::vector<AsioBlockableChain>& worklist) {
assertx(idx <= m_unfinished);
assertx(getState() == STATE_BLOCKED);
if (idx == m_unfinished) {
for (uint32_t next = idx - 1; next < idx; --next) {
auto const child = m_children[next].m_child;
if (!child->isFinished()) {
// Found the next unfinished child.
m_unfinished = next;
// Make sure there's no cyclic dependencies.
try {
detectCycle(child);
} catch (const Object& cycle_exception) {
assertx(cycle_exception->instanceof(SystemLib::getThrowableClass()));
throwable_recompute_backtrace_from_wh(cycle_exception.get(), this);
markAsFailed(cycle_exception, worklist);
}
return;
}
}
// All children finished.
markAsFinished(worklist);
}
}
void c_AwaitAllWaitHandle::markAsFinished(std::vector<AsioBlockableChain>& worklist) {
auto parentChain = getParentChain();
setState(STATE_SUCCEEDED);
tvWriteNull(m_resultOrException);
worklist.emplace_back(std::move(parentChain));
decRefObj(this);
}
void c_AwaitAllWaitHandle::markAsFailed(const Object& exception, std::vector<AsioBlockableChain>& worklist) {
for (uint32_t idx = 0; idx < m_cap; idx++) {
auto const child = m_children[idx].m_child;
if (!child->isFinished()) {
// Remove the current AAWH from the parent chain of all children.
child->getParentChain().removeFromChain(&m_children[idx].m_blockable);
}
}
auto parentChain = getParentChain();
setState(STATE_FAILED);
tvWriteObject(exception.get(), &m_resultOrException);
worklist.emplace_back(std::move(parentChain));
decRefObj(this);
}
String c_AwaitAllWaitHandle::getName() {
return s_awaitAll;
}
c_WaitableWaitHandle* c_AwaitAllWaitHandle::getChild() {
assertx(getState() == STATE_BLOCKED);
assertx(m_unfinished < m_cap);
return m_children[m_unfinished].m_child;
}
///////////////////////////////////////////////////////////////////////////////
void AsioExtension::registerNativeAwaitAllWaitHandle() {
#define AAWH_SME(meth) \
HHVM_STATIC_MALIAS(HH\\AwaitAllWaitHandle, meth, AwaitAllWaitHandle, meth)
AAWH_SME(fromVec);
AAWH_SME(fromDict);
AAWH_SME(setOnCreateCallback);
#undef AAWH_SME
Native::registerClassExtraDataHandler(
c_AwaitAllWaitHandle::className(), finish_class<c_AwaitAllWaitHandle>);
}
///////////////////////////////////////////////////////////////////////////////
}