From 2d7714802d661e02e3c8ac1f7410cc7ffe63780a Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 14 Feb 2019 13:10:04 +0100 Subject: [PATCH] Allow CpuBoundWork to be done before end of scope --- lib/base/io-engine.cpp | 14 +++++++++++++- lib/base/io-engine.hpp | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/base/io-engine.cpp b/lib/base/io-engine.cpp index e1aeeb094..482d57176 100644 --- a/lib/base/io-engine.cpp +++ b/lib/base/io-engine.cpp @@ -31,6 +31,7 @@ using namespace icinga; CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc) + : m_Done(false) { auto& ioEngine (IoEngine::Get()); @@ -49,7 +50,18 @@ CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc) CpuBoundWork::~CpuBoundWork() { - IoEngine::Get().m_CpuBoundSemaphore.fetch_add(1); + if (!m_Done) { + IoEngine::Get().m_CpuBoundSemaphore.fetch_add(1); + } +} + +void CpuBoundWork::Done() +{ + if (!m_Done) { + IoEngine::Get().m_CpuBoundSemaphore.fetch_add(1); + + m_Done = true; + } } LazyInit> IoEngine::m_Instance ([]() { return std::unique_ptr(new IoEngine()); }); diff --git a/lib/base/io-engine.hpp b/lib/base/io-engine.hpp index df84df9ce..efeb56f99 100644 --- a/lib/base/io-engine.hpp +++ b/lib/base/io-engine.hpp @@ -48,6 +48,11 @@ public: CpuBoundWork& operator=(const CpuBoundWork&) = delete; CpuBoundWork& operator=(CpuBoundWork&&) = delete; ~CpuBoundWork(); + + void Done(); + +private: + bool m_Done; }; /**