Merge pull request #141 from oatpp/minor_refactoring

Refactor. async::Executor choose IO event worker type.
This commit is contained in:
Leonid Stryzhevskyi 2019-10-13 23:18:21 +03:00 committed by GitHub
commit 01a97269a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 13 deletions

View File

@ -78,13 +78,14 @@ void Executor::SubmissionProcessor::detach() {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Executor
Executor::Executor(v_int32 processorWorkersCount, v_int32 ioWorkersCount, v_int32 timerWorkersCount, bool useIOEventWorker)
Executor::Executor(v_int32 processorWorkersCount, v_int32 ioWorkersCount, v_int32 timerWorkersCount, v_int32 ioWorkerType)
: m_balancer(0)
{
processorWorkersCount = chooseProcessorWorkersCount(processorWorkersCount);
ioWorkersCount = chooseIOWorkersCount(processorWorkersCount, ioWorkersCount);
timerWorkersCount = chooseTimerWorkersCount(timerWorkersCount);
ioWorkerType = chooseIOWorkerType(ioWorkerType);
for(v_int32 i = 0; i < processorWorkersCount; i ++) {
m_processorWorkers.push_back(std::make_shared<SubmissionProcessor>());
@ -93,14 +94,26 @@ Executor::Executor(v_int32 processorWorkersCount, v_int32 ioWorkersCount, v_int3
m_allWorkers.insert(m_allWorkers.end(), m_processorWorkers.begin(), m_processorWorkers.end());
std::vector<std::shared_ptr<worker::Worker>> ioWorkers;
if(useIOEventWorker) {
for (v_int32 i = 0; i < ioWorkersCount; i++) {
ioWorkers.push_back(std::make_shared<worker::IOEventWorkerForeman>());
switch(ioWorkerType) {
case IO_WORKER_TYPE_NAIVE: {
for (v_int32 i = 0; i < ioWorkersCount; i++) {
ioWorkers.push_back(std::make_shared<worker::IOWorker>());
}
break;
}
} else {
for (v_int32 i = 0; i < ioWorkersCount; i++) {
ioWorkers.push_back(std::make_shared<worker::IOWorker>());
case IO_WORKER_TYPE_EVENT: {
for (v_int32 i = 0; i < ioWorkersCount; i++) {
ioWorkers.push_back(std::make_shared<worker::IOEventWorkerForeman>());
}
break;
}
default:
throw std::runtime_error("[oatpp::async::Executor::Executor()]: Error. Unknown IO worker type.");
}
linkWorkers(ioWorkers);
@ -148,6 +161,20 @@ v_int32 Executor::chooseTimerWorkersCount(v_int32 timerWorkersCount) {
throw std::runtime_error("[oatpp::async::Executor::chooseTimerWorkersCount()]: Error. Invalid timer workers count specified.");
}
v_int32 Executor::chooseIOWorkerType(v_int32 ioWorkerType) {
if(ioWorkerType == VALUE_SUGGESTED) {
#if defined(WIN32) || defined(_WIN32)
return IO_WORKER_TYPE_NAIVE;
#else
return IO_WORKER_TYPE_EVENT;
#endif
}
return ioWorkerType;
}
void Executor::linkWorkers(const std::vector<std::shared_ptr<worker::Worker>>& workers) {
m_allWorkers.insert(m_allWorkers.end(), workers.begin(), workers.end());

View File

@ -84,6 +84,17 @@ public:
* Special value to indicate that Executor should choose it's own the value of specified parameter.
*/
static constexpr const v_int32 VALUE_SUGGESTED = -1000;
public:
/**
* IO Worker type naive.
*/
static constexpr const v_int32 IO_WORKER_TYPE_NAIVE = 0;
/**
* IO Worker type event.
*/
static constexpr const v_int32 IO_WORKER_TYPE_EVENT = 1;
private:
std::atomic<v_word32> m_balancer;
private:
@ -93,6 +104,7 @@ private:
static v_int32 chooseProcessorWorkersCount(v_int32 processorWorkersCount);
static v_int32 chooseIOWorkersCount(v_int32 processorWorkersCount, v_int32 ioWorkersCount);
static v_int32 chooseTimerWorkersCount(v_int32 timerWorkersCount);
static v_int32 chooseIOWorkerType(v_int32 ioWorkerType);
void linkWorkers(const std::vector<std::shared_ptr<worker::Worker>>& workers);
public:
@ -101,16 +113,12 @@ public:
* @param processorWorkersCount - number of data processing workers.
* @param ioWorkersCount - number of I/O processing workers.
* @param timerWorkersCount - number of timer processing workers.
* @param IOWorkerType
*/
Executor(v_int32 processorWorkersCount = VALUE_SUGGESTED,
v_int32 ioWorkersCount = VALUE_SUGGESTED,
v_int32 timerWorkersCount = VALUE_SUGGESTED,
#if defined(WIN32) || defined(_WIN32)
bool useIOEventWorker = false
#else
bool useIOEventWorker = true
#endif
);
v_int32 ioWorkerType = VALUE_SUGGESTED);
/**
* Non-virtual Destructor.