mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-26 07:04:37 +02:00
IoEngine: explicitly join I/O threads
This commit is contained in:
parent
493a97f4f3
commit
282f8fd173
@ -99,7 +99,7 @@ boost::asio::io_service& IoEngine::GetIoService()
|
|||||||
return m_IoService;
|
return m_IoService;
|
||||||
}
|
}
|
||||||
|
|
||||||
IoEngine::IoEngine() : m_IoService(), m_KeepAlive(m_IoService), m_AlreadyExpiredTimer(m_IoService)
|
IoEngine::IoEngine() : m_IoService(), m_KeepAlive(m_IoService), m_Threads(decltype(m_Threads)::size_type(std::thread::hardware_concurrency())), m_AlreadyExpiredTimer(m_IoService)
|
||||||
{
|
{
|
||||||
m_AlreadyExpiredTimer.expires_at(boost::posix_time::neg_infin);
|
m_AlreadyExpiredTimer.expires_at(boost::posix_time::neg_infin);
|
||||||
|
|
||||||
@ -111,8 +111,21 @@ IoEngine::IoEngine() : m_IoService(), m_KeepAlive(m_IoService), m_AlreadyExpired
|
|||||||
m_CpuBoundSemaphore.store(concurrency - 1u);
|
m_CpuBoundSemaphore.store(concurrency - 1u);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto i (std::thread::hardware_concurrency()); i; --i) {
|
for (auto& thread : m_Threads) {
|
||||||
std::thread(&IoEngine::RunEventLoop, this).detach();
|
thread = std::thread(&IoEngine::RunEventLoop, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IoEngine::~IoEngine()
|
||||||
|
{
|
||||||
|
for (auto& thread : m_Threads) {
|
||||||
|
m_IoService.post([]() {
|
||||||
|
throw TerminateIoThread();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& thread : m_Threads) {
|
||||||
|
thread.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,6 +135,8 @@ void IoEngine::RunEventLoop()
|
|||||||
try {
|
try {
|
||||||
m_IoService.run();
|
m_IoService.run();
|
||||||
|
|
||||||
|
break;
|
||||||
|
} catch (const TerminateIoThread&) {
|
||||||
break;
|
break;
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
Log(LogCritical, "IoEngine", "Exception during I/O operation!");
|
Log(LogCritical, "IoEngine", "Exception during I/O operation!");
|
||||||
|
@ -22,7 +22,10 @@
|
|||||||
|
|
||||||
#include "base/lazy-init.hpp"
|
#include "base/lazy-init.hpp"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <exception>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
#include <boost/asio/deadline_timer.hpp>
|
#include <boost/asio/deadline_timer.hpp>
|
||||||
#include <boost/asio/io_service.hpp>
|
#include <boost/asio/io_service.hpp>
|
||||||
#include <boost/asio/spawn.hpp>
|
#include <boost/asio/spawn.hpp>
|
||||||
@ -82,6 +85,7 @@ public:
|
|||||||
IoEngine(IoEngine&&) = delete;
|
IoEngine(IoEngine&&) = delete;
|
||||||
IoEngine& operator=(const IoEngine&) = delete;
|
IoEngine& operator=(const IoEngine&) = delete;
|
||||||
IoEngine& operator=(IoEngine&&) = delete;
|
IoEngine& operator=(IoEngine&&) = delete;
|
||||||
|
~IoEngine();
|
||||||
|
|
||||||
static IoEngine& Get();
|
static IoEngine& Get();
|
||||||
|
|
||||||
@ -96,8 +100,13 @@ private:
|
|||||||
|
|
||||||
boost::asio::io_service m_IoService;
|
boost::asio::io_service m_IoService;
|
||||||
boost::asio::io_service::work m_KeepAlive;
|
boost::asio::io_service::work m_KeepAlive;
|
||||||
|
std::vector<std::thread> m_Threads;
|
||||||
boost::asio::deadline_timer m_AlreadyExpiredTimer;
|
boost::asio::deadline_timer m_AlreadyExpiredTimer;
|
||||||
std::atomic_uint_fast32_t m_CpuBoundSemaphore;
|
std::atomic_uint_fast32_t m_CpuBoundSemaphore;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class TerminateIoThread : public std::exception
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
#endif /* IO_ENGINE_H */
|
#endif /* IO_ENGINE_H */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user