Skip to content

Commit

Permalink
Replace some deprecated facilities from Boost.Asio
Browse files Browse the repository at this point in the history
  • Loading branch information
0tkl authored and arch1t3cht committed Feb 8, 2025
1 parent b2a0b09 commit 71a4497
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
31 changes: 16 additions & 15 deletions libaegisub/common/dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,48 @@
#include "libaegisub/util.h"

#include <atomic>
#include <boost/asio/io_service.hpp>
#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/strand.hpp>
#include <condition_variable>
#include <mutex>
#include <thread>

namespace {
boost::asio::io_service *service;
boost::asio::io_context *service;
std::function<void (agi::dispatch::Thunk)> invoke_main;
std::atomic<uint_fast32_t> threads_running;

class MainQueue final : public agi::dispatch::Queue {
void DoInvoke(agi::dispatch::Thunk thunk) override {
void DoInvoke(agi::dispatch::Thunk&& thunk) override {
invoke_main(thunk);
}
};

class BackgroundQueue final : public agi::dispatch::Queue {
void DoInvoke(agi::dispatch::Thunk thunk) override {
service->post(thunk);
void DoInvoke(agi::dispatch::Thunk&& thunk) override {
boost::asio::post(*service, std::move(thunk));
}
};

class SerialQueue final : public agi::dispatch::Queue {
boost::asio::io_service::strand strand;
boost::asio::io_context::strand strand;

void DoInvoke(agi::dispatch::Thunk thunk) override {
strand.post(thunk);
void DoInvoke(agi::dispatch::Thunk&& thunk) override {
boost::asio::post(strand, std::move(thunk));
}
public:
SerialQueue() : strand(*service) { }
};

struct IOServiceThreadPool {
boost::asio::io_service io_service;
std::unique_ptr<boost::asio::io_service::work> work;
boost::asio::io_context io_context;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work_guard;
std::vector<std::thread> threads;

IOServiceThreadPool() : work(new boost::asio::io_service::work(io_service)) { }
IOServiceThreadPool() : work_guard(boost::asio::make_work_guard(io_context)) { }
~IOServiceThreadPool() {
work.reset();
work_guard.reset();
#ifndef _WIN32
for (auto& thread : threads) thread.join();
#else
Expand All @@ -76,7 +77,7 @@ namespace agi { namespace dispatch {

void Init(std::function<void (Thunk)> invoke_main) {
static IOServiceThreadPool thread_pool;
::service = &thread_pool.io_service;
::service = &thread_pool.io_context;
::invoke_main = invoke_main;

thread_pool.threads.reserve(std::max<unsigned>(4, std::thread::hardware_concurrency()));
Expand All @@ -90,7 +91,7 @@ void Init(std::function<void (Thunk)> invoke_main) {
}
}

void Queue::Async(Thunk thunk) {
void Queue::Async(Thunk&& thunk) {
DoInvoke([=] {
try {
thunk();
Expand All @@ -102,7 +103,7 @@ void Queue::Async(Thunk thunk) {
});
}

void Queue::Sync(Thunk thunk) {
void Queue::Sync(Thunk&& thunk) {
std::mutex m;
std::condition_variable cv;
std::unique_lock<std::mutex> l(m);
Expand Down
6 changes: 3 additions & 3 deletions libaegisub/include/libaegisub/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ namespace agi {
typedef std::function<void()> Thunk;

class Queue {
virtual void DoInvoke(Thunk thunk)=0;
virtual void DoInvoke(Thunk&& thunk)=0;
public:
virtual ~Queue() { }

/// Invoke the thunk on this processing queue, returning immediately
void Async(Thunk thunk);
void Async(Thunk&& thunk);

/// Invoke the thunk on this processing queue, returning only when
/// it's complete
void Sync(Thunk thunk);
void Sync(Thunk&& thunk);
};

/// Initialize the dispatch thread pools
Expand Down

0 comments on commit 71a4497

Please sign in to comment.