summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/common/async/context_pool.h23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/common/async/context_pool.h b/src/common/async/context_pool.h
index 0fee5dd7fca..91a7090ade7 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 <concepts>
#include <cstddef>
#include <cstdint>
#include <mutex>
@@ -46,9 +47,14 @@ class io_context_pool {
}
public:
io_context_pool() noexcept {}
+
io_context_pool(std::int64_t threadcnt) noexcept {
start(threadcnt);
}
+ template<std::invocable<> 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<std::invocable<> 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();
}));
}