From 82a40dc151cc4ec1576241b88f099abefc5b4b9a Mon Sep 17 00:00:00 2001 From: "Adam C. Emerson" Date: Fri, 14 Apr 2023 15:43:36 -0400 Subject: [PATCH] ceph/async: `io_context_pool` constructor/start takes init function Add overloads of the constructor and `start` that take an init function, for setting up the thread before dropping into `io_context::run`. Signed-off-by: Adam C. Emerson --- src/common/async/context_pool.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/common/async/context_pool.h b/src/common/async/context_pool.h index 0fee5dd7fcacd..91a7090ade766 100644 --- a/src/common/async/context_pool.h +++ b/src/common/async/context_pool.h @@ -16,6 +16,7 @@ #ifndef CEPH_COMMON_ASYNC_CONTEXT_POOL_H #define CEPH_COMMON_ASYNC_CONTEXT_POOL_H +#include #include #include #include @@ -46,9 +47,14 @@ class io_context_pool { } public: io_context_pool() noexcept {} + io_context_pool(std::int64_t threadcnt) noexcept { start(threadcnt); } + template Init> + io_context_pool(std::int64_t threadcnt, Init&& init) noexcept { + start(threadcnt, std::move(init)); + } ~io_context_pool() { stop(); } @@ -59,7 +65,22 @@ public: ioctx.restart(); for (std::int16_t i = 0; i < threadcnt; ++i) { threadvec.emplace_back(make_named_thread("io_context_pool", - [this]() { + [this] { + ioctx.run(); + })); + } + } + } + template Init> + void start(std::int16_t threadcnt, Init&& init) noexcept { + auto l = std::scoped_lock(m); + if (threadvec.empty()) { + guard.emplace(boost::asio::make_work_guard(ioctx)); + ioctx.restart(); + for (std::int16_t i = 0; i < threadcnt; ++i) { + threadvec.emplace_back(make_named_thread("io_context_pool", + [this, init=std::move(init)] { + std::move(init)(); ioctx.run(); })); } -- 2.39.5