2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 | Copyright (c) 1997-2010 The PHP Group |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 3.01 of the PHP license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.php.net/license/3_01.txt |
12 | If you did not receive a copy of the PHP license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@php.net so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 #include "hphp/runtime/ext/asio/asio_session.h"
19 #include "hphp/runtime/ext/ext_asio.h"
20 #include "hphp/system/lib/systemlib.h"
23 ///////////////////////////////////////////////////////////////////////////////
25 IMPLEMENT_THREAD_LOCAL_PROXY(AsioSession
, false, AsioSession::s_current
);
28 const context_idx_t MAX_CONTEXT_DEPTH
= std::numeric_limits
<context_idx_t
>::max();
31 void AsioSession::Init() {
32 s_current
.set(new AsioSession());
35 AsioSession::AsioSession()
36 : m_contexts(), m_readyExternalThreadEvents(nullptr),
37 m_readyExternalThreadEventsMutex(),
38 m_readyExternalThreadEventsCondition() {
41 void AsioSession::enterContext() {
42 assert(!isInContext() || getCurrentContext()->isRunning());
44 if (UNLIKELY(getCurrentContextIdx() >= MAX_CONTEXT_DEPTH
)) {
45 Object
e(SystemLib::AllocInvalidOperationExceptionObject(
46 "Unable to enter asio context: too many contexts open"));
50 m_contexts
.push_back(new AsioContext());
52 assert(static_cast<context_idx_t
>(m_contexts
.size()) == m_contexts
.size());
53 assert(isInContext());
54 assert(!getCurrentContext()->isRunning());
57 void AsioSession::exitContext() {
58 assert(isInContext());
59 assert(!getCurrentContext()->isRunning());
61 m_contexts
.back()->exit(m_contexts
.size());
62 delete m_contexts
.back();
63 m_contexts
.pop_back();
65 assert(!isInContext() || getCurrentContext()->isRunning());
68 uint16_t AsioSession::getCurrentWaitHandleDepth() {
69 assert(!isInContext() || getCurrentContext()->isRunning());
70 return isInContext() ? getCurrentWaitHandle()->getDepth() : 0;
73 c_ExternalThreadEventWaitHandle
* AsioSession::waitForExternalThreadEvents() {
74 // try check for ready external thread events without grabbing lock
75 auto ready
= m_readyExternalThreadEvents
.exchange(nullptr);
76 if (ready
!= nullptr) {
77 assert(ready
!= k_waitingForExternalThreadEvents
);
81 // no ready external thread events available, synchronization needed
82 std::unique_lock
<std::mutex
> lock(m_readyExternalThreadEventsMutex
);
84 // transition from empty to WAITING
85 if (m_readyExternalThreadEvents
.compare_exchange_strong(ready
, k_waitingForExternalThreadEvents
)) {
86 // wait for transition from WAITING to non-empty
88 m_readyExternalThreadEventsCondition
.wait(lock
);
89 } while (m_readyExternalThreadEvents
.load() == k_waitingForExternalThreadEvents
);
91 // external thread transitioned from empty to non-empty while grabbing lock
94 ready
= m_readyExternalThreadEvents
.exchange(nullptr);
95 assert(ready
!= nullptr);
96 assert(ready
!= k_waitingForExternalThreadEvents
);
100 void AsioSession::enqueueExternalThreadEvent(c_ExternalThreadEventWaitHandle
* wait_handle
) {
101 auto next
= m_readyExternalThreadEvents
.load();
103 while (next
!= k_waitingForExternalThreadEvents
) {
104 wait_handle
->setNextToProcess(next
);
105 if (m_readyExternalThreadEvents
.compare_exchange_weak(next
, wait_handle
)) {
110 // try to transition from WAITING to non-empty
111 wait_handle
->setNextToProcess(nullptr);
112 if (m_readyExternalThreadEvents
.compare_exchange_weak(next
, wait_handle
)) {
113 // succeeded, notify condition
114 std::unique_lock
<std::mutex
> lock(m_readyExternalThreadEventsMutex
);
115 m_readyExternalThreadEventsCondition
.notify_one();
121 void AsioSession::initAbruptInterruptException() {
122 assert(!hasAbruptInterruptException());
123 m_abruptInterruptException
= SystemLib::AllocInvalidOperationExceptionObject(
124 "The request was abruptly interrupted.");
127 void AsioSession::onFailed(CObjRef exception
) {
128 if (m_onFailedCallback
.get()) {
130 vm_call_user_func(m_onFailedCallback
, Array::Create(exception
));
131 } catch (const Object
& callback_exception
) {
132 raise_warning("[asio] Ignoring exception thrown by onFailed callback");
137 void AsioSession::onContinuationCreate(c_ContinuationWaitHandle
* cont
) {
138 assert(m_onContinuationCreateCallback
.get());
141 m_onContinuationCreateCallback
,
142 Array::Create(cont
));
143 } catch (const Object
& callback_exception
) {
144 raise_warning("[asio] Ignoring exception thrown by ContinuationWaitHandle::onCreate callback");
148 void AsioSession::onContinuationYield(c_ContinuationWaitHandle
* cont
, c_WaitHandle
* child
) {
149 assert(m_onContinuationYieldCallback
.get());
152 m_onContinuationYieldCallback
,
153 CREATE_VECTOR2(cont
, child
));
154 } catch (const Object
& callback_exception
) {
155 raise_warning("[asio] Ignoring exception thrown by ContinuationWaitHandle::onYield callback");
159 void AsioSession::onContinuationSuccess(c_ContinuationWaitHandle
* cont
, CVarRef result
) {
160 assert(m_onContinuationSuccessCallback
.get());
163 m_onContinuationSuccessCallback
,
164 CREATE_VECTOR2(cont
, result
));
165 } catch (const Object
& callback_exception
) {
166 raise_warning("[asio] Ignoring exception thrown by ContinuationWaitHandle::onSuccess callback");
170 void AsioSession::onContinuationFail(c_ContinuationWaitHandle
* cont
, CObjRef exception
) {
171 assert(m_onContinuationFailCallback
.get());
174 m_onContinuationFailCallback
,
175 CREATE_VECTOR2(cont
, exception
));
176 } catch (const Object
& callback_exception
) {
177 raise_warning("[asio] Ignoring exception thrown by ContinuationWaitHandle::onFail callback");
181 void AsioSession::onJoin(c_WaitHandle
* wait_handle
) {
182 assert(m_onJoinCallback
.get());
184 vm_call_user_func(m_onJoinCallback
, Array::Create(wait_handle
));
185 } catch (const Object
& callback_exception
) {
186 raise_warning("[asio] Ignoring exception thrown by WaitHandle::onJoin callback");
190 void AsioSession::onGenArrayCreate(c_GenArrayWaitHandle
* wait_handle
, CVarRef dependencies
) {
191 assert(m_onGenArrayCreateCallback
.get());
194 m_onGenArrayCreateCallback
,
195 CREATE_VECTOR2(wait_handle
, dependencies
));
196 } catch (const Object
& callback_exception
) {
197 raise_warning("[asio] Ignoring exception thrown by GenArrayWaitHandle::onCreate callback");
201 void AsioSession::onGenVectorCreate(c_GenVectorWaitHandle
* wait_handle
, CVarRef dependencies
) {
202 assert(m_onGenVectorCreateCallback
.get());
205 m_onGenVectorCreateCallback
,
206 CREATE_VECTOR2(wait_handle
, dependencies
));
207 } catch (const Object
& callback_exception
) {
208 raise_warning("[asio] Ignoring exception thrown by GenVectorWaitHandle::onCreate callback");
212 void AsioSession::onSetResultToRefCreate(c_SetResultToRefWaitHandle
* wait_handle
, CObjRef child
) {
213 assert(m_onSetResultToRefCreateCallback
.get());
216 m_onSetResultToRefCreateCallback
,
217 CREATE_VECTOR2(wait_handle
, child
));
218 } catch (const Object
& callback_exception
) {
219 raise_warning("[asio] Ignoring exception thrown by SetResultToRefWaitHandle::onCreate callback");
223 ///////////////////////////////////////////////////////////////////////////////