Hide Boost.Asio from worker headers

Signed-off-by: AlterEgo <admin@projectskyfire.org>
This commit is contained in:
AlterEgo 2026-06-04 00:34:12 -05:00
parent 3e856fa207
commit b9feb0bd3d
6 changed files with 95 additions and 61 deletions

View file

@ -7,6 +7,9 @@
set(BZIP2_LIBRARIES "bzip2")
set(ZLIB_LIBRARIES "zlib")
set(SKYFIRE_WINDOWS_TARGET_VERSION "0x0601" CACHE STRING "Minimum Windows API target version")
add_definitions(-D_WIN32_WINNT=${SKYFIRE_WINDOWS_TARGET_VERSION})
# We require at least Visual Studio 16.6 2019(aka 16.6) which has version nr 1926
IF(NOT FORCE_UNSUPPORTED_COMPILER AND MSVC_VERSION LESS 1926)
# MSVC 1925 contains internal compiler bug.

View file

@ -7,6 +7,10 @@
#include "Config.h"
#include "Util.h"
#if PLATFORM == PLATFORM_WINDOWS
#include <Windows.h>
#endif
#include <sstream>
AppenderConsole::AppenderConsole(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags) :

View file

@ -5,24 +5,46 @@
#include "LogWorker.h"
#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
LogWorker::LogWorker()
: m_ioContext(), m_workGuard(new WorkGuard(boost::asio::make_work_guard(m_ioContext))),
m_thread(&LogWorker::svc, this), m_active(true)
#include <mutex>
#include <thread>
struct LogWorker::Impl
{
typedef boost::asio::executor_work_guard<boost::asio::io_context::executor_type> WorkGuard;
Impl()
: ioContext(), workGuard(new WorkGuard(boost::asio::make_work_guard(ioContext))),
active(true)
{
}
boost::asio::io_context ioContext;
std::unique_ptr<WorkGuard> workGuard;
std::mutex queueLock;
std::thread thread;
bool active;
};
LogWorker::LogWorker()
: m_impl(new Impl)
{
m_impl->thread = std::thread(&LogWorker::svc, this);
}
LogWorker::~LogWorker()
{
{
std::lock_guard<std::mutex> guard(m_queueLock);
m_active = false;
m_workGuard.reset();
std::lock_guard<std::mutex> guard(m_impl->queueLock);
m_impl->active = false;
m_impl->workGuard.reset();
}
if (m_thread.joinable())
m_thread.join();
if (m_impl->thread.joinable())
m_impl->thread.join();
}
int LogWorker::enqueue(LogOperation* op)
@ -31,13 +53,13 @@ int LogWorker::enqueue(LogOperation* op)
return -1;
{
std::lock_guard<std::mutex> guard(m_queueLock);
std::lock_guard<std::mutex> guard(m_impl->queueLock);
if (!m_active)
if (!m_impl->active)
return -1;
}
boost::asio::post(m_ioContext,
boost::asio::post(m_impl->ioContext,
[op]
{
op->call();
@ -49,6 +71,6 @@ int LogWorker::enqueue(LogOperation* op)
int LogWorker::svc()
{
m_ioContext.run();
m_impl->ioContext.run();
return 0;
}

View file

@ -8,11 +8,7 @@
#include "LogOperation.h"
#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/io_context.hpp>
#include <memory>
#include <mutex>
#include <thread>
class LogWorker
{
@ -29,15 +25,11 @@ public:
int enqueue(LogOperation* op);
private:
typedef boost::asio::executor_work_guard<boost::asio::io_context::executor_type> WorkGuard;
struct Impl;
int svc();
boost::asio::io_context m_ioContext;
std::unique_ptr<WorkGuard> m_workGuard;
std::mutex m_queueLock;
std::thread m_thread;
bool m_active;
std::unique_ptr<Impl> m_impl;
};
#endif

View file

@ -6,15 +6,39 @@
#include "DelayExecutor.h"
#include "Platform/Singleton.h"
#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
#include <mutex>
#include <thread>
#include <vector>
struct DelayExecutor::Impl
{
typedef boost::asio::executor_work_guard<boost::asio::io_context::executor_type> WorkGuard;
Impl()
: activated(false)
{
}
boost::asio::io_context ioContext;
std::unique_ptr<WorkGuard> workGuard;
std::unique_ptr<DelayTask> preSvcHook;
std::unique_ptr<DelayTask> postSvcHook;
std::vector<std::thread> threads;
std::mutex stateLock;
bool activated;
};
DelayExecutor* DelayExecutor::instance()
{
return Skyfire::Singleton<DelayExecutor, Skyfire::Mutex>::instance();
}
DelayExecutor::DelayExecutor()
: activated_(false) { }
: impl_(new Impl) { }
DelayExecutor::~DelayExecutor()
{
@ -24,35 +48,35 @@ DelayExecutor::~DelayExecutor()
int DelayExecutor::deactivate()
{
{
std::lock_guard<std::mutex> guard(state_lock_);
std::lock_guard<std::mutex> guard(impl_->stateLock);
if (!activated_)
if (!impl_->activated)
return -1;
activated_ = false;
work_guard_.reset();
impl_->activated = false;
impl_->workGuard.reset();
}
for (std::thread& thread : threads_)
for (std::thread& thread : impl_->threads)
if (thread.joinable())
thread.join();
threads_.clear();
pre_svc_hook_.reset();
post_svc_hook_.reset();
impl_->threads.clear();
impl_->preSvcHook.reset();
impl_->postSvcHook.reset();
return 0;
}
int DelayExecutor::svc()
{
if (pre_svc_hook_)
pre_svc_hook_->call();
if (impl_->preSvcHook)
impl_->preSvcHook->call();
io_context_.run();
impl_->ioContext.run();
if (post_svc_hook_)
post_svc_hook_->call();
if (impl_->postSvcHook)
impl_->postSvcHook->call();
return 0;
}
@ -65,17 +89,17 @@ int DelayExecutor::start(int num_threads, std::unique_ptr<DelayTask> pre_svc_hoo
if (num_threads < 1)
return -1;
pre_svc_hook_ = std::move(pre_svc_hook);
post_svc_hook_ = std::move(post_svc_hook);
io_context_.restart();
work_guard_.reset(new WorkGuard(boost::asio::make_work_guard(io_context_)));
impl_->preSvcHook = std::move(pre_svc_hook);
impl_->postSvcHook = std::move(post_svc_hook);
impl_->ioContext.restart();
impl_->workGuard.reset(new Impl::WorkGuard(boost::asio::make_work_guard(impl_->ioContext)));
activated(true);
try
{
for (int i = 0; i < num_threads; ++i)
threads_.push_back(std::thread(&DelayExecutor::svc, this));
impl_->threads.push_back(std::thread(&DelayExecutor::svc, this));
}
catch (...)
{
@ -92,13 +116,13 @@ int DelayExecutor::execute(std::unique_ptr<DelayTask> new_req)
return -1;
{
std::lock_guard<std::mutex> guard(state_lock_);
std::lock_guard<std::mutex> guard(impl_->stateLock);
if (!activated_)
if (!impl_->activated)
return -1;
}
boost::asio::post(io_context_,
boost::asio::post(impl_->ioContext,
[task = std::move(new_req)]() mutable
{
task->call();
@ -109,12 +133,12 @@ int DelayExecutor::execute(std::unique_ptr<DelayTask> new_req)
bool DelayExecutor::activated()
{
std::lock_guard<std::mutex> guard(state_lock_);
return activated_;
std::lock_guard<std::mutex> guard(impl_->stateLock);
return impl_->activated;
}
void DelayExecutor::activated(bool s)
{
std::lock_guard<std::mutex> guard(state_lock_);
activated_ = s;
std::lock_guard<std::mutex> guard(impl_->stateLock);
impl_->activated = s;
}

View file

@ -6,12 +6,7 @@
#ifndef _M_DELAY_EXECUTOR_H
#define _M_DELAY_EXECUTOR_H
#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/io_context.hpp>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
class DelayTask
{
@ -33,15 +28,9 @@ public:
int svc();
private:
typedef boost::asio::executor_work_guard<boost::asio::io_context::executor_type> WorkGuard;
struct Impl;
boost::asio::io_context io_context_;
std::unique_ptr<WorkGuard> work_guard_;
std::unique_ptr<DelayTask> pre_svc_hook_;
std::unique_ptr<DelayTask> post_svc_hook_;
std::vector<std::thread> threads_;
std::mutex state_lock_;
bool activated_;
std::unique_ptr<Impl> impl_;
void activated(bool s);
};