diff --git a/libi2pd/util.cpp b/libi2pd/util.cpp index 65858389..9c0a5347 100644 --- a/libi2pd/util.cpp +++ b/libi2pd/util.cpp @@ -141,12 +141,13 @@ namespace util } } - void RunnableService::StopIOService () + void RunnableService::StopIOService (bool stopService) { if (m_IsRunning) { m_IsRunning = false; - m_Service.stop (); + if (stopService) + m_Service.stop (); if (m_Thread) { m_Thread->join (); @@ -180,6 +181,12 @@ namespace util m_Name = name.substr(0,15); } + void RunnableServiceWithWork::StopWorkAndFinishTasks () + { + m_Work.reset (); + StopIOService (false); + } + void SetThreadName (const char *name) { #if defined(__APPLE__) # if (!defined(MAC_OS_X_VERSION_10_6) || \ diff --git a/libi2pd/util.h b/libi2pd/util.h index d8d354c5..f3687c47 100644 --- a/libi2pd/util.h +++ b/libi2pd/util.h @@ -214,10 +214,10 @@ namespace util virtual ~RunnableService () {} auto& GetIOService () { return m_Service; } - bool IsRunning () const { return m_IsRunning; }; + bool IsRunning () const { return m_IsRunning; } void StartIOService (); - void StopIOService (); + void StopIOService (bool stopService = true); void SetName (std::string_view name); @@ -238,7 +238,9 @@ namespace util protected: RunnableServiceWithWork (const std::string& name): - RunnableService (name), m_Work (GetIOService ().get_executor ()) {} + RunnableService (name), m_Work (boost::asio::make_work_guard (GetIOService ())) {} + + void StopWorkAndFinishTasks (); // let all outstanding tasks finish and terminate private: