From 3ce789acb50efbda93d3ffc928c24867fddfc0d3 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sat, 1 Feb 2025 21:24:35 +0100 Subject: [PATCH] libutil: add async io roots to threadpool threads this needs a new method because overloading rules would make a pair of methods taking `std::function`s of different argument types ambiguous. we could've turned enqueue into a template and made `enqueueWithAio` a private method too, but the necessary template magic is wasted on this Change-Id: I93ccfde07126c287ad2a4dc5a07caec6a9d83944 --- lix/libutil/thread-pool.cc | 6 ++++-- lix/libutil/thread-pool.hh | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lix/libutil/thread-pool.cc b/lix/libutil/thread-pool.cc index ec7ac4563..9b6d6066f 100644 --- a/lix/libutil/thread-pool.cc +++ b/lix/libutil/thread-pool.cc @@ -41,7 +41,7 @@ void ThreadPool::shutdown() thr.join(); } -void ThreadPool::enqueue(const work_t & t) +void ThreadPool::enqueueWithAio(const work_t & t) { auto state(state_.lock()); if (quit) @@ -95,6 +95,8 @@ void ThreadPool::doWork() bool didWork = false; std::exception_ptr exc; + AsyncIoRoot aio; + while (true) { work_t w; { @@ -157,7 +159,7 @@ void ThreadPool::doWork() } try { - w(); + w(aio); } catch (...) { exc = std::current_exception(); } diff --git a/lix/libutil/thread-pool.hh b/lix/libutil/thread-pool.hh index 13df5fe3b..0e35fb27e 100644 --- a/lix/libutil/thread-pool.hh +++ b/lix/libutil/thread-pool.hh @@ -1,6 +1,7 @@ #pragma once ///@file +#include "lix/libutil/async.hh" #include "lix/libutil/error.hh" #include "lix/libutil/sync.hh" @@ -31,12 +32,17 @@ public: * * \todo use std::packaged_task? */ - typedef std::function work_t; + typedef std::function work_t; /** * Enqueue a function to be executed by the thread pool. */ - void enqueue(const work_t & t); + void enqueueWithAio(const work_t & t); + + void enqueue(std::function t) + { + enqueueWithAio([t{std::move(t)}](AsyncIoRoot &) { t(); }); + } /** * Execute work items until the queue is empty.