Unverified Commit ca858196 authored by kladko's avatar kladko

SKALE-4586 Added Thread Pool

parent bc2f6e44
......@@ -71,7 +71,7 @@ bin_PROGRAMS = sgxwallet testw sgx_util
COMMON_SRC = SGXException.cpp ExitHandler.cpp zmq_src/ZMQClient.cpp zmq_src/RspMessage.cpp zmq_src/ReqMessage.cpp \
zmq_src/ZMQMessage.cpp zmq_src/ZMQServer.cpp zmq_src/Agent.cpp ExitRequestedException.cpp \
zmq_src/ZMQMessage.cpp zmq_src/ZMQServer.cpp zmq_src/Agent.cpp zmq_src/WorkerThreadPool.cpp ExitRequestedException.cpp \
InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp TECrypto.cpp \
SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp CryptoTools.cpp \
DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \
......
/*
Copyright (C) 2021 SKALE Labs
This file is part of skale-consensus.
skale-consensus is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
@file WorkerThreadPool.cpp
@author Stan Kladko
@date 2021
*/
#include "common.h"
#include "third_party/spdlog/spdlog.h"
#include "WorkerThreadPool.h"
void WorkerThreadPool::startService() {
lock_guard<recursive_mutex> lock(threadPoolMutex);
CHECK_STATE(!started.exchange(true))
for (uint64_t i = 0; i < (uint64_t) numThreads; i++) {
createThread(i);
}
}
WorkerThreadPool::WorkerThreadPool(uint64_t _numThreads, Agent *_agent) : started(false), joined(false) {
CHECK_STATE(_numThreads > 0);
CHECK_STATE(_agent);
spdlog::info("Started thread pool. Threads count:" + to_string(_numThreads));
this->agent = _agent;
this->numThreads = _numThreads;;
}
void WorkerThreadPool::joinAll() {
if (joined)
return;
lock_guard<recursive_mutex> lock(threadPoolMutex);
joined = true;
for (auto &&thread : threadpool) {
if (thread->joinable())
thread->join();
CHECK_STATE(!thread->joinable());
}
}
bool WorkerThreadPool::isJoined() const {
return joined;
}
WorkerThreadPool::~WorkerThreadPool(){
}
/*
Copyright (C) 2018-2019 SKALE Labs
This file is part of skale-consensus.
skale-consensus is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
@file WorkerThreadPool.h
@author Stan Kladko
@date 2018
*/
#pragma once
#include <vector>
#include <atomic>
#include <thread>
#include "Agent.h"
class WorkerThreadPool {
atomic_bool started;
virtual void createThread( uint64_t threadNumber ) = 0;
protected:
atomic_bool joined;
vector<shared_ptr<thread>> threadpool;
recursive_mutex threadPoolMutex;
uint64_t numThreads = 0;
Agent* agent = nullptr;
protected:
WorkerThreadPool(uint64_t _numThreads, Agent *_agent);
public:
virtual ~WorkerThreadPool();
virtual void startService();
void joinAll();
bool isJoined() const;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment