2018-02-15 16:13:31 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2025-01-13 23:31:42 +01:00
|
|
|
* Copyright (C) 2016-2025 Tom Zander <tom@flowee.org>
|
2018-02-15 16:13:31 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
#include "WorkerThreads.h"
|
|
|
|
|
#include "Logger.h"
|
2019-11-13 11:46:09 +01:00
|
|
|
|
2025-02-08 19:05:26 +01:00
|
|
|
WorkerThreads::Work::Work(const std::shared_ptr<boost::asio::io_context> &context)
|
|
|
|
|
: work(boost::asio::make_work_guard(*context))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
2025-01-13 23:31:42 +01:00
|
|
|
WorkerThreads::WorkerThreads(int threadCount)
|
2018-02-15 16:13:31 +01:00
|
|
|
{
|
2025-01-13 23:31:42 +01:00
|
|
|
startThreads(threadCount);
|
2018-02-15 16:13:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 23:31:42 +01:00
|
|
|
void WorkerThreads::startThreads(int threadCount)
|
2018-02-15 16:13:31 +01:00
|
|
|
{
|
2025-01-13 23:31:42 +01:00
|
|
|
if (threadCount < 1)
|
|
|
|
|
threadCount = boost::thread::hardware_concurrency();
|
|
|
|
|
threadCount = std::max(threadCount, 2);
|
2025-02-08 19:05:26 +01:00
|
|
|
m_context = std::make_shared<boost::asio::io_context>();
|
|
|
|
|
m_work.reset(new Work(m_context));
|
2025-01-13 23:31:42 +01:00
|
|
|
for (int i = threadCount; i > 0; --i) {
|
2025-02-08 19:05:26 +01:00
|
|
|
auto context(m_context);
|
|
|
|
|
m_threads.create_thread([context] {
|
2019-11-13 11:46:09 +01:00
|
|
|
#if defined(PR_SET_NAME)
|
|
|
|
|
// Only the first 15 characters are used (16 - NUL terminator)
|
|
|
|
|
::prctl(PR_SET_NAME, "Worker-threads", 0, 0, 0);
|
|
|
|
|
#endif
|
2018-02-15 16:13:31 +01:00
|
|
|
while(true) {
|
|
|
|
|
try {
|
2025-02-08 19:05:26 +01:00
|
|
|
context->run();
|
2018-02-15 16:13:31 +01:00
|
|
|
return;
|
|
|
|
|
} catch (const boost::thread_interrupted&) {
|
|
|
|
|
return;
|
|
|
|
|
} catch (const std::exception& ex) {
|
2025-01-13 23:37:51 +01:00
|
|
|
logCritical(Log::Flowee) << "Threadgroup: uncaught exception" << ex;
|
2018-02-15 16:13:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WorkerThreads::~WorkerThreads()
|
|
|
|
|
{
|
|
|
|
|
stopThreads();
|
2019-03-31 15:11:38 +02:00
|
|
|
joinAll();
|
2018-02-15 16:13:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WorkerThreads::stopThreads()
|
|
|
|
|
{
|
|
|
|
|
m_work.reset();
|
2025-02-08 19:05:26 +01:00
|
|
|
if (m_context.get()) // it gets reset() by joinAll
|
|
|
|
|
m_context->stop();
|
2018-02-15 16:13:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WorkerThreads::joinAll()
|
|
|
|
|
{
|
|
|
|
|
m_threads.join_all();
|
2025-02-08 19:05:26 +01:00
|
|
|
m_context.reset(); // tasks don't get garbage-collected until the destructor is ran
|
2018-02-15 16:13:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-08 19:05:26 +01:00
|
|
|
boost::asio::io_context& WorkerThreads::ioContext() const
|
2018-02-15 16:13:31 +01:00
|
|
|
{
|
2025-02-08 19:05:26 +01:00
|
|
|
return *m_context;
|
2018-02-15 16:13:31 +01:00
|
|
|
}
|