oatpp/core/async/Processor.cpp

101 lines
2.7 KiB
C++
Raw Normal View History

2018-03-25 16:02:15 +08:00
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi, <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
2018-03-19 01:00:46 +08:00
#include "Processor.hpp"
2018-03-19 11:13:19 +08:00
namespace oatpp { namespace async {
2018-03-25 16:02:15 +08:00
bool Processor::checkWaitingQueue() {
2018-03-19 11:13:19 +08:00
bool hasActions = false;
2018-03-25 16:02:15 +08:00
AbstractCoroutine* curr = m_waitingQueue.first;
AbstractCoroutine* prev = nullptr;
2018-03-19 11:13:19 +08:00
while (curr != nullptr) {
2018-03-25 16:02:15 +08:00
const Action& action = curr->iterate();
if(action.m_type == Action::TYPE_ABORT) {
m_waitingQueue.removeEntry(curr, prev);
if(prev != nullptr) {
curr = prev;
} else {
curr = m_waitingQueue.first;
2018-03-19 11:13:19 +08:00
}
2018-03-25 16:02:15 +08:00
} else if(action.m_type != Action::TYPE_WAIT_RETRY) {
oatpp::collection::FastQueue<AbstractCoroutine>::moveEntry(m_waitingQueue, m_activeQueue, curr, prev);
2018-03-19 11:13:19 +08:00
hasActions = true;
if(prev != nullptr) {
curr = prev;
} else {
2018-03-25 16:02:15 +08:00
curr = m_waitingQueue.first;
2018-03-19 11:13:19 +08:00
}
}
prev = curr;
if(curr != nullptr) {
2018-03-25 16:02:15 +08:00
curr = curr->_ref;
2018-03-19 11:13:19 +08:00
}
2018-03-25 16:02:15 +08:00
2018-03-19 11:13:19 +08:00
}
return hasActions;
}
2018-03-25 16:02:15 +08:00
bool Processor::countdownToSleep() {
++ m_sleepCountdown;
if(m_sleepCountdown > 1000) {
return checkWaitingQueue();
2018-03-19 11:13:19 +08:00
}
2018-03-25 16:02:15 +08:00
checkWaitingQueue();
std::this_thread::yield();
2018-03-19 11:13:19 +08:00
return true;
}
2018-03-25 16:02:15 +08:00
void Processor::addCoroutine(AbstractCoroutine* coroutine) {
m_activeQueue.pushBack(coroutine);
2018-03-19 11:13:19 +08:00
}
2018-03-25 16:02:15 +08:00
bool Processor::iterate(v_int32 numIterations) {
2018-03-19 11:13:19 +08:00
2018-03-25 16:02:15 +08:00
for(v_int32 i = 0; i < numIterations; i++) {
2018-03-19 11:13:19 +08:00
2018-03-25 16:02:15 +08:00
auto CP = m_activeQueue.first;
if(CP == nullptr) {
break;
} else {
m_sleepCountdown = 0;
2018-03-19 11:13:19 +08:00
}
2018-03-25 16:02:15 +08:00
if(!CP->finished()) {
const Action& action = CP->iterate();
if(action.m_type == Action::TYPE_WAIT_RETRY) {
m_waitingQueue.pushBack(m_activeQueue.popFront());
2018-03-19 11:13:19 +08:00
}
2018-03-25 16:02:15 +08:00
} else {
m_activeQueue.popFrontNoData();
2018-03-19 11:13:19 +08:00
}
}
2018-03-25 16:02:15 +08:00
return ((m_activeQueue.first != nullptr) || countdownToSleep());
2018-03-19 11:13:19 +08:00
}
}}